From a14fae2f5d7820703de79412709dc682c8fbbfc1 Mon Sep 17 00:00:00 2001 From: Anders Fritz Fischer Date: Wed, 11 Mar 2026 07:30:24 +0100 Subject: [PATCH 01/10] Remove empty files --- src/Game.cpp | 0 src/Game.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/Game.cpp delete mode 100644 src/Game.h diff --git a/src/Game.cpp b/src/Game.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/Game.h b/src/Game.h deleted file mode 100644 index e69de29..0000000 From b7885bd8890dbb1d2b0a7c19f73c96767e087b6b Mon Sep 17 00:00:00 2001 From: Fritzn Date: Wed, 11 Mar 2026 10:33:38 +0100 Subject: [PATCH 02/10] 25 refactor UI (#26) * Add UIElement and use it in StartScene * Refactor EndScene buttons with UIElement * Code cleanup --- include/scenes/EndScene.h | 22 +++++-------- include/scenes/GameScene.h | 27 ++++++---------- include/scenes/StartScene.h | 25 ++++++--------- include/ui/Button.h | 24 +++++++++++++++ include/ui/UIElement.h | 16 ++++++++++ src/scenes/EndScene.cpp | 61 ++++++++++++++++++++++++++++--------- src/scenes/GameScene.cpp | 17 +++++++++-- src/scenes/SceneManager.cpp | 2 +- src/scenes/StartScene.cpp | 58 ++++++++++++++++++++++++++--------- src/ui/Button.cpp | 22 +++++++++++++ 10 files changed, 193 insertions(+), 81 deletions(-) create mode 100644 include/ui/Button.h create mode 100644 include/ui/UIElement.h create mode 100644 src/ui/Button.cpp diff --git a/include/scenes/EndScene.h b/include/scenes/EndScene.h index 4d28433..9aee343 100644 --- a/include/scenes/EndScene.h +++ b/include/scenes/EndScene.h @@ -1,25 +1,22 @@ #ifndef MEMORYSDL_ENDSCENE_H #define MEMORYSDL_ENDSCENE_H #include "scenes/Scene.h" + +#include "ui/UIElement.h" #include "scenes/SceneManager.h" +#include + class EndScene : public Scene { public: explicit EndScene(SceneManager &manager, GameContext &context) : Scene(context), m_sceneManager(manager) { - m_uiPlayButtonRect.x = static_cast(m_context.windowWidth) * 0.15f; - m_uiPlayButtonRect.y = static_cast(m_context.windowHeight) * 0.75f; - m_uiPlayButtonRect.w = static_cast(m_context.texWidth) * 0.5f; - m_uiPlayButtonRect.h = static_cast(m_context.texHeight) * 0.5f; - - m_uiQuitButtonRect.x = static_cast(m_context.windowWidth) * 0.75f; - m_uiQuitButtonRect.y = static_cast(m_context.windowHeight) * 0.75f; - m_uiQuitButtonRect.w = static_cast(m_context.texWidth) * 0.5f; - m_uiQuitButtonRect.h = static_cast(m_context.texHeight) * 0.5f; + InitUI(); } + void InitUI(); void HandleEvent(const SDL_Event &event) override; void Update(float dt) override; void Render(SDL_Renderer *renderer) override; @@ -28,12 +25,7 @@ class EndScene : public Scene SceneManager &m_sceneManager; // UI - const char* m_uiPlayButtonText = "PLAY"; - const char* m_uiQuitButtonText = "QUIT"; - SDL_FRect m_uiPlayButtonRect{}; - SDL_FRect m_uiQuitButtonRect{}; - bool uiPlayPressed = false; - bool uiQuitPressed = false; + std::vector> m_ui; }; #endif //MEMORYSDL_ENDSCENE_H \ No newline at end of file diff --git a/include/scenes/GameScene.h b/include/scenes/GameScene.h index a46751a..55188af 100644 --- a/include/scenes/GameScene.h +++ b/include/scenes/GameScene.h @@ -9,24 +9,16 @@ class GameScene : public Scene { public: explicit GameScene(SceneManager &manager, GameContext &context) - : Scene(context), m_sceneManager(manager), m_gameState(GameState::Paused), m_numOfCardsMatched(0), + : Scene(context), m_sceneManager(manager), m_numOfCardsMatched(0), m_cardsSelected(CardSelected::NoCard), m_firstCardIdx(-1), - m_secondCardIdx(-1), m_resolveCardsAtMs(0), - m_attempts(MAX_ATTEMPTS) + m_secondCardIdx(-1), m_attempts(MAX_ATTEMPTS), + m_resolveCardsAtMs(0) { - constexpr auto size = static_cast(MAX_ATTEMPTS); - m_uiHeartRects.resize(size); - for (size_t i = 0; i < size; ++i) { - m_uiHeartRects[i].x = (static_cast(m_context.windowWidth) * 0.5f + static_cast( - m_context.texWidth * i) - static_cast(m_context.texWidth * m_attempts) / - 2); - m_uiHeartRects[i].y = static_cast(m_context.windowHeight) * 0.001f; - m_uiHeartRects[i].w = static_cast(m_context.texWidth); - m_uiHeartRects[i].h = static_cast(m_context.texHeight); - } + Init(); } + void Init(); void HandleEvent(const SDL_Event &event) override; void Update(float dt) override; void Render(SDL_Renderer *renderer) override; @@ -34,20 +26,19 @@ class GameScene : public Scene private: SceneManager &m_sceneManager; - enum class GameState { Running, Ended, Paused, Starting }; - GameState m_gameState; - + // State enum class CardSelected { NoCard, OneCard, TwoCards }; size_t m_numOfCardsMatched; CardSelected m_cardsSelected; int m_firstCardIdx; int m_secondCardIdx; + int m_attempts; Uint64 m_resolveCardsAtMs; static constexpr Uint64 m_revealDelayMs = 800; - std::vector m_uiHeartRects; - int m_attempts; + // UI + std::vector m_uiHeartRects; // TODO: refactor to UIElement }; #endif //MEMORYSDL_GAMESCENE_H \ No newline at end of file diff --git a/include/scenes/StartScene.h b/include/scenes/StartScene.h index 3d216fe..9254f20 100644 --- a/include/scenes/StartScene.h +++ b/include/scenes/StartScene.h @@ -2,7 +2,11 @@ #define MEMORYSDL_STARTSCENE_H #include "scenes/Scene.h" -class SceneManager; +#include "ui/Button.h" +#include "scenes/SceneManager.h" + +#include +#include class StartScene : public Scene { @@ -10,17 +14,11 @@ class StartScene : public Scene explicit StartScene(SceneManager &manager, GameContext& context) : Scene(context), m_sceneManager(manager) { - m_uiPlayButtonRect.x = static_cast(m_context.windowWidth) * 0.15f; - m_uiPlayButtonRect.y = static_cast(m_context.windowHeight) * 0.75f; - m_uiPlayButtonRect.w = static_cast(m_context.texWidth) * 0.5f; - m_uiPlayButtonRect.h = static_cast(m_context.texHeight) * 0.5f; - - m_uiQuitButtonRect.x = static_cast(m_context.windowWidth) * 0.75f; - m_uiQuitButtonRect.y = static_cast(m_context.windowHeight) * 0.75f; - m_uiQuitButtonRect.w = static_cast(m_context.texWidth) * 0.5f; - m_uiQuitButtonRect.h = static_cast(m_context.texHeight) * 0.5f; + InitUI(); } + void InitUI(); + void HandleEvent(const SDL_Event &event) override; void Update(float dt) override {}; void Render(SDL_Renderer *renderer) override; @@ -29,12 +27,7 @@ class StartScene : public Scene SceneManager &m_sceneManager; // UI - const char* m_uiPlayButtonText = "PLAY"; - const char* m_uiQuitButtonText = "QUIT"; - SDL_FRect m_uiPlayButtonRect{}; - SDL_FRect m_uiQuitButtonRect{}; - bool uiPlayPressed = false; - bool uiQuitPressed = false; + std::vector> m_ui; }; #endif //MEMORYSDL_STARTSCENE_H \ No newline at end of file diff --git a/include/ui/Button.h b/include/ui/Button.h new file mode 100644 index 0000000..97ac6c8 --- /dev/null +++ b/include/ui/Button.h @@ -0,0 +1,24 @@ +#ifndef MEMORYSDL_BUTTON_H +#define MEMORYSDL_BUTTON_H +#include "UIElement.h" + +#include + +class Button : public UIElement +{ +public: + Button(const SDL_FRect rect, SDL_Texture* texture, + std::function onClick) + : m_rect(rect), m_texture(texture), m_onClick(onClick) + {} + + void HandleEvent(const SDL_Event& event) override; + void Render(SDL_Renderer* renderer) override; + +private: + SDL_FRect m_rect; + SDL_Texture* m_texture; + std::function m_onClick; +}; + +#endif //MEMORYSDL_BUTTON_H \ No newline at end of file diff --git a/include/ui/UIElement.h b/include/ui/UIElement.h new file mode 100644 index 0000000..db3a5ce --- /dev/null +++ b/include/ui/UIElement.h @@ -0,0 +1,16 @@ +#ifndef MEMORYSDL_UIELEMENT_H +#define MEMORYSDL_UIELEMENT_H +#include "SDL3/SDL_events.h" +#include "SDL3/SDL_render.h" + +class UIElement +{ +public: + virtual ~UIElement() = default; + + virtual void HandleEvent(const SDL_Event& event) = 0; + virtual void Update() {}; + virtual void Render(SDL_Renderer* renderer) = 0; +}; + +#endif //MEMORYSDL_UIELEMENT_H \ No newline at end of file diff --git a/src/scenes/EndScene.cpp b/src/scenes/EndScene.cpp index 432db9f..84f13b7 100644 --- a/src/scenes/EndScene.cpp +++ b/src/scenes/EndScene.cpp @@ -1,22 +1,56 @@ #include "scenes/EndScene.h" +#include "ui/Button.h" #include "scenes/GameScene.h" -void EndScene::HandleEvent(const SDL_Event &event) -{ - if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) - { - SDL_FPoint p {event.button.x, event.button.y}; +#include "memory" - if (SDL_PointInRectFloat(&p, &m_uiPlayButtonRect)) +void EndScene::InitUI() +{ + SDL_FRect tmpButton { + static_cast(m_context.windowWidth) * 0.15f, + static_cast(m_context.windowHeight) * 0.75f, + static_cast(m_context.texWidth) * 0.5f, + static_cast(m_context.texHeight) * 0.5f + }; + SDL_Texture* play = m_context.assetManager->GetTexture("UI_PlayButton"); + m_ui.push_back(std::make_unique