Slightly rework scene rendering

Changelog:
- Draw calls are now in rendering function of a scene
    - This is to allow flexibility on how a scene is rendered
- Modify level scene to render level using RenderTexture
    - This is to prepare for camera implementation later
- CloseWindow is called after all de-init functions is called
scene_man
En Yi 2023-05-07 16:25:03 +08:00
parent 9eb46d0e57
commit d4dfe01182
7 changed files with 163 additions and 142 deletions

View File

@ -56,6 +56,6 @@ int main(void)
spr2->elapsed = 0; spr2->elapsed = 0;
} }
} }
CloseWindow();
term_assets(&assets); term_assets(&assets);
CloseWindow();
} }

View File

@ -28,11 +28,13 @@ static inline unsigned int get_tile_idx(int x, int y, unsigned int tilemap_width
static void level_scene_render_func(Scene_t* scene) static void level_scene_render_func(Scene_t* scene)
{ {
LevelSceneData_t* data = (LevelSceneData_t *)scene->scene_data; LevelScene_t* lvl_scene = container_of(scene, LevelScene_t, scene);
TileGrid_t tilemap = data->tilemap; TileGrid_t tilemap = lvl_scene->data.tilemap;
Entity_t* p_ent; Entity_t* p_ent;
BeginTextureMode(lvl_scene->data.game_viewport);
ClearBackground(RAYWHITE);
for (size_t i = 0; i < tilemap.n_tiles; ++i) for (size_t i = 0; i < tilemap.n_tiles; ++i)
{ {
char buffer[6] = {0}; char buffer[6] = {0};
@ -140,7 +142,16 @@ static void level_scene_render_func(Scene_t* scene)
int y = (i+1)*TILE_SIZE; int y = (i+1)*TILE_SIZE;
DrawLine(0, y, tilemap.width * TILE_SIZE, y, BLACK); DrawLine(0, y, tilemap.width * TILE_SIZE, y, BLACK);
} }
EndTextureMode();
BeginDrawing();
ClearBackground(SKYBLUE);
DrawTextureRec(
lvl_scene->data.game_viewport.texture,
(Rectangle){0, 0, lvl_scene->data.game_sz.x, -lvl_scene->data.game_sz.y},
(Vector2){ 0, 0 },
WHITE
);
// For DEBUG // For DEBUG
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_ent) sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_ent)
{ {
@ -173,6 +184,7 @@ static void level_scene_render_func(Scene_t* scene)
static char mempool_stats[512]; static char mempool_stats[512];
print_mempool_stats(mempool_stats); print_mempool_stats(mempool_stats);
DrawText(mempool_stats, tilemap.width * TILE_SIZE + 1, 350, 12, BLACK); DrawText(mempool_stats, tilemap.width * TILE_SIZE + 1, 350, 12, BLACK);
EndDrawing();
} }
static void spawn_crate(Scene_t* scene, unsigned int tile_idx, bool metal) static void spawn_crate(Scene_t* scene, unsigned int tile_idx, bool metal)

View File

@ -634,7 +634,7 @@ void tile_collision_system(Scene_t* scene)
// Entity_t *p_other_ent = get_entity(&scene->ent_manager, other_ent_idx); // Entity_t *p_other_ent = get_entity(&scene->ent_manager, other_ent_idx);
// if (!p_ent->m_alive || !p_other_ent->m_alive) continue; // if (!p_ent->m_alive || !p_other_ent->m_alive) continue;
//} //}
sc_map_clear_32(&data->collision_events); //sc_map_clear_32(&data->collision_events);
// Level boundary collision // Level boundary collision
unsigned int level_width = tilemap.width * TILE_SIZE; unsigned int level_width = tilemap.width * TILE_SIZE;
@ -1094,12 +1094,15 @@ void sprite_animation_system(Scene_t* scene)
void init_level_scene_data(LevelSceneData_t* data) void init_level_scene_data(LevelSceneData_t* data)
{ {
sc_map_init_32(&data->collision_events, 128, 0); //sc_map_init_32(&data->collision_events, 128, 0);
data->game_viewport = LoadRenderTexture(32*TILE_SIZE, 16*TILE_SIZE);
data->game_sz = (Vector2){32*TILE_SIZE, 16*TILE_SIZE};
} }
void term_level_scene_data(LevelSceneData_t* data) void term_level_scene_data(LevelSceneData_t* data)
{ {
sc_map_term_32(&data->collision_events); //sc_map_term_32(&data->collision_events);
UnloadRenderTexture(data->game_viewport); // Unload render texture
} }
unsigned int player_sprite_transition_func(Entity_t* ent) unsigned int player_sprite_transition_func(Entity_t* ent)

View File

@ -6,10 +6,13 @@
static void menu_scene_render_func(Scene_t* scene) static void menu_scene_render_func(Scene_t* scene)
{ {
MenuSceneData_t* data = (MenuSceneData_t*)scene->scene_data; MenuSceneData_t* data = (MenuSceneData_t*)scene->scene_data;
BeginDrawing();
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, "Continue"); UI_button(data->buttons + 1, "Continue");
UI_button(data->buttons + 2, "Exit"); UI_button(data->buttons + 2, "Exit");
EndDrawing();
} }
static void menu_do_action(Scene_t* scene, ActionType_t action, bool pressed) static void menu_do_action(Scene_t* scene, ActionType_t action, bool pressed)

View File

@ -7,6 +7,10 @@
#include "engine.h" #include "engine.h"
#include "gui.h" #include "gui.h"
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
typedef enum TileType { typedef enum TileType {
EMPTY_TILE = 0, EMPTY_TILE = 0,
SOLID_TILE, SOLID_TILE,
@ -38,7 +42,8 @@ typedef struct TileGrid {
typedef struct LevelSceneData { typedef struct LevelSceneData {
TileGrid_t tilemap; TileGrid_t tilemap;
struct sc_map_32 collision_events; RenderTexture2D game_viewport;
Vector2 game_sz;
}LevelSceneData_t; }LevelSceneData_t;
typedef struct LevelScene { typedef struct LevelScene {

View File

@ -57,7 +57,7 @@ int main(void)
EndDrawing(); EndDrawing();
if (WindowShouldClose()) break; if (WindowShouldClose()) break;
} }
CloseWindow();
free_menu_scene(&scene); free_menu_scene(&scene);
sc_queue_term(&key_buffer); sc_queue_term(&key_buffer);
CloseWindow();
} }

View File

@ -77,13 +77,11 @@ int main(void)
update_scene(&scene.scene); update_scene(&scene.scene);
update_entity_manager(&scene.scene.ent_manager); update_entity_manager(&scene.scene.ent_manager);
// This is needed to advance time delta // This is needed to advance time delta
BeginDrawing();
render_scene(&scene.scene); render_scene(&scene.scene);
ClearBackground(RAYWHITE);
EndDrawing();
if (WindowShouldClose()) break; if (WindowShouldClose()) break;
} }
CloseWindow();
free_level_scene(&scene); free_level_scene(&scene);
sc_queue_term(&key_buffer); sc_queue_term(&key_buffer);
term_assets(&engine.assets);
CloseWindow();
} }