From 4a926f8c53cbfca760e950a0acdb79d4fdfec200 Mon Sep 17 00:00:00 2001 From: En Yi Date: Sat, 26 Nov 2022 23:27:45 +0800 Subject: [PATCH] Implement tag on entities Internal Changelog: - Use data store similar to component map - Add init and free for entities tag - Use enum instead of char for tag - Switch to use clang for compilation - Add -Wall for compilation --- CMakeLists.txt | 3 ++- entManager.c | 19 ++++++++++++++----- entManager.h | 7 ++++--- entity.h | 12 ++++++++++-- main.c | 4 ++-- mempool.c | 2 +- 6 files changed, 33 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 676313d..0c43be5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ set(PROJECT_NAME HATPC_remake) - +set(CMAKE_C_COMPILER clang) +set(CMAKE_C_FLAGS "-Wall") cmake_minimum_required(VERSION 3.22.1) project(${PROJECT_NAME} C) set(CMAKE_C_STANDARD 99) diff --git a/entManager.c b/entManager.c index 418a833..1ce26e9 100644 --- a/entManager.c +++ b/entManager.c @@ -7,6 +7,10 @@ void init_entity_manager(EntityManager_t *p_manager) { sc_map_init_64v(p_manager->component_map + i, MAX_COMP_POOL_SIZE, 0); } + for (size_t i=0; ientities_map + i, MAX_COMP_POOL_SIZE, 0); + } sc_queue_init(&p_manager->to_add); sc_queue_init(&p_manager->to_remove); } @@ -28,6 +32,7 @@ void update_entity_manager(EntityManager_t *p_manager) { free_component_to_mempool((ComponentEnum_t)comp_type_idx, comp_idx); sc_map_del_64v(&p_manager->component_map[comp_type_idx], comp_idx); + sc_map_del_64v(&p_manager->entities_map[p_entity->m_tag], e_idx); } free_entity_to_mempool(e_idx); sc_map_del_64v(&p_manager->entities, e_idx); @@ -35,8 +40,9 @@ void update_entity_manager(EntityManager_t *p_manager) sc_queue_clear(&p_manager->to_remove); sc_queue_foreach (&p_manager->to_add, e_idx) { - Entity_t *ent = get_entity_wtih_id(e_idx); - sc_map_put_64v(&p_manager->entities, e_idx, (void *)ent); + Entity_t *p_entity = get_entity_wtih_id(e_idx); + sc_map_put_64v(&p_manager->entities, e_idx, (void *)p_entity); + sc_map_put_64v(&p_manager->entities_map[p_entity->m_tag], e_idx, (void *)p_entity); } sc_queue_clear(&p_manager->to_add); @@ -61,14 +67,19 @@ void free_entity_manager(EntityManager_t *p_manager) { sc_map_term_64v(p_manager->component_map + i); } + for (size_t i=0; ientities_map + i); + } sc_queue_term(&p_manager->to_add); sc_queue_term(&p_manager->to_remove); } -Entity_t *add_entity(EntityManager_t *p_manager, const char *tag) +Entity_t *add_entity(EntityManager_t *p_manager, EntityTag_t tag) { unsigned long e_idx = 0; Entity_t * p_ent = new_entity_from_mempool(&e_idx); + p_ent->m_tag = tag; if (p_ent) { sc_queue_add_last(&p_manager->to_add, e_idx); @@ -78,8 +89,6 @@ Entity_t *add_entity(EntityManager_t *p_manager, const char *tag) void remove_entity(EntityManager_t *p_manager, unsigned long id) { - unsigned long comp_type, comp_id; - Entity_t *p_entity = sc_map_get_64v(&p_manager->entities, id); if(!sc_map_found(&p_manager->entities)) return; // This only marks the entity for deletion diff --git a/entManager.h b/entManager.h index c3342e0..553d90c 100644 --- a/entManager.h +++ b/entManager.h @@ -7,8 +7,9 @@ typedef struct EntityManager { // All fields are Read-Only - struct sc_map_64v entities; - struct sc_map_64v component_map[N_COMPONENTS]; + struct sc_map_64v entities; // id : entity + struct sc_map_64v entities_map[N_TAGS]; // [{id: ent}] + struct sc_map_64v component_map[N_COMPONENTS]; // [{id: comp}, ...] struct sc_queue_uint to_add; struct sc_queue_uint to_remove; }EntityManager_t; @@ -18,7 +19,7 @@ void update_entity_manager(EntityManager_t *p_manager); void clear_entity_manager(EntityManager_t *p_manager); void free_entity_manager(EntityManager_t *p_manager); -Entity_t *add_entity(EntityManager_t *p_manager, const char *tag); +Entity_t *add_entity(EntityManager_t *p_manager, EntityTag_t tag); void remove_entity(EntityManager_t *p_manager, unsigned long id); void *add_component(EntityManager_t *p_manager, Entity_t *entity, ComponentEnum_t comp_type); diff --git a/entity.h b/entity.h index 361d818..4adbed3 100644 --- a/entity.h +++ b/entity.h @@ -2,11 +2,19 @@ #define __ENTITY_H #include #include "sc/map/sc_map.h" -#define MAX_TAG_LEN 32 + +#define N_TAGS 3 +enum EntityTag +{ + NO_ENT_TAG, + PLAYER_ENT_TAG, + ENEMY_ENT_TAG, +}; +typedef enum EntityTag EntityTag_t; typedef struct Entity { unsigned long m_id; - char m_tag[MAX_TAG_LEN]; + EntityTag_t m_tag; bool m_alive; struct sc_map_64 components; }Entity_t; diff --git a/main.c b/main.c index 0090e94..2002692 100644 --- a/main.c +++ b/main.c @@ -11,10 +11,10 @@ int main(void) init_entity_manager(&manager); puts("Creating two entities"); - Entity_t *p_ent = add_entity(&manager, "player"); + Entity_t *p_ent = add_entity(&manager, PLAYER_ENT_TAG); CBBox_t * p_bbox = (CBBox_t *)add_component(&manager, p_ent, CBBOX_COMP_T); p_bbox->x = 15; - p_ent = add_entity(&manager, "enemy"); + p_ent = add_entity(&manager, ENEMY_ENT_TAG); p_bbox = (CBBox_t *)add_component(&manager, p_ent, CBBOX_COMP_T); p_bbox->x = 40; update_entity_manager(&manager); diff --git a/mempool.c b/mempool.c index 6d5ba5e..a9ca171 100644 --- a/mempool.c +++ b/mempool.c @@ -72,7 +72,7 @@ Entity_t* new_entity_from_mempool(unsigned long *p_e_idx) Entity_t * ent = entity_mem_pool.entity_buffer + e_idx; sc_map_clear_64(&ent->components); ent->m_alive = true; - memset(ent->m_tag, 0, MAX_TAG_LEN); + ent->m_tag = NO_ENT_TAG; return ent; }