From b09f1d4ca9844581c68893fa1c4f399dbfc9d612 Mon Sep 17 00:00:00 2001 From: Klemen Plestenjak Date: Wed, 10 Jan 2024 19:43:57 +0100 Subject: [PATCH] Fix screen switching --- game/game_state.h | 1 + game/main.c | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/game/game_state.h b/game/game_state.h index c277374..8f74a36 100644 --- a/game/game_state.h +++ b/game/game_state.h @@ -41,6 +41,7 @@ static Options getDefaultOptions() { typedef struct Game { GameScreen screen; + GameScreen nextScreen; GameScreen screenPrevFrame; Camera2D camera; BzTileset tileset; diff --git a/game/main.c b/game/main.c index 96a56b9..16afadc 100644 --- a/game/main.c +++ b/game/main.c @@ -162,6 +162,11 @@ bool deserializeOptions(const char *path, Options *optsOut) { return false; } + +void setScreen(Game *game, GameScreen newScreen) { + game->nextScreen = newScreen; +} + bool init(void *userData) { // Center window int monitor = GetCurrentMonitor(); @@ -187,7 +192,7 @@ bool init(void *userData) { ECS_COMPONENT_DEFINE(ECS, Game); ecs_singleton_set(ECS, Game, {}); Game *game = ecs_singleton_get_mut(ECS, Game); - game->screen = SCREEN_MAIN_MENU; + setScreen(game, SCREEN_MAIN_MENU); game->font = LoadFontEx("assets/fonts/CompassPro.ttf", 92, NULL, 0); game->drawData = bzArrayCreate(DrawData, 1000); game->drawQuery = ecs_query(ECS, { @@ -394,6 +399,10 @@ void update(float dt, void *userData) { Game *game = ecs_singleton_get_mut(ECS, Game); InputState *input = ecs_singleton_get_mut(ECS, InputState); + game->screenPrevFrame = game->screen; + if (game->screen != game->nextScreen) + game->screen = game->nextScreen; + BZ_ASSERT(game->stackAlloc.allocated == 0); bzStackAllocReset(&game->stackAlloc); @@ -411,7 +420,7 @@ void update(float dt, void *userData) { break; case SCREEN_PAUSE_MENU: if (IsKeyReleased(input->mapping.backBtn)) { - game->screen = SCREEN_GAME; + setScreen(game, SCREEN_GAME); } break; case SCREEN_MAIN_MENU: @@ -581,10 +590,10 @@ static void renderPauseMenu(Game *game, float dt) { .flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_ALIGN_CENTER }); if (uiMainMenuButton("Resume")) { - game->screen = SCREEN_GAME; + setScreen(game, SCREEN_GAME); } if (uiMainMenuButton("Exit")) { - game->screen = SCREEN_MAIN_MENU; + setScreen(game, SCREEN_MAIN_MENU); unloadMap(game); loadMap(game, "assets/maps/main_menu_01.tmj"); } @@ -618,13 +627,12 @@ static void renderMainMenu(Game *game, float dt) { .flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_ALIGN_CENTER }); if (uiMainMenuButton("Play")) { - game->screen = SCREEN_GAME; + setScreen(game, SCREEN_GAME); unloadMap(game); loadMap(game, "assets/maps/pathing_test.tmj"); } if (uiMainMenuButton("Settings")) { - game->screenPrevFrame = game->screen; - game->screen = SCREEN_SETTINGS; + setScreen(game, SCREEN_SETTINGS); } if (uiMainMenuButton("Exit")) { bzGameExit(); @@ -673,14 +681,14 @@ static void renderSettings(Game *game, float dt) { }); if (uiSettingsButton("Back")) { - game->screen = SCREEN_MAIN_MENU; + setScreen(game, SCREEN_MAIN_MENU); } if (uiSettingsButton("Reset")) { opts = game->options; } if (uiSettingsButton("Apply")) { game->options = opts; - game->screen = SCREEN_MAIN_MENU; + setScreen(game, SCREEN_MAIN_MENU); } bzUIEnd(UI);