Add menu UI

master
John Montagu, the 4th Earl of Sandvich 2026-03-16 13:48:07 -07:00
parent 15525c2278
commit bf729d4796
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
7 changed files with 45 additions and 14 deletions

View File

@ -30,10 +30,10 @@ inline void DrawHud(float value, int stars) {
} }
inline void DrawMainMenu() { inline void DrawMainMenu() {
GuiLabel((Rectangle){WINDOW_WIDTH / 2 - 100, WINDOW_HEIGHT / 2 - 60, 200, 40}, GuiLabel((Rectangle){WINDOW_WIDTH / 2 - 60, WINDOW_HEIGHT / 2 - 60, 200, 40},
"GRAVITY SURFING"); "GRAVITY SURFING");
GuiLabel((Rectangle){WINDOW_WIDTH / 2 - 200, WINDOW_HEIGHT / 2, 400, 60}, GuiLabel((Rectangle){WINDOW_WIDTH / 2 - 160, WINDOW_HEIGHT / 2, 400, 60},
"LEFT CLICK to place a black hole that lasts 1 second\n" "LEFT CLICK to place a black hole that lasts 1 second\n"
"Each black hole costs 10 meter and does not drain over time\n" "Each black hole costs 10 meter and does not drain over time\n"
"Avoid hazards and collect stars to refill meter\n" "Avoid hazards and collect stars to refill meter\n"
@ -41,11 +41,16 @@ inline void DrawMainMenu() {
"Press SPACE or LEFT CLICK to start"); "Press SPACE or LEFT CLICK to start");
} }
inline bool DrawMuteButton(bool muted) {
const char *label = muted ? "Audio: Muted" : "Audio: On";
return GuiButton((Rectangle){WINDOW_WIDTH - 136, 16, 120, 28}, label);
}
inline void DrawDeathStats(int stars) { inline void DrawDeathStats(int stars) {
GuiLabel((Rectangle){WINDOW_WIDTH / 2 - 150, WINDOW_HEIGHT / 2 - 40, 300, 32}, "YOU DIED"); GuiLabel((Rectangle){WINDOW_WIDTH / 2 - 120, WINDOW_HEIGHT / 2 - 40, 300, 32}, "YOU DIED");
GuiLabel((Rectangle){WINDOW_WIDTH / 2 - 150, WINDOW_HEIGHT / 2 - 4, 300, 24}, GuiLabel((Rectangle){WINDOW_WIDTH / 2 - 120, WINDOW_HEIGHT / 2 - 4, 300, 24},
TextFormat("Stars Collected: %d", stars)); TextFormat("Stars Collected: %d", stars));
GuiLabel((Rectangle){WINDOW_WIDTH / 2 - 150, WINDOW_HEIGHT / 2 + 28, 300, 20}, GuiLabel((Rectangle){WINDOW_WIDTH / 2 - 120, WINDOW_HEIGHT / 2 + 28, 300, 20},
"Press ENTER or LEFT CLICK to retry"); "Press ENTER or LEFT CLICK to retry");
} }

View File

@ -7,12 +7,12 @@
void DeathScene::Update(float) { void DeathScene::Update(float) {
if (IsKeyPressed(KEY_ENTER)) { if (IsKeyPressed(KEY_ENTER)) {
manager.QueueSceneChange<GameplayScene>(); manager.QueueSceneChange<GameplayScene>(this->isMuted);
return; return;
} }
if (IsKeyPressed(KEY_ESCAPE)) { if (IsKeyPressed(KEY_ESCAPE)) {
manager.QueueSceneChange<StartMenuScene>(); manager.QueueSceneChange<StartMenuScene>(this->isMuted);
} }
} }
@ -24,11 +24,10 @@ void DeathScene::Draw() {
if (GuiButton((Rectangle){WINDOW_WIDTH / 2 - 150, WINDOW_HEIGHT / 2 + 28, 300, 20}, if (GuiButton((Rectangle){WINDOW_WIDTH / 2 - 150, WINDOW_HEIGHT / 2 + 28, 300, 20},
"Main Menu")) { "Main Menu")) {
manager.QueueSceneChange<StartMenuScene>(); manager.QueueSceneChange<StartMenuScene>(this->isMuted);
} }
if (GuiButton((Rectangle){WINDOW_WIDTH / 2 - 150, WINDOW_HEIGHT / 2 + 60, 300, 20}, if (GuiButton((Rectangle){WINDOW_WIDTH / 2 - 150, WINDOW_HEIGHT / 2 + 60, 300, 20}, "Retry")) {
"Retry")) { manager.QueueSceneChange<GameplayScene>(this->isMuted);
manager.QueueSceneChange<GameplayScene>();
} }
} }

View File

@ -16,9 +16,12 @@ class DeathScene : public Scene {
explicit DeathScene(SceneManager &owner) : Scene(owner) {} explicit DeathScene(SceneManager &owner) : Scene(owner) {}
// Alternate constructor that accepts the star count to display. // Alternate constructor that accepts the star count to display.
explicit DeathScene(SceneManager &owner, int stars) : Scene(owner), collectedStars(stars) {} explicit DeathScene(SceneManager &owner, int stars) : Scene(owner), collectedStars(stars) {}
explicit DeathScene(SceneManager &owner, int stars, bool muted)
: Scene(owner), collectedStars(stars), isMuted(muted) {}
void Update(float dt) override; void Update(float dt) override;
void Draw() override; void Draw() override;
private: private:
int collectedStars = 0; int collectedStars = 0;
bool isMuted = false;
}; };

View File

@ -17,7 +17,7 @@ void GameplayScene::Enter() {
// Inject the collected star count into the queued DeathScene by passing it // Inject the collected star count into the queued DeathScene by passing it
// as a constructor argument. SceneManager::QueueSceneChange supports // as a constructor argument. SceneManager::QueueSceneChange supports
// forwarding constructor args. // forwarding constructor args.
manager.QueueSceneChange<DeathScene>(collectedCount); manager.QueueSceneChange<DeathScene>(collectedCount, this->isMuted);
}; };
context.AddCollectiblePickedListener([this](Entity &collectible) { context.AddCollectiblePickedListener([this](Entity &collectible) {
collectedCount++; collectedCount++;

View File

@ -20,6 +20,7 @@ class DeathScene;
class GameplayScene : public Scene { class GameplayScene : public Scene {
public: public:
explicit GameplayScene(SceneManager &owner) : Scene(owner) {} explicit GameplayScene(SceneManager &owner) : Scene(owner) {}
explicit GameplayScene(SceneManager &owner, bool muted) : Scene(owner), isMuted(muted) {}
void Enter() override; void Enter() override;
void Exit() override; void Exit() override;
void Update(float dt) override; void Update(float dt) override;
@ -29,4 +30,5 @@ class GameplayScene : public Scene {
GameContext context; GameContext context;
int collectedCount = 0; int collectedCount = 0;
float meterValue = 60.0f; float meterValue = 60.0f;
bool isMuted = false;
}; };

View File

@ -4,13 +4,30 @@
#include "scene/GameplayScene.hpp" #include "scene/GameplayScene.hpp"
#include "scene/SceneManager.hpp" #include "scene/SceneManager.hpp"
#include "raylib.h"
void StartMenuScene::Enter() { ::SetMasterVolume(isMuted ? 0.0f : 1.0f); }
void StartMenuScene::Update(float) { void StartMenuScene::Update(float) {
if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { if (IsKeyPressed(KEY_M)) {
manager.QueueSceneChange<GameplayScene>(); isMuted = !isMuted;
::SetMasterVolume(isMuted ? 0.0f : 1.0f);
}
if (IsKeyPressed(KEY_ENTER)) {
manager.QueueSceneChange<GameplayScene>(isMuted);
} }
} }
void StartMenuScene::Draw() { void StartMenuScene::Draw() {
DrawMainMenu(); DrawMainMenu();
if (DrawMuteButton(isMuted)) {
isMuted = !isMuted;
::SetMasterVolume(isMuted ? 0.0f : 1.0f);
}
if (GuiButton((Rectangle){WINDOW_WIDTH / 2 - 150, WINDOW_HEIGHT / 2 + 80, 300, 20}, "Start Game")) {
manager.QueueSceneChange<GameplayScene>(isMuted);
}
Scene::Draw(); Scene::Draw();
} }

View File

@ -13,6 +13,11 @@ class GameplayScene;
class StartMenuScene : public Scene { class StartMenuScene : public Scene {
public: public:
explicit StartMenuScene(SceneManager &owner) : Scene(owner) {} explicit StartMenuScene(SceneManager &owner) : Scene(owner) {}
explicit StartMenuScene(SceneManager &owner, bool muted) : Scene(owner), isMuted(muted) {}
void Enter() override;
void Update(float dt) override; void Update(float dt) override;
void Draw() override; void Draw() override;
private:
bool isMuted = false;
}; };