Rework scene rendering function

Split off- and on-screen rendering. Off-screen rendering would be a
system now
scene_man
En Yi 2023-10-21 10:48:21 +08:00
parent 7b2c48524d
commit 290dafdf86
4 changed files with 196 additions and 216 deletions

View File

@ -119,7 +119,9 @@ inline void update_scene(Scene_t* scene)
inline void render_scene(Scene_t* scene) inline void render_scene(Scene_t* scene)
{ {
BeginDrawing();
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

@ -81,6 +81,106 @@ static inline unsigned int get_tile_idx(int x, int y, const TileGrid_t* tilemap)
static RenderTexture2D selection_section; static RenderTexture2D selection_section;
static void level_scene_render_func(Scene_t* scene) static void level_scene_render_func(Scene_t* scene)
{
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
Entity_t* p_ent;
Rectangle draw_rec = data->game_rec;
draw_rec.x = 0;
draw_rec.y = 0;
draw_rec.height *= -1;
static char buffer[512];
ClearBackground(LIGHTGRAY);
DrawTextureRec(
data->game_viewport.texture,
draw_rec,
(Vector2){data->game_rec.x, data->game_rec.y},
WHITE
);
draw_rec.width = SELECTION_REGION_WIDTH;
draw_rec.height = -(SELECTION_REGION_HEIGHT * 2 + 10);
Vector2 draw_pos = {data->game_rec.x, data->game_rec.y + data->game_rec.height + SELECTION_GAP};
DrawTextureRec(
selection_section.texture,
draw_rec,
draw_pos,
WHITE
);
draw_pos.x = data->game_rec.x + current_spawn_selection * SELECTION_TILE_SIZE;
DrawRectangleLines(
draw_pos.x, draw_pos.y,
SELECTION_TILE_SIZE, SELECTION_TILE_SIZE, GREEN
);
draw_pos.x = data->game_rec.x + (MAX_SPAWN_TYPE + 1) * SELECTION_TILE_SIZE;
sprintf(buffer, "Selection: %s", get_spawn_selection_string(current_spawn_selection));
DrawText(buffer, draw_pos.x, draw_pos.y, 20, BLACK);
draw_pos.y += SELECTION_TILE_SIZE + 5;
sprintf(buffer, "Crate %s on spawn", crate_activation? "active" : "inactive");
DrawText(buffer, draw_pos.x, draw_pos.y, 20, BLACK);
// For DEBUG
const int gui_x = data->game_rec.x + data->game_rec.width + 10;
int gui_y = 15;
#if !defined(PLATFORM_WEB)
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_ent)
{
CTransform_t* p_ct = get_component(p_ent, CTRANSFORM_COMP_T);
CJump_t* p_cjump = get_component(p_ent, CJUMP_COMP_T);
CPlayerState_t* p_pstate = get_component(p_ent, CPLAYERSTATE_T);
CMovementState_t* p_mstate = get_component(p_ent, CMOVEMENTSTATE_T);
CAirTimer_t* p_air = get_component(p_ent, CAIRTIMER_T);
sprintf(buffer, "Pos: %.3f\n %.3f", p_ct->position.x, p_ct->position.y);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
sprintf(buffer, "Vel: %.3f\n %.3f", p_ct->velocity.x, p_ct->velocity.y);
DrawText(buffer, gui_x + 80, gui_y, 12, BLACK);
gui_y += 45;
sprintf(buffer, "Jumps: %u, %u, %u", p_cjump->jumps, p_cjump->jump_released, p_cjump->coyote_timer);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
sprintf(buffer, "Crouch: %u", p_pstate->is_crouch);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
sprintf(buffer, "Water: %s", p_mstate->water_state & 1? "YES":"NO");
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
sprintf(buffer, "Ladder: %u", p_pstate->ladder_state);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
Vector2 air_pos = {data->game_rec.x + data->game_rec.width - 16, data->game_rec.y + data->game_rec.height - 16};
for (uint8_t i = 0; i < p_air->curr_count; i++)
{
DrawCircleV(air_pos, 16, BLUE);
air_pos.x -= 32;
}
}
#endif
//sprintf(buffer, "Spawn Entity: %s", get_spawn_selection_string(current_spawn_selection));
//DrawText(buffer, gui_x, 240, 12, BLACK);
sprintf(buffer, "Number of Entities: %u", sc_map_size_64v(&scene->ent_manager.entities));
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
sprintf(buffer, "FPS: %u", GetFPS());
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
print_mempool_stats(buffer);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 300;
sprintf(buffer, "Chests: %u / %u", data->coins.current, data->coins.total);
DrawText(buffer, gui_x, gui_y, 24, BLACK);
}
static void render_editor_game_scene(Scene_t* scene)
{ {
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data); LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
TileGrid_t tilemap = data->tilemap; TileGrid_t tilemap = data->tilemap;
@ -383,102 +483,6 @@ static void level_scene_render_func(Scene_t* scene)
} }
EndMode2D(); EndMode2D();
EndTextureMode(); EndTextureMode();
Rectangle draw_rec = data->game_rec;
draw_rec.x = 0;
draw_rec.y = 0;
draw_rec.height *= -1;
BeginDrawing();
ClearBackground(LIGHTGRAY);
DrawTextureRec(
data->game_viewport.texture,
draw_rec,
(Vector2){data->game_rec.x, data->game_rec.y},
WHITE
);
draw_rec.width = SELECTION_REGION_WIDTH;
draw_rec.height = -(SELECTION_REGION_HEIGHT * 2 + 10);
Vector2 draw_pos = {data->game_rec.x, data->game_rec.y + data->game_rec.height + SELECTION_GAP};
DrawTextureRec(
selection_section.texture,
draw_rec,
draw_pos,
WHITE
);
draw_pos.x = data->game_rec.x + current_spawn_selection * SELECTION_TILE_SIZE;
DrawRectangleLines(
draw_pos.x, draw_pos.y,
SELECTION_TILE_SIZE, SELECTION_TILE_SIZE, GREEN
);
draw_pos.x = data->game_rec.x + (MAX_SPAWN_TYPE + 1) * SELECTION_TILE_SIZE;
sprintf(buffer, "Selection: %s", get_spawn_selection_string(current_spawn_selection));
DrawText(buffer, draw_pos.x, draw_pos.y, 20, BLACK);
draw_pos.y += SELECTION_TILE_SIZE + 5;
sprintf(buffer, "Crate %s on spawn", crate_activation? "active" : "inactive");
DrawText(buffer, draw_pos.x, draw_pos.y, 20, BLACK);
// For DEBUG
const int gui_x = data->game_rec.x + data->game_rec.width + 10;
int gui_y = 15;
#if !defined(PLATFORM_WEB)
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_ent)
{
CTransform_t* p_ct = get_component(p_ent, CTRANSFORM_COMP_T);
CJump_t* p_cjump = get_component(p_ent, CJUMP_COMP_T);
CPlayerState_t* p_pstate = get_component(p_ent, CPLAYERSTATE_T);
CMovementState_t* p_mstate = get_component(p_ent, CMOVEMENTSTATE_T);
CAirTimer_t* p_air = get_component(p_ent, CAIRTIMER_T);
sprintf(buffer, "Pos: %.3f\n %.3f", p_ct->position.x, p_ct->position.y);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
sprintf(buffer, "Vel: %.3f\n %.3f", p_ct->velocity.x, p_ct->velocity.y);
DrawText(buffer, gui_x + 80, gui_y, 12, BLACK);
gui_y += 45;
sprintf(buffer, "Jumps: %u, %u, %u", p_cjump->jumps, p_cjump->jump_released, p_cjump->coyote_timer);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
sprintf(buffer, "Crouch: %u", p_pstate->is_crouch);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
sprintf(buffer, "Water: %s", p_mstate->water_state & 1? "YES":"NO");
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
sprintf(buffer, "Ladder: %u", p_pstate->ladder_state);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
Vector2 air_pos = {data->game_rec.x + data->game_rec.width - 16, data->game_rec.y + data->game_rec.height - 16};
for (uint8_t i = 0; i < p_air->curr_count; i++)
{
DrawCircleV(air_pos, 16, BLUE);
air_pos.x -= 32;
}
}
#endif
//sprintf(buffer, "Spawn Entity: %s", get_spawn_selection_string(current_spawn_selection));
//DrawText(buffer, gui_x, 240, 12, BLACK);
sprintf(buffer, "Number of Entities: %u", sc_map_size_64v(&scene->ent_manager.entities));
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
sprintf(buffer, "FPS: %u", GetFPS());
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
static char mempool_stats[512];
print_mempool_stats(mempool_stats);
DrawText(mempool_stats, gui_x, gui_y, 12, BLACK);
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 spawn_chest(Scene_t* scene, unsigned int tile_idx) static void spawn_chest(Scene_t* scene, unsigned int tile_idx)
@ -1004,6 +1008,7 @@ void init_sandbox_scene(LevelScene_t* scene)
sc_array_add(&scene->scene.systems, &player_respawn_system); sc_array_add(&scene->scene.systems, &player_respawn_system);
sc_array_add(&scene->scene.systems, &level_end_detection_system); sc_array_add(&scene->scene.systems, &level_end_detection_system);
sc_array_add(&scene->scene.systems, &toggle_block_system); sc_array_add(&scene->scene.systems, &toggle_block_system);
sc_array_add(&scene->scene.systems, &render_editor_game_scene);
// This avoid graphical glitch, not essential // This avoid graphical glitch, not essential
//sc_array_add(&scene->scene.systems, &update_tilemap_system); //sc_array_add(&scene->scene.systems, &update_tilemap_system);

View File

@ -13,6 +13,92 @@ static Tile_t all_tiles[MAX_N_TILES] = {0};
static void level_scene_render_func(Scene_t* scene) static void level_scene_render_func(Scene_t* scene)
{ {
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data); LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
Rectangle draw_rec = data->game_rec;
draw_rec.x = 0;
draw_rec.y = 0;
draw_rec.height *= -1;
static char buffer[512];
ClearBackground(LIGHTGRAY);
DrawTextureRec(
data->game_viewport.texture,
draw_rec,
(Vector2){data->game_rec.x, data->game_rec.y},
WHITE
);
// For DEBUG
const int gui_x = data->game_rec.x + data->game_rec.width + 10;
//sprintf(buffer, "Spawn Entity: %s", get_spawn_selection_string(current_spawn_selection));
//DrawText(buffer, gui_x, 240, 12, BLACK);
sprintf(buffer, "Number of Entities: %u", sc_map_size_64v(&scene->ent_manager.entities));
DrawText(buffer, gui_x, 270, 12, BLACK);
sprintf(buffer, "FPS: %u", GetFPS());
DrawText(buffer, gui_x, 320, 12, BLACK);
print_mempool_stats(buffer);
DrawText(buffer, gui_x, 350, 12, BLACK);
}
static void level_do_action(Scene_t* scene, ActionType_t action, bool pressed)
{
CPlayerState_t* p_playerstate;
sc_map_foreach_value(&scene->ent_manager.component_map[CPLAYERSTATE_T], p_playerstate)
{
switch(action)
{
case ACTION_UP:
p_playerstate->player_dir.y = (pressed)? -1 : 0;
break;
case ACTION_DOWN:
p_playerstate->player_dir.y = (pressed)? 1 : 0;
break;
case ACTION_LEFT:
p_playerstate->player_dir.x = (pressed)? -1 : 0;
break;
case ACTION_RIGHT:
p_playerstate->player_dir.x = (pressed)? 1 : 0;
break;
case ACTION_JUMP:
p_playerstate->jump_pressed = pressed;
break;
default:
break;
}
}
if (!pressed)
{
switch (action)
{
case ACTION_RESTART:
reload_level_tilemap((LevelScene_t*)scene);
break;
case ACTION_NEXTLEVEL:
load_next_level_tilemap((LevelScene_t*)scene);
break;
case ACTION_PREVLEVEL:
load_prev_level_tilemap((LevelScene_t*)scene);
break;
case ACTION_EXIT:
if(scene->engine != NULL)
{
change_scene(scene->engine, 0);
}
break;
default:
break;
}
}
}
static void render_regular_game_scene(Scene_t* scene)
{
// This function will render the game scene outside of the intended draw function
// Just for clarity and separation of logic
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
TileGrid_t tilemap = data->tilemap; TileGrid_t tilemap = data->tilemap;
Entity_t* p_ent; Entity_t* p_ent;
@ -31,7 +117,6 @@ static void level_scene_render_func(Scene_t* scene)
max.x = (int)fmin(tilemap.width, max.x + 1); max.x = (int)fmin(tilemap.width, max.x + 1);
max.y = (int)fmin(tilemap.height, max.y + 1); max.y = (int)fmin(tilemap.height, max.y + 1);
BeginTextureMode(data->game_viewport); BeginTextureMode(data->game_viewport);
ClearBackground(WHITE); ClearBackground(WHITE);
BeginMode2D(data->camera.cam); BeginMode2D(data->camera.cam);
@ -115,7 +200,6 @@ static void level_scene_render_func(Scene_t* scene)
} }
} }
char buffer[64] = {0};
sc_map_foreach_value(&scene->ent_manager.entities, p_ent) sc_map_foreach_value(&scene->ent_manager.entities, p_ent)
{ {
CTransform_t* p_ct = get_component(p_ent, CTRANSFORM_COMP_T); CTransform_t* p_ct = get_component(p_ent, CTRANSFORM_COMP_T);
@ -245,115 +329,6 @@ static void level_scene_render_func(Scene_t* scene)
DrawLine(0, val, tilemap.width * TILE_SIZE, val, BLACK); DrawLine(0, val, tilemap.width * TILE_SIZE, val, BLACK);
EndMode2D(); EndMode2D();
EndTextureMode(); EndTextureMode();
Rectangle draw_rec = data->game_rec;
draw_rec.x = 0;
draw_rec.y = 0;
draw_rec.height *= -1;
BeginDrawing();
ClearBackground(LIGHTGRAY);
DrawTextureRec(
data->game_viewport.texture,
draw_rec,
(Vector2){data->game_rec.x, data->game_rec.y},
WHITE
);
// For DEBUG
const int gui_x = data->game_rec.x + data->game_rec.width + 10;
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_ent)
{
CTransform_t* p_ct = get_component(p_ent, CTRANSFORM_COMP_T);
CJump_t* p_cjump = get_component(p_ent, CJUMP_COMP_T);
CPlayerState_t* p_pstate = get_component(p_ent, CPLAYERSTATE_T);
CMovementState_t* p_mstate = get_component(p_ent, CMOVEMENTSTATE_T);
CAirTimer_t* p_air = get_component(p_ent, CAIRTIMER_T);
sprintf(buffer, "Pos: %.3f\n %.3f", p_ct->position.x, p_ct->position.y);
DrawText(buffer, gui_x, 15, 12, BLACK);
sprintf(buffer, "Vel: %.3f\n %.3f", p_ct->velocity.x, p_ct->velocity.y);
DrawText(buffer, gui_x + 80, 15, 12, BLACK);
//sprintf(buffer, "Accel: %.3f\n %.3f", p_ct->accel.x, p_ct->accel.y);
//DrawText(buffer, tilemap.width * TILE_SIZE + 128, 60, 12, BLACK);
sprintf(buffer, "Jumps: %u", p_cjump->jumps);
DrawText(buffer, gui_x, 60, 12, BLACK);
sprintf(buffer, "Crouch: %u", p_pstate->is_crouch);
DrawText(buffer, gui_x, 90, 12, BLACK);
sprintf(buffer, "Water: %s", p_mstate->water_state & 1? "YES":"NO");
DrawText(buffer, gui_x, 120, 12, BLACK);
sprintf(buffer, "Ladder: %u", p_pstate->ladder_state);
DrawText(buffer, gui_x, 150, 12, BLACK);
Vector2 air_pos = {data->game_rec.x + data->game_rec.width - 16, data->game_rec.y + data->game_rec.height - 16};
for (uint8_t i = 0; i < p_air->curr_count; i++)
{
DrawCircleV(air_pos, 16, BLUE);
air_pos.x -= 32;
}
}
//sprintf(buffer, "Spawn Entity: %s", get_spawn_selection_string(current_spawn_selection));
//DrawText(buffer, gui_x, 240, 12, BLACK);
sprintf(buffer, "Number of Entities: %u", sc_map_size_64v(&scene->ent_manager.entities));
DrawText(buffer, gui_x, 270, 12, BLACK);
sprintf(buffer, "FPS: %u", GetFPS());
DrawText(buffer, gui_x, 320, 12, BLACK);
static char mempool_stats[512];
print_mempool_stats(mempool_stats);
DrawText(mempool_stats, gui_x, 350, 12, BLACK);
EndDrawing();
}
static void level_do_action(Scene_t* scene, ActionType_t action, bool pressed)
{
CPlayerState_t* p_playerstate;
sc_map_foreach_value(&scene->ent_manager.component_map[CPLAYERSTATE_T], p_playerstate)
{
switch(action)
{
case ACTION_UP:
p_playerstate->player_dir.y = (pressed)? -1 : 0;
break;
case ACTION_DOWN:
p_playerstate->player_dir.y = (pressed)? 1 : 0;
break;
case ACTION_LEFT:
p_playerstate->player_dir.x = (pressed)? -1 : 0;
break;
case ACTION_RIGHT:
p_playerstate->player_dir.x = (pressed)? 1 : 0;
break;
case ACTION_JUMP:
p_playerstate->jump_pressed = pressed;
break;
default:
break;
}
}
if (!pressed)
{
switch (action)
{
case ACTION_RESTART:
reload_level_tilemap((LevelScene_t*)scene);
break;
case ACTION_NEXTLEVEL:
load_next_level_tilemap((LevelScene_t*)scene);
break;
case ACTION_PREVLEVEL:
load_prev_level_tilemap((LevelScene_t*)scene);
break;
case ACTION_EXIT:
if(scene->engine != NULL)
{
change_scene(scene->engine, 0);
}
break;
default:
break;
}
}
} }
void init_game_scene(LevelScene_t* scene) void init_game_scene(LevelScene_t* scene)
@ -397,7 +372,7 @@ void init_game_scene(LevelScene_t* scene)
sc_array_add(&scene->scene.systems, &player_dir_reset_system); sc_array_add(&scene->scene.systems, &player_dir_reset_system);
sc_array_add(&scene->scene.systems, &player_respawn_system); sc_array_add(&scene->scene.systems, &player_respawn_system);
sc_array_add(&scene->scene.systems, &update_water_runner_system); sc_array_add(&scene->scene.systems, &update_water_runner_system);
sc_array_add(&scene->scene.systems, &render_regular_game_scene);
// This avoid graphical glitch, not essential // This avoid graphical glitch, not essential
//sc_array_add(&scene->scene.systems, &update_tilemap_system); //sc_array_add(&scene->scene.systems, &update_tilemap_system);

View File

@ -6,14 +6,12 @@
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)