Replace component data struct to use array

This will use more memory from the get go. I just prefer to statically
allocate memory.
scene_man
En Yi 2023-05-22 21:00:50 +08:00
parent b0e1d33ad6
commit e1a7774bfb
4 changed files with 16 additions and 20 deletions

View File

@ -5,7 +5,6 @@
#include "entity.h" #include "entity.h"
// TODO: Look at sc to use macros to auto generate functions // TODO: Look at sc to use macros to auto generate functions
#define N_COMPONENTS 10
typedef enum ComponentEnum { typedef enum ComponentEnum {
CBBOX_COMP_T, CBBOX_COMP_T,
CTRANSFORM_COMP_T, CTRANSFORM_COMP_T,

View File

@ -21,20 +21,19 @@ void update_entity_manager(EntityManager_t* p_manager)
// It does not make new entities, but will free entity // It does not make new entities, but will free entity
// New entities are assigned during add_entity // New entities are assigned during add_entity
unsigned long e_idx; unsigned long e_idx;
unsigned long comp_type_idx;
unsigned long comp_idx;
sc_queue_foreach (&p_manager->to_remove, e_idx) sc_queue_foreach (&p_manager->to_remove, e_idx)
{ {
Entity_t *p_entity = (Entity_t *)sc_map_get_64v(&p_manager->entities, e_idx); Entity_t *p_entity = (Entity_t *)sc_map_get_64v(&p_manager->entities, e_idx);
if (!p_entity) continue; if (!p_entity) continue;
sc_map_foreach (&p_entity->components, comp_type_idx, comp_idx) for (size_t i = 0; i < N_COMPONENTS; ++i)
{ {
free_component_to_mempool((ComponentEnum_t)comp_type_idx, comp_idx); if (p_entity->components[i] == MAX_COMP_POOL_SIZE) continue;
sc_map_del_64v(&p_manager->component_map[comp_type_idx], e_idx);
free_component_to_mempool((ComponentEnum_t)i, p_entity->components[i]);
sc_map_del_64v(&p_manager->component_map[i], e_idx);
sc_map_del_64v(&p_manager->entities_map[p_entity->m_tag], e_idx); sc_map_del_64v(&p_manager->entities_map[p_entity->m_tag], e_idx);
} }
sc_map_clear_64(&p_entity->components);
free_entity_to_mempool(e_idx); free_entity_to_mempool(e_idx);
sc_map_del_64v(&p_manager->entities, e_idx); sc_map_del_64v(&p_manager->entities, e_idx);
} }
@ -116,7 +115,7 @@ void* add_component(EntityManager_t* p_manager, Entity_t* p_entity, ComponentEnu
void* p_comp = new_component_from_mempool(comp_type, &comp_idx); void* p_comp = new_component_from_mempool(comp_type, &comp_idx);
if (p_comp) if (p_comp)
{ {
sc_map_put_64(&p_entity->components, comp_type_idx, comp_idx); p_entity->components[comp_type] = comp_idx;
sc_map_put_64v(&p_manager->component_map[comp_type_idx], p_entity->m_id, p_comp); sc_map_put_64v(&p_manager->component_map[comp_type_idx], p_entity->m_id, p_comp);
} }
return p_comp; return p_comp;
@ -126,7 +125,6 @@ void* get_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentEnu
{ {
unsigned long comp_type_idx = (unsigned long)comp_type; unsigned long comp_type_idx = (unsigned long)comp_type;
void * p_comp = sc_map_get_64v(&p_manager->component_map[comp_type_idx], p_entity->m_id); void * p_comp = sc_map_get_64v(&p_manager->component_map[comp_type_idx], p_entity->m_id);
//unsigned long comp_idx = sc_map_get_64(&p_entity->components, comp_type_idx);
if (!sc_map_found(&p_manager->component_map[comp_type_idx])) return NULL; if (!sc_map_found(&p_manager->component_map[comp_type_idx])) return NULL;
return p_comp; return p_comp;
} }
@ -134,8 +132,7 @@ void* get_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentEnu
void remove_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentEnum_t comp_type) void remove_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentEnum_t comp_type)
{ {
unsigned long comp_type_idx = (unsigned long)comp_type; unsigned long comp_type_idx = (unsigned long)comp_type;
unsigned long comp_idx = sc_map_del_64(&p_entity->components, comp_type_idx); if (p_entity->components[comp_type] == MAX_COMP_POOL_SIZE) return;
if (!sc_map_found(&p_entity->components)) return;
sc_map_del_64v(&p_manager->component_map[comp_type_idx], p_entity->m_id); sc_map_del_64v(&p_manager->component_map[comp_type_idx], p_entity->m_id);
free_component_to_mempool(comp_type, comp_idx); free_component_to_mempool(comp_type, p_entity->components[comp_type]);
} }

View File

@ -1,9 +1,9 @@
#ifndef __ENTITY_H #ifndef __ENTITY_H
#define __ENTITY_H #define __ENTITY_H
#include <stdbool.h> #include <stdbool.h>
#include "sc/map/sc_map.h"
#define N_TAGS 4 #define N_TAGS 4
#define N_COMPONENTS 10
typedef enum EntityTag { typedef enum EntityTag {
NO_ENT_TAG, NO_ENT_TAG,
PLAYER_ENT_TAG, PLAYER_ENT_TAG,
@ -15,6 +15,7 @@ typedef struct Entity {
unsigned long m_id; unsigned long m_id;
EntityTag_t m_tag; EntityTag_t m_tag;
bool m_alive; bool m_alive;
struct sc_map_64 components; unsigned long components[N_COMPONENTS];
} Entity_t; } Entity_t;
#endif // __ENTITY_H #endif // __ENTITY_H

View File

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <assert.h> #include <assert.h>
#include <string.h>
// Static allocate buffers // Static allocate buffers
static Entity_t entity_buffer[MAX_COMP_POOL_SIZE]; static Entity_t entity_buffer[MAX_COMP_POOL_SIZE];
@ -118,7 +119,6 @@ void init_memory_pools(void)
for (size_t i = 0; i < ent_mempool.max_size; ++i) for (size_t i = 0; i < ent_mempool.max_size; ++i)
{ {
entity_buffer[i].m_id = i; entity_buffer[i].m_id = i;
sc_map_init_64(&entity_buffer[i].components, 16 ,0);
ent_mempool.free_list.buffer[i] = i; ent_mempool.free_list.buffer[i] = i;
} }
ent_mempool.free_list.count = ent_mempool.max_size; ent_mempool.free_list.count = ent_mempool.max_size;
@ -138,10 +138,6 @@ void free_memory_pools(void)
free(ent_mempool.use_list); free(ent_mempool.use_list);
cb_free(&ent_mempool.free_list); cb_free(&ent_mempool.free_list);
for (int i = 0; i < MAX_COMP_POOL_SIZE; ++i)
{
sc_map_term_64(&entity_buffer[i].components);
}
pool_inited = false; pool_inited = false;
} }
} }
@ -154,7 +150,10 @@ Entity_t* new_entity_from_mempool(unsigned long* e_idx_ptr)
*e_idx_ptr = e_idx; *e_idx_ptr = e_idx;
ent_mempool.use_list[e_idx] = true; ent_mempool.use_list[e_idx] = true;
Entity_t* ent = entity_buffer + e_idx; Entity_t* ent = entity_buffer + e_idx;
sc_map_clear_64(&ent->components); for (size_t j = 0; j< N_COMPONENTS; j++)
{
ent->components[j] = MAX_COMP_POOL_SIZE;
}
ent->m_alive = true; ent->m_alive = true;
ent->m_tag = NO_ENT_TAG; ent->m_tag = NO_ENT_TAG;
return ent; return ent;