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,151 +28,163 @@ 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;
for (size_t i = 0; i < tilemap.n_tiles; ++i) BeginTextureMode(lvl_scene->data.game_viewport);
{ ClearBackground(RAYWHITE);
char buffer[6] = {0}; for (size_t i = 0; i < tilemap.n_tiles; ++i)
int x = (i % tilemap.width) * TILE_SIZE; {
int y = (i / tilemap.width) * TILE_SIZE; char buffer[6] = {0};
sprintf(buffer, "%u", sc_map_size_64(&tilemap.tiles[i].entities_set)); int x = (i % tilemap.width) * TILE_SIZE;
int y = (i / tilemap.width) * TILE_SIZE;
sprintf(buffer, "%u", sc_map_size_64(&tilemap.tiles[i].entities_set));
if (tilemap.tiles[i].tile_type == SOLID_TILE) if (tilemap.tiles[i].tile_type == SOLID_TILE)
{
DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, BLACK);
}
else if (tilemap.tiles[i].tile_type == ONEWAY_TILE)
{
DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, MAROON);
}
else if (tilemap.tiles[i].tile_type == LADDER)
{
DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, ORANGE);
}
if (tilemap.tiles[i].water_level > 0)
{
// Draw water tile
Color water_colour = ColorAlpha(BLUE, 0.5);
DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, water_colour);
}
}
char buffer[64] = {0};
sc_map_foreach_value(&scene->ent_manager.entities, p_ent)
{
CTransform_t* p_ct = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
CBBox_t* p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T);
Color colour;
switch(p_ent->m_tag)
{
case PLAYER_ENT_TAG:
colour = RED;
break;
case CRATES_ENT_TAG:
colour = p_bbox->fragile? BROWN : GRAY;
break;
default:
colour = BLACK;
}
DrawRectangle(p_ct->position.x, p_ct->position.y, p_bbox->size.x, p_bbox->size.y, colour);
CHurtbox_t* p_hurtbox = get_component(&scene->ent_manager, p_ent, CHURTBOX_T);
CHitBoxes_t* p_hitbox = get_component(&scene->ent_manager, p_ent, CHITBOXES_T);
if (p_hitbox != NULL)
{
for (uint8_t i = 0;i < p_hitbox->n_boxes; ++i)
{ {
Rectangle rec = { DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, BLACK);
.x = p_ct->position.x + p_hitbox->boxes[i].x, }
.y = p_ct->position.y + p_hitbox->boxes[i].y, else if (tilemap.tiles[i].tile_type == ONEWAY_TILE)
.width = p_hitbox->boxes[i].width, {
.height = p_hitbox->boxes[i].height, DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, MAROON);
}; }
DrawRectangleLinesEx(rec, 1.5, ORANGE); else if (tilemap.tiles[i].tile_type == LADDER)
{
DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, ORANGE);
}
if (tilemap.tiles[i].water_level > 0)
{
// Draw water tile
Color water_colour = ColorAlpha(BLUE, 0.5);
DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, water_colour);
} }
} }
if (p_hurtbox != NULL)
char buffer[64] = {0};
sc_map_foreach_value(&scene->ent_manager.entities, p_ent)
{ {
Rectangle rec = { CTransform_t* p_ct = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
.x = p_ct->position.x + p_hurtbox->offset.x, CBBox_t* p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T);
.y = p_ct->position.y + p_hurtbox->offset.y, Color colour;
.width = p_hurtbox->size.x, switch(p_ent->m_tag)
.height = p_hurtbox->size.y, {
}; case PLAYER_ENT_TAG:
DrawRectangleLinesEx(rec, 1.5, PURPLE); colour = RED;
break;
case CRATES_ENT_TAG:
colour = p_bbox->fragile? BROWN : GRAY;
break;
default:
colour = BLACK;
}
DrawRectangle(p_ct->position.x, p_ct->position.y, p_bbox->size.x, p_bbox->size.y, colour);
CHurtbox_t* p_hurtbox = get_component(&scene->ent_manager, p_ent, CHURTBOX_T);
CHitBoxes_t* p_hitbox = get_component(&scene->ent_manager, p_ent, CHITBOXES_T);
if (p_hitbox != NULL)
{
for (uint8_t i = 0;i < p_hitbox->n_boxes; ++i)
{
Rectangle rec = {
.x = p_ct->position.x + p_hitbox->boxes[i].x,
.y = p_ct->position.y + p_hitbox->boxes[i].y,
.width = p_hitbox->boxes[i].width,
.height = p_hitbox->boxes[i].height,
};
DrawRectangleLinesEx(rec, 1.5, ORANGE);
}
}
if (p_hurtbox != NULL)
{
Rectangle rec = {
.x = p_ct->position.x + p_hurtbox->offset.x,
.y = p_ct->position.y + p_hurtbox->offset.y,
.width = p_hurtbox->size.x,
.height = p_hurtbox->size.y,
};
DrawRectangleLinesEx(rec, 1.5, PURPLE);
}
CSprite_t* p_cspr = get_component(&scene->ent_manager, p_ent, CSPRITE_T);
if (p_cspr != NULL)
{
Vector2 pos = Vector2Add(p_ct->position, p_cspr->offset);
draw_sprite(p_cspr->sprite, pos);
}
} }
CSprite_t* p_cspr = get_component(&scene->ent_manager, p_ent, CSPRITE_T);
if (p_cspr != NULL) for (size_t i = 0; i < tilemap.n_tiles; ++i)
{ {
Vector2 pos = Vector2Add(p_ct->position, p_cspr->offset); int x = (i % tilemap.width) * TILE_SIZE;
draw_sprite(p_cspr->sprite, pos); int y = (i / tilemap.width) * TILE_SIZE;
sprintf(buffer, "%u", sc_map_size_64(&tilemap.tiles[i].entities_set));
if (tilemap.tiles[i].solid > 0)
{
DrawText(buffer, x, y, 10, WHITE);
}
else
{
// Draw water tile
DrawText(buffer, x, y, 10, BLACK);
}
} }
}
for (size_t i = 0; i < tilemap.n_tiles; ++i) // Draw tile grid
{ for (size_t i = 0; i < tilemap.width; ++i)
int x = (i % tilemap.width) * TILE_SIZE;
int y = (i / tilemap.width) * TILE_SIZE;
sprintf(buffer, "%u", sc_map_size_64(&tilemap.tiles[i].entities_set));
if (tilemap.tiles[i].solid > 0)
{ {
DrawText(buffer, x, y, 10, WHITE); int x = (i+1)*TILE_SIZE;
DrawLine(x, 0, x, tilemap.height * TILE_SIZE, BLACK);
} }
else for (size_t i = 0; i < tilemap.height;++i)
{ {
// Draw water tile int y = (i+1)*TILE_SIZE;
DrawText(buffer, x, y, 10, BLACK); DrawLine(0, y, tilemap.width * TILE_SIZE, y, BLACK);
} }
} EndTextureMode();
// Draw tile grid BeginDrawing();
for (size_t i = 0; i < tilemap.width; ++i) ClearBackground(SKYBLUE);
{ DrawTextureRec(
int x = (i+1)*TILE_SIZE; lvl_scene->data.game_viewport.texture,
DrawLine(x, 0, x, tilemap.height * TILE_SIZE, BLACK); (Rectangle){0, 0, lvl_scene->data.game_sz.x, -lvl_scene->data.game_sz.y},
} (Vector2){ 0, 0 },
for (size_t i = 0; i < tilemap.height;++i) WHITE
{ );
int y = (i+1)*TILE_SIZE; // For DEBUG
DrawLine(0, y, tilemap.width * TILE_SIZE, y, BLACK); sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_ent)
} {
CTransform_t* p_ct = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
CJump_t* p_cjump = get_component(&scene->ent_manager, p_ent, CJUMP_COMP_T);
CPlayerState_t* p_pstate = get_component(&scene->ent_manager, p_ent, CPLAYERSTATE_T);
CMovementState_t* p_mstate = get_component(&scene->ent_manager, p_ent, CMOVEMENTSTATE_T);
sprintf(buffer, "Pos: %.3f\n %.3f", p_ct->position.x, p_ct->position.y);
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 15, 12, BLACK);
sprintf(buffer, "Vel: %.3f\n %.3f", p_ct->velocity.x, p_ct->velocity.y);
DrawText(buffer, tilemap.width * TILE_SIZE + 128, 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, tilemap.width * TILE_SIZE + 1, 60, 12, BLACK);
sprintf(buffer, "Crouch: %u", p_pstate->is_crouch);
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 90, 12, BLACK);
sprintf(buffer, "Water: %s", p_mstate->water_state & 1? "YES":"NO");
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 120, 12, BLACK);
sprintf(buffer, "Ladder: %u", p_pstate->ladder_state);
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 150, 12, BLACK);
}
sprintf(buffer, "Spawn Entity: %u", current_spawn_selection);
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 240, 12, BLACK);
sprintf(buffer, "Number of Entities: %u", sc_map_size_64v(&scene->ent_manager.entities));
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 270, 12, BLACK);
sprintf(buffer, "FPS: %u", GetFPS());
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 320, 12, BLACK);
// For DEBUG static char mempool_stats[512];
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_ent) print_mempool_stats(mempool_stats);
{ DrawText(mempool_stats, tilemap.width * TILE_SIZE + 1, 350, 12, BLACK);
CTransform_t* p_ct = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T); EndDrawing();
CJump_t* p_cjump = get_component(&scene->ent_manager, p_ent, CJUMP_COMP_T);
CPlayerState_t* p_pstate = get_component(&scene->ent_manager, p_ent, CPLAYERSTATE_T);
CMovementState_t* p_mstate = get_component(&scene->ent_manager, p_ent, CMOVEMENTSTATE_T);
sprintf(buffer, "Pos: %.3f\n %.3f", p_ct->position.x, p_ct->position.y);
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 15, 12, BLACK);
sprintf(buffer, "Vel: %.3f\n %.3f", p_ct->velocity.x, p_ct->velocity.y);
DrawText(buffer, tilemap.width * TILE_SIZE + 128, 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, tilemap.width * TILE_SIZE + 1, 60, 12, BLACK);
sprintf(buffer, "Crouch: %u", p_pstate->is_crouch);
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 90, 12, BLACK);
sprintf(buffer, "Water: %s", p_mstate->water_state & 1? "YES":"NO");
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 120, 12, BLACK);
sprintf(buffer, "Ladder: %u", p_pstate->ladder_state);
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 150, 12, BLACK);
}
sprintf(buffer, "Spawn Entity: %u", current_spawn_selection);
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 240, 12, BLACK);
sprintf(buffer, "Number of Entities: %u", sc_map_size_64v(&scene->ent_manager.entities));
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 270, 12, BLACK);
sprintf(buffer, "FPS: %u", GetFPS());
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 320, 12, BLACK);
static char mempool_stats[512];
print_mempool_stats(mempool_stats);
DrawText(mempool_stats, tilemap.width * TILE_SIZE + 1, 350, 12, BLACK);
} }
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;
DrawText("This is a game", 25, 220, 12, BLACK); BeginDrawing();
UI_button(data->buttons, "Start"); ClearBackground(RAYWHITE);
UI_button(data->buttons + 1, "Continue"); DrawText("This is a game", 25, 220, 12, BLACK);
UI_button(data->buttons + 2, "Exit"); UI_button(data->buttons, "Start");
UI_button(data->buttons + 1, "Continue");
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();
} }