From 29f9a26046e35c7eb0157df92757ce8e39f1ec74 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 28 Oct 2024 11:06:21 +0100 Subject: iMouseListener and iKeyListener implementation --- mwe/events/include/event.h | 51 ++++++++++++++++++++++++++++ mwe/events/include/gameObject.h | 20 +++++++++++ mwe/events/include/iKeyListener.h | 19 +++++++++++ mwe/events/include/iMouseListener.h | 40 ++++++++++++++++++++++ mwe/events/include/keyCodes.h | 11 +++++++ mwe/events/include/uiObject.h | 66 +++++++++++++++++++++++++++++++++++++ 6 files changed, 207 insertions(+) create mode 100644 mwe/events/include/gameObject.h create mode 100644 mwe/events/include/iKeyListener.h create mode 100644 mwe/events/include/iMouseListener.h create mode 100644 mwe/events/include/uiObject.h (limited to 'mwe/events/include') diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index 0208085..8968c6c 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -64,7 +64,20 @@ public: Keycode key = 0; int repeatCount = 0; }; +class MouseClickEvent : public Event { +public: + MouseClickEvent(int x, int y, MouseButton button); + + REGISTER_EVENT_TYPE("KeyClickedEvent"); + int getX() const { return x; } + int getY() const { return y; } + MouseButton getButton() const { return button; } +private: + int x; + int y; + MouseButton button; +}; // KeyReleasedEvent class class KeyReleasedEvent : public Event { public: @@ -87,6 +100,31 @@ public: std::pair getMousePosition() const; +private: + int mouseX = 0; + int mouseY = 0; + MouseButton button; +}; +class MouseReleasedEvent : public Event { +public: + MouseReleasedEvent(int mouseX, int mouseY, MouseButton button); + + REGISTER_EVENT_TYPE(MouseReleasedEvent) + + std::pair getMousePosition() const; + MouseButton getMouseButton() const; +private: + int mouseX = 0; + int mouseY = 0; + MouseButton button; +}; +class MouseMovedEvent : public Event { +public: + MouseMovedEvent(int mouseX, int mouseY); + + REGISTER_EVENT_TYPE(MouseMovedEvent) + + std::pair getMousePosition() const; private: int mouseX = 0; int mouseY = 0; @@ -103,3 +141,16 @@ private: Collision collisionData; }; +class TextSubmitEvent : public Event { +public: + TextSubmitEvent(std::string submittedText); + + REGISTER_EVENT_TYPE(TextSubmitEvent) + + std::string getText() const { + return text; + } + +private: + std::string text; +}; diff --git a/mwe/events/include/gameObject.h b/mwe/events/include/gameObject.h new file mode 100644 index 0000000..70d4517 --- /dev/null +++ b/mwe/events/include/gameObject.h @@ -0,0 +1,20 @@ +#pragma once +#include +#include +class GameObject { +public: + GameObject(std::uint32_t id, std::string name, std::string tag, int layer); + + template + void addSpriteComponent(Args &&... args); + template + void addRigidbodyComponent(Args &&... args); + template + void addColiderComponent(Args &&... args); + + std::uint32_t mId; + std::string mName; + std::string mTag; + bool mActive; + int mLayer; +}; diff --git a/mwe/events/include/iKeyListener.h b/mwe/events/include/iKeyListener.h new file mode 100644 index 0000000..87ee1d5 --- /dev/null +++ b/mwe/events/include/iKeyListener.h @@ -0,0 +1,19 @@ +#pragma once +#include "event.h" +#include "eventManager.h" +#include "eventHandler.h" +class IKeyListener { +public: + virtual ~IKeyListener(); + virtual void onKeyPressed(const KeyPressedEvent& event) = 0; + virtual void onKeyReleased(const KeyReleasedEvent& event) = 0; + +protected: + void subscribeEvents(int listenerId = 0); + void unsubscribeEvents(int listenerId = 0); + +private: + EventHandler keyPressedHandler; + EventHandler keyReleasedHandler; +}; + diff --git a/mwe/events/include/iMouseListener.h b/mwe/events/include/iMouseListener.h new file mode 100644 index 0000000..7ae371c --- /dev/null +++ b/mwe/events/include/iMouseListener.h @@ -0,0 +1,40 @@ +#include "event.h" +#include "eventManager.h" +#include "eventHandler.h" +class IMouseListener { +public: + virtual ~IMouseListener() { + unsubscribeEvents(); + } + + virtual void onMouseClicked(const MouseClickEvent& event) = 0; + virtual void onMousePressed(const MousePressedEvent& event) = 0; + virtual void onMouseReleased(const MouseReleasedEvent& event) = 0; + virtual void onMouseMoved(const MouseMovedEvent& event) = 0; + +protected: + void subscribeEvents(int listenerId = 0) { + mouseClickHandler = [this](const MouseClickEvent& event) { this->onMouseClicked(event); }; + mousePressHandler = [this](const MousePressedEvent& event) { this->onMousePressed(event); }; + mouseReleaseHandler = [this](const MouseReleasedEvent& event) { this->onMouseReleased(event); }; + mouseMoveHandler = [this](const MouseMovedEvent& event) { this->onMouseMoved(event); }; + + subscribe(mouseClickHandler, listenerId); + subscribe(mousePressHandler, listenerId); + subscribe(mouseReleaseHandler, listenerId); + subscribe(mouseMoveHandler, listenerId); + } + + void unsubscribeEvents(int listenerId = 0) { + unsubscribe(mouseClickHandler, listenerId); + unsubscribe(mousePressHandler, listenerId); + unsubscribe(mouseReleaseHandler, listenerId); + unsubscribe(mouseMoveHandler, listenerId); + } + +private: + EventHandler mouseClickHandler; + EventHandler mousePressHandler; + EventHandler mouseReleaseHandler; + EventHandler mouseMoveHandler; +}; diff --git a/mwe/events/include/keyCodes.h b/mwe/events/include/keyCodes.h index 0879efc..61deba2 100644 --- a/mwe/events/include/keyCodes.h +++ b/mwe/events/include/keyCodes.h @@ -3,6 +3,16 @@ #include #include using Keycode = uint16_t; +enum class MouseButton { + None = 0, + Left_Mouse = 1, + Right_Mouse = 2, + Middle_Mouse = 3, + X1_Mouse = 4, + X2_Mouse = 5, + Scroll_Up = 6, + Scroll_Down = 7, +}; enum : Keycode { // From glfw3.h Space = 32, @@ -136,6 +146,7 @@ enum : Keycode { RightSuper = 347, Menu = 348 }; + // Define the mapping extern const std::unordered_map sdlToCustom; diff --git a/mwe/events/include/uiObject.h b/mwe/events/include/uiObject.h new file mode 100644 index 0000000..46d32be --- /dev/null +++ b/mwe/events/include/uiObject.h @@ -0,0 +1,66 @@ +#include "gameObject.h" +#include +#include +#include "event.h" +#include "eventHandler.h" +struct Alignment { + enum class Horizontal { LEFT, CENTER, RIGHT }; + enum class Vertical { TOP, MIDDLE, BOTTOM }; + enum class PositioningMode { RELATIVE, STATIC,ABSOLUTE }; + + Horizontal horizontal = Horizontal::CENTER; + Vertical vertical = Vertical::MIDDLE; + PositioningMode mode = PositioningMode::RELATIVE; + + int staticX = 0; + int staticY = 0; + + int marginTop = 0; + int marginBottom = 0; + int marginLeft = 0; + int marginRight = 0; +}; +struct RGBColor{ + int red, + int green, + int blue +}; +class UIObject : public GameObject{ + public: + UIObject(int width,int height); + private: + int width; + int height; +}; +class Button : public UIObject{ + public: + Button(int width,int height); + EventHandler onKeyPressed; + EventHandler onKeyReleased; + +}; +class Text : public UIObject{ + public: + Text(int width,int height); + private: + std::string text; + int size; + Alignment alignment; + //font resource + TTF_Font *font; + RGBColor color; +}; +class TextInput : public UIObject{ + public: + TextInput (int width,int height); + std::string textBuffer; + std::string placeholder; + size_t caretPosition; + bool isActive; + RGBColor textColor; + RGBColor backgroundColor; + size_t maxLength; + Alignment alignment; + TTF_Font* font; + EventHandler onSubmit; +}; -- cgit v1.2.3 From f2136f836f9b9e9a6a6698f7bc6fba85a27ebebf Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 28 Oct 2024 11:34:43 +0100 Subject: iKeyListener test and iMouseListener test working --- mwe/events/CMakeLists.txt | 5 ++++- mwe/events/include/event.h | 13 +++++-------- mwe/events/include/iKeyListener.h | 2 ++ mwe/events/include/iMouseListener.h | 26 ++++---------------------- mwe/events/include/keyListenerTest.h | 12 ++++++++++++ mwe/events/include/mouseListenerTest.h | 14 ++++++++++++++ mwe/events/src/event.cpp | 24 +++++++++++++++++++++--- mwe/events/src/keyListenerTest.cpp | 17 +++++++++++++++++ mwe/events/src/main.cpp | 11 +++++++---- mwe/events/src/mouseListenerTest.cpp | 25 +++++++++++++++++++++++++ 10 files changed, 111 insertions(+), 38 deletions(-) create mode 100644 mwe/events/include/keyListenerTest.h create mode 100644 mwe/events/include/mouseListenerTest.h create mode 100644 mwe/events/src/keyListenerTest.cpp create mode 100644 mwe/events/src/mouseListenerTest.cpp (limited to 'mwe/events/include') diff --git a/mwe/events/CMakeLists.txt b/mwe/events/CMakeLists.txt index e429516..096b8ad 100644 --- a/mwe/events/CMakeLists.txt +++ b/mwe/events/CMakeLists.txt @@ -13,13 +13,16 @@ find_package(SDL2 REQUIRED) add_executable(gameloop src/window.cpp src/main.cpp - #src/eventHandler.cpp src/eventManager.cpp src/event.cpp src/loopManager.cpp src/timer.cpp src/keyCodes.cpp src/eventHandler.cpp + src/iMouseListener.cpp + src/iKeyListener.cpp + src/mouseListenerTest.cpp + src/keyListenerTest.cpp ) add_subdirectory(../../lib/SDL_ttf SDL_ttf) # Link the SDL2 library to your project diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index 8968c6c..65560a1 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -67,15 +67,14 @@ public: class MouseClickEvent : public Event { public: MouseClickEvent(int x, int y, MouseButton button); - + REGISTER_EVENT_TYPE("KeyClickedEvent"); - int getX() const { return x; } - int getY() const { return y; } + std::pair getMousePosition() const; MouseButton getButton() const { return button; } private: - int x; - int y; + int mouseX = 0; + int mouseY = 0; MouseButton button; }; // KeyReleasedEvent class @@ -147,9 +146,7 @@ public: REGISTER_EVENT_TYPE(TextSubmitEvent) - std::string getText() const { - return text; - } + std::string getText() const; private: std::string text; diff --git a/mwe/events/include/iKeyListener.h b/mwe/events/include/iKeyListener.h index 87ee1d5..5f39ece 100644 --- a/mwe/events/include/iKeyListener.h +++ b/mwe/events/include/iKeyListener.h @@ -11,6 +11,8 @@ public: protected: void subscribeEvents(int listenerId = 0); void unsubscribeEvents(int listenerId = 0); + void activate(int listenerId = 0) { subscribeEvents(listenerId); } + void deactivate(int listenerId = 0) { unsubscribeEvents(listenerId); } private: EventHandler keyPressedHandler; diff --git a/mwe/events/include/iMouseListener.h b/mwe/events/include/iMouseListener.h index 7ae371c..03fd4c3 100644 --- a/mwe/events/include/iMouseListener.h +++ b/mwe/events/include/iMouseListener.h @@ -1,36 +1,18 @@ +#pragma once #include "event.h" #include "eventManager.h" #include "eventHandler.h" class IMouseListener { public: - virtual ~IMouseListener() { - unsubscribeEvents(); - } + virtual ~IMouseListener(); virtual void onMouseClicked(const MouseClickEvent& event) = 0; virtual void onMousePressed(const MousePressedEvent& event) = 0; virtual void onMouseReleased(const MouseReleasedEvent& event) = 0; virtual void onMouseMoved(const MouseMovedEvent& event) = 0; - protected: - void subscribeEvents(int listenerId = 0) { - mouseClickHandler = [this](const MouseClickEvent& event) { this->onMouseClicked(event); }; - mousePressHandler = [this](const MousePressedEvent& event) { this->onMousePressed(event); }; - mouseReleaseHandler = [this](const MouseReleasedEvent& event) { this->onMouseReleased(event); }; - mouseMoveHandler = [this](const MouseMovedEvent& event) { this->onMouseMoved(event); }; - - subscribe(mouseClickHandler, listenerId); - subscribe(mousePressHandler, listenerId); - subscribe(mouseReleaseHandler, listenerId); - subscribe(mouseMoveHandler, listenerId); - } - - void unsubscribeEvents(int listenerId = 0) { - unsubscribe(mouseClickHandler, listenerId); - unsubscribe(mousePressHandler, listenerId); - unsubscribe(mouseReleaseHandler, listenerId); - unsubscribe(mouseMoveHandler, listenerId); - } + void subscribeEvents(int listenerId = 0); + void unsubscribeEvents(int listenerId = 0); private: EventHandler mouseClickHandler; diff --git a/mwe/events/include/keyListenerTest.h b/mwe/events/include/keyListenerTest.h new file mode 100644 index 0000000..5990cd1 --- /dev/null +++ b/mwe/events/include/keyListenerTest.h @@ -0,0 +1,12 @@ +#pragma once +#include "iKeyListener.h" +#include + +class KeyListenerTest : public IKeyListener { +public: + KeyListenerTest(int listenerId); + ~KeyListenerTest(); + + void onKeyPressed(const KeyPressedEvent& event) override; + void onKeyReleased(const KeyReleasedEvent& event) override; +}; diff --git a/mwe/events/include/mouseListenerTest.h b/mwe/events/include/mouseListenerTest.h new file mode 100644 index 0000000..dae04f5 --- /dev/null +++ b/mwe/events/include/mouseListenerTest.h @@ -0,0 +1,14 @@ +#pragma once +#include "iMouseListener.h" +#include + +class MouseListenerTest : public IMouseListener { +public: + MouseListenerTest(int listenerId); + ~MouseListenerTest(); + + void onMouseClicked(const MouseClickEvent& event) override; + void onMousePressed(const MousePressedEvent& event) override; + void onMouseReleased(const MouseReleasedEvent& event) override; + void onMouseMoved(const MouseMovedEvent& event) override; +}; diff --git a/mwe/events/src/event.cpp b/mwe/events/src/event.cpp index 2509088..fc8eec9 100644 --- a/mwe/events/src/event.cpp +++ b/mwe/events/src/event.cpp @@ -53,10 +53,28 @@ Collision CollisionEvent::getCollisionData() const return this->collisionData; } -TextSubmitEvent::TextSubmitEvent(std::string text) : Event("TextSubmitEvent"){ +TextSubmitEvent::TextSubmitEvent(std::string text) + : text(text), Event("TextSubmitEvent") {} +std::string TextSubmitEvent::getText() const { + return this->text; } -std::string TextSubmitEvent::getText() const{ - return this->text; + +MouseReleasedEvent::MouseReleasedEvent(int x, int y, MouseButton button) : mouseX(x), mouseY(y), button(button),Event("MouseReleased"){ + +} +std::pair MouseReleasedEvent::getMousePosition() const{ + return {mouseX,mouseY}; +} +MouseClickEvent::MouseClickEvent(int x,int y,MouseButton button) : mouseX(x), mouseY(y), button(button),Event("MouseClickEvent"){ + } +MouseMovedEvent::MouseMovedEvent(int x, int y) : mouseX(x), mouseY(y),Event("MouseMovedEvent"){ +} +std::pair MouseClickEvent::getMousePosition() const { + return {mouseX, mouseY}; +} +std::pair MouseMovedEvent::getMousePosition() const { + return {mouseX, mouseY}; +} diff --git a/mwe/events/src/keyListenerTest.cpp b/mwe/events/src/keyListenerTest.cpp new file mode 100644 index 0000000..90b92a5 --- /dev/null +++ b/mwe/events/src/keyListenerTest.cpp @@ -0,0 +1,17 @@ +#include "keyListenerTest.h" + +KeyListenerTest::KeyListenerTest(int listenerId) { + subscribeEvents(listenerId); +} + +KeyListenerTest::~KeyListenerTest() { + unsubscribeEvents(); +} + +void KeyListenerTest::onKeyPressed(const KeyPressedEvent& event) { + std::cout << "Key pressed: " << event.getKeyCode() << std::endl; +} + +void KeyListenerTest::onKeyReleased(const KeyReleasedEvent& event) { + std::cout << "Key released: " << event.getKeyCode() << std::endl; +} diff --git a/mwe/events/src/main.cpp b/mwe/events/src/main.cpp index d056f85..03dff16 100644 --- a/mwe/events/src/main.cpp +++ b/mwe/events/src/main.cpp @@ -6,6 +6,10 @@ #include "loopManager.h" #include "event.h" #include "customTypes.h" +#include "iKeyListener.h" +#include "iMouseListener.h" +#include "keyListenerTest.h" +#include "mouseListenerTest.h" class PlayerDamagedEvent : public Event { public: PlayerDamagedEvent(int damage, int playerID) @@ -46,10 +50,9 @@ void testCollisionEvent() { } int main(int argc, char * args[]) { LoopManager gameLoop; - // Create an event handler for KeyPressedEvent - // EventHandler callback = [](const KeyPressedEvent& e) { - // onKeyPressed(e); - // }; + int testListenerId = 0; + KeyListenerTest keyListener(testListenerId); + MouseListenerTest mouseListener(testListenerId); // custom event class poc subscribe(onPlayerDamaged); triggerEvent(PlayerDamagedEvent(50, 1)); diff --git a/mwe/events/src/mouseListenerTest.cpp b/mwe/events/src/mouseListenerTest.cpp new file mode 100644 index 0000000..4b3aa3e --- /dev/null +++ b/mwe/events/src/mouseListenerTest.cpp @@ -0,0 +1,25 @@ +#include "mouseListenerTest.h" + +MouseListenerTest::MouseListenerTest(int listenerId) { + subscribeEvents(listenerId); +} + +MouseListenerTest::~MouseListenerTest() { + unsubscribeEvents(); +} + +void MouseListenerTest::onMouseClicked(const MouseClickEvent& event) { + std::cout << "Mouse clicked at: (" << event.getMousePosition().first << ", " << event.getMousePosition().second << ")" << std::endl; +} + +void MouseListenerTest::onMousePressed(const MousePressedEvent& event) { + std::cout << "Mouse button pressed at: (" << event.getMousePosition().first << ", " << event.getMousePosition().second << ")" << std::endl; +} + +void MouseListenerTest::onMouseReleased(const MouseReleasedEvent& event) { + std::cout << "Mouse button released at: (" << event.getMousePosition().first << ", " << event.getMousePosition().second << ")" << std::endl; +} + +void MouseListenerTest::onMouseMoved(const MouseMovedEvent& event) { + std::cout << "Mouse moved to: (" << event.getMousePosition().first << ", " << event.getMousePosition().second << ")" << std::endl; +} -- cgit v1.2.3 From 5e833bba513d97c39f4e0d26b45a9095c32812a6 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 28 Oct 2024 17:58:53 +0100 Subject: button working --- mwe/events/CMakeLists.txt | 37 +++++++++++-------- mwe/events/include/event.h | 20 +++++++++- mwe/events/include/eventHandler.h | 4 +- mwe/events/include/gameObject.h | 14 +++---- mwe/events/include/iMouseListener.h | 3 +- mwe/events/include/inputSystem.h | 16 ++++++++ mwe/events/include/loopManager.h | 7 ++++ mwe/events/include/uiObject.h | 17 +++++---- mwe/events/include/uiRenderer.h | 17 +++++++++ mwe/events/include/window.h | 28 +++++++++----- mwe/events/src/event.cpp | 4 +- mwe/events/src/inputSystem.cpp | 39 +++++++++++++++++++ mwe/events/src/loopManager.cpp | 61 +++++++++++++++++------------- mwe/events/src/uiObject.cpp | 2 +- mwe/events/src/uiRenderer.cpp | 72 ++++++++++++++++++++++++++++++++++++ mwe/events/src/window.cpp | 74 +++++++++++++++++++++++++------------ 16 files changed, 321 insertions(+), 94 deletions(-) create mode 100644 mwe/events/include/inputSystem.h create mode 100644 mwe/events/include/uiRenderer.h create mode 100644 mwe/events/src/inputSystem.cpp create mode 100644 mwe/events/src/uiRenderer.cpp (limited to 'mwe/events/include') diff --git a/mwe/events/CMakeLists.txt b/mwe/events/CMakeLists.txt index 096b8ad..12e45a7 100644 --- a/mwe/events/CMakeLists.txt +++ b/mwe/events/CMakeLists.txt @@ -2,34 +2,39 @@ cmake_minimum_required(VERSION 3.5) project(gameloop) # Set the C++ standard (optional, but good practice) -set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD 17) set(CMAKE_CXX_STANDARD 20) set(CMAKE_EXPORT_COMPILE_COMMANDS 1) set(CMAKE_BUILD_TYPE Debug) # Find the SDL2 package find_package(SDL2 REQUIRED) +# Find the SDL2_ttf package +find_package(SDL2_ttf REQUIRED) add_executable(gameloop - src/window.cpp - src/main.cpp - src/eventManager.cpp - src/event.cpp - src/loopManager.cpp - src/timer.cpp - src/keyCodes.cpp - src/eventHandler.cpp - src/iMouseListener.cpp - src/iKeyListener.cpp - src/mouseListenerTest.cpp - src/keyListenerTest.cpp + src/window.cpp + src/main.cpp + src/eventManager.cpp + src/event.cpp + src/loopManager.cpp + src/timer.cpp + src/keyCodes.cpp + src/eventHandler.cpp + src/iMouseListener.cpp + src/iKeyListener.cpp + src/mouseListenerTest.cpp + src/keyListenerTest.cpp + src/inputSystem.cpp + src/uiRenderer.cpp + src/uiObject.cpp ) -add_subdirectory(../../lib/SDL_ttf SDL_ttf) -# Link the SDL2 library to your project -target_link_libraries(gameloop ${SDL2_LIBRARIES}) + +target_link_libraries(gameloop ${SDL2_LIBRARIES} SDL2_ttf::SDL2_ttf) # Include SDL2 header files and project headers target_include_directories(gameloop PRIVATE ${SDL2_INCLUDE_DIRS}) target_include_directories(gameloop PRIVATE ${CMAKE_SOURCE_DIR}/include) +# Copy font files to the build directory file(COPY ${PROJECT_SOURCE_DIR}/font DESTINATION ${CMAKE_BINARY_DIR}) diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index 65560a1..b1b6867 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -7,7 +7,6 @@ #include #include "keyCodes.h" #include "customTypes.h" - class UUIDGenerator { public: static std::uint32_t getUniqueID() { @@ -151,3 +150,22 @@ public: private: std::string text; }; +class ShutDownEvent : public Event { +public: + ShutDownEvent() : Event("ShutDownEvent"){}; + + REGISTER_EVENT_TYPE(ShutDownEvent) + +private: +}; +// class ButtonClickEvent : public Event { +// public: +// ButtonClickEvent(int x,int y,int width,int height); + +// REGISTER_EVENT_TYPE(TextSubmitEvent) + +// std::string getText() const; + +// private: +// std::string text; +// }; diff --git a/mwe/events/include/eventHandler.h b/mwe/events/include/eventHandler.h index 7414801..7d8f20a 100644 --- a/mwe/events/include/eventHandler.h +++ b/mwe/events/include/eventHandler.h @@ -1,9 +1,9 @@ #pragma once - #include "event.h" - #include #include + + template using EventHandler = std::function; diff --git a/mwe/events/include/gameObject.h b/mwe/events/include/gameObject.h index 70d4517..de5f3fe 100644 --- a/mwe/events/include/gameObject.h +++ b/mwe/events/include/gameObject.h @@ -3,14 +3,14 @@ #include class GameObject { public: - GameObject(std::uint32_t id, std::string name, std::string tag, int layer); + GameObject(){} - template - void addSpriteComponent(Args &&... args); - template - void addRigidbodyComponent(Args &&... args); - template - void addColiderComponent(Args &&... args); + // template + // void addSpriteComponent(Args &&... args); + // template + // void addRigidbodyComponent(Args &&... args); + // template + // void addColiderComponent(Args &&... args); std::uint32_t mId; std::string mName; diff --git a/mwe/events/include/iMouseListener.h b/mwe/events/include/iMouseListener.h index 03fd4c3..7d840c1 100644 --- a/mwe/events/include/iMouseListener.h +++ b/mwe/events/include/iMouseListener.h @@ -1,7 +1,8 @@ #pragma once #include "event.h" -#include "eventManager.h" #include "eventHandler.h" +#include "eventManager.h" + class IMouseListener { public: virtual ~IMouseListener(); diff --git a/mwe/events/include/inputSystem.h b/mwe/events/include/inputSystem.h new file mode 100644 index 0000000..06db74c --- /dev/null +++ b/mwe/events/include/inputSystem.h @@ -0,0 +1,16 @@ +#pragma once +#include +#include "uiObject.h" +#include "event.h" +#include "keyCodes.h" +#include "eventManager.h" +class InputSystem { +public: + InputSystem(); + void registerButton(Button* button); + void processInput(); + +private: + std::vector buttons; + void processMouseClick(int mouseX, int mouseY); +}; diff --git a/mwe/events/include/loopManager.h b/mwe/events/include/loopManager.h index baffb94..289ff0f 100644 --- a/mwe/events/include/loopManager.h +++ b/mwe/events/include/loopManager.h @@ -6,6 +6,10 @@ #include "eventHandler.h" #include "eventManager.h" #include "loopManager.h" +#include "uiRenderer.h" +#include "uiObject.h" +#include "inputSystem.h" +#include class LoopManager { public: LoopManager(); @@ -19,6 +23,7 @@ private: void lateUpdate(); void fixedUpdate(); void render(); + void onShutdown(const ShutDownEvent& e); bool gameRunning = false; WindowManager window; int timeScale = 1; @@ -26,4 +31,6 @@ private: double currentTime; double t = 0.0; double dt = 0.01; + std::unique_ptr inputSystem; + EventHandler shutdownHandler; }; diff --git a/mwe/events/include/uiObject.h b/mwe/events/include/uiObject.h index 46d32be..720d5c2 100644 --- a/mwe/events/include/uiObject.h +++ b/mwe/events/include/uiObject.h @@ -1,8 +1,10 @@ +#pragma once #include "gameObject.h" #include #include #include "event.h" #include "eventHandler.h" +#include struct Alignment { enum class Horizontal { LEFT, CENTER, RIGHT }; enum class Vertical { TOP, MIDDLE, BOTTOM }; @@ -21,28 +23,29 @@ struct Alignment { int marginRight = 0; }; struct RGBColor{ - int red, - int green, - int blue + int red; + int green; + int blue; }; class UIObject : public GameObject{ public: UIObject(int width,int height); - private: + virtual ~UIObject() {} int width; int height; + int x; + int y; }; class Button : public UIObject{ public: Button(int width,int height); - EventHandler onKeyPressed; - EventHandler onKeyReleased; + RGBColor color; + std::function onClick; }; class Text : public UIObject{ public: Text(int width,int height); - private: std::string text; int size; Alignment alignment; diff --git a/mwe/events/include/uiRenderer.h b/mwe/events/include/uiRenderer.h new file mode 100644 index 0000000..286ac1f --- /dev/null +++ b/mwe/events/include/uiRenderer.h @@ -0,0 +1,17 @@ +#pragma once +#include +#include +#include "uiObject.h" + +class UIRenderer { +public: + UIRenderer(SDL_Renderer* renderer); + void render(UIObject* uiObject); + +private: + SDL_Renderer* renderer; + + void renderButton(Button* button); + void renderText(Text* text); + void renderTextInput(TextInput* textInput); +}; diff --git a/mwe/events/include/window.h b/mwe/events/include/window.h index 9020b1a..3c34d4c 100644 --- a/mwe/events/include/window.h +++ b/mwe/events/include/window.h @@ -1,19 +1,27 @@ #pragma once #include -#include #include +#include "uiObject.h" +#include "uiRenderer.h" + class WindowManager { public: - WindowManager(); - virtual ~WindowManager(); - bool initWindow(); - void destroyWindow(); + WindowManager(); + virtual ~WindowManager(); + + bool initWindow(); + void destroyWindow(); + SDL_Renderer* getRenderer(); - SDL_Renderer * getRenderer(); + void addUIObject(UIObject* uiObject); + void renderUIObjects(); private: - const int SCREEN_WIDTH = 800; - const int SCREEN_HEIGHT = 600; - SDL_Window * window = NULL; - SDL_Renderer * renderer = NULL; + const int SCREEN_WIDTH = 800; + const int SCREEN_HEIGHT = 600; + SDL_Window* window = nullptr; + SDL_Renderer* renderer = nullptr; + + UIRenderer* uiRenderer; + std::vector uiObjects; }; diff --git a/mwe/events/src/event.cpp b/mwe/events/src/event.cpp index fc8eec9..0a7454d 100644 --- a/mwe/events/src/event.cpp +++ b/mwe/events/src/event.cpp @@ -54,7 +54,9 @@ Collision CollisionEvent::getCollisionData() const } TextSubmitEvent::TextSubmitEvent(std::string text) - : text(text), Event("TextSubmitEvent") {} + : text(text), Event("TextSubmitEvent") { + + } std::string TextSubmitEvent::getText() const { return this->text; diff --git a/mwe/events/src/inputSystem.cpp b/mwe/events/src/inputSystem.cpp new file mode 100644 index 0000000..ffab84b --- /dev/null +++ b/mwe/events/src/inputSystem.cpp @@ -0,0 +1,39 @@ +#include "inputSystem.h" + +InputSystem::InputSystem() {} + +void InputSystem::registerButton(Button* button) { + buttons.push_back(button); +} + +void InputSystem::processInput() { + + SDL_Event event; + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + triggerEvent(ShutDownEvent()); + break; + case SDL_KEYDOWN: + triggerEvent(KeyPressedEvent(getCustomKey(event.key.keysym.sym))); + break; + case SDL_MOUSEBUTTONDOWN: { + int mouseX, mouseY; + SDL_GetMouseState(&mouseX, &mouseY); + processMouseClick(mouseX, mouseY); + triggerEvent(MousePressedEvent(mouseX, mouseY)); + break; + } + } + } +} + +void InputSystem::processMouseClick(int mouseX, int mouseY) { + for (auto* button : buttons) { + if (mouseX >= button->x && mouseX <= (button->x + button->width) && + mouseY >= button->y && mouseY <= (button->y + button->height)) { + button->onClick(); + } + } +} + diff --git a/mwe/events/src/loopManager.cpp b/mwe/events/src/loopManager.cpp index 8ecb932..83ccfc3 100644 --- a/mwe/events/src/loopManager.cpp +++ b/mwe/events/src/loopManager.cpp @@ -1,22 +1,27 @@ #include "loopManager.h" -LoopManager::LoopManager() {} +LoopManager::LoopManager() + : inputSystem(std::make_unique()){ + shutdownHandler = [this](const ShutDownEvent& event) { this->onShutdown(event); }; + subscribe(shutdownHandler); +} void LoopManager::processInput() { - SDL_Event event; - SDL_PollEvent(&event); - switch (event.type) { - case SDL_QUIT: - gameRunning = false; - break; - case SDL_KEYDOWN: - triggerEvent(KeyPressedEvent(getCustomKey(event.key.keysym.sym))); - break; - case SDL_MOUSEBUTTONDOWN: - int x, y; - SDL_GetMouseState(&x, &y); - triggerEvent(MousePressedEvent(x, y)); - break; - } + inputSystem->processInput(); + // SDL_Event event; + // SDL_PollEvent(&event); + // switch (event.type) { + // case SDL_QUIT: + // gameRunning = false; + // break; + // case SDL_KEYDOWN: + // triggerEvent(KeyPressedEvent(getCustomKey(event.key.keysym.sym))); + // break; + // case SDL_MOUSEBUTTONDOWN: + // int x, y; + // SDL_GetMouseState(&x, &y); + // triggerEvent(MousePressedEvent(x, y)); + // break; + // } } void LoopManager::setRunning(bool running) { this->gameRunning = running; } void LoopManager::fixedUpdate() { @@ -28,9 +33,8 @@ void LoopManager::loop() { while (gameRunning) { timer.update(); - + processInput(); while (timer.getLag() >= timer.getFixedDeltaTime()) { - processInput(); fixedUpdate(); timer.advanceFixedUpdate(); } @@ -66,22 +70,29 @@ void LoopManager::setup() { } }; subscribe(closeWindowCallback, false); + Button* testButton = new Button(200,200); + testButton->color = {100,0,100}; + testButton->onClick = []() { + std::cout << "Button was clicked" << std::endl; + }; + testButton->x = 200; + testButton->y = 200; + inputSystem->registerButton(testButton); + + window.addUIObject(testButton); } void LoopManager::render() { //fprintf(stderr, "**********render********** \n"); if (gameRunning) { - //window.render(objectList); + window.renderUIObjects(); } } - +void LoopManager::onShutdown(const ShutDownEvent& e){ + this->gameRunning = false; +} void LoopManager::update() { //fprintf(stderr, "**********normal update********** \n"); LoopTimer & timer = LoopTimer::getInstance(); float delta = timer.getDeltaTime(); - - // for (int i = 0; i < objectList.size(); i++) { - // objectList[i]->setX(objectList[i]->getX() + 50 * delta); - // objectList[i]->setY(objectList[i]->getY() + 50 * delta); - // } } diff --git a/mwe/events/src/uiObject.cpp b/mwe/events/src/uiObject.cpp index b941858..ef4443f 100644 --- a/mwe/events/src/uiObject.cpp +++ b/mwe/events/src/uiObject.cpp @@ -2,7 +2,7 @@ // Constructor for UIObject UIObject::UIObject(int width, int height) - : width(width), height(height) { + : width(width), height(height){ } diff --git a/mwe/events/src/uiRenderer.cpp b/mwe/events/src/uiRenderer.cpp new file mode 100644 index 0000000..4a756f9 --- /dev/null +++ b/mwe/events/src/uiRenderer.cpp @@ -0,0 +1,72 @@ +#include "uiRenderer.h" + +// Constructor +UIRenderer::UIRenderer(SDL_Renderer* renderer) : renderer(renderer) {} + +// Render function +void UIRenderer::render(UIObject* uiObject) { + if (Button* button = dynamic_cast(uiObject)) { + renderButton(button); + } else if (Text* text = dynamic_cast(uiObject)) { + renderText(text); + } else if (TextInput* textInput = dynamic_cast(uiObject)) { + renderTextInput(textInput); + } +} + +// Private helper function to render a Button +void UIRenderer::renderButton(Button* button) { + SDL_Rect buttonRect = {button->x, button->y, button->width, button->height}; + SDL_SetRenderDrawColor(renderer, 100, 100, 255, 255); // Button color + SDL_RenderFillRect(renderer, &buttonRect); +} + +// Private helper function to render a Text +void UIRenderer::renderText(Text* text) { + if (text->font != nullptr) { + SDL_Color sdlColor = {text->color.red, text->color.green, text->color.blue, 255}; + SDL_Surface* textSurface = TTF_RenderText_Blended(text->font, text->text.c_str(), sdlColor); + if (!textSurface) { + std::cerr << "Error creating text surface: " << TTF_GetError() << std::endl; + return; + } + + SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); + if (!textTexture) { + std::cerr << "Error creating texture from surface: " << SDL_GetError() << std::endl; + SDL_FreeSurface(textSurface); + return; + } + + SDL_Rect textRect = {text->x, text->y, textSurface->w, textSurface->h}; + SDL_RenderCopy(renderer, textTexture, nullptr, &textRect); + SDL_FreeSurface(textSurface); + SDL_DestroyTexture(textTexture); + } +} + +// Private helper function to render a TextInput +void UIRenderer::renderTextInput(TextInput* textInput) { + SDL_Rect inputRect = {textInput->x, textInput->y, textInput->width, textInput->height}; + SDL_SetRenderDrawColor(renderer, textInput->backgroundColor.red, textInput->backgroundColor.green, textInput->backgroundColor.blue, 255); + SDL_RenderFillRect(renderer, &inputRect); + + // Render text or placeholder + if (!textInput->textBuffer.empty()) { + SDL_Color sdlColor = {textInput->textColor.red, textInput->textColor.green, textInput->textColor.blue, 255}; + SDL_Surface* textSurface = TTF_RenderText_Blended(textInput->font, textInput->textBuffer.c_str(), sdlColor); + SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); + SDL_Rect textRect = {textInput->x + 5, textInput->y + 5, textSurface->w, textSurface->h}; + SDL_RenderCopy(renderer, textTexture, nullptr, &textRect); + SDL_FreeSurface(textSurface); + SDL_DestroyTexture(textTexture); + } else if (!textInput->placeholder.empty()) { + SDL_Color sdlColor = {128, 128, 128, 255}; // Placeholder color + SDL_Surface* placeholderSurface = TTF_RenderText_Blended(textInput->font, textInput->placeholder.c_str(), sdlColor); + SDL_Texture* placeholderTexture = SDL_CreateTextureFromSurface(renderer, placeholderSurface); + SDL_Rect placeholderRect = {textInput->x + 5, textInput->y + 5, placeholderSurface->w, placeholderSurface->h}; + SDL_RenderCopy(renderer, placeholderTexture, nullptr, &placeholderRect); + SDL_FreeSurface(placeholderSurface); + SDL_DestroyTexture(placeholderTexture); + } +} diff --git a/mwe/events/src/window.cpp b/mwe/events/src/window.cpp index 41eb85f..a3bae40 100644 --- a/mwe/events/src/window.cpp +++ b/mwe/events/src/window.cpp @@ -1,29 +1,57 @@ #include "window.h" -WindowManager::WindowManager() {} -WindowManager::~WindowManager() { destroyWindow(); } -SDL_Renderer * WindowManager::getRenderer() { return renderer; } +#include + + +WindowManager::WindowManager() { + this->uiRenderer = nullptr; +} + + +WindowManager::~WindowManager() { + destroyWindow(); +} bool WindowManager::initWindow() { - if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { - fprintf(stderr, "Error inititalising SDL.\n"); - return false; - } - window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, - SCREEN_HEIGHT, SDL_WINDOW_SHOWN); - if (!window) { - fprintf(stderr, "Error creating SDL Window. \n"); - return false; - } - renderer = SDL_CreateRenderer(window, -1, 0); - if (!renderer) { - fprintf(stderr, "Error creating SDL renderer. \n"); - return false; - } - return true; + if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { + std::cerr << "Error initializing SDL.\n"; + return false; + } + + window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, + SCREEN_HEIGHT, SDL_WINDOW_SHOWN); + if (!window) { + std::cerr << "Error creating SDL Window.\n"; + return false; + } + + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + if (!renderer) { + std::cerr << "Error creating SDL renderer.\n"; + return false; + } + + uiRenderer = new UIRenderer(renderer); + return true; } + void WindowManager::destroyWindow() { - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); - SDL_Quit(); + delete uiRenderer; + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); +} + +SDL_Renderer* WindowManager::getRenderer() { + return renderer; +} +void WindowManager::addUIObject(UIObject* uiObject) { + uiObjects.push_back(uiObject); +} + +void WindowManager::renderUIObjects() { + for (UIObject* obj : uiObjects) { + uiRenderer->render(obj); + } + SDL_RenderPresent(this->renderer); } -- cgit v1.2.3 From ed8534e2d150428bcbc4a6df8940323ae8db2925 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sun, 3 Nov 2024 10:49:19 +0100 Subject: fixed gameloop double window and event poc is working again --- lib/SDL_ttf | 1 + mwe/events/include/inputSystem.h | 8 ++++ mwe/events/include/uiObject.h | 19 +++++----- mwe/events/include/uiRenderer.h | 4 ++ mwe/events/src/inputSystem.cpp | 62 ++++++++++++++++++++++++++++++- mwe/events/src/loopManager.cpp | 8 ++++ mwe/events/src/main.cpp | 18 +-------- mwe/events/src/uiObject.cpp | 2 +- mwe/events/src/uiRenderer.cpp | 75 +++++++++++++++++++++++++++----------- mwe/events/src/window.cpp | 1 - mwe/gameloop/include/loopManager.h | 4 +- mwe/gameloop/include/timer.h | 2 +- mwe/gameloop/src/loopManager.cpp | 21 ++++++++--- 13 files changed, 165 insertions(+), 60 deletions(-) create mode 160000 lib/SDL_ttf (limited to 'mwe/events/include') diff --git a/lib/SDL_ttf b/lib/SDL_ttf new file mode 160000 index 0000000..a3d0895 --- /dev/null +++ b/lib/SDL_ttf @@ -0,0 +1 @@ +Subproject commit a3d0895c1b60c41ff9e85d9203ddd7485c014dae diff --git a/mwe/events/include/inputSystem.h b/mwe/events/include/inputSystem.h index 06db74c..c20562d 100644 --- a/mwe/events/include/inputSystem.h +++ b/mwe/events/include/inputSystem.h @@ -8,9 +8,17 @@ class InputSystem { public: InputSystem(); void registerButton(Button* button); + void registerText(Text* label); + void registerTextInput(TextInput* input); void processInput(); private: std::vector buttons; + std::vector textInputs; + std::vector texts; void processMouseClick(int mouseX, int mouseY); + void processInputField(int mouseX, int mouseY); + void processKeyPress(Keycode); + void processTextInput(const std::string& text); + }; diff --git a/mwe/events/include/uiObject.h b/mwe/events/include/uiObject.h index 720d5c2..893232d 100644 --- a/mwe/events/include/uiObject.h +++ b/mwe/events/include/uiObject.h @@ -53,17 +53,18 @@ class Text : public UIObject{ TTF_Font *font; RGBColor color; }; -class TextInput : public UIObject{ - public: - TextInput (int width,int height); - std::string textBuffer; +class TextInput : public UIObject { +public: + TextInput(int width, int height); + std::string textBuffer; std::string placeholder; - size_t caretPosition; - bool isActive; + bool isActive = false; RGBColor textColor; RGBColor backgroundColor; - size_t maxLength; + size_t maxLength = 100; Alignment alignment; - TTF_Font* font; - EventHandler onSubmit; + TTF_Font* font = nullptr; + std::function onSubmit; + std::function onFocus; }; + diff --git a/mwe/events/include/uiRenderer.h b/mwe/events/include/uiRenderer.h index 286ac1f..32acecf 100644 --- a/mwe/events/include/uiRenderer.h +++ b/mwe/events/include/uiRenderer.h @@ -1,15 +1,19 @@ #pragma once #include #include +#include #include "uiObject.h" class UIRenderer { public: UIRenderer(SDL_Renderer* renderer); + ~UIRenderer(); + void render(UIObject* uiObject); private: SDL_Renderer* renderer; + TTF_Font* font; void renderButton(Button* button); void renderText(Text* text); diff --git a/mwe/events/src/inputSystem.cpp b/mwe/events/src/inputSystem.cpp index ffab84b..bb26e8b 100644 --- a/mwe/events/src/inputSystem.cpp +++ b/mwe/events/src/inputSystem.cpp @@ -5,9 +5,14 @@ InputSystem::InputSystem() {} void InputSystem::registerButton(Button* button) { buttons.push_back(button); } +void InputSystem::registerTextInput(TextInput* input) { + textInputs.push_back(input); +} +void InputSystem::registerText(Text* label) { + texts.push_back(label); +} void InputSystem::processInput() { - SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { @@ -16,6 +21,11 @@ void InputSystem::processInput() { break; case SDL_KEYDOWN: triggerEvent(KeyPressedEvent(getCustomKey(event.key.keysym.sym))); + processKeyPress(event.key.keysym.sym); + break; + case SDL_TEXTINPUT: + // Process typed characters + processTextInput(event.text.text); break; case SDL_MOUSEBUTTONDOWN: { int mouseX, mouseY; @@ -28,6 +38,7 @@ void InputSystem::processInput() { } } + void InputSystem::processMouseClick(int mouseX, int mouseY) { for (auto* button : buttons) { if (mouseX >= button->x && mouseX <= (button->x + button->width) && @@ -35,5 +46,54 @@ void InputSystem::processMouseClick(int mouseX, int mouseY) { button->onClick(); } } + for(auto* textInput : textInputs){ + if (mouseX >= textInput->x && mouseX <= textInput->x + textInput->width && + mouseY >= textInput->y && mouseY <= textInput->y + textInput->height) { + textInput->isActive = true; + } else { + textInput->isActive = false; + } + } +} +void InputSystem::processKeyPress(Keycode key) { + // for (auto* textInput : textInputs) { + // if (textInput->isActive) { + // if (key == SDLK_RETURN || key == SDLK_KP_ENTER) { + // // Submit the text + // if (textInput->onSubmit) { + // textInput->onSubmit(); + // } + // } + // else if (key == SDLK_BACKSPACE) { + // // Handle backspace + // if (!textInput->textBuffer.empty() && textInput->caretPosition > 0) { + // textInput->textBuffer.erase(textInput->caretPosition - 1, 1); + // textInput->caretPosition--; + // } + // } + // else if (key == SDLK_LEFT) { + // // Move caret left + // if (textInput->caretPosition > 0) { + // textInput->caretPosition--; + // } + // } + // else if (key == SDLK_RIGHT) { + // // Move caret right + // if (textInput->caretPosition < textInput->textBuffer.size()) { + // textInput->caretPosition++; + // } + // } + // } + // } +} + +void InputSystem::processTextInput(const std::string& text) { + // for (auto* textInput : textInputs) { + // if (textInput->isActive) { + // // Insert text at caret position + // textInput->textBuffer.insert(textInput->caretPosition, text); + // textInput->caretPosition += text.length(); + // } + // } } diff --git a/mwe/events/src/loopManager.cpp b/mwe/events/src/loopManager.cpp index 83ccfc3..f673d44 100644 --- a/mwe/events/src/loopManager.cpp +++ b/mwe/events/src/loopManager.cpp @@ -80,6 +80,14 @@ void LoopManager::setup() { inputSystem->registerButton(testButton); window.addUIObject(testButton); + + + TextInput* testInput = new TextInput(200,200); + testInput->x = 100; + testInput->y = 100; + testInput->backgroundColor = {20,50,80}; + inputSystem->registerTextInput(testInput); + window.addUIObject(testInput); } void LoopManager::render() { //fprintf(stderr, "**********render********** \n"); diff --git a/mwe/events/src/main.cpp b/mwe/events/src/main.cpp index 03dff16..8fd6d10 100644 --- a/mwe/events/src/main.cpp +++ b/mwe/events/src/main.cpp @@ -40,7 +40,7 @@ void onKeyPressed(const KeyPressedEvent& e) fprintf(stderr,"second function KeyCode %d\n",keyCode); } void CollisionHandler(const CollisionEvent& e){ - std::cout << "collision betwee object id: "<< e.getCollisionData().objectIdA << " and id: " << e.getCollisionData().objectIdB << std::endl; + std::cout << "collision between object id: "<< e.getCollisionData().objectIdA << " and id: " << e.getCollisionData().objectIdB << std::endl; } void testCollisionEvent() { Collision testCollision(1, 2, {3, 4}, {5, 6}, 7.8f); @@ -69,19 +69,3 @@ int main(int argc, char * args[]) { gameLoop.loop(); return 0; } -// void collisionUpdate(){ -// int count; -// //iedere collision -// for (int i = 0; i < count; i++) -// { -// //trigger object 1 -// //triger object 2 -// triggerEvent(CollisionEvent(1,2),1); -// triggerEvent(CollisionEvent(1,2),2); -// } - -// } -// int main(){ - -// return 0; -// } diff --git a/mwe/events/src/uiObject.cpp b/mwe/events/src/uiObject.cpp index ef4443f..31ff486 100644 --- a/mwe/events/src/uiObject.cpp +++ b/mwe/events/src/uiObject.cpp @@ -19,7 +19,7 @@ Text::Text(int width, int height) } TextInput::TextInput(int width, int height) - : UIObject(width, height), textBuffer(""), placeholder(""), caretPosition(0), + : UIObject(width, height), textBuffer(""), placeholder(""), isActive(false), textColor{255, 255, 255}, backgroundColor{0, 0, 0}, maxLength(100), font(nullptr) { alignment.horizontal = Alignment::Horizontal::LEFT; alignment.vertical = Alignment::Vertical::TOP; diff --git a/mwe/events/src/uiRenderer.cpp b/mwe/events/src/uiRenderer.cpp index 4a756f9..dbe8dfe 100644 --- a/mwe/events/src/uiRenderer.cpp +++ b/mwe/events/src/uiRenderer.cpp @@ -45,28 +45,59 @@ void UIRenderer::renderText(Text* text) { } } -// Private helper function to render a TextInput void UIRenderer::renderTextInput(TextInput* textInput) { - SDL_Rect inputRect = {textInput->x, textInput->y, textInput->width, textInput->height}; - SDL_SetRenderDrawColor(renderer, textInput->backgroundColor.red, textInput->backgroundColor.green, textInput->backgroundColor.blue, 255); - SDL_RenderFillRect(renderer, &inputRect); + // // Check if textInput or renderer is null to avoid segmentation faults + // if (!textInput || !renderer) { + // std::cerr << "Error: Null pointer detected for textInput or renderer." << std::endl; + // return; + // } - // Render text or placeholder - if (!textInput->textBuffer.empty()) { - SDL_Color sdlColor = {textInput->textColor.red, textInput->textColor.green, textInput->textColor.blue, 255}; - SDL_Surface* textSurface = TTF_RenderText_Blended(textInput->font, textInput->textBuffer.c_str(), sdlColor); - SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); - SDL_Rect textRect = {textInput->x + 5, textInput->y + 5, textSurface->w, textSurface->h}; - SDL_RenderCopy(renderer, textTexture, nullptr, &textRect); - SDL_FreeSurface(textSurface); - SDL_DestroyTexture(textTexture); - } else if (!textInput->placeholder.empty()) { - SDL_Color sdlColor = {128, 128, 128, 255}; // Placeholder color - SDL_Surface* placeholderSurface = TTF_RenderText_Blended(textInput->font, textInput->placeholder.c_str(), sdlColor); - SDL_Texture* placeholderTexture = SDL_CreateTextureFromSurface(renderer, placeholderSurface); - SDL_Rect placeholderRect = {textInput->x + 5, textInput->y + 5, placeholderSurface->w, placeholderSurface->h}; - SDL_RenderCopy(renderer, placeholderTexture, nullptr, &placeholderRect); - SDL_FreeSurface(placeholderSurface); - SDL_DestroyTexture(placeholderTexture); - } + // // Render the background rectangle for the text input + // SDL_Rect inputRect = {textInput->x, textInput->y, textInput->width, textInput->height}; + // SDL_SetRenderDrawColor(renderer, textInput->backgroundColor.red, textInput->backgroundColor.green, textInput->backgroundColor.blue, 255); + // SDL_RenderFillRect(renderer, &inputRect); + + // // Check if font is valid + // if (!textInput->font) { + // std::cerr << "Error: Font is not loaded for textInput." << std::endl; + // return; + // } + + // SDL_Color sdlColor = {textInput->textColor.red, textInput->textColor.green, textInput->textColor.blue, 255}; + + // if (!textInput->textBuffer.empty()) { + // // Render the text in the input field + // SDL_Surface* textSurface = TTF_RenderText_Blended(textInput->font, textInput->textBuffer.c_str(), sdlColor); + // if (textSurface) { + // SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); + // if (textTexture) { + // SDL_Rect textRect = {textInput->x + 5, textInput->y + 5, textSurface->w, textSurface->h}; + // SDL_RenderCopy(renderer, textTexture, nullptr, &textRect); + // SDL_DestroyTexture(textTexture); + // } else { + // std::cerr << "Error: Unable to create texture from text surface." << std::endl; + // } + // SDL_FreeSurface(textSurface); + // } else { + // std::cerr << "Error: Unable to create text surface." << std::endl; + // } + // } else if (!textInput->placeholder.empty()) { + // // Render the placeholder text + // SDL_Color placeholderColor = {128, 128, 128, 255}; // Light gray for placeholder + // SDL_Surface* placeholderSurface = TTF_RenderText_Blended(textInput->font, textInput->placeholder.c_str(), placeholderColor); + // if (placeholderSurface) { + // SDL_Texture* placeholderTexture = SDL_CreateTextureFromSurface(renderer, placeholderSurface); + // if (placeholderTexture) { + // SDL_Rect placeholderRect = {textInput->x + 5, textInput->y + 5, placeholderSurface->w, placeholderSurface->h}; + // SDL_RenderCopy(renderer, placeholderTexture, nullptr, &placeholderRect); + // SDL_DestroyTexture(placeholderTexture); + // } else { + // std::cerr << "Error: Unable to create texture from placeholder surface." << std::endl; + // } + // SDL_FreeSurface(placeholderSurface); + // } else { + // std::cerr << "Error: Unable to create placeholder surface." << std::endl; + // } + // } } + diff --git a/mwe/events/src/window.cpp b/mwe/events/src/window.cpp index a3bae40..7192e6b 100644 --- a/mwe/events/src/window.cpp +++ b/mwe/events/src/window.cpp @@ -36,7 +36,6 @@ bool WindowManager::initWindow() { } void WindowManager::destroyWindow() { - delete uiRenderer; SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); diff --git a/mwe/gameloop/include/loopManager.h b/mwe/gameloop/include/loopManager.h index 1befb69..e2c2e2c 100644 --- a/mwe/gameloop/include/loopManager.h +++ b/mwe/gameloop/include/loopManager.h @@ -1,11 +1,11 @@ #pragma once -#include "event.h" #include "gameObject.h" #include "window.h" #include class LoopManager { public: LoopManager(); + ~LoopManager(); void setup(); void loop(); @@ -17,7 +17,7 @@ private: void fixedUpdate(); void render(); bool gameRunning = false; - WindowManager window; + WindowManager* window; int timeScale = 1; float accumulator = 0.0; double currentTime; diff --git a/mwe/gameloop/include/timer.h b/mwe/gameloop/include/timer.h index 8273746..3c38594 100644 --- a/mwe/gameloop/include/timer.h +++ b/mwe/gameloop/include/timer.h @@ -25,7 +25,7 @@ private: double maximumDeltaTime = 0.25; double deltaTime; double frameTargetTime = FPS / 1000; - double fixedDeltaTime = 0.01; + double fixedDeltaTime = 0.02; double elapsedTime; double elapsedFixedTime; double time; diff --git a/mwe/gameloop/src/loopManager.cpp b/mwe/gameloop/src/loopManager.cpp index 0392853..b075bd8 100644 --- a/mwe/gameloop/src/loopManager.cpp +++ b/mwe/gameloop/src/loopManager.cpp @@ -1,6 +1,15 @@ #include "loopManager.h" #include "timer.h" -LoopManager::LoopManager() {} +LoopManager::LoopManager() { + this->window = new WindowManager(); + +} +LoopManager::~LoopManager(){ + for(GameObject* object : this->objectList){ + delete object; + } + delete this->window; +} void LoopManager::processInput() { SDL_Event event; SDL_PollEvent(&event); @@ -42,14 +51,14 @@ void LoopManager::loop() { timer.enforceFrameRate(); } - window.destroyWindow(); + window->destroyWindow(); } void LoopManager::setup() { - gameRunning = window.initWindow(); - LoopTimer::getInstance().start(); - LoopTimer::getInstance().setFPS(500); + LoopTimer::getInstance().start(); + LoopTimer::getInstance().setFPS(10); + this->gameRunning = true; for (int i = 1; i < 3; i++) { GameObject * square = new GameObject("square2", i * 60, i * 60, 20, 20, 0, 0); @@ -59,7 +68,7 @@ void LoopManager::setup() { void LoopManager::render() { fprintf(stderr, "**********render********** \n"); if (gameRunning) { - window.render(objectList); + window->render(objectList); } } -- cgit v1.2.3 From 2585dc3cab48ccad0cfa0c63354662d656c86c46 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 3 Nov 2024 13:34:27 +0100 Subject: `make format` --- mwe/audio/miniaudio/main.cpp | 6 +- mwe/events/include/event.h | 26 ++--- mwe/events/include/eventHandler.h | 1 - mwe/events/include/gameObject.h | 2 +- mwe/events/include/iKeyListener.h | 19 ++-- mwe/events/include/iMouseListener.h | 23 ++--- mwe/events/include/inputSystem.h | 27 +++--- mwe/events/include/keyCodes.h | 16 ++-- mwe/events/include/keyListenerTest.h | 8 +- mwe/events/include/loopManager.h | 6 +- mwe/events/include/mouseListenerTest.h | 12 +-- mwe/events/include/uiObject.h | 74 +++++++------- mwe/events/include/uiRenderer.h | 18 ++-- mwe/events/include/window.h | 30 +++--- mwe/events/src/event.cpp | 29 +++--- mwe/events/src/iKeyListener.cpp | 19 ++-- mwe/events/src/iMouseListener.cpp | 35 ++++--- mwe/events/src/inputSystem.cpp | 162 +++++++++++++++---------------- mwe/events/src/keyListenerTest.cpp | 14 ++- mwe/events/src/loopManager.cpp | 22 ++--- mwe/events/src/main.cpp | 13 +-- mwe/events/src/mouseListenerTest.cpp | 26 ++--- mwe/events/src/uiObject.cpp | 30 +++--- mwe/events/src/uiRenderer.cpp | 170 +++++++++++++++++---------------- mwe/events/src/window.cpp | 72 +++++++------- mwe/gameloop/include/loopManager.h | 2 +- mwe/gameloop/src/loopManager.cpp | 9 +- src/crepe/api/Transform.cpp | 3 +- src/example/audio_internal.cpp | 6 +- src/example/log.cpp | 4 +- src/example/script.cpp | 12 ++- 31 files changed, 435 insertions(+), 461 deletions(-) (limited to 'mwe/events/include') diff --git a/mwe/audio/miniaudio/main.cpp b/mwe/audio/miniaudio/main.cpp index bf31898..5c4f667 100644 --- a/mwe/audio/miniaudio/main.cpp +++ b/mwe/audio/miniaudio/main.cpp @@ -33,12 +33,10 @@ int main() { ma_sound_start(&sfx[2]); ma_sound_start(&bgm); // this actually resumes now this_thread::sleep_for(500ms); - for (unsigned i = 0; i < 3; i++) - ma_sound_seek_to_pcm_frame(&sfx[i], 0); + for (unsigned i = 0; i < 3; i++) ma_sound_seek_to_pcm_frame(&sfx[i], 0); // 5. play all samples simultaniously - for (unsigned i = 0; i < 3; i++) - ma_sound_start(&sfx[i]); + for (unsigned i = 0; i < 3; i++) ma_sound_start(&sfx[i]); this_thread::sleep_for(1000ms); ma_engine_uninit(&engine); diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index 62d8974..16c75bf 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -6,8 +6,6 @@ #include #include #include -#include "keyCodes.h" -#include "customTypes.h" class UUIDGenerator { public: static std::uint32_t getUniqueID() { @@ -66,16 +64,16 @@ public: }; class MouseClickEvent : public Event { public: - MouseClickEvent(int x, int y, MouseButton button); - + MouseClickEvent(int x, int y, MouseButton button); + REGISTER_EVENT_TYPE("KeyClickedEvent"); std::pair getMousePosition() const; - MouseButton getButton() const { return button; } + MouseButton getButton() const { return button; } private: - int mouseX = 0; + int mouseX = 0; int mouseY = 0; - MouseButton button; + MouseButton button; }; // KeyReleasedEvent class class KeyReleasedEvent : public Event { @@ -112,6 +110,7 @@ public: std::pair getMousePosition() const; MouseButton getMouseButton() const; + private: int mouseX = 0; int mouseY = 0; @@ -124,6 +123,7 @@ public: REGISTER_EVENT_TYPE(MouseMovedEvent) std::pair getMousePosition() const; + private: int mouseX = 0; int mouseY = 0; @@ -141,20 +141,20 @@ private: }; class TextSubmitEvent : public Event { public: - TextSubmitEvent(std::string submittedText); + TextSubmitEvent(std::string submittedText); - REGISTER_EVENT_TYPE(TextSubmitEvent) + REGISTER_EVENT_TYPE(TextSubmitEvent) - std::string getText() const; + std::string getText() const; private: - std::string text; + std::string text; }; class ShutDownEvent : public Event { public: - ShutDownEvent() : Event("ShutDownEvent"){}; + ShutDownEvent() : Event("ShutDownEvent") {}; - REGISTER_EVENT_TYPE(ShutDownEvent) + REGISTER_EVENT_TYPE(ShutDownEvent) private: }; diff --git a/mwe/events/include/eventHandler.h b/mwe/events/include/eventHandler.h index 7d8f20a..aa8f63b 100644 --- a/mwe/events/include/eventHandler.h +++ b/mwe/events/include/eventHandler.h @@ -3,7 +3,6 @@ #include #include - template using EventHandler = std::function; diff --git a/mwe/events/include/gameObject.h b/mwe/events/include/gameObject.h index de5f3fe..48e239b 100644 --- a/mwe/events/include/gameObject.h +++ b/mwe/events/include/gameObject.h @@ -3,7 +3,7 @@ #include class GameObject { public: - GameObject(){} + GameObject() {} // template // void addSpriteComponent(Args &&... args); diff --git a/mwe/events/include/iKeyListener.h b/mwe/events/include/iKeyListener.h index 5f39ece..5fee2eb 100644 --- a/mwe/events/include/iKeyListener.h +++ b/mwe/events/include/iKeyListener.h @@ -1,21 +1,20 @@ #pragma once #include "event.h" -#include "eventManager.h" #include "eventHandler.h" +#include "eventManager.h" class IKeyListener { public: - virtual ~IKeyListener(); - virtual void onKeyPressed(const KeyPressedEvent& event) = 0; - virtual void onKeyReleased(const KeyReleasedEvent& event) = 0; + virtual ~IKeyListener(); + virtual void onKeyPressed(const KeyPressedEvent & event) = 0; + virtual void onKeyReleased(const KeyReleasedEvent & event) = 0; protected: - void subscribeEvents(int listenerId = 0); - void unsubscribeEvents(int listenerId = 0); + void subscribeEvents(int listenerId = 0); + void unsubscribeEvents(int listenerId = 0); void activate(int listenerId = 0) { subscribeEvents(listenerId); } - void deactivate(int listenerId = 0) { unsubscribeEvents(listenerId); } + void deactivate(int listenerId = 0) { unsubscribeEvents(listenerId); } private: - EventHandler keyPressedHandler; - EventHandler keyReleasedHandler; + EventHandler keyPressedHandler; + EventHandler keyReleasedHandler; }; - diff --git a/mwe/events/include/iMouseListener.h b/mwe/events/include/iMouseListener.h index 7d840c1..5b1181c 100644 --- a/mwe/events/include/iMouseListener.h +++ b/mwe/events/include/iMouseListener.h @@ -5,19 +5,20 @@ class IMouseListener { public: - virtual ~IMouseListener(); + virtual ~IMouseListener(); + + virtual void onMouseClicked(const MouseClickEvent & event) = 0; + virtual void onMousePressed(const MousePressedEvent & event) = 0; + virtual void onMouseReleased(const MouseReleasedEvent & event) = 0; + virtual void onMouseMoved(const MouseMovedEvent & event) = 0; - virtual void onMouseClicked(const MouseClickEvent& event) = 0; - virtual void onMousePressed(const MousePressedEvent& event) = 0; - virtual void onMouseReleased(const MouseReleasedEvent& event) = 0; - virtual void onMouseMoved(const MouseMovedEvent& event) = 0; protected: - void subscribeEvents(int listenerId = 0); - void unsubscribeEvents(int listenerId = 0); + void subscribeEvents(int listenerId = 0); + void unsubscribeEvents(int listenerId = 0); private: - EventHandler mouseClickHandler; - EventHandler mousePressHandler; - EventHandler mouseReleaseHandler; - EventHandler mouseMoveHandler; + EventHandler mouseClickHandler; + EventHandler mousePressHandler; + EventHandler mouseReleaseHandler; + EventHandler mouseMoveHandler; }; diff --git a/mwe/events/include/inputSystem.h b/mwe/events/include/inputSystem.h index c20562d..3e53b7c 100644 --- a/mwe/events/include/inputSystem.h +++ b/mwe/events/include/inputSystem.h @@ -1,24 +1,23 @@ #pragma once -#include -#include "uiObject.h" #include "event.h" -#include "keyCodes.h" #include "eventManager.h" +#include "keyCodes.h" +#include "uiObject.h" +#include class InputSystem { public: - InputSystem(); - void registerButton(Button* button); - void registerText(Text* label); - void registerTextInput(TextInput* input); - void processInput(); + InputSystem(); + void registerButton(Button * button); + void registerText(Text * label); + void registerTextInput(TextInput * input); + void processInput(); private: - std::vector buttons; - std::vector textInputs; - std::vector texts; - void processMouseClick(int mouseX, int mouseY); + std::vector