Look into Entity component system ( https://en.wikipedia.org/wiki/Entity_component_system ) and similar Object Oriented Programming paradigms.
This can be done in C++, C, assembly, or just about any languages.
I've included an example in plain C further down showing one way how OOP & virtual functions can be done in C, but C++ was designed to do this stuff for you.
The idea as applied to your situation is you have a list of objects and each object has a creation (setup/constructor) function, an update function, a draw function, and a destroy function (destructor).
And your main loop calls the update and draw functions of all active objects in the list.
So you have a base class / struct which declares the virtual functions (or function pointers in C) :
class EntityBase
{
public:
virtual ~EntityBase() {}
virtual void Update() {}
virtual void Draw() {}
};
And then derived classes which override those functions:
class Bob : public EntityBase
{
public:
// bob variables here
int energy;
void Bob() {
energy = 10;
}
void ~Bob() {
// clean up
}
void Update() {
// update bob
}
void Draw() {
// draw bob
}
};
And your main loop can call the Draw & Update functions of all your objects as EntityBase without knowing the details.
in plain C it would be something like:
typedef struct EntityBase_ {
void (*Destroy)(struct EntityBase_ *me);
void (*Update)(struct EntityBase_ *me);
void (*Draw)(struct EntityBase_ *me);
} EntityBase;
and creating an object Bob would be like:
typedef struct Bob_ {
EntityBase base;
// bob variables here
} Bob;
void Bob_Destroy(struct EntityBase_ *me){
Bob *bob = (Bob *)me;
// clean up code here
free(me);
}
void Bob_Update(struct EntityBase_ *me){
Bob *bob = (Bob *)me;
// update bob here
}
void Bob_Draw(struct EntityBase_ *me){
Bob *bob = (Bob *)me;
// draw bob here
}
EntityBase *CreateBob()
{
Bob *bob = (Bob *)malloc(sizeof(Bob));
bob->base.Destroy = &Bob_Destroy;
bob->base.Draw = &Bob_Draw;
bob->base.Update = &Bob_Update;
// initialize bob variables here
return &(bob->base);
}
the main loop in C would do something like:
EntityBase *eb = entity_pointer_array[i];
eb->base.Update(eb);
eb->base.Draw(eb);
std::maps. There's no need to create a class for it... \$\endgroup\$