Compare commits

...

23 Commits

Author SHA1 Message Date
Sara 30defc0db8 Merge pull request 'Allow the world to grow as required by the number of objects being spawned' (#12) from dynamic-size-world into main
Reviewed-on: #12
2023-07-12 00:53:04 +00:00
Sara ec50000130 un-negated uses of object_is_valid that shouldn't be, and negated ones that should be 2023-07-12 02:52:04 +02:00
Sara 0698121853 object_is_valid now confirms that an object is not null and is active 2023-07-12 02:49:36 +02:00
Sara 8a6a0e7493 physics.c: can_collide now checks for object_is_valid before comfirming other variables 2023-07-12 02:47:31 +02:00
Sara 78c0e3a9c0 object_is_valid now takes a constant pointer 2023-07-12 02:46:26 +02:00
Sara 59de57fe3a simplified error checking, out of memory now results in an immediate crash when trying to expand world 2023-07-12 02:44:21 +02:00
Sara 5e001bc345 object_is_valid now confirms if an object is valid, as opposed to if an object is invalid 2023-07-12 02:43:53 +02:00
Sara 3e3f322529 _find_free_object now returns the index of the found object / make_object now allocates the object if the found slot is NULL 2023-07-12 02:38:26 +02:00
Sara 30d4e4c39a replaced initialization code of _world_objects.objects with new_list 2023-07-12 02:29:33 +02:00
Sara a139e4f3bb now initializing newly expanded world space with nullptrs 2023-07-12 02:28:13 +02:00
Sara 6a52f2795d object_is_valid now does not attempt to validate internals if object is NULL 2023-07-12 02:25:32 +02:00
Sara d915047bd1 removed uses of world_num_objects from world.c and replaced them with _world_objects.num 2023-07-12 02:25:11 +02:00
Sara c1fa6e2956 now creating world array with 16 elements if world array length is 0 2023-07-12 02:19:32 +02:00
Sara 544f18e9c6 added error when failing to expand world 2023-07-12 02:16:05 +02:00
Sara e6b66884ec added expandable world objects array 2023-07-12 02:07:29 +02:00
Sara 3ed5755e52 physics.h no longer depends on g_objects being public, or WORLD_NUM_OBJECTS being constant 2023-07-12 02:07:08 +02:00
Sara a5c6a7eceb added objecadded object_is_valid 2023-07-12 02:06:40 +02:00
Sara 1eb8ab2fd9 WIP: world array is now an array of pointers to objects 2023-06-28 08:54:22 +02:00
Sara a18d5599b4 Merge remote-tracking branch 'origin' into dynamic-size-world 2023-06-28 08:52:35 +02:00
Sara de7d14ae92 added corelib.h include for minimal setup 2023-06-27 08:16:45 +02:00
Sara f395f27532 moved entry to corelib 2023-06-27 08:16:31 +02:00
Sara 528e35537f renamed engine module to entry module 2023-06-26 17:43:15 +02:00
Sara f115334644 moved engine module to corelib 2023-06-26 17:42:02 +02: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 "corelib/assets.h"
#include "corelib/render.h"
#include "corelib/input.h"
#include "assets.h"
#include "render.h"
#include "input.h"
#include "time.h"
static double _delta_time = 0;

View File

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

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(const object_t* object) {
return object != NULL && object->active != 0;
}
#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) {
return this->active && this->enabled;
return object_is_valid(this) && this->enabled;
}
static inline
@ -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,26 +1,64 @@
#include "world.h"
#include "memory.h"
#include "assert.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
};
static inline
int _expand_world() {
size_t new_num = _world_objects.num * 2;
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;
}
static inline
size_t _find_free_object() {
for(int i = 0; i < _world_objects.num; ++i) {
if(!object_is_valid(_world_objects.objects[i])) {
return i;
}
}
size_t num = _world_objects.num;
_expand_world();
return num;
}
void world_clear() {
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) {
g_objects[i].active = 0;
g_objects[i].enabled = 0;
for(int i = 0; i < _world_objects.num; ++i) {
_world_objects.objects[i]->active = 0;
_world_objects.objects[i]->enabled = 0;
}
}
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();
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();
return o;
}
@ -33,21 +71,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_objects.num; ++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_objects.num; ++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();
}
}

View File

@ -3,18 +3,21 @@
#include "object.h"
#define WORLD_NUM_OBJECTS 255
#define BASE_WORLD_SIZE 24
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();
object_t* instantiate_object(const object_t* original);
extern void world_clear();
void world_clear();
extern void world_update();
extern void world_draw();
void world_update();
void world_draw();
extern object_t* world_get_object(size_t at);
extern size_t world_num_objects();
extern void world_reserve_objects(size_t min);
#endif /* _world_h */

View File

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