Experiment with extern component mempool

Internal Changelog:
- Split assets from engine so that rres_packer do not need to implement
  mempools
- The idea is that mempool is incomplete and requires implementation of
  the mempool in scenes.
    - Components are usually game-specific, so this is the first step to
      decouple it from the engine.
main
En Yi 2024-08-19 17:33:05 +08:00
parent e4b5695a15
commit d2af974b29
6 changed files with 75 additions and 62 deletions

View File

@ -1,34 +1,42 @@
add_subdirectory(sc)
add_library(lib_engine STATIC
add_library(lib_assets STATIC
assets.c
AABB.c
gui.c
engine.c
collisions.c
rres.c
mempool.c
entManager.c
particle_sys.c
)
target_include_directories(lib_engine
target_include_directories(lib_assets
PRIVATE
${LIBZSTD_DIR}/include
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
${RAYLIB_DIR}/include
)
target_link_directories(lib_engine
target_link_directories(lib_assets
PUBLIC
${RAYLIB_DIR}/lib
${LIBZSTD_DIR}/lib
)
target_link_libraries(lib_engine
target_link_libraries(lib_assets
PUBLIC
zstd
raylib
sc_queue
sc_heap
sc_map
m
)
add_library(lib_engine OBJECT
AABB.c
gui.c
engine.c
collisions.c
mempool.c
entManager.c
)
target_link_libraries(lib_engine
PUBLIC
lib_assets
sc_heap
sc_array
m
)

View File

@ -5,14 +5,6 @@
#include <assert.h>
#include <string.h>
typedef struct ULongCircBuffer {
unsigned long* buffer; // data buffer
unsigned long* buffer_end; // end of data buffer
uint32_t capacity; // maximum number of items in the buffer
uint32_t count; // number of items in the buffer
unsigned long* head; // pointer to head
unsigned long* tail; // pointer to tail
}ULongCircBuffer_t;
static void cb_init(ULongCircBuffer_t* cb, size_t capacity)
{
@ -53,50 +45,8 @@ static bool cb_push_back(ULongCircBuffer_t* cb, unsigned long item)
return true;
}
typedef struct MemPool {
void * const buffer;
const unsigned long max_size;
const unsigned long elem_size;
bool *use_list;
ULongCircBuffer_t free_list;
} MemPool_t;
// Static allocate buffers
static Entity_t entity_buffer[MAX_COMP_POOL_SIZE];
static CBBox_t bbox_buffer[MAX_COMP_POOL_SIZE];
static CTransform_t ctransform_buffer[MAX_COMP_POOL_SIZE];
static CTileCoord_t ctilecoord_buffer[MAX_COMP_POOL_SIZE];
static CMovementState_t cmstate_buffer[MAX_COMP_POOL_SIZE];
static CJump_t cjump_buffer[8]; // Only player is expected to have this
static CPlayerState_t cplayerstate_buffer[8]; // Only player is expected to have this
static CContainer_t ccontainer_buffer[MAX_COMP_POOL_SIZE];
static CHitBoxes_t chitboxes_buffer[MAX_COMP_POOL_SIZE];
static CHurtbox_t churtbox_buffer[MAX_COMP_POOL_SIZE];
static CSprite_t csprite_buffer[MAX_COMP_POOL_SIZE];
static CMoveable_t cmoveable_buffer[MAX_COMP_POOL_SIZE];
static CLifeTimer_t clifetimer_buffer[MAX_COMP_POOL_SIZE];
static CWaterRunner_t cwaterrunner_buffer[32];
static CAirTimer_t cairtimer_buffer[8]; // Only player is expected to have this
static CEmitter_t cemitter_buffer[MAX_COMP_POOL_SIZE]; // Only player is expected to have this
// Static allocate mempools
static MemPool_t comp_mempools[N_COMPONENTS] = {
{bbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CBBox_t), NULL, {0}},
{ctransform_buffer, MAX_COMP_POOL_SIZE, sizeof(CTransform_t), NULL, {0}},
{ctilecoord_buffer, MAX_COMP_POOL_SIZE, sizeof(CTileCoord_t), NULL, {0}},
{cmstate_buffer, MAX_COMP_POOL_SIZE, sizeof(CMovementState_t), NULL, {0}},
{cjump_buffer, 8, sizeof(CJump_t), NULL, {0}},
{cplayerstate_buffer, 8, sizeof(CPlayerState_t), NULL, {0}},
{ccontainer_buffer, MAX_COMP_POOL_SIZE, sizeof(CContainer_t), NULL, {0}},
{chitboxes_buffer, MAX_COMP_POOL_SIZE, sizeof(CHitBoxes_t), NULL, {0}},
{churtbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CHurtbox_t), NULL, {0}},
{csprite_buffer, MAX_COMP_POOL_SIZE, sizeof(CSprite_t), NULL, {0}},
{cmoveable_buffer, MAX_COMP_POOL_SIZE, sizeof(CMoveable_t), NULL, {0}},
{clifetimer_buffer, MAX_COMP_POOL_SIZE, sizeof(CLifeTimer_t), NULL, {0}},
{cwaterrunner_buffer, 32, sizeof(CWaterRunner_t), NULL, {0}},
{cairtimer_buffer, 8, sizeof(CAirTimer_t), NULL, {0}},
{cemitter_buffer, MAX_COMP_POOL_SIZE, sizeof(CEmitter_t), NULL, {0}},
};
static MemPool_t ent_mempool = {
.buffer = entity_buffer,
.max_size = MAX_COMP_POOL_SIZE,

View File

@ -4,6 +4,24 @@
void init_memory_pools(void);
void free_memory_pools(void);
typedef struct ULongCircBuffer {
unsigned long* buffer; // data buffer
unsigned long* buffer_end; // end of data buffer
uint32_t capacity; // maximum number of items in the buffer
uint32_t count; // number of items in the buffer
unsigned long* head; // pointer to head
unsigned long* tail; // pointer to tail
}ULongCircBuffer_t;
typedef struct MemPool {
void * const buffer;
const unsigned long max_size;
const unsigned long elem_size;
bool *use_list;
ULongCircBuffer_t free_list;
} MemPool_t;
extern MemPool_t comp_mempools[N_COMPONENTS];
Entity_t* new_entity_from_mempool(unsigned long* e_idx_ptr);
Entity_t* get_entity_wtih_id(unsigned long idx);
void free_entity_to_mempool(unsigned long idx);

View File

@ -8,6 +8,6 @@ set_target_properties(rres_packer
target_link_libraries(rres_packer
PRIVATE
lib_engine
lib_assets
)

View File

@ -10,6 +10,7 @@ add_library(lib_scenes STATIC
game_systems.c
scene_systems.c
camera_systems.c
engine_impl.c
)
target_include_directories(lib_scenes
PUBLIC

View File

@ -0,0 +1,36 @@
#include "mempool.h"
// Static allocate buffers
static CBBox_t bbox_buffer[MAX_COMP_POOL_SIZE];
static CTransform_t ctransform_buffer[MAX_COMP_POOL_SIZE];
static CTileCoord_t ctilecoord_buffer[MAX_COMP_POOL_SIZE];
static CMovementState_t cmstate_buffer[MAX_COMP_POOL_SIZE];
static CJump_t cjump_buffer[8]; // Only player is expected to have this
static CPlayerState_t cplayerstate_buffer[8]; // Only player is expected to have this
static CContainer_t ccontainer_buffer[MAX_COMP_POOL_SIZE];
static CHitBoxes_t chitboxes_buffer[MAX_COMP_POOL_SIZE];
static CHurtbox_t churtbox_buffer[MAX_COMP_POOL_SIZE];
static CSprite_t csprite_buffer[MAX_COMP_POOL_SIZE];
static CMoveable_t cmoveable_buffer[MAX_COMP_POOL_SIZE];
static CLifeTimer_t clifetimer_buffer[MAX_COMP_POOL_SIZE];
static CWaterRunner_t cwaterrunner_buffer[32];
static CAirTimer_t cairtimer_buffer[8]; // Only player is expected to have this
static CEmitter_t cemitter_buffer[MAX_COMP_POOL_SIZE]; // Only player is expected to have this
;
MemPool_t comp_mempools[N_COMPONENTS] = {
{bbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CBBox_t), NULL, {0}},
{ctransform_buffer, MAX_COMP_POOL_SIZE, sizeof(CTransform_t), NULL, {0}},
{ctilecoord_buffer, MAX_COMP_POOL_SIZE, sizeof(CTileCoord_t), NULL, {0}},
{cmstate_buffer, MAX_COMP_POOL_SIZE, sizeof(CMovementState_t), NULL, {0}},
{cjump_buffer, 8, sizeof(CJump_t), NULL, {0}},
{cplayerstate_buffer, 8, sizeof(CPlayerState_t), NULL, {0}},
{ccontainer_buffer, MAX_COMP_POOL_SIZE, sizeof(CContainer_t), NULL, {0}},
{chitboxes_buffer, MAX_COMP_POOL_SIZE, sizeof(CHitBoxes_t), NULL, {0}},
{churtbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CHurtbox_t), NULL, {0}},
{csprite_buffer, MAX_COMP_POOL_SIZE, sizeof(CSprite_t), NULL, {0}},
{cmoveable_buffer, MAX_COMP_POOL_SIZE, sizeof(CMoveable_t), NULL, {0}},
{clifetimer_buffer, MAX_COMP_POOL_SIZE, sizeof(CLifeTimer_t), NULL, {0}},
{cwaterrunner_buffer, 32, sizeof(CWaterRunner_t), NULL, {0}},
{cairtimer_buffer, 8, sizeof(CAirTimer_t), NULL, {0}},
{cemitter_buffer, MAX_COMP_POOL_SIZE, sizeof(CEmitter_t), NULL, {0}},
};