From 0d4089d9ce95912d16748d5465b37d1cada478ce Mon Sep 17 00:00:00 2001 From: En Yi Date: Wed, 1 May 2024 19:56:10 +0800 Subject: [PATCH] Tweak scene render function Delegate BeginDrawing and EndDrawing to individual scenes --- engine/engine.c | 9 +++++---- engine/engine.h | 5 +++-- engine/engine_conf.h | 9 +++++---- scenes/editor_scene.c | 2 ++ scenes/game_scene.c | 2 ++ scenes/menu_scene.c | 14 ++++++++------ 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/engine/engine.c b/engine/engine.c index c98747b..caa0b26 100644 --- a/engine/engine.c +++ b/engine/engine.c @@ -143,7 +143,7 @@ void update_sfx_list(GameEngine_t* engine) } //void init_scene(Scene_t* scene, SceneType_t scene_type, system_func_t render_func, action_func_t action_func) -void init_scene(Scene_t* scene, system_func_t render_func, action_func_t action_func) +void init_scene(Scene_t* scene, render_func_t render_func, action_func_t action_func) { sc_map_init_64(&scene->action_map, 32, 0); sc_array_init(&scene->systems); @@ -178,9 +178,10 @@ inline void update_scene(Scene_t* scene, float delta_time) inline void render_scene(Scene_t* scene) { - BeginDrawing(); - scene->render_function(scene); - EndDrawing(); + if (scene->render_function != NULL) + { + scene->render_function(scene); + } } inline void do_action(Scene_t* scene, ActionType_t action, bool pressed) diff --git a/engine/engine.h b/engine/engine.h index bf77219..1147550 100644 --- a/engine/engine.h +++ b/engine/engine.h @@ -37,6 +37,7 @@ typedef enum SceneState { SCENE_ENDED, }SceneState_t; +typedef void(*render_func_t)(Scene_t*); typedef void(*system_func_t)(Scene_t*); typedef void(*action_func_t)(Scene_t*, ActionType_t, bool); sc_array_def(system_func_t, systems); @@ -44,7 +45,7 @@ sc_array_def(system_func_t, systems); struct Scene { struct sc_map_64 action_map; // key -> actions struct sc_array_systems systems; - system_func_t render_function; + render_func_t render_function; action_func_t action_function; EntityManager_t ent_manager; float delta_time; @@ -72,7 +73,7 @@ extern void render_scene(Scene_t* scene); extern void do_action(Scene_t* scene, ActionType_t action, bool pressed); //void init_scene(Scene_t* scene, SceneType_t scene_type, system_func_t render_func, action_func_t action_func); -void init_scene(Scene_t* scene, system_func_t render_func, action_func_t action_func); +void init_scene(Scene_t* scene, render_func_t render_func, action_func_t action_func); void free_scene(Scene_t* scene); #endif // __ENGINE_H diff --git a/engine/engine_conf.h b/engine/engine_conf.h index cddd4ec..68c195d 100644 --- a/engine/engine_conf.h +++ b/engine/engine_conf.h @@ -1,6 +1,7 @@ #ifndef _ENGINE_CONF_H #define _ENGINE_CONF_H +#define MAX_ENTITIES 2048 #define MAX_TEXTURES 16 #define MAX_SPRITES 64 #define MAX_SOUNDS 32 @@ -11,12 +12,12 @@ #define N_SFX 32 #define MAX_EMITTER_CONF 8 //#define MAX_PARTICLE_EMITTER 8 -#define MAX_ACTIVE_PARTICLE_EMITTER 128 -#define MAX_PARTICLES 32 +#define MAX_ACTIVE_PARTICLE_EMITTER 512 +#define MAX_PARTICLES 64 #define MAX_TILE_TYPES 16 #define N_TAGS 10 -#define N_COMPONENTS 15 -#define MAX_COMP_POOL_SIZE 4096 +#define N_COMPONENTS 20 +#define MAX_COMP_POOL_SIZE MAX_ENTITIES #endif // _ENGINE_CONF_H diff --git a/scenes/editor_scene.c b/scenes/editor_scene.c index 68f150c..e1f0efc 100644 --- a/scenes/editor_scene.c +++ b/scenes/editor_scene.c @@ -92,6 +92,7 @@ static void level_scene_render_func(Scene_t* scene) draw_rec.y = 0; draw_rec.height *= -1; static char buffer[512]; + BeginDrawing(); ClearBackground(LIGHTGRAY); DrawTextureRec( data->game_viewport.texture, @@ -187,6 +188,7 @@ static void level_scene_render_func(Scene_t* scene) gui_y += 300; sprintf(buffer, "Chests: %u / %u", data->coins.current, data->coins.total); DrawText(buffer, gui_x, gui_y, 24, BLACK); + EndDrawing(); } static void render_editor_game_scene(Scene_t* scene) diff --git a/scenes/game_scene.c b/scenes/game_scene.c index c03b955..93cc9a9 100644 --- a/scenes/game_scene.c +++ b/scenes/game_scene.c @@ -20,6 +20,7 @@ static void level_scene_render_func(Scene_t* scene) draw_rec.height *= -1; static char buffer[512]; + BeginDrawing(); ClearBackground(LIGHTGRAY); DrawTextureRec( data->game_viewport.texture, @@ -50,6 +51,7 @@ static void level_scene_render_func(Scene_t* scene) print_mempool_stats(buffer); DrawText(buffer, gui_x, 350, 12, BLACK); + EndDrawing(); } static void level_do_action(Scene_t* scene, ActionType_t action, bool pressed) diff --git a/scenes/menu_scene.c b/scenes/menu_scene.c index e88fdfb..a2e2d73 100644 --- a/scenes/menu_scene.c +++ b/scenes/menu_scene.c @@ -6,12 +6,14 @@ static void menu_scene_render_func(Scene_t* scene) { MenuSceneData_t* data = &(CONTAINER_OF(scene, MenuScene_t, scene)->data); - ClearBackground(RAYWHITE); - DrawText("This is a game", 25, 220, 12, BLACK); - UI_button(data->buttons, "Start"); - UI_button(data->buttons + 1, "Sandbox"); - UI_button(data->buttons + 2, "Continue"); - UI_button(data->buttons + 3, "Exit"); + BeginDrawing(); + ClearBackground(RAYWHITE); + DrawText("This is a game", 25, 220, 12, BLACK); + UI_button(data->buttons, "Start"); + UI_button(data->buttons + 1, "Sandbox"); + UI_button(data->buttons + 2, "Continue"); + UI_button(data->buttons + 3, "Exit"); + EndDrawing(); } static void exec_component_function(Scene_t* scene, int sel)