Compare commits
No commits in common. "30defc0db82ab807dd445cf931e0479b1dab39fc" and "ca3b2af7039cdcd20a9f4e703aa40dea7b9efd39" have entirely different histories.
30defc0db8
...
ca3b2af703
|
@ -1,6 +0,0 @@
|
||||||
#include "object.h"
|
|
||||||
#include "render.h"
|
|
||||||
#include "assets.h"
|
|
||||||
#include "physics.h"
|
|
||||||
#include "world.h"
|
|
||||||
#include "entry.h"
|
|
|
@ -24,9 +24,5 @@ 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 */
|
|
@ -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 object_is_valid(this) && this->enabled;
|
return this->active && 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 = world_get_object(i);
|
object_t* other = g_objects + 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);
|
||||||
|
|
|
@ -1,64 +1,26 @@
|
||||||
#include "world.h"
|
#include "world.h"
|
||||||
#include "memory.h"
|
|
||||||
#include "assert.h"
|
|
||||||
#include "math/vec.h"
|
#include "math/vec.h"
|
||||||
#include "object.h"
|
|
||||||
|
|
||||||
struct {
|
object_t g_objects[WORLD_NUM_OBJECTS];
|
||||||
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() {
|
void world_clear() {
|
||||||
for(int i = 0; i < _world_objects.num; ++i) {
|
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) {
|
||||||
_world_objects.objects[i]->active = 0;
|
g_objects[i].active = 0;
|
||||||
_world_objects.objects[i]->enabled = 0;
|
g_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* make_object() {
|
||||||
size_t index = _find_free_object();
|
object_t* o = _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;
|
||||||
}
|
}
|
||||||
|
@ -71,39 +33,21 @@ object_t* instantiate_object(const object_t *original) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void world_update() {
|
void world_update() {
|
||||||
for(int i = 0; i < _world_objects.num; ++i) {
|
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) {
|
||||||
object_t* object = world_get_object(i);
|
if(g_objects[i].active == 1
|
||||||
if(object_is_valid(object)
|
&& g_objects[i].enabled == 1
|
||||||
&& object->evt_update != NULL) {
|
&& g_objects[i].evt_update != NULL) {
|
||||||
object->evt_update(object);
|
g_objects[i].evt_update(g_objects + i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void world_draw() {
|
void world_draw() {
|
||||||
for(int i = 0; i < _world_objects.num; ++i) {
|
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) {
|
||||||
object_t* object = world_get_object(i);
|
if(g_objects[i].active == 1
|
||||||
if(object_is_valid(object)
|
&& g_objects[i].enabled == 1
|
||||||
&& object->evt_draw != NULL) {
|
&& g_objects[i].evt_draw != NULL) {
|
||||||
object->evt_draw(object);
|
g_objects[i].evt_draw(g_objects + i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -3,21 +3,18 @@
|
||||||
|
|
||||||
#include "object.h"
|
#include "object.h"
|
||||||
|
|
||||||
#define BASE_WORLD_SIZE 24
|
#define WORLD_NUM_OBJECTS 255
|
||||||
|
|
||||||
typedef struct object_t object_t;
|
typedef struct object_t object_t;
|
||||||
|
|
||||||
extern object_t* make_object();
|
extern object_t g_objects[WORLD_NUM_OBJECTS];
|
||||||
extern object_t* instantiate_object(const object_t* original);
|
|
||||||
|
|
||||||
extern void world_clear();
|
object_t* make_object();
|
||||||
|
object_t* instantiate_object(const object_t* original);
|
||||||
|
|
||||||
extern void world_update();
|
void world_clear();
|
||||||
extern void world_draw();
|
|
||||||
|
|
||||||
extern object_t* world_get_object(size_t at);
|
void world_update();
|
||||||
|
void world_draw();
|
||||||
extern size_t world_num_objects();
|
|
||||||
extern void world_reserve_objects(size_t min);
|
|
||||||
|
|
||||||
#endif /* _world_h */
|
#endif /* _world_h */
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
#include "entry.h"
|
#include "engine.h"
|
||||||
#include "world.h"
|
#include "world.h"
|
||||||
#include "assets.h"
|
#include "corelib/assets.h"
|
||||||
#include "render.h"
|
#include "corelib/render.h"
|
||||||
#include "input.h"
|
#include "corelib/input.h"
|
||||||
|
|
||||||
#include "time.h"
|
#include "time.h"
|
||||||
|
|
||||||
static double _delta_time = 0;
|
static double _delta_time = 0;
|
|
@ -1,6 +1,10 @@
|
||||||
#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);
|
||||||
|
@ -12,4 +16,8 @@ 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 */
|
|
@ -1,5 +1,5 @@
|
||||||
#include "corelib/entry.h"
|
#include "engine.h"
|
||||||
#include "corelib/world.h"
|
#include "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"
|
||||||
|
|
Loading…
Reference in New Issue