Fix screen switching

This commit is contained in:
2024-01-10 19:43:57 +01:00
parent 692f5744ad
commit b09f1d4ca9
2 changed files with 18 additions and 9 deletions

View File

@@ -41,6 +41,7 @@ static Options getDefaultOptions() {
typedef struct Game { typedef struct Game {
GameScreen screen; GameScreen screen;
GameScreen nextScreen;
GameScreen screenPrevFrame; GameScreen screenPrevFrame;
Camera2D camera; Camera2D camera;
BzTileset tileset; BzTileset tileset;

View File

@@ -162,6 +162,11 @@ bool deserializeOptions(const char *path, Options *optsOut) {
return false; return false;
} }
void setScreen(Game *game, GameScreen newScreen) {
game->nextScreen = newScreen;
}
bool init(void *userData) { bool init(void *userData) {
// Center window // Center window
int monitor = GetCurrentMonitor(); int monitor = GetCurrentMonitor();
@@ -187,7 +192,7 @@ bool init(void *userData) {
ECS_COMPONENT_DEFINE(ECS, Game); ECS_COMPONENT_DEFINE(ECS, Game);
ecs_singleton_set(ECS, Game, {}); ecs_singleton_set(ECS, Game, {});
Game *game = ecs_singleton_get_mut(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->font = LoadFontEx("assets/fonts/CompassPro.ttf", 92, NULL, 0);
game->drawData = bzArrayCreate(DrawData, 1000); game->drawData = bzArrayCreate(DrawData, 1000);
game->drawQuery = ecs_query(ECS, { game->drawQuery = ecs_query(ECS, {
@@ -394,6 +399,10 @@ void update(float dt, void *userData) {
Game *game = ecs_singleton_get_mut(ECS, Game); Game *game = ecs_singleton_get_mut(ECS, Game);
InputState *input = ecs_singleton_get_mut(ECS, InputState); 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); BZ_ASSERT(game->stackAlloc.allocated == 0);
bzStackAllocReset(&game->stackAlloc); bzStackAllocReset(&game->stackAlloc);
@@ -411,7 +420,7 @@ void update(float dt, void *userData) {
break; break;
case SCREEN_PAUSE_MENU: case SCREEN_PAUSE_MENU:
if (IsKeyReleased(input->mapping.backBtn)) { if (IsKeyReleased(input->mapping.backBtn)) {
game->screen = SCREEN_GAME; setScreen(game, SCREEN_GAME);
} }
break; break;
case SCREEN_MAIN_MENU: 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 .flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_ALIGN_CENTER
}); });
if (uiMainMenuButton("Resume")) { if (uiMainMenuButton("Resume")) {
game->screen = SCREEN_GAME; setScreen(game, SCREEN_GAME);
} }
if (uiMainMenuButton("Exit")) { if (uiMainMenuButton("Exit")) {
game->screen = SCREEN_MAIN_MENU; setScreen(game, SCREEN_MAIN_MENU);
unloadMap(game); unloadMap(game);
loadMap(game, "assets/maps/main_menu_01.tmj"); 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 .flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_ALIGN_CENTER
}); });
if (uiMainMenuButton("Play")) { if (uiMainMenuButton("Play")) {
game->screen = SCREEN_GAME; setScreen(game, SCREEN_GAME);
unloadMap(game); unloadMap(game);
loadMap(game, "assets/maps/pathing_test.tmj"); loadMap(game, "assets/maps/pathing_test.tmj");
} }
if (uiMainMenuButton("Settings")) { if (uiMainMenuButton("Settings")) {
game->screenPrevFrame = game->screen; setScreen(game, SCREEN_SETTINGS);
game->screen = SCREEN_SETTINGS;
} }
if (uiMainMenuButton("Exit")) { if (uiMainMenuButton("Exit")) {
bzGameExit(); bzGameExit();
@@ -673,14 +681,14 @@ static void renderSettings(Game *game, float dt) {
}); });
if (uiSettingsButton("Back")) { if (uiSettingsButton("Back")) {
game->screen = SCREEN_MAIN_MENU; setScreen(game, SCREEN_MAIN_MENU);
} }
if (uiSettingsButton("Reset")) { if (uiSettingsButton("Reset")) {
opts = game->options; opts = game->options;
} }
if (uiSettingsButton("Apply")) { if (uiSettingsButton("Apply")) {
game->options = opts; game->options = opts;
game->screen = SCREEN_MAIN_MENU; setScreen(game, SCREEN_MAIN_MENU);
} }
bzUIEnd(UI); bzUIEnd(UI);