feature: quit button, start button changes to continue

This commit is contained in:
huaqo-games
2025-03-18 17:34:14 +01:00
parent a4d9b22c32
commit 0e2db9707b
4 changed files with 41 additions and 23 deletions
+2 -2
View File
@@ -7,10 +7,10 @@
typedef struct typedef struct
{ {
Rectangle rec; Rectangle rec;
const char *text; char *text;
} Button; } Button;
void initButton(Button *b, const char *text, int buttonWidth, int buttonHeight, int buttonPosX, int buttonPosY) void initButton(Button *b, char *text, int buttonWidth, int buttonHeight, int buttonPosX, int buttonPosY)
{ {
b->rec = (Rectangle){buttonPosX - buttonWidth / 2, buttonPosY - buttonHeight / 2, buttonWidth, buttonHeight}; b->rec = (Rectangle){buttonPosX - buttonWidth / 2, buttonPosY - buttonHeight / 2, buttonWidth, buttonHeight};
b->text = text; b->text = text;
+1 -1
View File
@@ -66,7 +66,7 @@ void InitGame(GameState* g){
g->target = LoadRenderTexture(GetScreenWidth(), GetScreenHeight()); g->target = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
} }
void UpdateGame(GameState* g, Screen *currentScreen){ void UpdateGame(GameState* g, Screen *currentScreen, bool *running, bool *gameStarted){
UpdatePostFX(&g->postFX); UpdatePostFX(&g->postFX);
UpdateMusic(&g->ambient); UpdateMusic(&g->ambient);
UpdateMouse(&g->mouse, &g->camera); UpdateMouse(&g->mouse, &g->camera);
+9 -1
View File
@@ -5,22 +5,30 @@
typedef struct { typedef struct {
Button startButton; Button startButton;
Button quitButton;
} MenuState; } MenuState;
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);
} }
void UpdateMenu(MenuState *t, Screen *currentScreen){ void UpdateMenu(MenuState *t, Screen *currentScreen, bool *running, bool *gameStarted){
if (isButtonClicked(&t->startButton)) { if (isButtonClicked(&t->startButton)) {
*gameStarted = true;
t->startButton.text = "Continue";
*currentScreen = GAMEPLAY; *currentScreen = GAMEPLAY;
} }
if (isButtonClicked(&t->quitButton)) {
*running = false;
}
} }
void RenderMenu(MenuState *t){ void RenderMenu(MenuState *t){
BeginDrawing(); BeginDrawing();
ClearBackground(WHITE); ClearBackground(WHITE);
renderButton(&t->startButton); renderButton(&t->startButton);
renderButton(&t->quitButton);
EndDrawing(); EndDrawing();
} }
+29 -19
View File
@@ -10,46 +10,56 @@
int main(void) int main(void)
{ {
Settings settings = {
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.settings = (Settings){
.name = "The Last Hive", .name = "The Last Hive",
.screenWidth = 1280, .screenWidth = 1280,
.screenHeight = 720 .screenHeight = 720
}; };
InitWindow(settings.screenWidth, settings.screenHeight, settings.name); InitWindow(app.settings.screenWidth, app.settings.screenHeight, app.settings.name);
Screen currentScreen = GAMEPLAY;
LogoState logo;
GameState game;
MenuState menu;
InitLogo(&logo); InitLogo(&app.logo);
InitMenu(&menu); InitMenu(&app.menu);
InitGame(&game); InitGame(&app.game);
while (true) while (app.running)
{ {
switch (currentScreen) switch (app.currentScreen)
{ {
case LOGO: case LOGO:
{ {
UpdateLogo(&logo,&currentScreen); UpdateLogo(&app.logo,&app.currentScreen);
RenderLogo(&logo); RenderLogo(&app.logo);
}break; }break;
case MENU: case MENU:
{ {
UpdateMenu(&menu, &currentScreen); UpdateMenu(&app.menu, &app.currentScreen, &app.running, &app.gameStarted);
RenderMenu(&menu); RenderMenu(&app.menu);
}break; }break;
case GAMEPLAY: case GAMEPLAY:
{ {
UpdateGame(&game, &currentScreen); UpdateGame(&app.game, &app.currentScreen, &app.running, &app.gameStarted);
RenderGame(&game); RenderGame(&app.game);
}break; }break;
default: break; default: break;
} }
} }
CleanupGame(&game); CleanupGame(&app.game);
CloseWindow(); CloseWindow();
return 0; return 0;
} }