From d5016ac164882b048711ecbd460fbbd691443b87 Mon Sep 17 00:00:00 2001 From: huaqo-games Date: Sun, 23 Mar 2025 21:08:53 +0100 Subject: [PATCH] new texture buttons --- src/game/menu.h | 90 +++++++++++++++++++++++++++++++++++-------- src/game/menuassets.h | 14 +++++++ 2 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 src/game/menuassets.h diff --git a/src/game/menu.h b/src/game/menu.h index 9fee1a3..4c877be 100644 --- a/src/game/menu.h +++ b/src/game/menu.h @@ -3,41 +3,101 @@ #include "../engine/gui.h" #include "../engine/applicationstate.h" +#include "../engine/asset.h" +#include "../engine/gamegui.h" +#include "menuassets.h" typedef struct { + Assets assets; + Texture2D textures[MENU_TEX_COUNT]; Button startButton; Button quitButton; + ImageButton newStartButton; + ImageButton newQuitButton; } 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 * 2)); +void InitMenu(MenuState *m){ + m->assets = (Assets){ + .textureAssets = menuTextureAssets + }; + for (int i = 0; i < MENU_TEX_COUNT; i++){ + m->textures[i] = LoadTexture(m->assets.textureAssets[i].path); + } + + Rectangle startButtonBounds = { + GetScreenWidth() / 2 - (GetScreenWidth() / 10) / 2, + GetScreenHeight() / 2 - (GetScreenHeight() / 20) / 2, + GetScreenWidth() / 10, + GetScreenHeight() / 20 + }; + + Rectangle quitButtonBounds = { + GetScreenWidth() / 2 - (GetScreenWidth() / 10) / 2, + GetScreenHeight() / 2 + (GetScreenHeight() / 10) / 2, + GetScreenWidth() / 10, + GetScreenHeight() / 20 + }; + + Rectangle playTexRecs[2] = { + { 0.0f, 96.0f, m->assets.textureAssets[BIG_BUTTONS].frameWidth, m->assets.textureAssets[BIG_BUTTONS].frameHeight }, + { m->assets.textureAssets[BIG_BUTTONS].frameWidth, 96.0f, m->assets.textureAssets[BIG_BUTTONS].frameWidth, m->assets.textureAssets[BIG_BUTTONS].frameHeight } + }; + + Rectangle emptyTexRecs[2] = { + { 0.0f, 0.0f, m->assets.textureAssets[BIG_BUTTONS].frameWidth, m->assets.textureAssets[BIG_BUTTONS].frameHeight }, + { m->assets.textureAssets[BIG_BUTTONS].frameWidth, 0.0f, m->assets.textureAssets[BIG_BUTTONS].frameWidth, m->assets.textureAssets[BIG_BUTTONS].frameHeight } + }; + + initImageButton(&m->newStartButton, playTexRecs, startButtonBounds, m->textures[BIG_BUTTONS]); + initImageButton(&m->newQuitButton, emptyTexRecs, quitButtonBounds, m->textures[BIG_BUTTONS]); + // initButton(&m->startButton, "Start Game", GetScreenWidth() / 10, GetScreenHeight() / 20, GetScreenWidth() / 2, GetScreenHeight() / 2); + // initButton(&m->quitButton, "Quit Game", GetScreenWidth() / 10, GetScreenHeight() / 20, GetScreenWidth() / 2, + // GetScreenHeight() / 2 + (GetScreenHeight() / 20 * 2)); } -void UpdateMenu(MenuState *t, State *state){ - if (isButtonClicked(&t->startButton)) { +void UpdateMenu(MenuState *m, State *state){ + + if (isImageButtonClicked(&m->newStartButton)){ state->gameStarted = true; - t->startButton.text = "Continue"; + m->startButton.text = "Continue"; state->currentScreen = GAMEPLAY; } - if (isButtonClicked(&t->quitButton) || (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE))) { + + if (isImageButtonClicked(&m->newQuitButton) || (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE))) { state->running = false; } - if (state->gameStarted){ - if (IsKeyPressed(KEY_ESCAPE)){ - state->currentScreen = GAMEPLAY; - } - } + + // if (isButtonClicked(&m->startButton)) { + // state->gameStarted = true; + // m->startButton.text = "Continue"; + // state->currentScreen = GAMEPLAY; + // } + // if (isButtonClicked(&m->quitButton) || (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE))) { + // state->running = false; + // } + + // if (state->gameStarted){ + // if (IsKeyPressed(KEY_ESCAPE)){ + // state->currentScreen = GAMEPLAY; + // } + // } } -void RenderMenu(MenuState *t){ +void RenderMenu(MenuState *m){ BeginDrawing(); ClearBackground(WHITE); - renderButton(&t->startButton); - renderButton(&t->quitButton); + // renderButton(&m->startButton); + // renderButton(&m->quitButton); + renderImageButton(&m->newStartButton); + renderImageButton(&m->newQuitButton); + DrawFPS(10, 10); EndDrawing(); } +void CleanupMenu(MenuState *m){ + for (int i = 0; i < MENU_TEX_COUNT; i++){ if (m->textures[i].id > 0) { UnloadTexture(m->textures[i]); } } +} + #endif // MENU_H diff --git a/src/game/menuassets.h b/src/game/menuassets.h new file mode 100644 index 0000000..0743ce8 --- /dev/null +++ b/src/game/menuassets.h @@ -0,0 +1,14 @@ +#ifndef MENUASSETS_H +#define MENUASSETS_H +#include "../engine/asset.h" + +typedef enum { + BIG_BUTTONS, + MENU_TEX_COUNT +} MenuTextureID; + +const TextureAsset menuTextureAssets[MENU_TEX_COUNT] = { + {"assets/SproutLandsUI/Spritesheets/UIBigPlayButton.png", 96.0f, 32.0f, 180.0f} +}; + +#endif // MENUASSETS_H