From e2ae30e45ecf42900c4b6eae3ee742f1fe1f8d7a Mon Sep 17 00:00:00 2001 From: En Yi Date: Tue, 31 Jan 2023 21:17:11 +0800 Subject: [PATCH] Prepare for menu scene implementation Changelog: - Add components: - selectable : indicate an entity is selectable - selection : indicate an entity has a list of choices - Add menu scene implementation and test - Reduce repetition in CMakeLists in specifying libraries --- CMakeLists.txt | 56 +++++++++++++++++++++++-------------- engine/CMakeLists.txt | 1 + engine/EC/components.h | 19 ++++++++++++- engine/EC/mempool.c | 4 +++ engine/menu_impl.c | 21 ++++++++++++++ engine/menu_impl.h | 18 ++++++++++++ engine/scene.h | 1 + menu_test.c | 63 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 162 insertions(+), 21 deletions(-) create mode 100644 engine/menu_impl.c create mode 100644 engine/menu_impl.h create mode 100644 menu_test.c diff --git a/CMakeLists.txt b/CMakeLists.txt index b4873f0..9f321e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,16 @@ project(${PROJECT_NAME} C) set(CMAKE_C_STANDARD 99) set(RAYLIB_DIR $ENV{HOME}/Documents/Coding/raylib/out/) +set(GAME_LIBS + lib_EC + lib_scenes + sc_queue + sc_map + sc_array + raylib + m +) + add_subdirectory(engine) add_executable(${PROJECT_NAME} @@ -23,12 +33,7 @@ target_link_directories(${PROJECT_NAME} ) target_link_libraries(${PROJECT_NAME} - lib_EC - lib_scenes - sc_queue - sc_map - raylib - m + ${GAME_LIBS} ) add_executable(EntManager_test @@ -71,13 +76,7 @@ target_link_directories(scene_test ) target_link_libraries(scene_test - lib_EC - lib_scenes - sc_queue - sc_map - sc_array - raylib - m + ${GAME_LIBS} ) add_executable(scene_test_mem @@ -98,12 +97,29 @@ target_link_directories(scene_test_mem ) target_link_libraries(scene_test_mem - lib_EC - lib_scenes - sc_queue - sc_map - sc_array - raylib - m + ${GAME_LIBS} ) target_link_options(scene_test PRIVATE -fsanitize=address -gdwarf-4 ) + +add_executable(menu_test + menu_test.c +) + +target_include_directories(menu_test + PRIVATE + ${CMAKE_CURRENT_LIST_DIR} + ${RAYLIB_DIR}/include +) + +target_compile_options(menu_test PRIVATE -fsanitize=address -gdwarf-4) +target_link_options(menu_test PRIVATE -fsanitize=address -gdwarf-4) + +target_link_directories(menu_test + PRIVATE + ${RAYLIB_DIR}/lib +) + +target_link_libraries(menu_test + ${GAME_LIBS} +) + diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index d2e8283..6e7c565 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -2,6 +2,7 @@ add_subdirectory(EC) add_library(lib_scenes STATIC scene.c scene_impl.c + menu_impl.c game_systems.c AABB.c ) diff --git a/engine/EC/components.h b/engine/EC/components.h index 94cf840..cd4dec7 100644 --- a/engine/EC/components.h +++ b/engine/EC/components.h @@ -4,7 +4,7 @@ #include // TODO: Look at sc to use macros to auto generate functions -#define N_COMPONENTS 7 +#define N_COMPONENTS 9 enum ComponentEnum { CBBOX_COMP_T, @@ -14,6 +14,8 @@ enum ComponentEnum CJUMP_COMP_T, CPLAYERSTATE_T, CCONTAINER_T, + CSELECTABLE_T, + CSELECTION_T, }; typedef enum ComponentEnum ComponentEnum_t; @@ -95,6 +97,21 @@ typedef struct _CContainer_t ContainerItem_t item; }CContainer_t; +typedef struct _CSelection_t +{ + uint8_t max_choices; + uint8_t curr_choice; + void * data_arr; + unsigned int elem_size; +}CSelection_t; + +typedef struct _CSelectable_t CSelectable_t; +typedef void(*selected_callback_t)(CSelectable_t *); + +struct _CSelectable_t +{ + selected_callback_t callback; +}; static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y) { diff --git a/engine/EC/mempool.c b/engine/EC/mempool.c index 9c573a5..fdfec19 100644 --- a/engine/EC/mempool.c +++ b/engine/EC/mempool.c @@ -11,6 +11,8 @@ static CMovementState_t cmstate_buffer[MAX_COMP_POOL_SIZE]; static CJump_t cjump_buffer[1]; // Only player is expected to have this static CPlayerState_t cplayerstate_buffer[1]; // Only player is expected to have this static CContainer_t ccontainer_buffer[MAX_COMP_POOL_SIZE]; +static CSelectable_t cselectable_buffer[32]; +static CSelection_t cselection_buffer[32]; // Use hashmap as a Set // Use list will be used to check if an object exist @@ -37,6 +39,8 @@ static MemPool_t comp_mempools[N_COMPONENTS] = {cjump_buffer, 1, sizeof(CJump_t), NULL, {0}}, {cplayerstate_buffer, 1, sizeof(CPlayerState_t), NULL, {0}}, {ccontainer_buffer, MAX_COMP_POOL_SIZE, sizeof(CContainer_t), NULL, {0}}, + {cselectable_buffer, 32, sizeof(CSelectable_t), NULL, {0}}, + {cselection_buffer, 32, sizeof(CSelection_t), NULL, {0}}, }; static MemPool_t ent_mempool = {entity_buffer, MAX_COMP_POOL_SIZE, sizeof(Entity_t), NULL, {0}}; diff --git a/engine/menu_impl.c b/engine/menu_impl.c new file mode 100644 index 0000000..ee0b56d --- /dev/null +++ b/engine/menu_impl.c @@ -0,0 +1,21 @@ +#include "menu_impl.h" + + +static void menu_scene_render_func(Scene_t *scene) +{ +} + +static void menu_do_action(Scene_t *scene, ActionType_t action, bool pressed) +{ +} + +void init_menu_scene(MenuScene_t *scene) +{ + init_scene(&scene->scene, MENU_SCENE, &menu_scene_render_func, &menu_do_action); + scene->scene.scene_data = &scene->data; +} + +void free_menu_scene(MenuScene_t *scene) +{ + free_scene(&scene->scene); +} diff --git a/engine/menu_impl.h b/engine/menu_impl.h new file mode 100644 index 0000000..77bf842 --- /dev/null +++ b/engine/menu_impl.h @@ -0,0 +1,18 @@ +#ifndef __MENU_IMPL_H +#define __MENU_IMPL_H +#include "scene.h" + +typedef struct MenuSceneData +{ + Entity_t menus[32]; +}MenuSceneData_t; + +typedef struct MenuScene +{ + Scene_t scene; + MenuSceneData_t data; +}MenuScene_t; + +void init_menu_scene(MenuScene_t *scene); +void free_menu_scene(MenuScene_t *scene); +#endif diff --git a/engine/scene.h b/engine/scene.h index cafe45d..c342a52 100644 --- a/engine/scene.h +++ b/engine/scene.h @@ -6,6 +6,7 @@ typedef enum SceneType { LEVEL_SCENE = 0, + MENU_SCENE, }SceneType_t; typedef struct Scene Scene_t; diff --git a/menu_test.c b/menu_test.c new file mode 100644 index 0000000..70b4f8c --- /dev/null +++ b/menu_test.c @@ -0,0 +1,63 @@ +#include "mempool.h" +#include "menu_impl.h" +#include +#include + +// Maintain own queue to handle key presses +struct sc_queue_32 key_buffer; + +int main(void) +{ + sc_queue_init(&key_buffer); + InitWindow(1280, 640, "raylib"); + SetTargetFPS(60); + init_memory_pools(); + MenuScene_t scene; + init_menu_scene(&scene); + while(true) + { + + // This entire key processing relies on the assumption that a pressed key will + // appear in the polling of raylib + + unsigned int sz = sc_queue_size(&key_buffer); + // Process any existing pressed key + for (size_t i=0; i