diff --git a/include/game/agame/background.hpp b/include/game/agame/background.hpp index f5af0cc..01f5291 100644 --- a/include/game/agame/background.hpp +++ b/include/game/agame/background.hpp @@ -6,15 +6,8 @@ #include namespace Game::AGame { - class Background : public Object::Entity { - using Object::Entity::Entity; - - public: - ~Background() override = default; - void start() override; - void update(float deltaTime) override; - +GAME_ENTITY(Background) private: Object::Sound mSound; - }; +END_GAME_ENTITY() } \ No newline at end of file diff --git a/include/game/agame/camcontroller.hpp b/include/game/agame/camcontroller.hpp index c4bb579..9a7c084 100644 --- a/include/game/agame/camcontroller.hpp +++ b/include/game/agame/camcontroller.hpp @@ -6,13 +6,8 @@ #include namespace Game::AGame { - class CamController : public Object::Entity { - using Object::Entity::Entity; - +GAME_ENTITY(CamController) public: - ~CamController() override = default; - void start() override; - void update(float deltaTime) override; void onWindowResized(int newWidth, int newHeight) override { mScreenW = newWidth; mScreenH = newHeight; @@ -20,5 +15,5 @@ namespace Game::AGame { private: int mScreenW, mScreenH; - }; +END_GAME_ENTITY() } \ No newline at end of file diff --git a/include/game/agame/player.hpp b/include/game/agame/player.hpp index 6e8a398..297009a 100644 --- a/include/game/agame/player.hpp +++ b/include/game/agame/player.hpp @@ -6,16 +6,9 @@ #include namespace Game::AGame { - class Player : public Object::Entity { - using Object::Entity::Entity; - - public: - ~Player() override = default; - void start() override; - void update(float deltaTime) override; - +GAME_ENTITY(Player) private: Object::Sound mSound; float mSpeed = 200.f; // Pixels per second - }; +END_GAME_ENTITY() } \ No newline at end of file diff --git a/include/game/input.hpp b/include/game/input.hpp index fdc5a33..c080360 100644 --- a/include/game/input.hpp +++ b/include/game/input.hpp @@ -11,9 +11,18 @@ namespace Game { static bool isKeyPressed(SDL_Scancode key); static bool isKeyJustPressed(SDL_Scancode key); static bool isKeyJustReleased(SDL_Scancode key); + static bool isMouseButtonPressed(Uint8 button); + static bool isMouseButtonJustPressed(Uint8 button); + static bool isMouseButtonJustReleased(Uint8 button); + static float getMouseX(); + static float getMouseY(); private: static const bool* mCurrentKeyStates; - static bool* mPreviousKeyStates; + static const bool* mPreviousKeyStates; static int mNumKeys; + static SDL_MouseButtonFlags mCurrentMouseButtonStates; + static SDL_MouseButtonFlags mPreviousMouseButtonStates; + static float mMouseX; + static float mMouseY; }; } \ No newline at end of file diff --git a/include/object/ui/uibutton.hpp b/include/object/ui/uibutton.hpp new file mode 100644 index 0000000..2c7f84f --- /dev/null +++ b/include/object/ui/uibutton.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace Game::Object { + class UIButton : public Entity { + public: + UIButton(const std::string& name, std::shared_ptr texture, const Transform& transform, void* clickFunction = nullptr, float x = 0.f, float y = 0.f); + ~UIButton() override = default; + + void start() override; + void update(float deltaTime) override; + + void setText(const std::string& text); + std::string getText() const; + + void setPosition(float x, float y) { mX = x; mY = y; } + std::pair getPosition() const { return {mX, mY}; } + + private: + void* mClickFunction = nullptr; + float mX, mY; + std::string mText; + }; +} \ No newline at end of file diff --git a/include/object/ui/uitext.hpp b/include/object/ui/uitext.hpp new file mode 100644 index 0000000..e06f5ab --- /dev/null +++ b/include/object/ui/uitext.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace Game::Object { + class UIText : public Entity { + public: + UIText(const std::string& name, std::shared_ptr font, const Transform& transform, float x = 0.f, float y = 0.f); + ~UIText() override = default; + + void start() override; + void update(float deltaTime) override; + + void setText(const std::string& text); + std::string getText() const; + + void setPosition(float x, float y) { mX = x; mY = y; } + std::pair getPosition() const { return {mX, mY}; } + + private: + //void* mClickFunction = nullptr; + float mX, mY; + std::string mText; + }; +} \ No newline at end of file diff --git a/include/utils.hpp b/include/utils.hpp index 7f2471d..d8980df 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -29,7 +29,11 @@ #define GAME_ENTITY(ClassName) \ class ClassName : public Object::Entity { \ - using Object::Entity::Entity; + using Object::Entity::Entity; \ + public: \ + ~ClassName() override = default; \ + void start() override; \ + void update(float deltaTime) override; #define END_GAME_ENTITY() \ }; diff --git a/src/game/gamemanager.cpp b/src/game/gamemanager.cpp index 279f4e3..4999bb9 100644 --- a/src/game/gamemanager.cpp +++ b/src/game/gamemanager.cpp @@ -4,7 +4,7 @@ namespace Game { void GameManager::run(std::stop_token stopToken) { using namespace std::chrono_literals; - LOG("GameManager thread started"); + LOG("GameManager slave thread started"); Object::Camera::getInstance().setPosition(0.f, 0.f); // Start with camera at (0, 0) mLastUpdate = clock::now(); // Get the update diff --git a/src/game/input.cpp b/src/game/input.cpp index 86b2518..44d2732 100644 --- a/src/game/input.cpp +++ b/src/game/input.cpp @@ -2,12 +2,19 @@ namespace Game { const bool* Input::mCurrentKeyStates = nullptr; - bool* Input::mPreviousKeyStates = nullptr; + const bool* Input::mPreviousKeyStates = nullptr; int Input::mNumKeys = 0; + SDL_MouseButtonFlags Input::mCurrentMouseButtonStates = 0; + SDL_MouseButtonFlags Input::mPreviousMouseButtonStates = 0; + float Input::mMouseX = 0.0f; + float Input::mMouseY = 0.0f; void Input::update() { - mPreviousKeyStates = const_cast(mCurrentKeyStates); + mPreviousKeyStates = mCurrentKeyStates; mCurrentKeyStates = SDL_GetKeyboardState(&mNumKeys); + + mPreviousMouseButtonStates = mCurrentMouseButtonStates; + mCurrentMouseButtonStates = SDL_GetMouseState(&mMouseX, &mMouseY); } bool Input::isKeyPressed(SDL_Scancode key) { @@ -24,4 +31,24 @@ namespace Game { if (key < 0 || key >= mNumKeys) return false; return (!mCurrentKeyStates[key]) && mPreviousKeyStates && mPreviousKeyStates[key]; } + + bool Input::isMouseButtonPressed(Uint8 button) { + return (mCurrentMouseButtonStates & SDL_BUTTON_MASK(button)) != 0; + } + + bool Input::isMouseButtonJustPressed(Uint8 button) { + return (mCurrentMouseButtonStates & SDL_BUTTON_MASK(button)) != 0 && (mPreviousMouseButtonStates & SDL_BUTTON_MASK(button)) == 0; + } + + bool Input::isMouseButtonJustReleased(Uint8 button) { + return (mCurrentMouseButtonStates & SDL_BUTTON_MASK(button)) == 0 && (mPreviousMouseButtonStates & SDL_BUTTON_MASK(button)) != 0; + } + + float Input::getMouseX() { + return mMouseX; + } + + float Input::getMouseY() { + return mMouseY; + } } \ No newline at end of file diff --git a/src/object/ui/uibutton.cpp b/src/object/ui/uibutton.cpp new file mode 100644 index 0000000..98030b9 --- /dev/null +++ b/src/object/ui/uibutton.cpp @@ -0,0 +1,41 @@ +#include + +namespace Game::Object { + UIButton::UIButton(const std::string& name, std::shared_ptr texture, const Transform& transform, void* clickFunction, float x, float y) + : Entity(name, texture, transform), mClickFunction(clickFunction), mX(x), mY(y) { } + + void UIButton::start() { + // Center the button on the position + mTransform.x -= mTex->getWidth() * mTransform.adjustedScaleX() / 2.f; + mTransform.y -= mTex->getHeight() * mTransform.adjustedScaleY() / 2.f; + } + + void UIButton::update(float deltaTime) { + if (!mIsActive) return; + float mouseX = Input::getMouseX(); + float mouseY = Input::getMouseY(); + float textLeft = mTransform.x; + float textRight = mTransform.x + mTex->getWidth() * mTransform.adjustedScaleX(); + float textTop = mTransform.y; + float textBottom = mTransform.y + mTex->getHeight() * mTransform.adjustedScaleY(); + + if (mouseX >= textLeft && mouseX <= textRight && mouseY >= textTop && mouseY <= textBottom) { + std::dynamic_pointer_cast(mTex)->build({200, 200, 200, 255}, mText); // Darken text when hovered + if (Input::isMouseButtonJustPressed(SDL_BUTTON_LEFT) && mClickFunction) { + using ClickFnType = void(*)(); + ClickFnType clickFn = reinterpret_cast(mClickFunction); + clickFn(); + } + } + + } + + void UIButton::setText(const std::string& text) { + mText = text; + std::dynamic_pointer_cast(mTex)->build({255, 255, 255, 255}, text); + } + + std::string UIButton::getText() const { + return mText; + } +} \ No newline at end of file diff --git a/src/object/ui/uitext.cpp b/src/object/ui/uitext.cpp new file mode 100644 index 0000000..ee76224 --- /dev/null +++ b/src/object/ui/uitext.cpp @@ -0,0 +1,40 @@ +#include + +namespace Game::Object { + UIText::UIText(const std::string& name, std::shared_ptr font, const Transform& transform, float x, float y) + : Entity(name, font, transform), mX(x), mY(y) { } + + void UIText::start() { + // Center the text on the position + mTransform.x -= mTex->getWidth() * mTransform.adjustedScaleX() / 2.f; + mTransform.y -= mTex->getHeight() * mTransform.adjustedScaleY() / 2.f; + } + + void UIText::update(float deltaTime) {} /* { + if (!mIsActive) return; + + if (Input::isMouseButtonJustPressed(SDL_BUTTON_LEFT) && mClickFunction) { + float mouseX = Input::getMouseX(); + float mouseY = Input::getMouseY(); + float textLeft = mTransform.x; + float textRight = mTransform.x + mTex->getWidth() * mTransform.adjustedScaleX(); + float textTop = mTransform.y; + float textBottom = mTransform.y + mTex->getHeight() * mTransform.adjustedScaleY(); + + if (mouseX >= textLeft && mouseX <= textRight && mouseY >= textTop && mouseY <= textBottom) { + using ClickFnType = void(*)(); + ClickFnType clickFn = reinterpret_cast(mClickFunction); + clickFn(); + } + } + }*/ + + void UIText::setText(const std::string& text) { + mText = text; + std::dynamic_pointer_cast(mTex)->build({255, 255, 255, 255}, text); + } + + std::string UIText::getText() const { + return mText; + } +} \ No newline at end of file