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
scene_man
En Yi 2023-01-31 21:17:11 +08:00
parent 896a23fb5c
commit e2ae30e45e
8 changed files with 162 additions and 21 deletions

View File

@ -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}
)

View File

@ -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
)

View File

@ -4,7 +4,7 @@
#include <stdint.h>
// 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)
{

View File

@ -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}};

21
engine/menu_impl.c 100644
View File

@ -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);
}

18
engine/menu_impl.h 100644
View File

@ -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

View File

@ -6,6 +6,7 @@
typedef enum SceneType
{
LEVEL_SCENE = 0,
MENU_SCENE,
}SceneType_t;
typedef struct Scene Scene_t;

63
menu_test.c 100644
View File

@ -0,0 +1,63 @@
#include "mempool.h"
#include "menu_impl.h"
#include <stdio.h>
#include <unistd.h>
// 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<sz; i++)
{
int button = sc_queue_del_first(&key_buffer);
ActionType_t action = sc_map_get_64(&scene.scene.action_map, button);
if (IsKeyReleased(button))
{
do_action(&scene.scene, action, false);
}
else
{
do_action(&scene.scene, action, true);
sc_queue_add_last(&key_buffer, button);
}
}
// Detect new key presses
while(true)
{
int button = GetKeyPressed();
if (button == 0) break;
ActionType_t action = sc_map_get_64(&scene.scene.action_map, button);
if (!sc_map_found(&scene.scene.action_map)) continue;
do_action(&scene.scene, action, true);
sc_queue_add_last(&key_buffer, button);
}
update_scene(&scene.scene);
update_entity_manager(&scene.scene.ent_manager);
// This is needed to advance time delta
BeginDrawing();
render_scene(&scene.scene);
ClearBackground(RAYWHITE);
EndDrawing();
if (WindowShouldClose()) break;
}
CloseWindow();
free_menu_scene(&scene);
sc_queue_term(&key_buffer);
}