Compare commits

...

3 Commits

3 changed files with 44 additions and 17 deletions

View File

@ -24,5 +24,9 @@ struct object_t {
object_t object_default(); object_t object_default();
void object_draw_sprite(object_t* object); void object_draw_sprite(object_t* object);
static inline
int object_is_valid(object_t* object) {
return object != NULL || object->active <= 0;
}
#endif /* _object_h */ #endif /* _object_h */

View File

@ -191,9 +191,9 @@ void solve_collision_slide(object_t* left, object_t* right) {
static inline static inline
void _solve_move(object_t* this) { void _solve_move(object_t* this) {
// loop over all objects and check collision if applicable // loop over all objects and check collision if applicable
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) { for(int i = 0; i < world_num_objects(); ++i) {
// get pointer to other object // get pointer to other object
object_t* other = g_objects + i; object_t* other = world_get_object(i);
// check collision, return if found // check collision, return if found
if(can_collide(other) && this != other && _collision_check(other, this)) { if(can_collide(other) && this != other && _collision_check(other, this)) {
object_broadcast_collision(other, this); object_broadcast_collision(other, this);

View File

@ -1,6 +1,7 @@
#include "world.h" #include "world.h"
#include "memory.h" #include "memory.h"
#include "math/vec.h" #include "math/vec.h"
#include "object.h"
struct { struct {
size_t num; size_t num;
@ -26,12 +27,16 @@ int _expand_world() {
static inline static inline
object_t* _find_free_object() { object_t* _find_free_object() {
for(int i = 0; i < _world_objects.num; ++i) { for(int i = 0; i < _world_objects.num; ++i) {
if(_world_objects.objects[i]->active == 0) { if(object_is_valid(_world_objects.objects[i])) {
return _world_objects.objects[i]; return _world_objects.objects[i];
} }
} }
_expand_world(); size_t num = _world_objects.num;
return NULL; if(_expand_world()) {
return _world_objects.objects[num];
} else {
return NULL;
}
} }
void world_clear() { void world_clear() {
@ -55,21 +60,39 @@ object_t* instantiate_object(const object_t *original) {
} }
void world_update() { void world_update() {
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) { for(int i = 0; i < world_num_objects(); ++i) {
if(g_objects[i].active == 1 object_t* object = world_get_object(i);
&& g_objects[i].enabled == 1 if(!object_is_valid(object)
&& g_objects[i].evt_update != NULL) { && object->evt_update != NULL) {
g_objects[i].evt_update(g_objects + i); object->evt_update(object);
} }
} }
} }
void world_draw() { void world_draw() {
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) { for(int i = 0; i < world_num_objects(); ++i) {
if(g_objects[i].active == 1 object_t* object = world_get_object(i);
&& g_objects[i].enabled == 1 if(!object_is_valid(object)
&& g_objects[i].evt_draw != NULL) { && object->evt_draw != NULL) {
g_objects[i].evt_draw(g_objects + i); object->evt_draw(object);
} }
} }
} }
object_t* world_get_object(size_t at) {
if(at < _world_objects.num) {
return _world_objects.objects[at];
} else {
return NULL;
}
}
size_t world_num_objects() {
return _world_objects.num;
}
void world_reserve_objects(size_t min) {
while(_world_objects.num < min) {
_expand_world();
}
}