macros and ui element
This commit is contained in:
@@ -6,15 +6,8 @@
|
|||||||
#include <object/sound.hpp>
|
#include <object/sound.hpp>
|
||||||
|
|
||||||
namespace Game::AGame {
|
namespace Game::AGame {
|
||||||
class Background : public Object::Entity {
|
GAME_ENTITY(Background)
|
||||||
using Object::Entity::Entity;
|
|
||||||
|
|
||||||
public:
|
|
||||||
~Background() override = default;
|
|
||||||
void start() override;
|
|
||||||
void update(float deltaTime) override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Object::Sound mSound;
|
Object::Sound mSound;
|
||||||
};
|
END_GAME_ENTITY()
|
||||||
}
|
}
|
||||||
@@ -6,13 +6,8 @@
|
|||||||
#include <object/sound.hpp>
|
#include <object/sound.hpp>
|
||||||
|
|
||||||
namespace Game::AGame {
|
namespace Game::AGame {
|
||||||
class CamController : public Object::Entity {
|
GAME_ENTITY(CamController)
|
||||||
using Object::Entity::Entity;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
~CamController() override = default;
|
|
||||||
void start() override;
|
|
||||||
void update(float deltaTime) override;
|
|
||||||
void onWindowResized(int newWidth, int newHeight) override {
|
void onWindowResized(int newWidth, int newHeight) override {
|
||||||
mScreenW = newWidth;
|
mScreenW = newWidth;
|
||||||
mScreenH = newHeight;
|
mScreenH = newHeight;
|
||||||
@@ -20,5 +15,5 @@ namespace Game::AGame {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
int mScreenW, mScreenH;
|
int mScreenW, mScreenH;
|
||||||
};
|
END_GAME_ENTITY()
|
||||||
}
|
}
|
||||||
@@ -6,16 +6,9 @@
|
|||||||
#include <object/sound.hpp>
|
#include <object/sound.hpp>
|
||||||
|
|
||||||
namespace Game::AGame {
|
namespace Game::AGame {
|
||||||
class Player : public Object::Entity {
|
GAME_ENTITY(Player)
|
||||||
using Object::Entity::Entity;
|
|
||||||
|
|
||||||
public:
|
|
||||||
~Player() override = default;
|
|
||||||
void start() override;
|
|
||||||
void update(float deltaTime) override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Object::Sound mSound;
|
Object::Sound mSound;
|
||||||
float mSpeed = 200.f; // Pixels per second
|
float mSpeed = 200.f; // Pixels per second
|
||||||
};
|
END_GAME_ENTITY()
|
||||||
}
|
}
|
||||||
@@ -11,9 +11,18 @@ namespace Game {
|
|||||||
static bool isKeyPressed(SDL_Scancode key);
|
static bool isKeyPressed(SDL_Scancode key);
|
||||||
static bool isKeyJustPressed(SDL_Scancode key);
|
static bool isKeyJustPressed(SDL_Scancode key);
|
||||||
static bool isKeyJustReleased(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:
|
private:
|
||||||
static const bool* mCurrentKeyStates;
|
static const bool* mCurrentKeyStates;
|
||||||
static bool* mPreviousKeyStates;
|
static const bool* mPreviousKeyStates;
|
||||||
static int mNumKeys;
|
static int mNumKeys;
|
||||||
|
static SDL_MouseButtonFlags mCurrentMouseButtonStates;
|
||||||
|
static SDL_MouseButtonFlags mPreviousMouseButtonStates;
|
||||||
|
static float mMouseX;
|
||||||
|
static float mMouseY;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
29
include/object/ui/uibutton.hpp
Normal file
29
include/object/ui/uibutton.hpp
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <object/entity.hpp>
|
||||||
|
#include <renderer/font.hpp>
|
||||||
|
#include <renderer/texture.hpp>
|
||||||
|
#include <utility>
|
||||||
|
#include <game/input.hpp>
|
||||||
|
|
||||||
|
namespace Game::Object {
|
||||||
|
class UIButton : public Entity {
|
||||||
|
public:
|
||||||
|
UIButton(const std::string& name, std::shared_ptr<Renderer::Texture> 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<float, float> getPosition() const { return {mX, mY}; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
void* mClickFunction = nullptr;
|
||||||
|
float mX, mY;
|
||||||
|
std::string mText;
|
||||||
|
};
|
||||||
|
}
|
||||||
29
include/object/ui/uitext.hpp
Normal file
29
include/object/ui/uitext.hpp
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <object/entity.hpp>
|
||||||
|
#include <renderer/font.hpp>
|
||||||
|
#include <renderer/texture.hpp>
|
||||||
|
#include <utility>
|
||||||
|
#include <game/input.hpp>
|
||||||
|
|
||||||
|
namespace Game::Object {
|
||||||
|
class UIText : public Entity {
|
||||||
|
public:
|
||||||
|
UIText(const std::string& name, std::shared_ptr<Renderer::Font> 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<float, float> getPosition() const { return {mX, mY}; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
//void* mClickFunction = nullptr;
|
||||||
|
float mX, mY;
|
||||||
|
std::string mText;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -29,7 +29,11 @@
|
|||||||
|
|
||||||
#define GAME_ENTITY(ClassName) \
|
#define GAME_ENTITY(ClassName) \
|
||||||
class ClassName : public Object::Entity { \
|
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() \
|
#define END_GAME_ENTITY() \
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
namespace Game {
|
namespace Game {
|
||||||
void GameManager::run(std::stop_token stopToken) {
|
void GameManager::run(std::stop_token stopToken) {
|
||||||
using namespace std::chrono_literals;
|
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)
|
Object::Camera::getInstance().setPosition(0.f, 0.f); // Start with camera at (0, 0)
|
||||||
|
|
||||||
mLastUpdate = clock::now(); // Get the update
|
mLastUpdate = clock::now(); // Get the update
|
||||||
|
|||||||
@@ -2,12 +2,19 @@
|
|||||||
|
|
||||||
namespace Game {
|
namespace Game {
|
||||||
const bool* Input::mCurrentKeyStates = nullptr;
|
const bool* Input::mCurrentKeyStates = nullptr;
|
||||||
bool* Input::mPreviousKeyStates = nullptr;
|
const bool* Input::mPreviousKeyStates = nullptr;
|
||||||
int Input::mNumKeys = 0;
|
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() {
|
void Input::update() {
|
||||||
mPreviousKeyStates = const_cast<bool*>(mCurrentKeyStates);
|
mPreviousKeyStates = mCurrentKeyStates;
|
||||||
mCurrentKeyStates = SDL_GetKeyboardState(&mNumKeys);
|
mCurrentKeyStates = SDL_GetKeyboardState(&mNumKeys);
|
||||||
|
|
||||||
|
mPreviousMouseButtonStates = mCurrentMouseButtonStates;
|
||||||
|
mCurrentMouseButtonStates = SDL_GetMouseState(&mMouseX, &mMouseY);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Input::isKeyPressed(SDL_Scancode key) {
|
bool Input::isKeyPressed(SDL_Scancode key) {
|
||||||
@@ -24,4 +31,24 @@ namespace Game {
|
|||||||
if (key < 0 || key >= mNumKeys) return false;
|
if (key < 0 || key >= mNumKeys) return false;
|
||||||
return (!mCurrentKeyStates[key]) && mPreviousKeyStates && mPreviousKeyStates[key];
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
41
src/object/ui/uibutton.cpp
Normal file
41
src/object/ui/uibutton.cpp
Normal 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
40
src/object/ui/uitext.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user