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

@@ -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);