From 4628638f952f882e494788ca1cf880a029eba5ab Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 1 Oct 2024 13:40:56 +0200 Subject: first try at events --- mwe/events/.vscode/settings.json | 54 ++++++++++ mwe/events/CMakeLists.txt | 25 +++++ mwe/events/imgs/demo.bmp | Bin 0 -> 7998698 bytes mwe/events/imgs/demo.jpg | Bin 0 -> 903706 bytes mwe/events/include/event.h | 207 ++++++++++++++++++++++++++++++++++++ mwe/events/include/eventHandler.h | 39 +++++++ mwe/events/include/window.h | 19 ++++ mwe/events/src/eventManager.cpp | 1 + mwe/events/src/main.cpp | 14 +++ mwe/events/src/window.cpp | 33 ++++++ mwe/events/versions/delayBased.cpp | 56 ++++++++++ mwe/gameloop/include/eventManager.h | 6 +- 12 files changed, 453 insertions(+), 1 deletion(-) create mode 100644 mwe/events/.vscode/settings.json create mode 100644 mwe/events/CMakeLists.txt create mode 100644 mwe/events/imgs/demo.bmp create mode 100644 mwe/events/imgs/demo.jpg create mode 100644 mwe/events/include/event.h create mode 100644 mwe/events/include/eventHandler.h create mode 100644 mwe/events/include/window.h create mode 100644 mwe/events/src/eventManager.cpp create mode 100644 mwe/events/src/main.cpp create mode 100644 mwe/events/src/window.cpp create mode 100644 mwe/events/versions/delayBased.cpp (limited to 'mwe') diff --git a/mwe/events/.vscode/settings.json b/mwe/events/.vscode/settings.json new file mode 100644 index 0000000..b2c5be7 --- /dev/null +++ b/mwe/events/.vscode/settings.json @@ -0,0 +1,54 @@ +{ + "files.associations": { + "iostream": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "list": "cpp", + "map": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp", + "valarray": "cpp" + } +} diff --git a/mwe/events/CMakeLists.txt b/mwe/events/CMakeLists.txt new file mode 100644 index 0000000..3bbda37 --- /dev/null +++ b/mwe/events/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.5) +project(gameloop) + +# Set the C++ standard (optional, but good practice) +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_EXPORT_COMPILE_COMMANDS 1) +set(CMAKE_BUILD_TYPE Debug) + +# Find the SDL2 package +find_package(SDL2 REQUIRED) + +add_executable(gameloop + src/window.cpp + src/main.cpp + src/eventManager.cpp +) + +# Link the SDL2 library to your project +target_link_libraries(gameloop ${SDL2_LIBRARIES}) + +# Include SDL2 header files and project headers +target_include_directories(gameloop PRIVATE ${SDL2_INCLUDE_DIRS}) +target_include_directories(gameloop PRIVATE ${CMAKE_SOURCE_DIR}/include) + diff --git a/mwe/events/imgs/demo.bmp b/mwe/events/imgs/demo.bmp new file mode 100644 index 0000000..65354fe Binary files /dev/null and b/mwe/events/imgs/demo.bmp differ diff --git a/mwe/events/imgs/demo.jpg b/mwe/events/imgs/demo.jpg new file mode 100644 index 0000000..f534e1b Binary files /dev/null and b/mwe/events/imgs/demo.jpg differ diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h new file mode 100644 index 0000000..8774a7d --- /dev/null +++ b/mwe/events/include/event.h @@ -0,0 +1,207 @@ +#pragma once +#include +#include +#include +#include +using Keycode = uint16_t; +enum : Keycode { + // From glfw3.h + Space = 32, + Apostrophe = 39, /* ' */ + Comma = 44, /* , */ + Minus = 45, /* - */ + Period = 46, /* . */ + Slash = 47, /* / */ + + D0 = 48, /* 0 */ + D1 = 49, /* 1 */ + D2 = 50, /* 2 */ + D3 = 51, /* 3 */ + D4 = 52, /* 4 */ + D5 = 53, /* 5 */ + D6 = 54, /* 6 */ + D7 = 55, /* 7 */ + D8 = 56, /* 8 */ + D9 = 57, /* 9 */ + + Semicolon = 59, /* ; */ + Equal = 61, /* = */ + + A = 65, + B = 66, + C = 67, + D = 68, + E = 69, + F = 70, + G = 71, + H = 72, + I = 73, + J = 74, + K = 75, + L = 76, + M = 77, + N = 78, + O = 79, + P = 80, + Q = 81, + R = 82, + S = 83, + T = 84, + U = 85, + V = 86, + W = 87, + X = 88, + Y = 89, + Z = 90, + + LeftBracket = 91, /* [ */ + Backslash = 92, /* \ */ + RightBracket = 93, /* ] */ + GraveAccent = 96, /* ` */ + + World1 = 161, /* non-US #1 */ + World2 = 162, /* non-US #2 */ + + /* Function keys */ + Escape = 256, + Enter = 257, + Tab = 258, + Backspace = 259, + Insert = 260, + Delete = 261, + Right = 262, + Left = 263, + Down = 264, + Up = 265, + PageUp = 266, + PageDown = 267, + Home = 268, + End = 269, + CapsLock = 280, + ScrollLock = 281, + NumLock = 282, + PrintScreen = 283, + Pause = 284, + F1 = 290, + F2 = 291, + F3 = 292, + F4 = 293, + F5 = 294, + F6 = 295, + F7 = 296, + F8 = 297, + F9 = 298, + F10 = 299, + F11 = 300, + F12 = 301, + F13 = 302, + F14 = 303, + F15 = 304, + F16 = 305, + F17 = 306, + F18 = 307, + F19 = 308, + F20 = 309, + F21 = 310, + F22 = 311, + F23 = 312, + F24 = 313, + F25 = 314, + + /* Keypad */ + KP0 = 320, + KP1 = 321, + KP2 = 322, + KP3 = 323, + KP4 = 324, + KP5 = 325, + KP6 = 326, + KP7 = 327, + KP8 = 328, + KP9 = 329, + KPDecimal = 330, + KPDivide = 331, + KPMultiply = 332, + KPSubtract = 333, + KPAdd = 334, + KPEnter = 335, + KPEqual = 336, + + LeftShift = 340, + LeftControl = 341, + LeftAlt = 342, + LeftSuper = 343, + RightShift = 344, + RightControl = 345, + RightAlt = 346, + RightSuper = 347, + Menu = 348 +}; + +class UUIDGenerator { +public: + static std::uint32_t GetUniqueID() + { + static std::uint32_t id = 0; + return ++id; + } +}; +class Event { +public: + virtual ~Event() = default; + virtual std::uint32_t GetEventType() const = 0; + + virtual std::string ToString() const { + return std::to_string(GetEventType()); + }; + + bool isHandled { false }; +}; +#define EVENT_TYPE(event_type) \ + static std::uint32_t GetStaticEventType() \ + { \ + static std::uint32_t type = UUIDGenerator::GetUniqueID(); \ + return type; \ + } \ + std::uint32_t GetEventType() const override \ + { \ + return GetStaticEventType(); \ + } + +class KeyPressedEvent : public Event { +public: + EVENT_TYPE("KeyPressedEvent"); + + KeyPressedEvent(int keyCode, int repeatCount) + : key(keyCode) + , repeatCount(repeatCount) + { + } + std::string ToString() const override + { + return "KeyPressedEvent KeyPressed" + std::to_string(key); + } + +public: + Keycode key { 0 }; + int repeatCount { 0 }; +}; + +class KeyReleasedEvent : public Event { +public: + EVENT_TYPE("KeyReleased"); + + KeyReleasedEvent(int keyCode) + : key(keyCode) + { + } + std::string ToString() const override + { + return "KeyPressedEvent KeyPressed" + std::to_string(key); + } + +public: + Keycode key { 0 }; +}; + + diff --git a/mwe/events/include/eventHandler.h b/mwe/events/include/eventHandler.h new file mode 100644 index 0000000..c17c586 --- /dev/null +++ b/mwe/events/include/eventHandler.h @@ -0,0 +1,39 @@ +#pragma once +#include "event.h" +#include +#include +template +using EventHandler = std::function; +class EventHandlerWrapperInterface { +public: + void Exec(const Event& e) + { + Call(e); + } + + virtual std::string GetType() const = 0; + +private: + virtual void Call(const Event& e) = 0; +}; + +template +class EventHandlerWrapper : public EventHandlerWrapperInterface { +public: + explicit EventHandlerWrapper(const EventHandler& handler) + : m_handler(handler) + , m_handlerType(m_handler.target_type().name()) {}; + +private: + void Call(const Event& e) override + { + if (e.GetEventType() == EventType::GetStaticEventType()) { + m_handler(static_cast(e)); + } + } + + std::string GetType() const override { return m_handlerType; } + + EventHandler m_handler; + const std::string m_handlerType; +}; diff --git a/mwe/events/include/window.h b/mwe/events/include/window.h new file mode 100644 index 0000000..9020b1a --- /dev/null +++ b/mwe/events/include/window.h @@ -0,0 +1,19 @@ +#pragma once +#include +#include +#include +class WindowManager { +public: + WindowManager(); + virtual ~WindowManager(); + bool initWindow(); + void destroyWindow(); + + SDL_Renderer * getRenderer(); + +private: + const int SCREEN_WIDTH = 800; + const int SCREEN_HEIGHT = 600; + SDL_Window * window = NULL; + SDL_Renderer * renderer = NULL; +}; diff --git a/mwe/events/src/eventManager.cpp b/mwe/events/src/eventManager.cpp new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/mwe/events/src/eventManager.cpp @@ -0,0 +1 @@ + diff --git a/mwe/events/src/main.cpp b/mwe/events/src/main.cpp new file mode 100644 index 0000000..8d8567d --- /dev/null +++ b/mwe/events/src/main.cpp @@ -0,0 +1,14 @@ +#include +#include +#include "event.h" + + +int main(int argc, char * args[]) { + + KeyPressedEvent keyEvent(1,1); + KeyReleasedEvent keyRelease(1); + std::cout << keyEvent.GetEventType() << std::endl; + std::cout << keyRelease.GetEventType() << std::endl; + std::cin; + return 0; +} diff --git a/mwe/events/src/window.cpp b/mwe/events/src/window.cpp new file mode 100644 index 0000000..61a4105 --- /dev/null +++ b/mwe/events/src/window.cpp @@ -0,0 +1,33 @@ +#include "window.h" +WindowManager::WindowManager() { + if (!initWindow()) { + printf("Failed to initialize!\n"); + } +} +WindowManager::~WindowManager() { destroyWindow(); } +SDL_Renderer * WindowManager::getRenderer() { return renderer; } + +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; +} +void WindowManager::destroyWindow() { + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); +} diff --git a/mwe/events/versions/delayBased.cpp b/mwe/events/versions/delayBased.cpp new file mode 100644 index 0000000..253a03a --- /dev/null +++ b/mwe/events/versions/delayBased.cpp @@ -0,0 +1,56 @@ +#include "loopManager.h" +#include "timer.h" +LoopManager::LoopManager() {} +void LoopManager::processInput() { + SDL_Event event; + SDL_PollEvent(&event); + switch (event.type) { + case SDL_QUIT: + gameRunning = false; + break; + case SDL_KEYDOWN: + if (event.key.keysym.sym == SDLK_ESCAPE) { + gameRunning = false; + } + break; + } +} +void LoopManager::loop() { + fprintf(stderr, "loop. \n"); + while (gameRunning) { + //Timer::getInstance().update(); + //deltaTime = Timer::getInstance().getDeltaTime(); + processInput(); + update(); + render(); + } + window.destroyWindow(); +} +void LoopManager::setup() { + gameRunning = window.initWindow(); + LoopTimer::getInstance().start(); + LoopTimer::getInstance().setFPS(210); + + for (int i = 0; i < 2; i++) { + GameObject * square2 + = new GameObject("square2", i * 40, i * 40, 20, 20, 0, 0); + objectList.push_back(square2); + } +} +void LoopManager::render() { + if (gameRunning) { + window.render(objectList); + } +} + +void LoopManager::update() { + LoopTimer & timer = LoopTimer::getInstance(); + timer.enforceFrameRate(); + timer.update(); + 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/gameloop/include/eventManager.h b/mwe/gameloop/include/eventManager.h index 69c6801..ba1ca32 100644 --- a/mwe/gameloop/include/eventManager.h +++ b/mwe/gameloop/include/eventManager.h @@ -1 +1,5 @@ -class EventManager {}; +#pragma once +class EventManager { + public: + EventManager(); +}; -- cgit v1.2.3