ecs updates, still no compile

This commit is contained in:
2023-08-11 14:24:46 -04:00
parent 68746a654f
commit 6325c4e4a7
14 changed files with 358 additions and 1956 deletions

View File

@ -1,6 +1,6 @@
#pragma once
#include "../../COMMON.h"
#include "../../engine/vector.h"
#define ECS_FUNCTION_COUNT ECS_FUNCTION_DRAW + 1
typedef enum ECSFunctionType
@ -17,11 +17,11 @@ typedef enum ECSComponentType
ECS_COMPONENT_SPRITE
} ECSComponentType;
typedef struct ECS;
typedef struct Game;
typedef struct ECS ECS;
typedef struct Game Game;
typedef void (*ECSRegister)(ECS);
typedef void (*ECSFunction)(ECS, void*);
typedef void (*ECSRegister)(ECS*);
typedef void (*ECSFunction)(void*, ECS*);
typedef struct ECSsystem
{
@ -33,14 +33,15 @@ typedef struct ECSComponentInfo
ECSSystem system;
ECSComponentType type;
size_t size;
};
} ECSComponentInfo;
typedef struct ECSComponentList
{
ECSSustem system;
ECS* ecs;
ECSSystem system;
ECSComponentType type;
void* components; /* dynamic array */
};
Vector components; /* whatever type is being used */
} ECSComponentList;
typedef struct ECS
{

View File

@ -1,10 +1,51 @@
#include "component_sprite.h"
static void _tick(ComponentSprite* self, ECS* ecs);
static void _draw(ComponentSprite* self, ECS* ecs);
const ECSComponentInfo COMPONENT_SPRITE_INFO =
{
.system =
{
.functions =
{
NULL,
NULL,
(ECSFunction)_tick,
(ECSFunction)_draw
}
},
.type = ECS_COMPONENT_SPRITE,
.size = sizeof(ComponentSprite)
};
/* Ticks sprite component. */
static void
_tick(ComponentSprite* self, ECS* ecs)
{
}
/* Draws sprite component. */
static void
_draw(ComponentSprite* self, ECS* ecs)
{
}
/* Initializes sprite component. */
void
component_sprite_init
(
ComponentSprite* self
@ -22,26 +63,4 @@ component_sprite_init
}
/* Ticks sprite component. */
void
component_sprite_tick(ComponentSprite* self, ECS* ecs)
{
}
/* Draws sprite component. */
void
component_sprite_draw(ComponentSprite* self, ECS* ecs)
{
}

View File

@ -2,6 +2,8 @@
#include "../../GAME_COMMON.h"
extern ECSComponentInfo COMPONENT_SPRITE_INFO;
typedef struct ComponentSprite
{
u32 id;
@ -13,12 +15,4 @@ typedef struct ComponentSprite
f32 rotation;
} ComponentSprite;
static const ECSComponentInfo COMPONENT_SPRITE_INFO =
{
.system =
{
.functions = { NULL, NULL, NULL, NULL}
},
.type = ECS_COMPONENT_SPRITE,
.size = sizeof(ComponentSprite)
};
void component_sprite_init(ComponentSprite* self);

View File

@ -9,15 +9,11 @@ _ecs_function(ECS* self, ECSFunctionType type)
{
for (s32 i = 0; i < ECS_COMPONENT_COUNT; i++)
{
ECSFunction function;
struct ECSComponentList* list;
ECSComponentList* list;
list = &game.ecs.lists[i];
list = &self->lists[i];
function = list->system.functions[type];
if (function)
ecs_component_function(list, function);
ecs_component_list_function(list, type);
}
}
@ -40,11 +36,25 @@ void
ecs_init(ECS* self, Game* game)
{
self->game = game;
for (s32 i = 0 ; i < ECS_COMPONENT_COUNT; i++)
ecs_component_list_init(&self->lists[i], self, ECS_COMPONENT_INFO[i]);
}
/* Clears ECS. */
void
ecs_clear(ECS* self)
{
for (s32 i = 0 ; i < ECS_COMPONENT_COUNT; i++)
ecs_component_list_clear(&self->lists[i]);
}
/* Frees ECS. */
void
ecs_free(ECS* self)
{
for (s32 i = 0 ; i < ECS_COMPONENT_COUNT; i++)
ecs_component_list_free(&self->lists[i]);
memset(self, '\0', sizeof(ECS));
}

15
src/game/ecs/ecs.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include "ecs_component_list.h"
#include "component/component_sprite.h"
static const ECSComponentInfo ECS_COMPONENT_INFO[ECS_COMPONENT_COUNT] =
{
COMPONENT_SPRITE_INFO
};
void ecs_tick(ECS* self);
void ecs_draw(ECS* self);
void ecs_init(ECS* self, Game* game);
void ecs_clear(ECS* self);
void ecs_free(ECS* self);

View File

@ -1,10 +0,0 @@
#include "ecs_component.h"
/* Executes a function on a component. */
void
ecs_component_function(ECSComponentList* self, ECSFunction function)
{
}

View File

@ -0,0 +1,42 @@
#include "ecs_component_list.h"
/* From ECSComponentInfo, initialize a component list. */
void
ecs_component_list_init(ECSComponentList* self, ECS* ecs, ECSComponentInfo info)
{
self->ecs = ecs;
self->type = info.type;
self->system = info.system;
vector_init(&self->vector, info.size);
}
/* Executes a function on a component list. */
/* Exits if component list has a NULL function. */
void
ecs_component_list_function(ECSComponentList* self, ECSFunctionType type)
{
ECSFunction function;
function = self->system.functions[type];
if (!function)
return;
for (s32 i = 0; i < self->components.count; i++)
function(vector_get(i), self->ecs);
}
/* Clears all elements in a component list. */
void
ecs_component_list_clear(ECSComponentList* self)
{
vector_clear(&self->components);
}
/* Frees a component list. */
void
ecs_component_list_free(ECSComponentList* self)
{
vector_free(&self->components);
memset(self, '\0', sizeof(ECSComponentList));
}

View File

@ -0,0 +1,8 @@
#pragma once
#include "ECS_COMMON.h"
void ecs_component_list_init(ECSComponentList* self, ECS* ecs, ECSComponentInfo info);
void ecs_component_list_function(ECSComponentList* self, ECSFunctionType type);
void ecs_component_list_clear(ECSComponentList* self);
void ecs_component_list_free(ECSComponentList* self);