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();
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 */

View File

@ -191,9 +191,9 @@ void solve_collision_slide(object_t* left, object_t* right) {
static inline
void _solve_move(object_t* this) {
// 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
object_t* other = g_objects + i;
object_t* other = world_get_object(i);
// check collision, return if found
if(can_collide(other) && this != other && _collision_check(other, this)) {
object_broadcast_collision(other, this);

View File

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