Spawning multiple objects

I am new to SDL and C++ and I am trying to make a very simple game.
I tried to spawn multiple enemies using the for loop but I see the enemies flickering for a moment and they disappear.

Here’s the full main.cpp. I know it’s messy, I am sorry.

#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <random>

SDL_Window* window  = NULL;
SDL_Renderer* renderer = NULL;


class Game_object {
public:
  SDL_Surface* temp_surf;
  SDL_Texture* texture = NULL;

  SDL_Rect src_rect;
  SDL_Rect des_rect;



  void render(){

    texture = SDL_CreateTextureFromSurface(renderer, temp_surf);

    SDL_RenderCopy(renderer, texture, &src_rect, &des_rect);

    SDL_RenderPresent(renderer);

  }

};


int main(int argc, char *argv[]) {

  const int fps = 60;
  const int frame_delay = 1000/fps;


  //init SDL
  SDL_Init(SDL_INIT_EVERYTHING);
  if (SDL_Init(SDL_INIT_EVERYTHING > 0)) {
    std::cout << "Could not init SDL, Error: " << SDL_GetError() << std::endl;
  }
  else {
    window = SDL_CreateWindow("You Ded", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 500 , 500, SDL_WINDOW_SHOWN);

    if (window == NULL) {
      std::cout << "Could not create window, Error: " << SDL_GetError() << std::endl;
    }
    else {
      renderer = SDL_CreateRenderer(window, -1, 0);
      if (renderer == NULL) {
        std::cout << "Could not create renderer, Error: " << SDL_GetError() << std::endl;
      }
    }
  }



 

  bool is_running = true;
  SDL_Event event;
  int player_pos_x;
  int player_pos_y;

  int i = 1;

  while (is_running) {

    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        is_running = false;
      }
      else if (event.type == SDL_KEYDOWN) {
        switch (event.key.keysym.sym) {
        case SDLK_UP:
          player_pos_y -= 1;
          break;
        case SDLK_DOWN:
          player_pos_y += 1;
          break;
        case SDLK_RIGHT:
          player_pos_x += 1;
          break;
        case SDLK_LEFT:
          player_pos_x -= 1;
          break;
        default:
          player_pos_y = player_pos_y;
          player_pos_x = player_pos_x;
        }

      }


    }

    Game_object player;
    player.temp_surf = IMG_Load("player.png");
    player.src_rect.w = 32;
    player.src_rect.h = 32;
    player.src_rect.x = 0;
    player.src_rect.y = 0;
    player.des_rect.x = player_pos_x;
    player.des_rect.y = player_pos_y;
    player.des_rect.w = player.src_rect.w * 2;
    player.des_rect.h = player.src_rect.h * 2;
    player.render();


    Game_object enemy;
    for (i; i<=5; i++) {

      enemy.temp_surf = IMG_Load("enemy.png");
      enemy.src_rect.w = 32;
      enemy.src_rect.h = 32;
      enemy.src_rect.x = 0;
      enemy.src_rect.y = 0;
      enemy.des_rect.x = rand() % 500;
      enemy.des_rect.y = rand() % 500;
      enemy.des_rect.w = player.src_rect.w * 2;
      enemy.des_rect.h = player.src_rect.h * 2;
      enemy.render();
    }


  }

  
  return 0;
}

Thanks,

Let’s read this semantically:

  • While (is_running), open loop scope.
    • Process events, updating external state.
    • Create and render player Game_object at position determined by external state.
    • Create and render 15 enemy Game_objects at random positions.
  • Close loop scope, destructing all objects created within.

“I see the enemies flickering for a moment and they disappear” is precisely what you’d expect from this. Every frame you’re creating, rendering, and destroying the enemies. And also the player, but you don’t see the player object flickering because its position state is maintained external to the loop.

What you really want is to create all of these objects outside the loop and store them in a list object somewhere.

Thanks : )
I was just experimenting and added a frame limiter and the flickering and disappearing of enemy objects is fixed somehow… strange