From ad47a5071580147f9fe2fda33c44b84abdb82b50 Mon Sep 17 00:00:00 2001 From: huaqo-games Date: Wed, 19 Mar 2025 09:38:03 +0100 Subject: [PATCH] refactor: applicationstate.h for main.c logo.h menu.h game.h --- .vscode/settings.json | 5 +++++ src/engine/applicationstate.h | 10 ++++++++++ src/game/game.h | 9 +++++++-- src/game/logo.h | 11 +++++++++-- src/game/menu.h | 20 +++++++++++++------ src/main.c | 36 +++++++++++++++++------------------ 6 files changed, 62 insertions(+), 29 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 src/engine/applicationstate.h diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e5189ba --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "menu.h": "c" + } +} \ No newline at end of file diff --git a/src/engine/applicationstate.h b/src/engine/applicationstate.h new file mode 100644 index 0000000..2681416 --- /dev/null +++ b/src/engine/applicationstate.h @@ -0,0 +1,10 @@ +#ifndef APPLICATIONSTATE_H +#define APPLICATIONSTATE_H + +typedef struct { + Screen currentScreen; + bool running; + bool gameStarted; +} State; + +#endif // APPLICATIONSTATE_H diff --git a/src/game/game.h b/src/game/game.h index 0dc1654..a88ee58 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -6,6 +6,7 @@ #include "../engine/postfx.h" #include "../engine/musicmanager.h" #include "../engine/asset.h" +#include "../engine/applicationstate.h" #include "floor.h" #include "player.h" #include "bee.h" @@ -66,7 +67,7 @@ void InitGame(GameState* g){ g->target = LoadRenderTexture(GetScreenWidth(), GetScreenHeight()); } -void UpdateGame(GameState* g, Screen *currentScreen, bool *running, bool *gameStarted){ +void UpdateGame(GameState* g, State *state){ UpdatePostFX(&g->postFX); UpdateMusic(&g->ambient); UpdateMouse(&g->mouse, &g->camera); @@ -76,7 +77,11 @@ void UpdateGame(GameState* g, Screen *currentScreen, bool *running, bool *gameSt UpdateBee(&g->bee1, &g->flowers, &g->hive); if (IsKeyPressed(KEY_ESCAPE)){ - *currentScreen = MENU; + state->currentScreen = MENU; + } + + if (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE)) { + state->running = false; } } diff --git a/src/game/logo.h b/src/game/logo.h index e82c054..790a12f 100644 --- a/src/game/logo.h +++ b/src/game/logo.h @@ -1,6 +1,8 @@ #ifndef LOGO_H #define LOGO_H +#include "../engine/applicationstate.h" + typedef struct { int frameCounter; int positionX; @@ -29,7 +31,12 @@ void InitLogo(LogoState *l) { l->alpha = 1.0f; } -void UpdateLogo(LogoState *l, Screen *currentScreen) { +void UpdateLogo(LogoState *l, State *state) { + + if (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE)) { + state->running = false; + } + l->frameCounter++; // Increment the frame counter each frame if (l->state == 0) { // State 0: Small box blinking for 120 frames (2 seconds at 60 FPS) @@ -71,7 +78,7 @@ void UpdateLogo(LogoState *l, Screen *currentScreen) { } else if (l->state == 4) { // State 4: Transition to GAMEPLAY SetTargetFPS(0); - *currentScreen = MENU; + state->currentScreen = MENU; } } diff --git a/src/game/menu.h b/src/game/menu.h index 7c4dec2..9fee1a3 100644 --- a/src/game/menu.h +++ b/src/game/menu.h @@ -2,6 +2,7 @@ #define MENU_H #include "../engine/gui.h" +#include "../engine/applicationstate.h" typedef struct { Button startButton; @@ -10,18 +11,25 @@ typedef struct { void InitMenu(MenuState *t){ initButton(&t->startButton, "Start Game", GetScreenWidth() / 10, GetScreenHeight() / 20, GetScreenWidth() / 2, GetScreenHeight() / 2); - initButton(&t->quitButton, "Quit Game", GetScreenWidth() / 10, GetScreenHeight() / 20, GetScreenWidth() / 2, GetScreenHeight() / 2 + GetScreenHeight() / 20); + initButton(&t->quitButton, "Quit Game", GetScreenWidth() / 10, GetScreenHeight() / 20, GetScreenWidth() / 2, GetScreenHeight() / 2 + (GetScreenHeight() / 20 * 2)); } -void UpdateMenu(MenuState *t, Screen *currentScreen, bool *running, bool *gameStarted){ +void UpdateMenu(MenuState *t, State *state){ if (isButtonClicked(&t->startButton)) { - *gameStarted = true; + state->gameStarted = true; t->startButton.text = "Continue"; - *currentScreen = GAMEPLAY; + state->currentScreen = GAMEPLAY; } - if (isButtonClicked(&t->quitButton)) { - *running = false; + if (isButtonClicked(&t->quitButton) || (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE))) { + state->running = false; } + + if (state->gameStarted){ + if (IsKeyPressed(KEY_ESCAPE)){ + state->currentScreen = GAMEPLAY; + } + } + } void RenderMenu(MenuState *t){ diff --git a/src/main.c b/src/main.c index fa67309..d5c3706 100644 --- a/src/main.c +++ b/src/main.c @@ -4,27 +4,25 @@ #include #include "engine/screen.h" #include "engine/settings.h" +#include "engine/applicationstate.h" #include "game/logo.h" #include "game/menu.h" #include "game/game.h" +typedef struct { + State state; + Settings settings; + LogoState logo; + GameState game; + MenuState menu; +} Application; + int main(void) { - - typedef struct { - Screen currentScreen; - bool running; - bool gameStarted; - Settings settings; - LogoState logo; - GameState game; - MenuState menu; - } Application; - Application app; - app.currentScreen = LOGO; - app.running = true; - app.gameStarted = false; + app.state.currentScreen = LOGO; + app.state.running = true; + app.state.gameStarted = false; app.settings = (Settings){ .name = "The Last Hive", .screenWidth = 1280, @@ -37,23 +35,23 @@ int main(void) InitMenu(&app.menu); InitGame(&app.game); - while (app.running) + while (app.state.running) { - switch (app.currentScreen) + switch (app.state.currentScreen) { case LOGO: { - UpdateLogo(&app.logo,&app.currentScreen); + UpdateLogo(&app.logo, &app.state); RenderLogo(&app.logo); }break; case MENU: { - UpdateMenu(&app.menu, &app.currentScreen, &app.running, &app.gameStarted); + UpdateMenu(&app.menu, &app.state); RenderMenu(&app.menu); }break; case GAMEPLAY: { - UpdateGame(&app.game, &app.currentScreen, &app.running, &app.gameStarted); + UpdateGame(&app.game, &app.state); RenderGame(&app.game); }break; default: break;