Allow the world to grow as required by the number of objects being spawned #12

Merged
Sara merged 22 commits from dynamic-size-world into main 2023-07-12 00:53:05 +00:00
8 changed files with 109 additions and 47 deletions

6
src/corelib/corelib.h Normal file
View File

@ -0,0 +1,6 @@
#include "object.h"
#include "render.h"
#include "assets.h"
#include "physics.h"
#include "world.h"
#include "entry.h"

View File

@ -1,8 +1,9 @@
#include "engine.h" #include "entry.h"
#include "world.h" #include "world.h"
#include "corelib/assets.h" #include "assets.h"
#include "corelib/render.h" #include "render.h"
#include "corelib/input.h" #include "input.h"
#include "time.h" #include "time.h"
static double _delta_time = 0; static double _delta_time = 0;

View File

@ -1,10 +1,6 @@
#ifndef _engine_h #ifndef _engine_h
#define _engine_h #define _engine_h
#ifdef __cplusplus
extern "C" {
#endif
extern float delta_time(); extern float delta_time();
extern void set_frame_interval(double frame_interval); extern void set_frame_interval(double frame_interval);
extern void set_frame_rate_limit(int fps); extern void set_frame_rate_limit(int fps);
@ -16,8 +12,4 @@ extern void update_game();
extern void update_ui(); extern void update_ui();
extern void game_exit(); extern void game_exit();
#ifdef __cplusplus
}
#endif
#endif /* _engine_h */ #endif /* _engine_h */

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(const object_t* object) {
return object != NULL && object->active != 0;
}
#endif /* _object_h */ #endif /* _object_h */

View File

@ -24,7 +24,7 @@ void object_broadcast_collision(object_t* this, object_t* other) {
} }
} }
short can_collide(const object_t* this) { short can_collide(const object_t* this) {
return this->active && this->enabled; return object_is_valid(this) && this->enabled;
} }
static inline static inline
@ -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,26 +1,64 @@
#include "world.h" #include "world.h"
#include "memory.h"
#include "assert.h"
#include "math/vec.h" #include "math/vec.h"
#include "object.h"
object_t g_objects[WORLD_NUM_OBJECTS]; struct {
size_t num;
object_t** objects;
} _world_objects = {
.num = 0,
.objects = NULL
};
void world_clear() { static inline
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) { int _expand_world() {
g_objects[i].active = 0; size_t new_num = _world_objects.num * 2;
g_objects[i].enabled = 0; if(new_num == 0) {
new_num = 16;
} }
object_t** new_list = realloc(_world_objects.objects, new_num * sizeof(object_t*));
if(new_list == NULL) {
assert(!"ERROR: Out of memory");
exit(-10);
}
for(size_t i = _world_objects.num; i < new_num; ++i) {
new_list[i] = NULL;
}
_world_objects.objects = new_list;
_world_objects.num = new_num;
return 1;
} }
object_t* _find_free_object() { static inline
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) { size_t _find_free_object() {
if(g_objects[i].active == 0) { for(int i = 0; i < _world_objects.num; ++i) {
return g_objects + i; if(!object_is_valid(_world_objects.objects[i])) {
return i;
} }
} }
return NULL; size_t num = _world_objects.num;
_expand_world();
return num;
}
void world_clear() {
for(int i = 0; i < _world_objects.num; ++i) {
_world_objects.objects[i]->active = 0;
_world_objects.objects[i]->enabled = 0;
}
} }
object_t* make_object() { object_t* make_object() {
object_t* o = _find_free_object(); size_t index = _find_free_object();
if(_world_objects.objects[index] == NULL) {
_world_objects.objects[index] = malloc(sizeof(object_t));
}
object_t* o = _world_objects.objects[index];
*o = object_default(); *o = object_default();
return o; return o;
} }
@ -33,21 +71,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_objects.num; ++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_objects.num; ++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();
}
}

View File

@ -3,18 +3,21 @@
#include "object.h" #include "object.h"
#define WORLD_NUM_OBJECTS 255 #define BASE_WORLD_SIZE 24
typedef struct object_t object_t; typedef struct object_t object_t;
extern object_t g_objects[WORLD_NUM_OBJECTS]; extern object_t* make_object();
extern object_t* instantiate_object(const object_t* original);
object_t* make_object(); extern void world_clear();
object_t* instantiate_object(const object_t* original);
void world_clear(); extern void world_update();
extern void world_draw();
void world_update(); extern object_t* world_get_object(size_t at);
void world_draw();
extern size_t world_num_objects();
extern void world_reserve_objects(size_t min);
#endif /* _world_h */ #endif /* _world_h */

View File

@ -1,5 +1,5 @@
#include "engine.h" #include "corelib/entry.h"
#include "world.h" #include "corelib/world.h"
#include "corelib/input.h" #include "corelib/input.h"
#include "corelib/render.h" #include "corelib/render.h"
#include "corelib/assets.h" #include "corelib/assets.h"