61 lines
1.2 KiB
C
61 lines
1.2 KiB
C
|
#include "world.h"
|
||
|
|
||
|
object_t g_objects[WORLD_NUM_OBJECTS];
|
||
|
|
||
|
void world_clear() {
|
||
|
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) {
|
||
|
g_objects[i].active = 0;
|
||
|
g_objects[i].enabled = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void object_draw_sprite(object_t* object) {
|
||
|
draw_sprite(&object->sprite);
|
||
|
}
|
||
|
|
||
|
object_t* _find_free_object() {
|
||
|
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) {
|
||
|
if(g_objects[i].active == 0) {
|
||
|
return g_objects + i;
|
||
|
}
|
||
|
}
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
object_t* make_object() {
|
||
|
object_t* o = _find_free_object();
|
||
|
o->active = 1;
|
||
|
o->enabled = 1;
|
||
|
o->evt_draw = &object_draw_sprite;
|
||
|
o->evt_update = NULL;
|
||
|
memset(&o->sprite, 0, sizeof(sprite_t));
|
||
|
return o;
|
||
|
}
|
||
|
|
||
|
object_t* instantiate_object(const object_t *original) {
|
||
|
object_t* obj = make_object();
|
||
|
*obj = *original;
|
||
|
obj->active = 1;
|
||
|
return obj;
|
||
|
}
|
||
|
|
||
|
void update_objects() {
|
||
|
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) {
|
||
|
if(g_objects[i].active == 1
|
||
|
&& g_objects[i].enabled == 1
|
||
|
&& g_objects[i].evt_update != NULL) {
|
||
|
g_objects[i].evt_update(g_objects + i);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void draw_objects() {
|
||
|
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) {
|
||
|
if(g_objects[i].active == 1
|
||
|
&& g_objects[i].enabled == 1
|
||
|
&& g_objects[i].evt_draw != NULL) {
|
||
|
g_objects[i].evt_draw(g_objects + i);
|
||
|
}
|
||
|
}
|
||
|
}
|