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
{
Rectangle rec;
const char *text;
char *text;
} 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->text = text;
+1 -1
View File
@@ -66,7 +66,7 @@ void InitGame(GameState* g){
g->target = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
}
void UpdateGame(GameState* g, Screen *currentScreen){
void UpdateGame(GameState* g, Screen *currentScreen, bool *running, bool *gameStarted){
UpdatePostFX(&g->postFX);
UpdateMusic(&g->ambient);
UpdateMouse(&g->mouse, &g->camera);
+9 -1
View File
@@ -5,22 +5,30 @@
typedef struct {
Button startButton;
Button quitButton;
} MenuState;
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);
}
void UpdateMenu(MenuState *t, Screen *currentScreen){
void UpdateMenu(MenuState *t, Screen *currentScreen, bool *running, bool *gameStarted){
if (isButtonClicked(&t->startButton)) {
*gameStarted = true;
t->startButton.text = "Continue";
*currentScreen = GAMEPLAY;
}
if (isButtonClicked(&t->quitButton)) {
*running = false;
}
}
void RenderMenu(MenuState *t){
BeginDrawing();
ClearBackground(WHITE);
renderButton(&t->startButton);
renderButton(&t->quitButton);
EndDrawing();
}
+29 -19
View File
@@ -10,46 +10,56 @@
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",
.screenWidth = 1280,
.screenHeight = 720
};
InitWindow(settings.screenWidth, settings.screenHeight, settings.name);
Screen currentScreen = GAMEPLAY;
LogoState logo;
GameState game;
MenuState menu;
InitWindow(app.settings.screenWidth, app.settings.screenHeight, app.settings.name);
InitLogo(&logo);
InitMenu(&menu);
InitGame(&game);
InitLogo(&app.logo);
InitMenu(&app.menu);
InitGame(&app.game);
while (true)
while (app.running)
{
switch (currentScreen)
switch (app.currentScreen)
{
case LOGO:
{
UpdateLogo(&logo,&currentScreen);
RenderLogo(&logo);
UpdateLogo(&app.logo,&app.currentScreen);
RenderLogo(&app.logo);
}break;
case MENU:
{
UpdateMenu(&menu, &currentScreen);
RenderMenu(&menu);
UpdateMenu(&app.menu, &app.currentScreen, &app.running, &app.gameStarted);
RenderMenu(&app.menu);
}break;
case GAMEPLAY:
{
UpdateGame(&game, &currentScreen);
RenderGame(&game);
UpdateGame(&app.game, &app.currentScreen, &app.running, &app.gameStarted);
RenderGame(&app.game);
}break;
default: break;
}
}
CleanupGame(&game);
CleanupGame(&app.game);
CloseWindow();
return 0;
}