macros and ui element

This commit is contained in:
2026-03-16 14:56:32 +01:00
parent 3a22a3746f
commit 29bb9ca0fe
11 changed files with 190 additions and 30 deletions

View File

@@ -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

View File

@@ -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<bool*>(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;
}
}

View File

@@ -0,0 +1,41 @@
#include <object/ui/uibutton.hpp>
namespace Game::Object {
UIButton::UIButton(const std::string& name, std::shared_ptr<Renderer::Texture> 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<Renderer::Font>(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<ClickFnType>(mClickFunction);
clickFn();
}
}
}
void UIButton::setText(const std::string& text) {
mText = text;
std::dynamic_pointer_cast<Renderer::Font>(mTex)->build({255, 255, 255, 255}, text);
}
std::string UIButton::getText() const {
return mText;
}
}

40
src/object/ui/uitext.cpp Normal file
View File

@@ -0,0 +1,40 @@
#include <object/ui/uitext.hpp>
namespace Game::Object {
UIText::UIText(const std::string& name, std::shared_ptr<Renderer::Font> 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<ClickFnType>(mClickFunction);
clickFn();
}
}
}*/
void UIText::setText(const std::string& text) {
mText = text;
std::dynamic_pointer_cast<Renderer::Font>(mTex)->build({255, 255, 255, 255}, text);
}
std::string UIText::getText() const {
return mText;
}
}