[Solved] How do I spawn more enemies? SDL2/C++


I suggest placing your enemies into a std::vector or std::queue. Each enemy should have it’s own coordinates (for easy placement).

In your update or refresh loop, iterator over the enemy list, processing them as necessary.

In another timing loop or thread, you can add enemies to the container. This should not affect the code of the rest of the program; except that there may be more data (enemies) in the vector to process.

Example:

class Enemy_Base
{
  public:  
    virtual void draw(/* drawing surface */);
};

class Troll : public Enemy
{
  //...
};

std::vector<Enemy_Base *> enemy_container;

//...
std::vector<Enemy_Base *>::iterator enemy_iter;

// Rendering loop
for (enemy_iter = enemy_container.begin();
     enemy_iter != enemy_container.end();
     ++enemy_iter)
{
  (*enemy_iter)->draw(/*..*/);
}

// Create a new enemy
Troll * p_troll = new Troll;
enemy_container.push_back(p_troll);

This example is to demonstrate concepts. The real implementation should use smart pointers, and factory design pattern.

3

solved How do I spawn more enemies? SDL2/C++