refactor: applicationstate.h for main.c logo.h menu.h game.h

This commit is contained in:
huaqo-games
2025-03-19 09:38:03 +01:00
parent 0e2db9707b
commit ad47a50715
6 changed files with 62 additions and 29 deletions
+5
View File
@@ -0,0 +1,5 @@
{
"files.associations": {
"menu.h": "c"
}
}
+10
View File
@@ -0,0 +1,10 @@
#ifndef APPLICATIONSTATE_H
#define APPLICATIONSTATE_H
typedef struct {
Screen currentScreen;
bool running;
bool gameStarted;
} State;
#endif // APPLICATIONSTATE_H
+7 -2
View File
@@ -6,6 +6,7 @@
#include "../engine/postfx.h" #include "../engine/postfx.h"
#include "../engine/musicmanager.h" #include "../engine/musicmanager.h"
#include "../engine/asset.h" #include "../engine/asset.h"
#include "../engine/applicationstate.h"
#include "floor.h" #include "floor.h"
#include "player.h" #include "player.h"
#include "bee.h" #include "bee.h"
@@ -66,7 +67,7 @@ void InitGame(GameState* g){
g->target = LoadRenderTexture(GetScreenWidth(), GetScreenHeight()); g->target = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
} }
void UpdateGame(GameState* g, Screen *currentScreen, bool *running, bool *gameStarted){ void UpdateGame(GameState* g, State *state){
UpdatePostFX(&g->postFX); UpdatePostFX(&g->postFX);
UpdateMusic(&g->ambient); UpdateMusic(&g->ambient);
UpdateMouse(&g->mouse, &g->camera); 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); UpdateBee(&g->bee1, &g->flowers, &g->hive);
if (IsKeyPressed(KEY_ESCAPE)){ if (IsKeyPressed(KEY_ESCAPE)){
*currentScreen = MENU; state->currentScreen = MENU;
}
if (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE)) {
state->running = false;
} }
} }
+9 -2
View File
@@ -1,6 +1,8 @@
#ifndef LOGO_H #ifndef LOGO_H
#define LOGO_H #define LOGO_H
#include "../engine/applicationstate.h"
typedef struct { typedef struct {
int frameCounter; int frameCounter;
int positionX; int positionX;
@@ -29,7 +31,12 @@ void InitLogo(LogoState *l) {
l->alpha = 1.0f; 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 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) 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 else if (l->state == 4) { // State 4: Transition to GAMEPLAY
SetTargetFPS(0); SetTargetFPS(0);
*currentScreen = MENU; state->currentScreen = MENU;
} }
} }
+14 -6
View File
@@ -2,6 +2,7 @@
#define MENU_H #define MENU_H
#include "../engine/gui.h" #include "../engine/gui.h"
#include "../engine/applicationstate.h"
typedef struct { typedef struct {
Button startButton; Button startButton;
@@ -10,18 +11,25 @@ typedef struct {
void InitMenu(MenuState *t){ void InitMenu(MenuState *t){
initButton(&t->startButton, "Start Game", GetScreenWidth() / 10, GetScreenHeight() / 20, GetScreenWidth() / 2, GetScreenHeight() / 2); 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)) { if (isButtonClicked(&t->startButton)) {
*gameStarted = true; state->gameStarted = true;
t->startButton.text = "Continue"; t->startButton.text = "Continue";
*currentScreen = GAMEPLAY; state->currentScreen = GAMEPLAY;
} }
if (isButtonClicked(&t->quitButton)) { if (isButtonClicked(&t->quitButton) || (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE))) {
*running = false; state->running = false;
} }
if (state->gameStarted){
if (IsKeyPressed(KEY_ESCAPE)){
state->currentScreen = GAMEPLAY;
}
}
} }
void RenderMenu(MenuState *t){ void RenderMenu(MenuState *t){
+17 -19
View File
@@ -4,27 +4,25 @@
#include <stdlib.h> #include <stdlib.h>
#include "engine/screen.h" #include "engine/screen.h"
#include "engine/settings.h" #include "engine/settings.h"
#include "engine/applicationstate.h"
#include "game/logo.h" #include "game/logo.h"
#include "game/menu.h" #include "game/menu.h"
#include "game/game.h" #include "game/game.h"
typedef struct {
State state;
Settings settings;
LogoState logo;
GameState game;
MenuState menu;
} Application;
int main(void) int main(void)
{ {
typedef struct {
Screen currentScreen;
bool running;
bool gameStarted;
Settings settings;
LogoState logo;
GameState game;
MenuState menu;
} Application;
Application app; Application app;
app.currentScreen = LOGO; app.state.currentScreen = LOGO;
app.running = true; app.state.running = true;
app.gameStarted = false; app.state.gameStarted = false;
app.settings = (Settings){ app.settings = (Settings){
.name = "The Last Hive", .name = "The Last Hive",
.screenWidth = 1280, .screenWidth = 1280,
@@ -37,23 +35,23 @@ int main(void)
InitMenu(&app.menu); InitMenu(&app.menu);
InitGame(&app.game); InitGame(&app.game);
while (app.running) while (app.state.running)
{ {
switch (app.currentScreen) switch (app.state.currentScreen)
{ {
case LOGO: case LOGO:
{ {
UpdateLogo(&app.logo,&app.currentScreen); UpdateLogo(&app.logo, &app.state);
RenderLogo(&app.logo); RenderLogo(&app.logo);
}break; }break;
case MENU: case MENU:
{ {
UpdateMenu(&app.menu, &app.currentScreen, &app.running, &app.gameStarted); UpdateMenu(&app.menu, &app.state);
RenderMenu(&app.menu); RenderMenu(&app.menu);
}break; }break;
case GAMEPLAY: case GAMEPLAY:
{ {
UpdateGame(&app.game, &app.currentScreen, &app.running, &app.gameStarted); UpdateGame(&app.game, &app.state);
RenderGame(&app.game); RenderGame(&app.game);
}break; }break;
default: break; default: break;