Я не уверен, но я сделал так.
Создай динамический массив, при добавлении объекта меняй размер, при рендеринге перебирай объекты. Объекты - простые структуры.
Вот мой код на плюсах, но в стиле си
Code
struct state {
AppState* ptr;
char* name;
};
state** _states;
size_t _size = 0;
bool running = true;
void attachState (char* name, AppState* s) {
state* st = new state;
st->ptr = s;
st->name = name;
if (_size == 0) {
_states = new state*[1];
} else {
state** old = _states;
_states = new state*[_size + 1];
memcpy(_states, old, _size * sizeof(state*));
delete old;
}
_states[_size] = st;
_size++;
st->ptr->onAttach();
}
AppState* detachState (char* name) {
for (unsigned i = 0; i < _size; i++) {
if (strcmp(_states[i]->name, name) == 0) {
AppState* res = _states[i]->ptr;
delete _states[i];
state** old = _states;
_states = new state*[_size - 1];
for (unsigned n = 0; n < _size - 1; n++) {
if (n < i) _states[n] = old[n];
else _states[n] = old[n + 1];
}
delete old;
res->onDetach();
_size--;
if (_size == 0) delete _states;
return res;
}
}
return 0;
}
AppState* getState (char* name) {
for (unsigned i = 0; i < _size; i++) {
if (strcmp(_states[i]->name, name) == 0) {
return _states[i]->ptr;
}
}
return 0;
}
void gangan::run () {
while (running) {
for (unsigned i = 0; i < _size; i++) {
_states[i]->ptr->update();
}
}
}
void quit () {
running = false;
}