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/gameloop/include/eventManager.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'mwe/gameloop/include/eventManager.h') 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 From e7377daf2fe3b759fbbf7e57f88c412c3b1cf011 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 24 Oct 2024 10:56:19 +0200 Subject: `make format` --- mwe/events/include/event.h | 82 ++++++------ mwe/events/include/eventHandler.h | 50 ++++---- mwe/events/include/eventManager.h | 81 ++++++------ mwe/events/include/keyCodes.h | 248 ++++++++++++++++++------------------ mwe/events/include/loopManager.h | 5 +- mwe/events/src/event.cpp | 48 +++---- mwe/events/src/eventHandler.cpp | 5 +- mwe/events/src/eventManager.cpp | 231 +++++++++++++++++++-------------- mwe/events/src/keyCodes.cpp | 247 ++++++++++++++++++----------------- mwe/events/src/loopManager.cpp | 45 +++---- mwe/events/src/main.cpp | 49 +++---- mwe/events/src/timer.cpp | 3 +- mwe/events/src/window.cpp | 3 +- mwe/gameloop/include/eventManager.h | 4 +- mwe/gameloop/include/loopManager.h | 2 +- src/crepe/api/AssetManager.h | 4 +- src/crepe/api/AssetManager.hpp | 3 +- src/crepe/api/Color.h | 1 + src/crepe/api/Config.h | 4 +- src/crepe/api/Transform.h | 1 + src/crepe/util/color.cpp | 67 +++++++--- src/crepe/util/color.h | 2 +- src/crepe/util/log.cpp | 18 ++- src/crepe/util/log.h | 13 +- src/example/log.cpp | 3 +- src/example/script.cpp | 2 +- src/makefile | 4 +- 27 files changed, 653 insertions(+), 572 deletions(-) (limited to 'mwe/gameloop/include/eventManager.h') diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index 802140c..5eef32f 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -1,87 +1,91 @@ #pragma once +#include "keyCodes.h" #include #include #include #include #include -#include "keyCodes.h" class UUIDGenerator { public: - static std::uint32_t getUniqueID() { - static std::uint32_t id = 0; - return ++id; - } + static std::uint32_t getUniqueID() { + static std::uint32_t id = 0; + return ++id; + } }; #define REGISTER_EVENT_TYPE(ClassName) \ +\ public: \ - static std::uint32_t getStaticEventType() { \ - static std::uint32_t typeID = UUIDGenerator::getUniqueID(); \ - return typeID; \ - } \ - virtual std::uint32_t getEventType() const override { \ - return getStaticEventType(); \ - } + static std::uint32_t getStaticEventType() { \ + static std::uint32_t typeID = UUIDGenerator::getUniqueID(); \ + return typeID; \ + } \ + virtual std::uint32_t getEventType() const override { \ + return getStaticEventType(); \ + } class Event { public: - Event(std::string eventType); - virtual ~Event() = default; - virtual std::uint32_t getEventType() const = 0; - virtual std::string toString() const; - void addArgument(const std::string& key, const std::variant& value); + Event(std::string eventType); + virtual ~Event() = default; + virtual std::uint32_t getEventType() const = 0; + virtual std::string toString() const; + void addArgument(const std::string & key, + const std::variant & value); - std::variant getArgument(const std::string& key) const; + std::variant + getArgument(const std::string & key) const; - std::string getType() const; - bool getHandled() const; - void markHandled(); + std::string getType() const; + bool getHandled() const; + void markHandled(); private: - std::unordered_map> eventData; - bool isHandled = false; + std::unordered_map> + eventData; + bool isHandled = false; }; // KeyPressedEvent class class KeyPressedEvent : public Event { public: - KeyPressedEvent(int keyCode); + KeyPressedEvent(int keyCode); - REGISTER_EVENT_TYPE("KeyPressedEvent"); + REGISTER_EVENT_TYPE("KeyPressedEvent"); - Keycode getKeyCode() const; - int getRepeatCount() const; + Keycode getKeyCode() const; + int getRepeatCount() const; private: - Keycode keycode; + Keycode keycode; public: - Keycode key = 0; - int repeatCount = 0; + Keycode key = 0; + int repeatCount = 0; }; // KeyReleasedEvent class class KeyReleasedEvent : public Event { public: - KeyReleasedEvent(int keyCode); + KeyReleasedEvent(int keyCode); - REGISTER_EVENT_TYPE(KeyReleasedEvent); + REGISTER_EVENT_TYPE(KeyReleasedEvent); - Keycode getKeyCode() const; + Keycode getKeyCode() const; private: - Keycode key = 0; + Keycode key = 0; }; // MousePressedEvent class class MousePressedEvent : public Event { public: - MousePressedEvent(int mouseX, int mouseY); + MousePressedEvent(int mouseX, int mouseY); - REGISTER_EVENT_TYPE(MousePressedEvent) + REGISTER_EVENT_TYPE(MousePressedEvent) - std::pair getMousePosition() const; + std::pair getMousePosition() const; private: - int mouseX = 0; - int mouseY = 0; + int mouseX = 0; + int mouseY = 0; }; diff --git a/mwe/events/include/eventHandler.h b/mwe/events/include/eventHandler.h index e5b99d6..7414801 100644 --- a/mwe/events/include/eventHandler.h +++ b/mwe/events/include/eventHandler.h @@ -4,45 +4,43 @@ #include #include -template -using EventHandler = std::function; +template +using EventHandler = std::function; class IEventHandlerWrapper { public: - virtual ~IEventHandlerWrapper() = default; + virtual ~IEventHandlerWrapper() = default; - void exec(const Event& e); + void exec(const Event & e); - virtual std::string getType() const = 0; - virtual bool isDestroyOnSuccess() const = 0; + virtual std::string getType() const = 0; + virtual bool isDestroyOnSuccess() const = 0; private: - virtual void call(const Event& e) = 0; + virtual void call(const Event & e) = 0; }; -template +template class EventHandlerWrapper : public IEventHandlerWrapper { public: - explicit EventHandlerWrapper(const EventHandler& handler, const bool destroyOnSuccess = false) - : m_handler(handler) - , m_handlerType(m_handler.target_type().name()) - , m_destroyOnSuccess(destroyOnSuccess) - { + explicit EventHandlerWrapper(const EventHandler & handler, + const bool destroyOnSuccess = false) + : m_handler(handler), m_handlerType(m_handler.target_type().name()), + m_destroyOnSuccess(destroyOnSuccess) { // std::cout << m_handlerType << std::endl; } 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; } - bool isDestroyOnSuccess() const { return m_destroyOnSuccess; } - - EventHandler m_handler; - const std::string m_handlerType; - bool m_destroyOnSuccess { false }; + void call(const Event & e) override { + if (e.getEventType() == EventType::getStaticEventType()) { + m_handler(static_cast(e)); + } + } + + std::string getType() const override { return m_handlerType; } + bool isDestroyOnSuccess() const { return m_destroyOnSuccess; } + + EventHandler m_handler; + const std::string m_handlerType; + bool m_destroyOnSuccess{false}; }; diff --git a/mwe/events/include/eventManager.h b/mwe/events/include/eventManager.h index 709796a..af41b82 100644 --- a/mwe/events/include/eventManager.h +++ b/mwe/events/include/eventManager.h @@ -1,57 +1,66 @@ #pragma once -#include -#include #include "event.h" -#include "keyCodes.h" #include "eventHandler.h" +#include "keyCodes.h" +#include +#include #include // using EventType = std::uint32_t; // using EventId = std::uint64_t; class EventManager { public: - EventManager(const EventManager&) = delete; - const EventManager& operator=(const EventManager&) = delete; - static EventManager& getInstance() { - static EventManager instance; - return instance; - } + EventManager(const EventManager &) = delete; + const EventManager & operator=(const EventManager &) = delete; + static EventManager & getInstance() { + static EventManager instance; + return instance; + } - void shutdown(); - void subscribe(int eventType, std::unique_ptr&& handler, int eventId); - void unsubscribe(int eventType, const std::string& handlerName, int eventId); - void triggerEvent(const Event& event_, int eventId); - void queueEvent(std::unique_ptr&& event_, int eventId); - void dispatchEvents(); + void shutdown(); + void subscribe(int eventType, + std::unique_ptr && handler, + int eventId); + void unsubscribe(int eventType, const std::string & handlerName, + int eventId); + void triggerEvent(const Event & event_, int eventId); + void queueEvent(std::unique_ptr && event_, int eventId); + void dispatchEvents(); private: - EventManager() = default; - std::vector, int>> m_eventsQueue; - std::unordered_map>> m_subscribers; - std::unordered_map>>> m_subscribersByEventId; + EventManager() = default; + std::vector, int>> m_eventsQueue; + std::unordered_map>> + m_subscribers; + std::unordered_map< + int, std::unordered_map< + int, std::vector>>> + m_subscribersByEventId; }; - -template -inline void subscribe(const EventHandler& callback, int eventId = 0, const bool unsubscribeOnSuccess = false) -{ - std::unique_ptr handler = std::make_unique>(callback, unsubscribeOnSuccess); - EventManager::getInstance().subscribe(EventType::getStaticEventType(), std::move(handler), eventId); +template +inline void subscribe(const EventHandler & callback, int eventId = 0, + const bool unsubscribeOnSuccess = false) { + std::unique_ptr handler + = std::make_unique>( + callback, unsubscribeOnSuccess); + EventManager::getInstance().subscribe(EventType::getStaticEventType(), + std::move(handler), eventId); } -template -inline void unsubscribe(const EventHandler& callback, int eventId = 0) -{ - const std::string handlerName = callback.target_type().name(); - EventManager::getInstance().unsubscribe(EventType::getStaticEventType(), handlerName, eventId); +template +inline void unsubscribe(const EventHandler & callback, + int eventId = 0) { + const std::string handlerName = callback.target_type().name(); + EventManager::getInstance().unsubscribe(EventType::getStaticEventType(), + handlerName, eventId); } -inline void triggerEvent(const Event& triggeredEvent, int eventId = 0) -{ - EventManager::getInstance().triggerEvent(triggeredEvent, eventId); +inline void triggerEvent(const Event & triggeredEvent, int eventId = 0) { + EventManager::getInstance().triggerEvent(triggeredEvent, eventId); } -inline void queueEvent(std::unique_ptr&& queuedEvent, int eventId = 0) -{ - EventManager::getInstance().queueEvent(std::forward>(queuedEvent), eventId); +inline void queueEvent(std::unique_ptr && queuedEvent, int eventId = 0) { + EventManager::getInstance().queueEvent( + std::forward>(queuedEvent), eventId); } diff --git a/mwe/events/include/keyCodes.h b/mwe/events/include/keyCodes.h index c3a7826..0879efc 100644 --- a/mwe/events/include/keyCodes.h +++ b/mwe/events/include/keyCodes.h @@ -1,140 +1,140 @@ #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, /* / */ + // 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 */ + 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, /* = */ + 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, + 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, /* ` */ + LeftBracket = 91, /* [ */ + Backslash = 92, /* \ */ + RightBracket = 93, /* ] */ + GraveAccent = 96, /* ` */ - World1 = 161, /* non-US #1 */ - World2 = 162, /* non-US #2 */ + 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, + /* 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, + /* 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 + LeftShift = 340, + LeftControl = 341, + LeftAlt = 342, + LeftSuper = 343, + RightShift = 344, + RightControl = 345, + RightAlt = 346, + RightSuper = 347, + Menu = 348 }; // Define the mapping extern const std::unordered_map sdlToCustom; diff --git a/mwe/events/include/loopManager.h b/mwe/events/include/loopManager.h index 5c519d9..baffb94 100644 --- a/mwe/events/include/loopManager.h +++ b/mwe/events/include/loopManager.h @@ -1,17 +1,18 @@ #pragma once +#include "timer.h" #include "window.h" #include -#include "timer.h" //#include "combinedEvent.h" +#include "eventHandler.h" #include "eventManager.h" #include "loopManager.h" -#include "eventHandler.h" class LoopManager { public: LoopManager(); void setup(); void loop(); void setRunning(bool running); + private: void processInput(); void update(); diff --git a/mwe/events/src/event.cpp b/mwe/events/src/event.cpp index fecae76..c0ae9ed 100644 --- a/mwe/events/src/event.cpp +++ b/mwe/events/src/event.cpp @@ -1,56 +1,44 @@ #include "event.h" #include "keyCodes.h" // Event class methods -Event::Event(std::string eventType) { - eventData["eventType"] = eventType; -} +Event::Event(std::string eventType) { eventData["eventType"] = eventType; } -void Event::addArgument(const std::string& key, const std::variant& value) { - eventData[key] = value; +void Event::addArgument(const std::string & key, + const std::variant & value) { + eventData[key] = value; } -std::variant Event::getArgument(const std::string& key) const { - return eventData.at(key); +std::variant +Event::getArgument(const std::string & key) const { + return eventData.at(key); } std::string Event::getType() const { - return std::get(eventData.at("eventType")); -} -std::string Event::toString() const { - return std::to_string(getEventType()); -} -bool Event::getHandled() const { - return isHandled; + return std::get(eventData.at("eventType")); } +std::string Event::toString() const { return std::to_string(getEventType()); } +bool Event::getHandled() const { return isHandled; } -void Event::markHandled() { - isHandled = true; -} +void Event::markHandled() { isHandled = true; } // KeyPressedEvent class methods KeyPressedEvent::KeyPressedEvent(int keycode) - : Event("KeyPressedEvent"), key(keycode), repeatCount(0) {} + : Event("KeyPressedEvent"), key(keycode), repeatCount(0) {} -Keycode KeyPressedEvent::getKeyCode() const { - return key; -} +Keycode KeyPressedEvent::getKeyCode() const { return key; } -int KeyPressedEvent::getRepeatCount() const { - return repeatCount; -} +int KeyPressedEvent::getRepeatCount() const { return repeatCount; } // KeyReleasedEvent class methods KeyReleasedEvent::KeyReleasedEvent(int keycode) - : Event("KeyReleasedEvent"), key(keycode) {} + : Event("KeyReleasedEvent"), key(keycode) {} -Keycode KeyReleasedEvent::getKeyCode() const { - return key; -} +Keycode KeyReleasedEvent::getKeyCode() const { return key; } // MousePressedEvent class methods MousePressedEvent::MousePressedEvent(int mouseX, int mouseY) - : Event("MousePressedEvent"), mouseX(mouseX), mouseY(mouseY) {} + : Event("MousePressedEvent"), mouseX(mouseX), mouseY(mouseY) {} std::pair MousePressedEvent::getMousePosition() const { - return {mouseX, mouseY}; + return {mouseX, mouseY}; } diff --git a/mwe/events/src/eventHandler.cpp b/mwe/events/src/eventHandler.cpp index 7040d8d..7fb8d70 100644 --- a/mwe/events/src/eventHandler.cpp +++ b/mwe/events/src/eventHandler.cpp @@ -1,5 +1,2 @@ #include "eventHandler.h" -void IEventHandlerWrapper::exec(const Event& e) - { - call(e); - } +void IEventHandlerWrapper::exec(const Event & e) { call(e); } diff --git a/mwe/events/src/eventManager.cpp b/mwe/events/src/eventManager.cpp index ce8e940..2aef89d 100644 --- a/mwe/events/src/eventManager.cpp +++ b/mwe/events/src/eventManager.cpp @@ -1,109 +1,152 @@ #include "eventManager.h" -void EventManager::shutdown() -{ - m_subscribers.clear(); -} +void EventManager::shutdown() { m_subscribers.clear(); } -void EventManager::subscribe(int eventType, std::unique_ptr&& handler, int eventId) -{ - if (eventId) { - std::unordered_map>>>::iterator subscribers = m_subscribersByEventId.find(eventType); +void EventManager::subscribe(int eventType, + std::unique_ptr && handler, + int eventId) { + if (eventId) { + std::unordered_map< + int, std::unordered_map< + int, std::vector>>>:: + iterator subscribers + = m_subscribersByEventId.find(eventType); - if (subscribers != m_subscribersByEventId.end()) { - std::unordered_map>>& handlersMap = subscribers->second; - std::unordered_map>>::iterator handlers = handlersMap.find(eventId); - if (handlers != handlersMap.end()) { - handlers->second.emplace_back(std::move(handler)); - return; - } - } - m_subscribersByEventId[eventType][eventId].emplace_back(std::move(handler)); + if (subscribers != m_subscribersByEventId.end()) { + std::unordered_map< + int, std::vector>> & + handlersMap + = subscribers->second; + std::unordered_map< + int, + std::vector>>::iterator + handlers + = handlersMap.find(eventId); + if (handlers != handlersMap.end()) { + handlers->second.emplace_back(std::move(handler)); + return; + } + } + m_subscribersByEventId[eventType][eventId].emplace_back( + std::move(handler)); - } else { - std::unordered_map>>::iterator subscribers = m_subscribers.find(eventType); - if (subscribers != m_subscribers.end()) { - std::vector>& handlers = subscribers->second; - for (std::unique_ptr& it : handlers) { - if (it->getType() == handler->getType()) { - // log for double register - return; - } - } - handlers.emplace_back(std::move(handler)); - } else { - m_subscribers[eventType].emplace_back(std::move(handler)); - } - } + } else { + std::unordered_map< + int, std::vector>>::iterator + subscribers + = m_subscribers.find(eventType); + if (subscribers != m_subscribers.end()) { + std::vector> & handlers + = subscribers->second; + for (std::unique_ptr & it : handlers) { + if (it->getType() == handler->getType()) { + // log for double register + return; + } + } + handlers.emplace_back(std::move(handler)); + } else { + m_subscribers[eventType].emplace_back(std::move(handler)); + } + } } -void EventManager::unsubscribe(int eventType, const std::string& handlerName, int eventId) -{ - if (eventId) { - std::unordered_map>>>::iterator subscribers = m_subscribersByEventId.find(eventType); - if (subscribers != m_subscribersByEventId.end()) { - std::unordered_map>>& handlersMap = subscribers->second; - std::unordered_map>>::iterator handlers = handlersMap.find(eventId); - if (handlers != handlersMap.end()) { - std::vector>& callbacks = handlers->second; - for (std::vector>::iterator it = callbacks.begin(); it != callbacks.end(); ++it) { - if (it->get()->getType() == handlerName) { - it = callbacks.erase(it); - return; - } - } - } - } - } else { - std::unordered_map>>::iterator handlersIt = m_subscribers.find(eventType); - if (handlersIt != m_subscribers.end()) { - std::vector>& handlers = handlersIt->second; - for (std::vector>::iterator it = handlers.begin(); it != handlers.end(); ++it) { - if (it->get()->getType() == handlerName) { - it = handlers.erase(it); - return; - } - } - } - } +void EventManager::unsubscribe(int eventType, const std::string & handlerName, + int eventId) { + if (eventId) { + std::unordered_map< + int, std::unordered_map< + int, std::vector>>>:: + iterator subscribers + = m_subscribersByEventId.find(eventType); + if (subscribers != m_subscribersByEventId.end()) { + std::unordered_map< + int, std::vector>> & + handlersMap + = subscribers->second; + std::unordered_map< + int, + std::vector>>::iterator + handlers + = handlersMap.find(eventId); + if (handlers != handlersMap.end()) { + std::vector> & callbacks + = handlers->second; + for (std::vector< + std::unique_ptr>::iterator it + = callbacks.begin(); + it != callbacks.end(); ++it) { + if (it->get()->getType() == handlerName) { + it = callbacks.erase(it); + return; + } + } + } + } + } else { + std::unordered_map< + int, std::vector>>::iterator + handlersIt + = m_subscribers.find(eventType); + if (handlersIt != m_subscribers.end()) { + std::vector> & handlers + = handlersIt->second; + for (std::vector>::iterator it + = handlers.begin(); + it != handlers.end(); ++it) { + if (it->get()->getType() == handlerName) { + it = handlers.erase(it); + return; + } + } + } + } } -void EventManager::triggerEvent(const Event& event_, int eventId) -{ - std::vector>& handlers = m_subscribers[event_.getEventType()]; - for (std::unique_ptr& handler : handlers) { - handler->exec(event_); - } +void EventManager::triggerEvent(const Event & event_, int eventId) { + std::vector> & handlers + = m_subscribers[event_.getEventType()]; + for (std::unique_ptr & handler : handlers) { + handler->exec(event_); + } - std::unordered_map>>& handlersMap = m_subscribersByEventId[event_.getEventType()]; - std::unordered_map>>::iterator handlersIt = handlersMap.find(eventId); - if (handlersIt != handlersMap.end()) { - std::vector>& callbacks = handlersIt->second; - for (std::vector>::iterator it = callbacks.begin(); it != callbacks.end();) { - std::unique_ptr& handler = *it; - handler->exec(event_); - if (handler->isDestroyOnSuccess()) { - it = callbacks.erase(it); - } else { - ++it; - } - } - } + std::unordered_map< + int, std::vector>> & handlersMap + = m_subscribersByEventId[event_.getEventType()]; + std::unordered_map< + int, std::vector>>::iterator + handlersIt + = handlersMap.find(eventId); + if (handlersIt != handlersMap.end()) { + std::vector> & callbacks + = handlersIt->second; + for (std::vector>::iterator it + = callbacks.begin(); + it != callbacks.end();) { + std::unique_ptr & handler = *it; + handler->exec(event_); + if (handler->isDestroyOnSuccess()) { + it = callbacks.erase(it); + } else { + ++it; + } + } + } } -void EventManager::queueEvent(std::unique_ptr&& event_, int eventId) -{ - m_eventsQueue.emplace_back(std::move(event_), eventId); +void EventManager::queueEvent(std::unique_ptr && event_, int eventId) { + m_eventsQueue.emplace_back(std::move(event_), eventId); } -void EventManager::dispatchEvents() -{ - for (std::vector, int>>::iterator eventIt = m_eventsQueue.begin(); eventIt != m_eventsQueue.end();) { - if (!eventIt->first.get()->getHandled()) { - triggerEvent(*eventIt->first.get(), eventIt->second); - eventIt = m_eventsQueue.erase(eventIt); - } else { - ++eventIt; - } - } +void EventManager::dispatchEvents() { + for (std::vector, int>>::iterator eventIt + = m_eventsQueue.begin(); + eventIt != m_eventsQueue.end();) { + if (!eventIt->first.get()->getHandled()) { + triggerEvent(*eventIt->first.get(), eventIt->second); + eventIt = m_eventsQueue.erase(eventIt); + } else { + ++eventIt; + } + } } diff --git a/mwe/events/src/keyCodes.cpp b/mwe/events/src/keyCodes.cpp index 6f35d11..5f3aa96 100644 --- a/mwe/events/src/keyCodes.cpp +++ b/mwe/events/src/keyCodes.cpp @@ -1,140 +1,139 @@ #include "keyCodes.h" -const std::unordered_map sdlToCustom = { - { SDLK_SPACE, Space }, - { SDLK_QUOTE, Apostrophe }, - { SDLK_COMMA, Comma }, - { SDLK_MINUS, Minus }, - { SDLK_PERIOD, Period }, - { SDLK_SLASH, Slash }, +const std::unordered_map sdlToCustom + = {{SDLK_SPACE, Space}, + {SDLK_QUOTE, Apostrophe}, + {SDLK_COMMA, Comma}, + {SDLK_MINUS, Minus}, + {SDLK_PERIOD, Period}, + {SDLK_SLASH, Slash}, - { SDLK_0, D0 }, - { SDLK_1, D1 }, - { SDLK_2, D2 }, - { SDLK_3, D3 }, - { SDLK_4, D4 }, - { SDLK_5, D5 }, - { SDLK_6, D6 }, - { SDLK_7, D7 }, - { SDLK_8, D8 }, - { SDLK_9, D9 }, + {SDLK_0, D0}, + {SDLK_1, D1}, + {SDLK_2, D2}, + {SDLK_3, D3}, + {SDLK_4, D4}, + {SDLK_5, D5}, + {SDLK_6, D6}, + {SDLK_7, D7}, + {SDLK_8, D8}, + {SDLK_9, D9}, - { SDLK_SEMICOLON, Semicolon }, - { SDLK_EQUALS, Equal }, + {SDLK_SEMICOLON, Semicolon}, + {SDLK_EQUALS, Equal}, - { SDLK_a, A }, - { SDLK_b, B }, - { SDLK_c, C }, - { SDLK_d, D }, - { SDLK_e, E }, - { SDLK_f, F }, - { SDLK_g, G }, - { SDLK_h, H }, - { SDLK_i, I }, - { SDLK_j, J }, - { SDLK_k, K }, - { SDLK_l, L }, - { SDLK_m, M }, - { SDLK_n, N }, - { SDLK_o, O }, - { SDLK_p, P }, - { SDLK_q, Q }, - { SDLK_r, R }, - { SDLK_s, S }, - { SDLK_t, T }, - { SDLK_u, U }, - { SDLK_v, V }, - { SDLK_w, W }, - { SDLK_x, X }, - { SDLK_y, Y }, - { SDLK_z, Z }, + {SDLK_a, A}, + {SDLK_b, B}, + {SDLK_c, C}, + {SDLK_d, D}, + {SDLK_e, E}, + {SDLK_f, F}, + {SDLK_g, G}, + {SDLK_h, H}, + {SDLK_i, I}, + {SDLK_j, J}, + {SDLK_k, K}, + {SDLK_l, L}, + {SDLK_m, M}, + {SDLK_n, N}, + {SDLK_o, O}, + {SDLK_p, P}, + {SDLK_q, Q}, + {SDLK_r, R}, + {SDLK_s, S}, + {SDLK_t, T}, + {SDLK_u, U}, + {SDLK_v, V}, + {SDLK_w, W}, + {SDLK_x, X}, + {SDLK_y, Y}, + {SDLK_z, Z}, - { SDLK_LEFTBRACKET, LeftBracket }, - { SDLK_BACKSLASH, Backslash }, - { SDLK_RIGHTBRACKET, RightBracket }, - { SDLK_BACKQUOTE, GraveAccent }, + {SDLK_LEFTBRACKET, LeftBracket}, + {SDLK_BACKSLASH, Backslash}, + {SDLK_RIGHTBRACKET, RightBracket}, + {SDLK_BACKQUOTE, GraveAccent}, - { SDLK_ESCAPE, Escape }, - { SDLK_RETURN, Enter }, - { SDLK_TAB, Tab }, - { SDLK_BACKSPACE, Backspace }, - { SDLK_INSERT, Insert }, - { SDLK_DELETE, Delete }, - { SDLK_RIGHT, Right }, - { SDLK_LEFT, Left }, - { SDLK_DOWN, Down }, - { SDLK_UP, Up }, - { SDLK_PAGEUP, PageUp }, - { SDLK_PAGEDOWN, PageDown }, - { SDLK_HOME, Home }, - { SDLK_END, End }, + {SDLK_ESCAPE, Escape}, + {SDLK_RETURN, Enter}, + {SDLK_TAB, Tab}, + {SDLK_BACKSPACE, Backspace}, + {SDLK_INSERT, Insert}, + {SDLK_DELETE, Delete}, + {SDLK_RIGHT, Right}, + {SDLK_LEFT, Left}, + {SDLK_DOWN, Down}, + {SDLK_UP, Up}, + {SDLK_PAGEUP, PageUp}, + {SDLK_PAGEDOWN, PageDown}, + {SDLK_HOME, Home}, + {SDLK_END, End}, - { SDLK_CAPSLOCK, CapsLock }, - { SDLK_SCROLLLOCK, ScrollLock }, - { SDLK_NUMLOCKCLEAR, NumLock }, - { SDLK_PRINTSCREEN, PrintScreen }, - { SDLK_PAUSE, Pause }, + {SDLK_CAPSLOCK, CapsLock}, + {SDLK_SCROLLLOCK, ScrollLock}, + {SDLK_NUMLOCKCLEAR, NumLock}, + {SDLK_PRINTSCREEN, PrintScreen}, + {SDLK_PAUSE, Pause}, - { SDLK_F1, F1 }, - { SDLK_F2, F2 }, - { SDLK_F3, F3 }, - { SDLK_F4, F4 }, - { SDLK_F5, F5 }, - { SDLK_F6, F6 }, - { SDLK_F7, F7 }, - { SDLK_F8, F8 }, - { SDLK_F9, F9 }, - { SDLK_F10, F10 }, - { SDLK_F11, F11 }, - { SDLK_F12, F12 }, - { SDLK_F13, F13 }, - { SDLK_F14, F14 }, - { SDLK_F15, F15 }, - { SDLK_F16, F16 }, - { SDLK_F17, F17 }, - { SDLK_F18, F18 }, - { SDLK_F19, F19 }, - { SDLK_F20, F20 }, - { SDLK_F21, F21 }, - { SDLK_F22, F22 }, - { SDLK_F23, F23 }, - { SDLK_F24, F24 }, + {SDLK_F1, F1}, + {SDLK_F2, F2}, + {SDLK_F3, F3}, + {SDLK_F4, F4}, + {SDLK_F5, F5}, + {SDLK_F6, F6}, + {SDLK_F7, F7}, + {SDLK_F8, F8}, + {SDLK_F9, F9}, + {SDLK_F10, F10}, + {SDLK_F11, F11}, + {SDLK_F12, F12}, + {SDLK_F13, F13}, + {SDLK_F14, F14}, + {SDLK_F15, F15}, + {SDLK_F16, F16}, + {SDLK_F17, F17}, + {SDLK_F18, F18}, + {SDLK_F19, F19}, + {SDLK_F20, F20}, + {SDLK_F21, F21}, + {SDLK_F22, F22}, + {SDLK_F23, F23}, + {SDLK_F24, F24}, - { SDLK_KP_0, KP0 }, - { SDLK_KP_1, KP1 }, - { SDLK_KP_2, KP2 }, - { SDLK_KP_3, KP3 }, - { SDLK_KP_4, KP4 }, - { SDLK_KP_5, KP5 }, - { SDLK_KP_6, KP6 }, - { SDLK_KP_7, KP7 }, - { SDLK_KP_8, KP8 }, - { SDLK_KP_9, KP9 }, + {SDLK_KP_0, KP0}, + {SDLK_KP_1, KP1}, + {SDLK_KP_2, KP2}, + {SDLK_KP_3, KP3}, + {SDLK_KP_4, KP4}, + {SDLK_KP_5, KP5}, + {SDLK_KP_6, KP6}, + {SDLK_KP_7, KP7}, + {SDLK_KP_8, KP8}, + {SDLK_KP_9, KP9}, - { SDLK_KP_DECIMAL, KPDecimal }, - { SDLK_KP_DIVIDE, KPDivide }, - { SDLK_KP_MULTIPLY, KPMultiply }, - { SDLK_KP_MINUS, KPSubtract }, - { SDLK_KP_PLUS, KPAdd }, - { SDLK_KP_ENTER, KPEnter }, - { SDLK_KP_EQUALS, KPEqual }, + {SDLK_KP_DECIMAL, KPDecimal}, + {SDLK_KP_DIVIDE, KPDivide}, + {SDLK_KP_MULTIPLY, KPMultiply}, + {SDLK_KP_MINUS, KPSubtract}, + {SDLK_KP_PLUS, KPAdd}, + {SDLK_KP_ENTER, KPEnter}, + {SDLK_KP_EQUALS, KPEqual}, - { SDLK_LSHIFT, LeftShift }, - { SDLK_LCTRL, LeftControl }, - { SDLK_LALT, LeftAlt }, - { SDLK_LGUI, LeftSuper }, + {SDLK_LSHIFT, LeftShift}, + {SDLK_LCTRL, LeftControl}, + {SDLK_LALT, LeftAlt}, + {SDLK_LGUI, LeftSuper}, - { SDLK_RSHIFT, RightShift }, - { SDLK_RCTRL, RightControl }, - { SDLK_RALT, RightAlt }, - { SDLK_RGUI, RightSuper }, + {SDLK_RSHIFT, RightShift}, + {SDLK_RCTRL, RightControl}, + {SDLK_RALT, RightAlt}, + {SDLK_RGUI, RightSuper}, - { SDLK_MENU, Menu } -}; + {SDLK_MENU, Menu}}; Keycode getCustomKey(SDL_Keycode sdlKey) { - auto it = sdlToCustom.find(sdlKey); - if (it != sdlToCustom.end()) { - return it->second; - } - return 0; + auto it = sdlToCustom.find(sdlKey); + if (it != sdlToCustom.end()) { + return it->second; + } + return 0; } diff --git a/mwe/events/src/loopManager.cpp b/mwe/events/src/loopManager.cpp index 6defda0..8ecb932 100644 --- a/mwe/events/src/loopManager.cpp +++ b/mwe/events/src/loopManager.cpp @@ -12,17 +12,15 @@ void LoopManager::processInput() { triggerEvent(KeyPressedEvent(getCustomKey(event.key.keysym.sym))); break; case SDL_MOUSEBUTTONDOWN: - int x, y; - SDL_GetMouseState(&x, &y); - triggerEvent(MousePressedEvent(x, y)); - break; + int x, y; + SDL_GetMouseState(&x, &y); + triggerEvent(MousePressedEvent(x, y)); + break; } } -void LoopManager::setRunning(bool running){ - this->gameRunning = running; -} -void LoopManager::fixedUpdate() { - //fprintf(stderr, "fixed update\n"); +void LoopManager::setRunning(bool running) { this->gameRunning = running; } +void LoopManager::fixedUpdate() { + //fprintf(stderr, "fixed update\n"); } void LoopManager::loop() { LoopTimer & timer = LoopTimer::getInstance(); @@ -45,30 +43,29 @@ void LoopManager::loop() { window.destroyWindow(); } -void onKey(const KeyPressedEvent& e) -{ - int keyCode = e.getKeyCode(); +void onKey(const KeyPressedEvent & e) { + int keyCode = e.getKeyCode(); std::cout << "keycode pressed: " << keyCode << std::endl; - } -void onMouse(const MousePressedEvent& e){ - fprintf(stderr, "mouse Position X: %d Y: %d\n", e.getMousePosition().first, e.getMousePosition().second); +void onMouse(const MousePressedEvent & e) { + fprintf(stderr, "mouse Position X: %d Y: %d\n", e.getMousePosition().first, + e.getMousePosition().second); } void LoopManager::setup() { gameRunning = window.initWindow(); LoopTimer::getInstance().start(); LoopTimer::getInstance().setFPS(50); EventHandler callback = onKey; - subscribe(callback,false); + subscribe(callback, false); EventHandler mouseCallback = onMouse; - subscribe(mouseCallback ,false); - EventHandler closeWindowCallback = [this](const KeyPressedEvent& e) { - if(e.getKeyCode() == Escape){ - this->setRunning(false); - } - }; - subscribe(closeWindowCallback,false); - + subscribe(mouseCallback, false); + EventHandler closeWindowCallback + = [this](const KeyPressedEvent & e) { + if (e.getKeyCode() == Escape) { + this->setRunning(false); + } + }; + subscribe(closeWindowCallback, false); } void LoopManager::render() { //fprintf(stderr, "**********render********** \n"); diff --git a/mwe/events/src/main.cpp b/mwe/events/src/main.cpp index 3940c60..55c562f 100644 --- a/mwe/events/src/main.cpp +++ b/mwe/events/src/main.cpp @@ -1,50 +1,51 @@ +#include "event.h" +#include "loopManager.h" #include #include #include -#include "loopManager.h" -#include "event.h" class PlayerDamagedEvent : public Event { public: - PlayerDamagedEvent(int damage, int playerID) - : Event("PlayerDamaged"), damage(damage), playerID(playerID) {} + PlayerDamagedEvent(int damage, int playerID) + : Event("PlayerDamaged"), damage(damage), playerID(playerID) {} - REGISTER_EVENT_TYPE(PlayerDamagedEvent); + REGISTER_EVENT_TYPE(PlayerDamagedEvent); - int getDamage() const { return damage; } - int getPlayerID() const { return playerID; } + int getDamage() const { return damage; } + int getPlayerID() const { return playerID; } private: - int damage; - int playerID; + int damage; + int playerID; }; -void onPlayerDamaged(const PlayerDamagedEvent& e) { - std::cout << "Player " << e.getPlayerID() << " took " << e.getDamage() << " damage." << std::endl; +void onPlayerDamaged(const PlayerDamagedEvent & e) { + std::cout << "Player " << e.getPlayerID() << " took " << e.getDamage() + << " damage." << std::endl; } -void onKeyPressed(const KeyPressedEvent& e) -{ - const int keyCode = e.getKeyCode(); - fprintf(stderr,"KeyCode %d\n",keyCode); +void onKeyPressed(const KeyPressedEvent & e) { + const int keyCode = e.getKeyCode(); + fprintf(stderr, "KeyCode %d\n", keyCode); } -int main(int argc, char* args[]) { +int main(int argc, char * args[]) { LoopManager gameLoop; // Create an event handler for KeyPressedEvent - // EventHandler callback = [](const KeyPressedEvent& e) { - // onKeyPressed(e); - // }; + // EventHandler callback = [](const KeyPressedEvent& e) { + // onKeyPressed(e); + // }; // custom event class poc EventHandler playerDamagedHandler = onPlayerDamaged; subscribe(playerDamagedHandler); triggerEvent(PlayerDamagedEvent(50, 1)); //EventHandler callback = onKeyPressed; - //subscribe(callback,false); - std::unique_ptr anotherKeyPressEvent = std::make_unique(65); - queueEvent(std::move(anotherKeyPressEvent)); - triggerEvent(KeyPressedEvent(42)); + //subscribe(callback,false); + std::unique_ptr anotherKeyPressEvent + = std::make_unique(65); + queueEvent(std::move(anotherKeyPressEvent)); + triggerEvent(KeyPressedEvent(42)); EventManager::getInstance().dispatchEvents(); gameLoop.setup(); gameLoop.loop(); - return 0; + return 0; } diff --git a/mwe/events/src/timer.cpp b/mwe/events/src/timer.cpp index f4fee23..0b89bf5 100644 --- a/mwe/events/src/timer.cpp +++ b/mwe/events/src/timer.cpp @@ -16,8 +16,7 @@ void LoopTimer::start() { // Update the timer, calculate deltaTime void LoopTimer::update() { uint64_t currentFrameTime = SDL_GetTicks64(); - deltaTime - = (currentFrameTime - lastFrameTime) / 1000.0; + deltaTime = (currentFrameTime - lastFrameTime) / 1000.0; if (deltaTime > maximumDeltaTime) { deltaTime = maximumDeltaTime; diff --git a/mwe/events/src/window.cpp b/mwe/events/src/window.cpp index 9cafd57..41eb85f 100644 --- a/mwe/events/src/window.cpp +++ b/mwe/events/src/window.cpp @@ -1,6 +1,5 @@ #include "window.h" -WindowManager::WindowManager() { -} +WindowManager::WindowManager() {} WindowManager::~WindowManager() { destroyWindow(); } SDL_Renderer * WindowManager::getRenderer() { return renderer; } diff --git a/mwe/gameloop/include/eventManager.h b/mwe/gameloop/include/eventManager.h index ba1ca32..a1b9f92 100644 --- a/mwe/gameloop/include/eventManager.h +++ b/mwe/gameloop/include/eventManager.h @@ -1,5 +1,5 @@ #pragma once class EventManager { - public: - EventManager(); +public: + EventManager(); }; diff --git a/mwe/gameloop/include/loopManager.h b/mwe/gameloop/include/loopManager.h index dcbfedc..1befb69 100644 --- a/mwe/gameloop/include/loopManager.h +++ b/mwe/gameloop/include/loopManager.h @@ -1,8 +1,8 @@ #pragma once +#include "event.h" #include "gameObject.h" #include "window.h" #include -#include "event.h" class LoopManager { public: LoopManager(); diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h index a53ace5..3e72a49 100644 --- a/src/crepe/api/AssetManager.h +++ b/src/crepe/api/AssetManager.h @@ -26,10 +26,10 @@ public: public: template - std::shared_ptr cache(const std::string & file_path, bool reload = false); + std::shared_ptr cache(const std::string & file_path, + bool reload = false); }; } // namespace crepe::api #include "AssetManager.hpp" - diff --git a/src/crepe/api/AssetManager.hpp b/src/crepe/api/AssetManager.hpp index 083fd9d..468724c 100644 --- a/src/crepe/api/AssetManager.hpp +++ b/src/crepe/api/AssetManager.hpp @@ -5,7 +5,8 @@ namespace crepe::api { template -std::shared_ptr AssetManager::cache(const std::string & file_path, bool reload) { +std::shared_ptr AssetManager::cache(const std::string & file_path, + bool reload) { auto it = asset_cache.find(file_path); if (!reload && it != asset_cache.end()) { diff --git a/src/crepe/api/Color.h b/src/crepe/api/Color.h index 36d91ef..e818de4 100644 --- a/src/crepe/api/Color.h +++ b/src/crepe/api/Color.h @@ -6,6 +6,7 @@ class Color { // FIXME: can't these colors be defined as a `static constexpr const Color` // instead? + public: Color(double red, double green, double blue, double alpha); static const Color & get_white(); diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 24b5529..8a7f268 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -7,6 +7,7 @@ namespace crepe::api { class Config { private: Config() = default; + public: ~Config() = default; @@ -36,5 +37,4 @@ public: } log; }; -} - +} // namespace crepe::api diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index c17ae34..c69ec61 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -12,6 +12,7 @@ class Transform : public Component { // FIXME: What's the difference between the `Point` and `Position` // classes/structs? How about we replace both with a universal `Vec2` that // works similar (or the same) as those found in GLSL? + public: Transform(uint32_t id, Point &, double, double); ~Transform(); diff --git a/src/crepe/util/color.cpp b/src/crepe/util/color.cpp index bdf5809..a7bbc81 100644 --- a/src/crepe/util/color.cpp +++ b/src/crepe/util/color.cpp @@ -1,8 +1,8 @@ #include +#include "../api/Config.h" #include "color.h" #include "fmt.h" -#include "../api/Config.h" using namespace crepe::util; using namespace std; @@ -41,20 +41,51 @@ LogColor & LogColor::reset() { return *this; } -LogColor & LogColor::fg_black(bool bright) { return this->add_code(bright ? 90 : 30); } -LogColor & LogColor::fg_red(bool bright) { return this->add_code(bright ? 91 : 31); } -LogColor & LogColor::fg_green(bool bright) { return this->add_code(bright ? 92 : 32); } -LogColor & LogColor::fg_yellow(bool bright) { return this->add_code(bright ? 93 : 33); } -LogColor & LogColor::fg_blue(bool bright) { return this->add_code(bright ? 94 : 34); } -LogColor & LogColor::fg_magenta(bool bright) { return this->add_code(bright ? 95 : 35); } -LogColor & LogColor::fg_cyan(bool bright) { return this->add_code(bright ? 96 : 36); } -LogColor & LogColor::fg_white(bool bright) { return this->add_code(bright ? 97 : 37); } -LogColor & LogColor::bg_black(bool bright) { return this->add_code(bright ? 100 : 40); } -LogColor & LogColor::bg_red(bool bright) { return this->add_code(bright ? 101 : 41); } -LogColor & LogColor::bg_green(bool bright) { return this->add_code(bright ? 102 : 42); } -LogColor & LogColor::bg_yellow(bool bright) { return this->add_code(bright ? 103 : 43); } -LogColor & LogColor::bg_blue(bool bright) { return this->add_code(bright ? 104 : 44); } -LogColor & LogColor::bg_magenta(bool bright) { return this->add_code(bright ? 105 : 45); } -LogColor & LogColor::bg_cyan(bool bright) { return this->add_code(bright ? 106 : 46); } -LogColor & LogColor::bg_white(bool bright) { return this->add_code(bright ? 107 : 47); } - +LogColor & LogColor::fg_black(bool bright) { + return this->add_code(bright ? 90 : 30); +} +LogColor & LogColor::fg_red(bool bright) { + return this->add_code(bright ? 91 : 31); +} +LogColor & LogColor::fg_green(bool bright) { + return this->add_code(bright ? 92 : 32); +} +LogColor & LogColor::fg_yellow(bool bright) { + return this->add_code(bright ? 93 : 33); +} +LogColor & LogColor::fg_blue(bool bright) { + return this->add_code(bright ? 94 : 34); +} +LogColor & LogColor::fg_magenta(bool bright) { + return this->add_code(bright ? 95 : 35); +} +LogColor & LogColor::fg_cyan(bool bright) { + return this->add_code(bright ? 96 : 36); +} +LogColor & LogColor::fg_white(bool bright) { + return this->add_code(bright ? 97 : 37); +} +LogColor & LogColor::bg_black(bool bright) { + return this->add_code(bright ? 100 : 40); +} +LogColor & LogColor::bg_red(bool bright) { + return this->add_code(bright ? 101 : 41); +} +LogColor & LogColor::bg_green(bool bright) { + return this->add_code(bright ? 102 : 42); +} +LogColor & LogColor::bg_yellow(bool bright) { + return this->add_code(bright ? 103 : 43); +} +LogColor & LogColor::bg_blue(bool bright) { + return this->add_code(bright ? 104 : 44); +} +LogColor & LogColor::bg_magenta(bool bright) { + return this->add_code(bright ? 105 : 45); +} +LogColor & LogColor::bg_cyan(bool bright) { + return this->add_code(bright ? 106 : 46); +} +LogColor & LogColor::bg_white(bool bright) { + return this->add_code(bright ? 107 : 47); +} diff --git a/src/crepe/util/color.h b/src/crepe/util/color.h index 66f3a28..91e1abe 100644 --- a/src/crepe/util/color.h +++ b/src/crepe/util/color.h @@ -48,4 +48,4 @@ private: std::string final = ""; }; -} // namespace crepe::util::color +} // namespace crepe::util diff --git a/src/crepe/util/log.cpp b/src/crepe/util/log.cpp index 5ff2ad4..6bcc4ae 100644 --- a/src/crepe/util/log.cpp +++ b/src/crepe/util/log.cpp @@ -3,20 +3,25 @@ #include #include +#include "../api/Config.h" #include "fmt.h" #include "log.h" -#include "../api/Config.h" using namespace crepe::util; using namespace std; string log_prefix(LogLevel level) { switch (level) { - case LogLevel::TRACE: return LogColor().fg_white().str("[TRACE]") + " "; - case LogLevel::DEBUG: return LogColor().fg_magenta().str("[DEBUG]") + " "; - case LogLevel::INFO: return LogColor().fg_blue().str("[INFO]") + " "; - case LogLevel::WARNING: return LogColor().fg_yellow().str("[WARN]") + " "; - case LogLevel::ERROR: return LogColor().fg_red().str("[ERROR]") + " "; + case LogLevel::TRACE: + return LogColor().fg_white().str("[TRACE]") + " "; + case LogLevel::DEBUG: + return LogColor().fg_magenta().str("[DEBUG]") + " "; + case LogLevel::INFO: + return LogColor().fg_blue().str("[INFO]") + " "; + case LogLevel::WARNING: + return LogColor().fg_yellow().str("[WARN]") + " "; + case LogLevel::ERROR: + return LogColor().fg_red().str("[ERROR]") + " "; } return ""; } @@ -46,4 +51,3 @@ void crepe::util::logf(LogLevel level, const char * fmt, ...) { log(level, va_stringf(args, fmt)); va_end(args); } - diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h index 5ee67eb..308ba96 100644 --- a/src/crepe/util/log.h +++ b/src/crepe/util/log.h @@ -6,12 +6,19 @@ #include "color.h" // utility macros -#define _crepe_logf_here(level, format, ...) crepe::util::logf(level, "%s" format, crepe::util::LogColor().fg_white(false).fmt("%s (%s:%d)", __PRETTY_FUNCTION__, __FILE_NAME__, __LINE__), __VA_ARGS__) +#define _crepe_logf_here(level, format, ...) \ + crepe::util::logf( \ + level, "%s" format, \ + crepe::util::LogColor().fg_white(false).fmt( \ + "%s (%s:%d)", __PRETTY_FUNCTION__, __FILE_NAME__, __LINE__), \ + __VA_ARGS__) // very illegal global function-style macros // NOLINTBEGIN -#define dbg_logf(fmt, ...) _crepe_logf_here(crepe::util::LogLevel::DEBUG, ": " fmt, __VA_ARGS__) -#define dbg_log(str) _crepe_logf_here(crepe::util::LogLevel::DEBUG, "%s: " str, "") +#define dbg_logf(fmt, ...) \ + _crepe_logf_here(crepe::util::LogLevel::DEBUG, ": " fmt, __VA_ARGS__) +#define dbg_log(str) \ + _crepe_logf_here(crepe::util::LogLevel::DEBUG, "%s: " str, "") #define dbg_trace() _crepe_logf_here(crepe::util::LogLevel::TRACE, "%s", "") // NOLINTEND diff --git a/src/example/log.cpp b/src/example/log.cpp index 86ef193..3bc6d80 100644 --- a/src/example/log.cpp +++ b/src/example/log.cpp @@ -3,8 +3,8 @@ * Standalone example for usage of the logging functions */ -#include #include +#include using namespace crepe; using namespace crepe::util; @@ -22,4 +22,3 @@ int main() { return 0; } - diff --git a/src/example/script.cpp b/src/example/script.cpp index f737a90..d0cc121 100644 --- a/src/example/script.cpp +++ b/src/example/script.cpp @@ -7,8 +7,8 @@ #include #include -#include #include +#include #include #include diff --git a/src/makefile b/src/makefile index c1ef601..3f74a2a 100644 --- a/src/makefile +++ b/src/makefile @@ -2,5 +2,7 @@ FMT += $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp') format: FORCE - clang-tidy -p build/compile_commands.json --fix-errors $(FMT) + # clang-tidy -p build/compile_commands.json --fix-errors $(FMT) + +# TODO: re-enable linter after 2024-11-10 -- cgit v1.2.3