changed the way objects are notified of destruction, simplified game.c preset
parent
14a81b0f2a
commit
33c1a5e9d2
|
@ -3,7 +3,6 @@
|
||||||
object_t object_default() {
|
object_t object_default() {
|
||||||
return (object_t){
|
return (object_t){
|
||||||
.active = 1,
|
.active = 1,
|
||||||
.enabled = 1,
|
|
||||||
.physics = physics_default(),
|
.physics = physics_default(),
|
||||||
.evt_draw = &object_draw_sprite,
|
.evt_draw = &object_draw_sprite,
|
||||||
.evt_update = NULL,
|
.evt_update = NULL,
|
||||||
|
|
|
@ -11,8 +11,8 @@ typedef void(*draw_fn)(struct object_t*);
|
||||||
|
|
||||||
struct object_t {
|
struct object_t {
|
||||||
sprite_t sprite;
|
sprite_t sprite;
|
||||||
int active; // 1 if this object is in use and should not be overriden.
|
int active; // 1 if this object's events should be triggered by the game world update.
|
||||||
int enabled; // 1 if this object's events should be triggered.
|
int wants_to_be_deleted;
|
||||||
physics_t physics; // the collider to use for this object's physics interaction.
|
physics_t physics; // the collider to use for this object's physics interaction.
|
||||||
|
|
||||||
uintptr_t timer; // free to use for whatever
|
uintptr_t timer; // free to use for whatever
|
||||||
|
@ -26,7 +26,7 @@ object_t object_default();
|
||||||
void object_draw_sprite(object_t* object);
|
void object_draw_sprite(object_t* object);
|
||||||
static inline
|
static inline
|
||||||
int object_is_valid(const object_t* object) {
|
int object_is_valid(const object_t* object) {
|
||||||
return object != NULL && object->active != 0;
|
return object != NULL && object->wants_to_be_deleted == 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 object_is_valid(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline
|
static inline
|
||||||
|
|
|
@ -104,7 +104,6 @@ extern sprite_t make_sprite(const char* file, float x, float y);
|
||||||
extern sprite_t sprite_from_spritesheet(spritesheet_t* sheet, int index);
|
extern sprite_t sprite_from_spritesheet(spritesheet_t* sheet, int index);
|
||||||
extern text_style_t make_text_style(const char* font, SDL_Color color, int dpi, float size);
|
extern text_style_t make_text_style(const char* font, SDL_Color color, int dpi, float size);
|
||||||
extern SDL_Rect get_srcrect_from(spritesheet_t* sheet, int index);
|
extern SDL_Rect get_srcrect_from(spritesheet_t* sheet, int index);
|
||||||
extern void set_active_view(const view_t* view);
|
|
||||||
|
|
||||||
#define no_sprite (sprite_t){NULL, 0.0, 0.0, {0.0, 0.0}, 0.0, 0.0, 0.0, 0, {0, 0, 0, 0}}
|
#define no_sprite (sprite_t){NULL, 0.0, 0.0, {0.0, 0.0}, 0.0, 0.0, 0.0, 0, {0, 0, 0, 0}}
|
||||||
|
|
||||||
|
|
|
@ -47,23 +47,36 @@ size_t _find_free_object() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void world_clear() {
|
void world_clear() {
|
||||||
|
// free all allocated objects
|
||||||
for(int i = 0; i < _world_objects.num; ++i) {
|
for(int i = 0; i < _world_objects.num; ++i) {
|
||||||
_world_objects.objects[i]->active = 0;
|
object_t* object = _world_objects.objects[i];
|
||||||
_world_objects.objects[i]->enabled = 0;
|
if(object != NULL) {
|
||||||
|
free(object);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// free world array
|
||||||
|
free(_world_objects.objects);
|
||||||
|
// reset world objects struct
|
||||||
|
_world_objects.objects = NULL;
|
||||||
|
_world_objects.num = 0;
|
||||||
|
}
|
||||||
|
|
||||||
object_t* make_object() {
|
object_t* make_object() {
|
||||||
|
// acquire pointer to empty slot
|
||||||
size_t index = _find_free_object();
|
size_t index = _find_free_object();
|
||||||
|
// allocate new object
|
||||||
if(_world_objects.objects[index] == NULL) {
|
if(_world_objects.objects[index] == NULL) {
|
||||||
_world_objects.objects[index] = malloc(sizeof(object_t));
|
_world_objects.objects[index] = malloc(sizeof(object_t));
|
||||||
}
|
}
|
||||||
|
// initialize object to default
|
||||||
object_t* o = _world_objects.objects[index];
|
object_t* o = _world_objects.objects[index];
|
||||||
*o = object_default();
|
*o = object_default();
|
||||||
|
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
object_t* instantiate_object(const object_t *original) {
|
object_t* instantiate_object(const object_t *original) {
|
||||||
|
// create new object with default settings
|
||||||
object_t* obj = make_object();
|
object_t* obj = make_object();
|
||||||
*obj = *original;
|
*obj = *original;
|
||||||
obj->active = 1;
|
obj->active = 1;
|
||||||
|
|
|
@ -4,8 +4,6 @@
|
||||||
#include "corelib/render.h"
|
#include "corelib/render.h"
|
||||||
#include "corelib/assets.h"
|
#include "corelib/assets.h"
|
||||||
#include "corelib/layers.h"
|
#include "corelib/layers.h"
|
||||||
#include "SDL2/SDL_mouse.h"
|
|
||||||
#include "SDL2/SDL_scancode.h"
|
|
||||||
|
|
||||||
void start_game() {
|
void start_game() {
|
||||||
// called when the game is done initializing the backend and ready to start
|
// called when the game is done initializing the backend and ready to start
|
||||||
|
|
Loading…
Reference in New Issue