Entity system & malloc initialization fixes
This commit is contained in:
parent
07b5a8df79
commit
abb9eb569f
4
.clangd
4
.clangd
@ -1,9 +1,9 @@
|
|||||||
If:
|
If:
|
||||||
PathMatch: [.*\.h, .*\.c]
|
PathMatch: [.*\.h, .*\.c]
|
||||||
CompileFlags:
|
CompileFlags:
|
||||||
Add: [-xc, -DWIN32_LEAN_AND_MEAN, -I/home/edgar/Codes/JacksEscape/easyx/include, -target, x86_64-pc-windows-gnu]
|
Add: [-xc, -Wall, -DWIN32_LEAN_AND_MEAN, -I/home/edgar/Codes/JacksEscape/easyx/include, -target, x86_64-pc-windows-gnu]
|
||||||
---
|
---
|
||||||
If:
|
If:
|
||||||
PathMatch: [.*\.hpp, .*\.cpp]
|
PathMatch: [.*\.hpp, .*\.cpp]
|
||||||
CompileFlags:
|
CompileFlags:
|
||||||
Add: [-xc++, -DWIN32_LEAN_AND_MEAN, -I/home/edgar/Codes/JacksEscape/easyx/include, -target, x86_64-pc-windows-gnu]
|
Add: [-xc++, -Wall, -DWIN32_LEAN_AND_MEAN, -I/home/edgar/Codes/JacksEscape/easyx/include, -target, x86_64-pc-windows-gnu]
|
||||||
|
4
App.c
4
App.c
@ -1,5 +1,6 @@
|
|||||||
|
|
||||||
#include "App.h"
|
#include "App.h"
|
||||||
|
#include "Entity.h"
|
||||||
#include "Input.h"
|
#include "Input.h"
|
||||||
#include "Physics_Component.h"
|
#include "Physics_Component.h"
|
||||||
#include "Player_Component.h"
|
#include "Player_Component.h"
|
||||||
@ -12,12 +13,14 @@ App *app_NewApp() {
|
|||||||
app->input = input_NewSystem(app);
|
app->input = input_NewSystem(app);
|
||||||
app->physics = physics_NewSystem(app);
|
app->physics = physics_NewSystem(app);
|
||||||
app->player = player_NewSystem(app);
|
app->player = player_NewSystem(app);
|
||||||
|
app->entity = entity_NewSystem(app);
|
||||||
|
|
||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
|
|
||||||
void app_DeleteApp(App *app) {
|
void app_DeleteApp(App *app) {
|
||||||
input_DeleteSystem(app->input);
|
input_DeleteSystem(app->input);
|
||||||
|
entity_DeleteSystem(app->entity);
|
||||||
physics_DeleteSystem(app->physics);
|
physics_DeleteSystem(app->physics);
|
||||||
player_DeleteSystem(app->player);
|
player_DeleteSystem(app->player);
|
||||||
|
|
||||||
@ -29,4 +32,5 @@ void app_Advance(App *app, Duration deltaTime) {
|
|||||||
input_Advance(app->input);
|
input_Advance(app->input);
|
||||||
player_Advance(app->player, deltaTime);
|
player_Advance(app->player, deltaTime);
|
||||||
physics_Advance(app->physics, deltaTime);
|
physics_Advance(app->physics, deltaTime);
|
||||||
|
entity_Advance(app->entity, deltaTime);
|
||||||
}
|
}
|
||||||
|
1
App.h
1
App.h
@ -11,6 +11,7 @@ typedef struct _App {
|
|||||||
System_Physics *physics;
|
System_Physics *physics;
|
||||||
System_Player *player;
|
System_Player *player;
|
||||||
System_Input *input;
|
System_Input *input;
|
||||||
|
System_Entity *entity;
|
||||||
} App;
|
} App;
|
||||||
|
|
||||||
App *app_NewApp();
|
App *app_NewApp();
|
||||||
|
7
App_Render.cpp
Normal file
7
App_Render.cpp
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
#include "App.h"
|
||||||
|
#include <graphics.h>
|
||||||
|
|
||||||
|
|
||||||
|
void app_Render() {
|
||||||
|
}
|
112
Entity.c
Normal file
112
Entity.c
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
|
||||||
|
#include "Entity.h"
|
||||||
|
#include "App.h"
|
||||||
|
#include "Physics_Component.h"
|
||||||
|
#include "util/assert.h"
|
||||||
|
#include "util/tree.h"
|
||||||
|
#include "util/vector.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
|
System_Entity *entity_NewSystem(App *super) {
|
||||||
|
System_Entity *sys = malloc(sizeof(System_Entity));
|
||||||
|
|
||||||
|
sys->entities = tree_Create(sizeof(Entity));
|
||||||
|
sys->maxID = 0;
|
||||||
|
sys->super = super;
|
||||||
|
sys->flaggedDelete = vector_Create(sizeof(uintptr_t));
|
||||||
|
|
||||||
|
return sys;
|
||||||
|
}
|
||||||
|
|
||||||
|
void entity_DeleteSystem(System_Entity *sys) {
|
||||||
|
for (tree_Node *i = tree_FirstNode(sys->entities);
|
||||||
|
i != NULL;
|
||||||
|
i = tree_Node_Next(i)) {
|
||||||
|
Entity *e = (Entity *)(i->data);
|
||||||
|
|
||||||
|
physics_DeleteEntity(sys->super->physics, e->id);
|
||||||
|
player_DeleteEntity(sys->super->player, e->id);
|
||||||
|
|
||||||
|
if (e->position)
|
||||||
|
free(e->position);
|
||||||
|
if (e->hitbox)
|
||||||
|
free(e->hitbox);
|
||||||
|
if (e->player)
|
||||||
|
free(e->player);
|
||||||
|
if (e->name)
|
||||||
|
free(e->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
tree_Destroy(sys->entities);
|
||||||
|
vector_Destroy(sys->flaggedDelete);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Entity *entity_Create(System_Entity *sys, const char *name) {
|
||||||
|
uintptr_t id = ++sys->maxID;
|
||||||
|
|
||||||
|
bool added = false;
|
||||||
|
Entity *e = (Entity *)(tree_Insert(sys->entities, id, &added));
|
||||||
|
ASSERT(added && "entity ID conflict in entity_Create");
|
||||||
|
|
||||||
|
memset(e, 0, sizeof(Entity));
|
||||||
|
e->id = id;
|
||||||
|
|
||||||
|
size_t len;
|
||||||
|
if (name && (len = strlen(name)) > 0) {
|
||||||
|
e->name = malloc(len + 1);
|
||||||
|
memcpy(e->name, name, len + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static inline void _entity_Delete(System_Entity *sys, uintptr_t id) {
|
||||||
|
tree_Node *node = tree_FindNode(sys->entities, id);
|
||||||
|
if (!node) {
|
||||||
|
fprintf(stderr, "[entity_Delete][WARN] Missing entity ID [%d]\n", id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Entity *e = (Entity *)(node->data);
|
||||||
|
|
||||||
|
fprintf(stderr, "[entity_Delete] Deleting \"%s\"[%d]\n", e->name, e->id);
|
||||||
|
|
||||||
|
physics_DeleteEntity(sys->super->physics, e->id);
|
||||||
|
player_DeleteEntity(sys->super->player, e->id);
|
||||||
|
|
||||||
|
if (e->position)
|
||||||
|
free(e->position);
|
||||||
|
if (e->hitbox)
|
||||||
|
free(e->hitbox);
|
||||||
|
if (e->player)
|
||||||
|
free(e->player);
|
||||||
|
if (e->name)
|
||||||
|
free(e->name);
|
||||||
|
|
||||||
|
tree_Delete(sys->entities, node);
|
||||||
|
}
|
||||||
|
|
||||||
|
void entity_Delete(System_Entity *sys, uintptr_t id) {
|
||||||
|
vector_Push(sys->flaggedDelete, &id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void entity_Advance(System_Entity *sys, Duration deltaTime) {
|
||||||
|
// Delete flagged entities
|
||||||
|
for (int i = 0; i < vector_Size(sys->flaggedDelete); i++) {
|
||||||
|
_entity_Delete(sys, *(uintptr_t *)(vector_At(sys->flaggedDelete, i)));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (tree_Node *i = tree_FirstNode(sys->entities);
|
||||||
|
i != NULL;
|
||||||
|
i = tree_Node_Next(i)) {
|
||||||
|
Entity *e = (Entity *)(i->data);
|
||||||
|
|
||||||
|
if (e->thinker)
|
||||||
|
e->thinker(e, deltaTime);
|
||||||
|
}
|
||||||
|
}
|
31
Entity.h
31
Entity.h
@ -1,7 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include "Types.h"
|
||||||
#include "util/vector.h"
|
#include "util/vector.h"
|
||||||
|
#include "util/tree.h"
|
||||||
#include "Physics_Component.h"
|
#include "Physics_Component.h"
|
||||||
#include "Player_Component.h"
|
#include "Player_Component.h"
|
||||||
|
|
||||||
@ -11,17 +13,44 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct _Entity Entity;
|
||||||
|
typedef void (*entity_Thinker)(Entity *e, Duration deltaTime); // The Thinker function type assigned to Entities
|
||||||
|
|
||||||
|
|
||||||
// Entity.
|
// Entity.
|
||||||
typedef struct _Entity {
|
typedef struct _Entity {
|
||||||
uintptr_t id;
|
uintptr_t id;
|
||||||
char *name;
|
char *name; // Allocated on the stack & copied
|
||||||
|
|
||||||
Component_Position *position;
|
Component_Position *position;
|
||||||
Component_Hitbox *hitbox;
|
Component_Hitbox *hitbox;
|
||||||
Component_Player *player;
|
Component_Player *player;
|
||||||
|
|
||||||
|
entity_Thinker thinker; // Called by System_Entity each frame if not NULL.
|
||||||
|
void *thinkerData; // Data managed by the Thinker, if exists.
|
||||||
} Entity;
|
} Entity;
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct _App App;
|
||||||
|
|
||||||
|
// Entity manager.
|
||||||
|
typedef struct {
|
||||||
|
App *super;
|
||||||
|
tree_Tree *entities; // Contains Entity objects on the nodes
|
||||||
|
uintptr_t maxID;
|
||||||
|
|
||||||
|
vector_Vector *flaggedDelete;
|
||||||
|
} System_Entity;
|
||||||
|
|
||||||
|
System_Entity *entity_NewSystem(App *super);
|
||||||
|
void entity_DeleteSystem(System_Entity *sys);
|
||||||
|
|
||||||
|
Entity *entity_Create(System_Entity *sys, const char *name);
|
||||||
|
void entity_Delete(System_Entity *sys, uintptr_t id);
|
||||||
|
|
||||||
|
void entity_Advance(System_Entity *sys, Duration deltaTime);
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
2
Input.c
2
Input.c
@ -19,7 +19,9 @@ void input_SetDefaultKeymap(System_Input *sys) {
|
|||||||
|
|
||||||
System_Input *input_NewSystem(App *super) {
|
System_Input *input_NewSystem(App *super) {
|
||||||
System_Input *sys = malloc(sizeof(System_Input));
|
System_Input *sys = malloc(sizeof(System_Input));
|
||||||
|
memset(sys, 0, sizeof(System_Input));
|
||||||
sys->super = super;
|
sys->super = super;
|
||||||
|
input_SetDefaultKeymap(sys);
|
||||||
return sys;
|
return sys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
6
Input.h
6
Input.h
@ -36,10 +36,10 @@ const char *input_KeyNames[input_Key_Count] = {
|
|||||||
|
|
||||||
// States a key might in
|
// States a key might in
|
||||||
typedef enum {
|
typedef enum {
|
||||||
Pressed,
|
|
||||||
JustPressed,
|
|
||||||
Released,
|
Released,
|
||||||
JustReleased
|
JustReleased,
|
||||||
|
Pressed,
|
||||||
|
JustPressed
|
||||||
} input_KeyState;
|
} input_KeyState;
|
||||||
|
|
||||||
static inline bool input_IsPressed(input_KeyState state) { return state == Pressed || state == JustPressed; }
|
static inline bool input_IsPressed(input_KeyState state) { return state == Pressed || state == JustPressed; }
|
||||||
|
@ -30,7 +30,6 @@ System_Physics *physics_NewSystem(App *super) {
|
|||||||
|
|
||||||
sys->pos = tree_Create(sizeof(uintptr_t));
|
sys->pos = tree_Create(sizeof(uintptr_t));
|
||||||
sys->hit = tree_Create(sizeof(uintptr_t));
|
sys->hit = tree_Create(sizeof(uintptr_t));
|
||||||
sys->flaggedDelete = vector_Create(sizeof(uintptr_t));
|
|
||||||
|
|
||||||
return sys;
|
return sys;
|
||||||
}
|
}
|
||||||
@ -38,7 +37,6 @@ System_Physics *physics_NewSystem(App *super) {
|
|||||||
void physics_DeleteSystem(System_Physics *sys) {
|
void physics_DeleteSystem(System_Physics *sys) {
|
||||||
tree_Destroy(sys->pos);
|
tree_Destroy(sys->pos);
|
||||||
tree_Destroy(sys->hit);
|
tree_Destroy(sys->hit);
|
||||||
vector_Destroy(sys->flaggedDelete);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -52,7 +50,13 @@ void physics_AddEntity(System_Physics *sys, Entity *e) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void physics_DeleteEntity(System_Physics *sys, uintptr_t id) {
|
void physics_DeleteEntity(System_Physics *sys, uintptr_t id) {
|
||||||
vector_Push(sys->flaggedDelete, &id);
|
tree_Node *n;
|
||||||
|
n = tree_FindNode(sys->pos, id);
|
||||||
|
if (n)
|
||||||
|
tree_Delete(sys->pos, n);
|
||||||
|
n = tree_FindNode(sys->hit, id);
|
||||||
|
if (n)
|
||||||
|
tree_Delete(sys->hit, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -82,20 +86,6 @@ static inline void _physics_AdvanceEntity(System_Physics *sys, Entity *e, Durati
|
|||||||
|
|
||||||
|
|
||||||
void physics_Advance(System_Physics *sys, Duration deltaTime) {
|
void physics_Advance(System_Physics *sys, Duration deltaTime) {
|
||||||
// Delete flagged entities
|
|
||||||
while (vector_Size(sys->flaggedDelete)) {
|
|
||||||
uintptr_t id;
|
|
||||||
vector_Pop(sys->flaggedDelete, &id);
|
|
||||||
|
|
||||||
tree_Node *n;
|
|
||||||
n = tree_FindNode(sys->pos, id);
|
|
||||||
if (n)
|
|
||||||
tree_Delete(sys->pos, n);
|
|
||||||
n = tree_FindNode(sys->hit, id);
|
|
||||||
if (n)
|
|
||||||
tree_Delete(sys->hit, n);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (tree_Node *i = tree_FirstNode(sys->pos);
|
for (tree_Node *i = tree_FirstNode(sys->pos);
|
||||||
i != NULL;
|
i != NULL;
|
||||||
i = tree_Node_Next(i)) {
|
i = tree_Node_Next(i)) {
|
||||||
|
@ -54,7 +54,6 @@ typedef struct {
|
|||||||
App *super;
|
App *super;
|
||||||
// Every Position & Hitbox components
|
// Every Position & Hitbox components
|
||||||
tree_Tree *pos, *hit;
|
tree_Tree *pos, *hit;
|
||||||
vector_Vector *flaggedDelete;
|
|
||||||
} System_Physics;
|
} System_Physics;
|
||||||
|
|
||||||
// Returns an empty physics system.
|
// Returns an empty physics system.
|
||||||
|
Loading…
Reference in New Issue
Block a user