Tweak scene render function

Delegate BeginDrawing and EndDrawing to individual scenes
scene_man
En Yi 2024-05-01 19:56:10 +08:00
parent 41e4b34869
commit 0d4089d9ce
6 changed files with 25 additions and 16 deletions

View File

@ -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, 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_map_init_64(&scene->action_map, 32, 0);
sc_array_init(&scene->systems); 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) inline void render_scene(Scene_t* scene)
{ {
BeginDrawing(); if (scene->render_function != NULL)
{
scene->render_function(scene); scene->render_function(scene);
EndDrawing(); }
} }
inline void do_action(Scene_t* scene, ActionType_t action, bool pressed) inline void do_action(Scene_t* scene, ActionType_t action, bool pressed)

View File

@ -37,6 +37,7 @@ typedef enum SceneState {
SCENE_ENDED, SCENE_ENDED,
}SceneState_t; }SceneState_t;
typedef void(*render_func_t)(Scene_t*);
typedef void(*system_func_t)(Scene_t*); typedef void(*system_func_t)(Scene_t*);
typedef void(*action_func_t)(Scene_t*, ActionType_t, bool); typedef void(*action_func_t)(Scene_t*, ActionType_t, bool);
sc_array_def(system_func_t, systems); sc_array_def(system_func_t, systems);
@ -44,7 +45,7 @@ sc_array_def(system_func_t, systems);
struct Scene { struct Scene {
struct sc_map_64 action_map; // key -> actions struct sc_map_64 action_map; // key -> actions
struct sc_array_systems systems; struct sc_array_systems systems;
system_func_t render_function; render_func_t render_function;
action_func_t action_function; action_func_t action_function;
EntityManager_t ent_manager; EntityManager_t ent_manager;
float delta_time; 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); 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, 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); void free_scene(Scene_t* scene);
#endif // __ENGINE_H #endif // __ENGINE_H

View File

@ -1,6 +1,7 @@
#ifndef _ENGINE_CONF_H #ifndef _ENGINE_CONF_H
#define _ENGINE_CONF_H #define _ENGINE_CONF_H
#define MAX_ENTITIES 2048
#define MAX_TEXTURES 16 #define MAX_TEXTURES 16
#define MAX_SPRITES 64 #define MAX_SPRITES 64
#define MAX_SOUNDS 32 #define MAX_SOUNDS 32
@ -11,12 +12,12 @@
#define N_SFX 32 #define N_SFX 32
#define MAX_EMITTER_CONF 8 #define MAX_EMITTER_CONF 8
//#define MAX_PARTICLE_EMITTER 8 //#define MAX_PARTICLE_EMITTER 8
#define MAX_ACTIVE_PARTICLE_EMITTER 128 #define MAX_ACTIVE_PARTICLE_EMITTER 512
#define MAX_PARTICLES 32 #define MAX_PARTICLES 64
#define MAX_TILE_TYPES 16 #define MAX_TILE_TYPES 16
#define N_TAGS 10 #define N_TAGS 10
#define N_COMPONENTS 15 #define N_COMPONENTS 20
#define MAX_COMP_POOL_SIZE 4096 #define MAX_COMP_POOL_SIZE MAX_ENTITIES
#endif // _ENGINE_CONF_H #endif // _ENGINE_CONF_H

View File

@ -92,6 +92,7 @@ static void level_scene_render_func(Scene_t* scene)
draw_rec.y = 0; draw_rec.y = 0;
draw_rec.height *= -1; draw_rec.height *= -1;
static char buffer[512]; static char buffer[512];
BeginDrawing();
ClearBackground(LIGHTGRAY); ClearBackground(LIGHTGRAY);
DrawTextureRec( DrawTextureRec(
data->game_viewport.texture, data->game_viewport.texture,
@ -187,6 +188,7 @@ static void level_scene_render_func(Scene_t* scene)
gui_y += 300; gui_y += 300;
sprintf(buffer, "Chests: %u / %u", data->coins.current, data->coins.total); sprintf(buffer, "Chests: %u / %u", data->coins.current, data->coins.total);
DrawText(buffer, gui_x, gui_y, 24, BLACK); DrawText(buffer, gui_x, gui_y, 24, BLACK);
EndDrawing();
} }
static void render_editor_game_scene(Scene_t* scene) static void render_editor_game_scene(Scene_t* scene)

View File

@ -20,6 +20,7 @@ static void level_scene_render_func(Scene_t* scene)
draw_rec.height *= -1; draw_rec.height *= -1;
static char buffer[512]; static char buffer[512];
BeginDrawing();
ClearBackground(LIGHTGRAY); ClearBackground(LIGHTGRAY);
DrawTextureRec( DrawTextureRec(
data->game_viewport.texture, data->game_viewport.texture,
@ -50,6 +51,7 @@ static void level_scene_render_func(Scene_t* scene)
print_mempool_stats(buffer); print_mempool_stats(buffer);
DrawText(buffer, gui_x, 350, 12, BLACK); DrawText(buffer, gui_x, 350, 12, BLACK);
EndDrawing();
} }
static void level_do_action(Scene_t* scene, ActionType_t action, bool pressed) static void level_do_action(Scene_t* scene, ActionType_t action, bool pressed)

View File

@ -6,12 +6,14 @@
static void menu_scene_render_func(Scene_t* scene) static void menu_scene_render_func(Scene_t* scene)
{ {
MenuSceneData_t* data = &(CONTAINER_OF(scene, MenuScene_t, scene)->data); MenuSceneData_t* data = &(CONTAINER_OF(scene, MenuScene_t, scene)->data);
BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
DrawText("This is a game", 25, 220, 12, BLACK); DrawText("This is a game", 25, 220, 12, BLACK);
UI_button(data->buttons, "Start"); UI_button(data->buttons, "Start");
UI_button(data->buttons + 1, "Sandbox"); UI_button(data->buttons + 1, "Sandbox");
UI_button(data->buttons + 2, "Continue"); UI_button(data->buttons + 2, "Continue");
UI_button(data->buttons + 3, "Exit"); UI_button(data->buttons + 3, "Exit");
EndDrawing();
} }
static void exec_component_function(Scene_t* scene, int sel) static void exec_component_function(Scene_t* scene, int sel)