aboutsummaryrefslogtreecommitdiff
path: root/mwe/events/src
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-18 14:27:58 +0100
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-18 14:27:58 +0100
commit03e493012edc106b00bc5676c1830ce2085a7ff9 (patch)
treeff06d4314552e82f98d08c52d7a15edb3a74436e /mwe/events/src
parent21f5b66d0bcc13d903800adf3bb614e380ff8591 (diff)
parent121b64b1cb6cfead5814070c8b0185d3d7308095 (diff)
Merge remote-tracking branch 'origin/master' into niels/sorting_rendering
Diffstat (limited to 'mwe/events/src')
-rw-r--r--mwe/events/src/event.cpp35
-rw-r--r--mwe/events/src/eventManager.cpp41
-rw-r--r--mwe/events/src/iKeyListener.cpp8
-rw-r--r--mwe/events/src/iMouseListener.cpp17
-rw-r--r--mwe/events/src/inputSystem.cpp14
-rw-r--r--mwe/events/src/keyListenerTest.cpp4
-rw-r--r--mwe/events/src/loopManager.cpp21
-rw-r--r--mwe/events/src/main.cpp7
-rw-r--r--mwe/events/src/mouseListenerTest.cpp12
-rw-r--r--mwe/events/src/timer.cpp3
-rw-r--r--mwe/events/src/uiRenderer.cpp13
-rw-r--r--mwe/events/src/window.cpp9
12 files changed, 62 insertions, 122 deletions
diff --git a/mwe/events/src/event.cpp b/mwe/events/src/event.cpp
index 8ffa0b1..0040c73 100644
--- a/mwe/events/src/event.cpp
+++ b/mwe/events/src/event.cpp
@@ -8,14 +8,11 @@ void Event::addArgument(const std::string & key,
eventData[key] = value;
}
-std::variant<int, std::string, float>
-Event::getArgument(const std::string & key) const {
+std::variant<int, std::string, float> Event::getArgument(const std::string & key) const {
return eventData.at(key);
}
-std::string Event::getType() const {
- return std::get<std::string>(eventData.at("eventType"));
-}
+std::string Event::getType() const { return std::get<std::string>(eventData.at("eventType")); }
std::string Event::toString() const { return std::to_string(getEventType()); }
bool Event::getHandled() const { return isHandled; }
@@ -32,9 +29,7 @@ Keycode KeyPressedEvent::getKeyCode() const { return key; }
int KeyPressedEvent::getRepeatCount() const { return repeatCount; }
// KeyReleasedEvent class methods
-KeyReleasedEvent::KeyReleasedEvent(int keycode)
- : Event("KeyReleasedEvent"),
- key(keycode) {}
+KeyReleasedEvent::KeyReleasedEvent(int keycode) : Event("KeyReleasedEvent"), key(keycode) {}
Keycode KeyReleasedEvent::getKeyCode() const { return key; }
@@ -44,22 +39,16 @@ MousePressedEvent::MousePressedEvent(int mouseX, int mouseY)
mouseX(mouseX),
mouseY(mouseY) {}
-std::pair<int, int> MousePressedEvent::getMousePosition() const {
- return {mouseX, mouseY};
-}
+std::pair<int, int> MousePressedEvent::getMousePosition() const { return {mouseX, mouseY}; }
//Collision event
CollisionEvent::CollisionEvent(Collision collision)
: collisionData(collision),
Event("CollisionEvent") {}
-Collision CollisionEvent::getCollisionData() const {
- return this->collisionData;
-}
+Collision CollisionEvent::getCollisionData() const { return this->collisionData; }
-TextSubmitEvent::TextSubmitEvent(std::string text)
- : text(text),
- Event("TextSubmitEvent") {}
+TextSubmitEvent::TextSubmitEvent(std::string text) : text(text), Event("TextSubmitEvent") {}
std::string TextSubmitEvent::getText() const { return this->text; }
@@ -68,9 +57,7 @@ MouseReleasedEvent::MouseReleasedEvent(int x, int y, MouseButton button)
mouseY(y),
button(button),
Event("MouseReleased") {}
-std::pair<int, int> MouseReleasedEvent::getMousePosition() const {
- return {mouseX, mouseY};
-}
+std::pair<int, int> MouseReleasedEvent::getMousePosition() const { return {mouseX, mouseY}; }
MouseClickEvent::MouseClickEvent(int x, int y, MouseButton button)
: mouseX(x),
mouseY(y),
@@ -80,9 +67,5 @@ MouseMovedEvent::MouseMovedEvent(int x, int y)
: mouseX(x),
mouseY(y),
Event("MouseMovedEvent") {}
-std::pair<int, int> MouseClickEvent::getMousePosition() const {
- return {mouseX, mouseY};
-}
-std::pair<int, int> MouseMovedEvent::getMousePosition() const {
- return {mouseX, mouseY};
-}
+std::pair<int, int> MouseClickEvent::getMousePosition() const { return {mouseX, mouseY}; }
+std::pair<int, int> MouseMovedEvent::getMousePosition() const { return {mouseX, mouseY}; }
diff --git a/mwe/events/src/eventManager.cpp b/mwe/events/src/eventManager.cpp
index 34a093d..9e7d880 100644
--- a/mwe/events/src/eventManager.cpp
+++ b/mwe/events/src/eventManager.cpp
@@ -2,33 +2,27 @@
void EventManager::shutdown() { subscribers.clear(); }
-void EventManager::subscribe(int eventType,
- std::unique_ptr<IEventHandlerWrapper> && handler,
+void EventManager::subscribe(int eventType, std::unique_ptr<IEventHandlerWrapper> && handler,
int eventId) {
if (eventId) {
std::unordered_map<
- int, std::unordered_map<
- int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>>::
+ int, std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>>::
iterator subscribers
= subscribersByEventId.find(eventType);
if (subscribers != subscribersByEventId.end()) {
- std::unordered_map<
- int, std::vector<std::unique_ptr<IEventHandlerWrapper>>> &
+ std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>> &
handlersMap
= subscribers->second;
std::unordered_map<
- int,
- std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator
- handlers
+ int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator handlers
= handlersMap.find(eventId);
if (handlers != handlersMap.end()) {
handlers->second.emplace_back(std::move(handler));
return;
}
}
- subscribersByEventId[eventType][eventId].emplace_back(
- std::move(handler));
+ subscribersByEventId[eventType][eventId].emplace_back(std::move(handler));
} else {
auto & handlers = subscribers[eventType];
@@ -36,29 +30,23 @@ void EventManager::subscribe(int eventType,
}
}
-void EventManager::unsubscribe(int eventType, const std::string & handlerName,
- int eventId) {
+void EventManager::unsubscribe(int eventType, const std::string & handlerName, int eventId) {
if (eventId) {
std::unordered_map<
- int, std::unordered_map<
- int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>>::
+ int, std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>>::
iterator subscriberList
= subscribersByEventId.find(eventType);
if (subscriberList != subscribersByEventId.end()) {
- std::unordered_map<
- int, std::vector<std::unique_ptr<IEventHandlerWrapper>>> &
+ std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>> &
handlersMap
= subscriberList->second;
std::unordered_map<
- int,
- std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator
- handlers
+ int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator handlers
= handlersMap.find(eventId);
if (handlers != handlersMap.end()) {
std::vector<std::unique_ptr<IEventHandlerWrapper>> & callbacks
= handlers->second;
- for (std::vector<
- std::unique_ptr<IEventHandlerWrapper>>::iterator it
+ for (std::vector<std::unique_ptr<IEventHandlerWrapper>>::iterator it
= callbacks.begin();
it != callbacks.end(); ++it) {
if (it->get()->getType() == handlerName) {
@@ -69,13 +57,11 @@ void EventManager::unsubscribe(int eventType, const std::string & handlerName,
}
}
} else {
- std::unordered_map<
- int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator
+ std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator
handlersIt
= subscribers.find(eventType);
if (handlersIt != subscribers.end()) {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> & handlers
- = handlersIt->second;
+ std::vector<std::unique_ptr<IEventHandlerWrapper>> & handlers = handlersIt->second;
for (std::vector<std::unique_ptr<IEventHandlerWrapper>>::iterator it
= handlers.begin();
it != handlers.end(); ++it) {
@@ -90,8 +76,7 @@ void EventManager::unsubscribe(int eventType, const std::string & handlerName,
void EventManager::triggerEvent(const Event & event_, int eventId) {
if (eventId > 0) {
- auto handlersIt
- = subscribersByEventId[event_.getEventType()].find(eventId);
+ auto handlersIt = subscribersByEventId[event_.getEventType()].find(eventId);
if (handlersIt != subscribersByEventId[event_.getEventType()].end()) {
std::vector<std::unique_ptr<IEventHandlerWrapper>> & callbacks
= handlersIt->second;
diff --git a/mwe/events/src/iKeyListener.cpp b/mwe/events/src/iKeyListener.cpp
index 0235fab..d5d1d90 100644
--- a/mwe/events/src/iKeyListener.cpp
+++ b/mwe/events/src/iKeyListener.cpp
@@ -3,11 +3,9 @@
IKeyListener::~IKeyListener() { unsubscribeEvents(); }
void IKeyListener::subscribeEvents(int listenerId) {
- keyPressedHandler
- = [this](const KeyPressedEvent & event) { this->onKeyPressed(event); };
- keyReleasedHandler = [this](const KeyReleasedEvent & event) {
- this->onKeyReleased(event);
- };
+ keyPressedHandler = [this](const KeyPressedEvent & event) { this->onKeyPressed(event); };
+ keyReleasedHandler
+ = [this](const KeyReleasedEvent & event) { this->onKeyReleased(event); };
subscribe<KeyPressedEvent>(keyPressedHandler, listenerId);
subscribe<KeyReleasedEvent>(keyReleasedHandler, listenerId);
diff --git a/mwe/events/src/iMouseListener.cpp b/mwe/events/src/iMouseListener.cpp
index 69d8c9f..53e6f80 100644
--- a/mwe/events/src/iMouseListener.cpp
+++ b/mwe/events/src/iMouseListener.cpp
@@ -2,17 +2,12 @@
IMouseListener::~IMouseListener() { unsubscribeEvents(); }
void IMouseListener::subscribeEvents(int listenerId) {
- 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); };
+ 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<MouseClickEvent>(mouseClickHandler, listenerId);
subscribe<MousePressedEvent>(mousePressHandler, listenerId);
diff --git a/mwe/events/src/inputSystem.cpp b/mwe/events/src/inputSystem.cpp
index b87b12e..d740c9e 100644
--- a/mwe/events/src/inputSystem.cpp
+++ b/mwe/events/src/inputSystem.cpp
@@ -3,9 +3,7 @@
InputSystem::InputSystem() {}
void InputSystem::registerButton(Button * button) { buttons.push_back(button); }
-void InputSystem::registerTextInput(TextInput * input) {
- textInputs.push_back(input);
-}
+void InputSystem::registerTextInput(TextInput * input) { textInputs.push_back(input); }
void InputSystem::registerText(Text * label) { texts.push_back(label); }
void InputSystem::processInput() {
@@ -16,8 +14,7 @@ void InputSystem::processInput() {
triggerEvent(ShutDownEvent());
break;
case SDL_KEYDOWN:
- triggerEvent(
- KeyPressedEvent(getCustomKey(event.key.keysym.sym)));
+ triggerEvent(KeyPressedEvent(getCustomKey(event.key.keysym.sym)));
processKeyPress(event.key.keysym.sym);
break;
case SDL_TEXTINPUT:
@@ -37,15 +34,14 @@ void InputSystem::processInput() {
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)) {
+ if (mouseX >= button->x && mouseX <= (button->x + button->width) && mouseY >= button->y
+ && mouseY <= (button->y + button->height)) {
button->onClick();
}
}
for (auto * textInput : textInputs) {
if (mouseX >= textInput->x && mouseX <= textInput->x + textInput->width
- && mouseY >= textInput->y
- && mouseY <= textInput->y + textInput->height) {
+ && mouseY >= textInput->y && mouseY <= textInput->y + textInput->height) {
textInput->isActive = true;
} else {
textInput->isActive = false;
diff --git a/mwe/events/src/keyListenerTest.cpp b/mwe/events/src/keyListenerTest.cpp
index 8446dfa..f45dffd 100644
--- a/mwe/events/src/keyListenerTest.cpp
+++ b/mwe/events/src/keyListenerTest.cpp
@@ -1,8 +1,6 @@
#include "keyListenerTest.h"
-KeyListenerTest::KeyListenerTest(int listenerId) {
- subscribeEvents(listenerId);
-}
+KeyListenerTest::KeyListenerTest(int listenerId) { subscribeEvents(listenerId); }
KeyListenerTest::~KeyListenerTest() { unsubscribeEvents(); }
diff --git a/mwe/events/src/loopManager.cpp b/mwe/events/src/loopManager.cpp
index 0b7d888..c58a5e7 100644
--- a/mwe/events/src/loopManager.cpp
+++ b/mwe/events/src/loopManager.cpp
@@ -1,8 +1,7 @@
#include "loopManager.h"
LoopManager::LoopManager() : inputSystem(std::make_unique<InputSystem>()) {
- shutdownHandler
- = [this](const ShutDownEvent & event) { this->onShutdown(event); };
+ shutdownHandler = [this](const ShutDownEvent & event) { this->onShutdown(event); };
subscribe(shutdownHandler);
}
void LoopManager::processInput() {
@@ -63,17 +62,15 @@ void LoopManager::setup() {
subscribe<KeyPressedEvent>(callback, false);
EventHandler<MousePressedEvent> mouseCallback = onMouse;
subscribe<MousePressedEvent>(mouseCallback, false);
- EventHandler<KeyPressedEvent> closeWindowCallback
- = [this](const KeyPressedEvent & e) {
- if (e.getKeyCode() == Escape) {
- this->setRunning(false);
- }
- };
+ EventHandler<KeyPressedEvent> closeWindowCallback = [this](const KeyPressedEvent & e) {
+ if (e.getKeyCode() == Escape) {
+ this->setRunning(false);
+ }
+ };
subscribe<KeyPressedEvent>(closeWindowCallback, false);
Button * testButton = new Button(200, 200);
testButton->color = {100, 0, 100};
- testButton->onClick
- = []() { std::cout << "Button was clicked" << std::endl; };
+ testButton->onClick = []() { std::cout << "Button was clicked" << std::endl; };
testButton->x = 200;
testButton->y = 200;
inputSystem->registerButton(testButton);
@@ -93,9 +90,7 @@ void LoopManager::render() {
window.renderUIObjects();
}
}
-void LoopManager::onShutdown(const ShutDownEvent & e) {
- this->gameRunning = false;
-}
+void LoopManager::onShutdown(const ShutDownEvent & e) { this->gameRunning = false; }
void LoopManager::update() {
//fprintf(stderr, "**********normal update********** \n");
LoopTimer & timer = LoopTimer::getInstance();
diff --git a/mwe/events/src/main.cpp b/mwe/events/src/main.cpp
index f4e7390..ad0fd69 100644
--- a/mwe/events/src/main.cpp
+++ b/mwe/events/src/main.cpp
@@ -25,8 +25,8 @@ private:
int playerID;
};
void onPlayerDamaged(const PlayerDamagedEvent & e) {
- std::cout << "Player " << e.getPlayerID() << " took " << e.getDamage()
- << " damage." << std::endl;
+ std::cout << "Player " << e.getPlayerID() << " took " << e.getDamage() << " damage."
+ << std::endl;
}
void onKeyPressed1(const KeyPressedEvent & e) {
@@ -38,8 +38,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
+ std::cout << "collision betwee object id: " << e.getCollisionData().objectIdA
<< " and id: " << e.getCollisionData().objectIdB << std::endl;
}
void testCollisionEvent() {
diff --git a/mwe/events/src/mouseListenerTest.cpp b/mwe/events/src/mouseListenerTest.cpp
index a2a7e6d..7b35f4e 100644
--- a/mwe/events/src/mouseListenerTest.cpp
+++ b/mwe/events/src/mouseListenerTest.cpp
@@ -1,8 +1,6 @@
#include "mouseListenerTest.h"
-MouseListenerTest::MouseListenerTest(int listenerId) {
- subscribeEvents(listenerId);
-}
+MouseListenerTest::MouseListenerTest(int listenerId) { subscribeEvents(listenerId); }
MouseListenerTest::~MouseListenerTest() { unsubscribeEvents(); }
@@ -12,13 +10,13 @@ void MouseListenerTest::onMouseClicked(const MouseClickEvent & event) {
}
void MouseListenerTest::onMousePressed(const MousePressedEvent & event) {
- std::cout << "Mouse button pressed at: (" << event.getMousePosition().first
- << ", " << event.getMousePosition().second << ")" << std::endl;
+ 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;
+ std::cout << "Mouse button released at: (" << event.getMousePosition().first << ", "
+ << event.getMousePosition().second << ")" << std::endl;
}
void MouseListenerTest::onMouseMoved(const MouseMovedEvent & event) {
diff --git a/mwe/events/src/timer.cpp b/mwe/events/src/timer.cpp
index 0b89bf5..0f8339f 100644
--- a/mwe/events/src/timer.cpp
+++ b/mwe/events/src/timer.cpp
@@ -45,8 +45,7 @@ void LoopTimer::enforceFrameRate() {
double frameDuration = (currentFrameTime - lastFrameTime) / 1000.0;
if (frameDuration < frameTargetTime) {
- uint32_t delayTime
- = (uint32_t) ((frameTargetTime - frameDuration) * 1000.0);
+ uint32_t delayTime = (uint32_t) ((frameTargetTime - frameDuration) * 1000.0);
SDL_Delay(delayTime);
}
}
diff --git a/mwe/events/src/uiRenderer.cpp b/mwe/events/src/uiRenderer.cpp
index 9fec272..ca8d284 100644
--- a/mwe/events/src/uiRenderer.cpp
+++ b/mwe/events/src/uiRenderer.cpp
@@ -24,21 +24,18 @@ void UIRenderer::renderButton(Button * button) {
// 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_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;
+ std::cerr << "Error creating text surface: " << TTF_GetError() << std::endl;
return;
}
- SDL_Texture * textTexture
- = SDL_CreateTextureFromSurface(renderer, textSurface);
+ SDL_Texture * textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
if (!textTexture) {
- std::cerr << "Error creating texture from surface: "
- << SDL_GetError() << std::endl;
+ std::cerr << "Error creating texture from surface: " << SDL_GetError()
+ << std::endl;
SDL_FreeSurface(textSurface);
return;
}
diff --git a/mwe/events/src/window.cpp b/mwe/events/src/window.cpp
index f482b7f..af2b627 100644
--- a/mwe/events/src/window.cpp
+++ b/mwe/events/src/window.cpp
@@ -11,9 +11,8 @@ bool WindowManager::initWindow() {
return false;
}
- window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,
- SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
+ 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;
@@ -36,9 +35,7 @@ void WindowManager::destroyWindow() {
}
SDL_Renderer * WindowManager::getRenderer() { return renderer; }
-void WindowManager::addUIObject(UIObject * uiObject) {
- uiObjects.push_back(uiObject);
-}
+void WindowManager::addUIObject(UIObject * uiObject) { uiObjects.push_back(uiObject); }
void WindowManager::renderUIObjects() {
for (UIObject * obj : uiObjects) {