aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-12 08:58:08 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-12 08:58:08 +0100
commit44cab6ad59d257d949112dcd4a9e0bdc9302e92b (patch)
tree8b00630668082554ab8b9ca600cc40856e133184 /src
parent0f68177a0384d41a7feff36cc0e1869b6d2ac9aa (diff)
save
Diffstat (limited to 'src')
-rw-r--r--src/crepe/facade/SDLContext.cpp144
-rw-r--r--src/crepe/facade/SDLContext.h19
-rw-r--r--src/crepe/system/InputSystem.cpp53
-rw-r--r--src/test/EventTest.cpp26
4 files changed, 140 insertions, 102 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index 4552605..75ad0e8 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -1,3 +1,4 @@
+#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_blendmode.h>
#include <SDL2/SDL_image.h>
@@ -383,59 +384,82 @@ ivec2 SDLContext::get_size(const Texture & ctx) {
std::vector<SDLContext::EventData> SDLContext::get_events() {
std::vector<SDLContext::EventData> event_list;
SDL_Event event;
+ const CameraAuxiliaryData & cam = this->cam_aux_data;
while (SDL_PollEvent(&event)) {
ivec2 mouse_pos;
mouse_pos.x = (event.button.x - cam.bar_size.x) / cam.render_scale.x;
mouse_pos.y = (event.button.y - cam.bar_size.y) / cam.render_scale.y;
+ std::cout << "SDL mousePos X: " << mouse_pos.x << std::endl;
+ std::cout << "SDL mousePos Y: " << mouse_pos.y << std::endl;
switch (event.type) {
case SDL_QUIT:
- event_list.push_back({SDLContext::EventType::SHUTDOWN, {}, {}, {}});
+ event_list.push_back({.event_type = SDLContext::EventType::SHUTDOWN});
break;
case SDL_KEYDOWN:
- event_list.push_back(
- {SDLContext::EventType::KEYDOWN,
- {sdl_to_keycode(event.key.keysym.scancode), event.key.repeat != 0},
- {},
- {}});
+ {
+
+ EventData transfer_event;
+ transfer_event.event_type = SDLContext::EventType::KEYDOWN;
+ transfer_event.data.key_data = KeyData{
+ .key = sdl_to_keycode(event.key.keysym.scancode),
+ .key_repeat = event.key.repeat != 0,
+ };
+ event_list.push_back(transfer_event);
+ }
break;
case SDL_KEYUP:
- event_list.push_back({SDLContext::EventType::KEYUP,
- {sdl_to_keycode(event.key.keysym.scancode), false},
- {},
- {}});
+ {
+ EventData transfer_event;
+ transfer_event.event_type = SDLContext::EventType::KEYUP;
+ transfer_event.data.key_data = KeyData{
+ .key = sdl_to_keycode(event.key.keysym.scancode),
+ .key_repeat = false,
+ };
+ event_list.push_back(transfer_event);
+ }
break;
case SDL_MOUSEBUTTONDOWN:
- event_list.push_back(EventData{
- .event_type = SDLContext::EventType::MOUSEDOWN,
+ {
+ EventData transfer_event;
+ transfer_event.event_type = SDLContext::EventType::MOUSEDOWN;
+ transfer_event.data.mouse_data = MouseData{
.mouse_button = sdl_to_mousebutton(event.button.button),
- .mouse_position = {event.button.x, event.button.y},
- });
- break;
- case SDL_MOUSEBUTTONUP: {
- int x, y;
- SDL_GetMouseState(&x, &y);
- event_list.push_back(EventData{
- .event_type = SDLContext::EventType::MOUSEUP,
+ .mouse_position = mouse_pos,
+ };
+ event_list.push_back(transfer_event);
+
+ } break;
+ case SDL_MOUSEBUTTONUP:
+ {
+ EventData transfer_event;
+ transfer_event.event_type = SDLContext::EventType::MOUSEUP;
+ transfer_event.data.mouse_data = MouseData{
.mouse_button = sdl_to_mousebutton(event.button.button),
- .mouse_position = {event.button.x, event.button.y},
- });
+ .mouse_position = mouse_pos,
+ };
+ event_list.push_back(transfer_event);
+
} break;
case SDL_MOUSEMOTION: {
- event_list.push_back(
- EventData{.event_type = SDLContext::EventType::MOUSEMOVE,
- .mouse_position = {event.motion.x, event.motion.y},
- .rel_mouse_move = {event.motion.xrel, event.motion.yrel}});
+ EventData transfer_event;
+ transfer_event.event_type = SDLContext::EventType::MOUSEMOVE;
+ transfer_event.data.mouse_data = MouseData{
+ .mouse_position = mouse_pos,
+ .rel_mouse_move = {event.motion.xrel, event.motion.yrel},
+ };
+ event_list.push_back(transfer_event);
} break;
case SDL_MOUSEWHEEL: {
- event_list.push_back(EventData{
- .event_type = SDLContext::EventType::MOUSEWHEEL,
- .mouse_position = {event.motion.x, event.motion.y},
- // TODO: why is this needed?
+ EventData transfer_event;
+ transfer_event.event_type = SDLContext::EventType::MOUSEWHEEL;
+ transfer_event.data.mouse_data = MouseData{
+ .mouse_position = mouse_pos,
.scroll_direction = event.wheel.y < 0 ? -1 : 1,
.scroll_delta = event.wheel.preciseY,
- });
+ };
+ event_list.push_back(transfer_event);
} break;
}
}
@@ -446,35 +470,35 @@ std::vector<SDLContext::EventData> SDLContext::get_events() {
// Separate function for SDL_WINDOWEVENT subtypes
void SDLContext::handle_window_event(const SDL_WindowEvent & window_event,
std::vector<SDLContext::EventData> & event_list) {
- switch (window_event.event) {
- case SDL_WINDOWEVENT_EXPOSED:
- event_list.push_back({SDLContext::EventType::WINDOW_EXPOSE, {}, {}, {}});
- break;
- case SDL_WINDOWEVENT_RESIZED:
- event_list.push_back({SDLContext::EventType::WINDOW_RESIZE,
- {},
- {},
- {{}, {window_event.data1, window_event.data2}}});
- break;
- case SDL_WINDOWEVENT_MOVED:
- event_list.push_back({SDLContext::EventType::WINDOW_MOVE,
- {},
- {},
- {{window_event.data1, window_event.data2}, {}}});
- break;
- case SDL_WINDOWEVENT_MINIMIZED:
- event_list.push_back({SDLContext::EventType::WINDOW_MINIMIZE, {}, {}, {}});
- break;
- case SDL_WINDOWEVENT_MAXIMIZED:
- event_list.push_back({SDLContext::EventType::WINDOW_MAXIMIZE, {}, {}, {}});
- break;
- case SDL_WINDOWEVENT_FOCUS_GAINED:
- event_list.push_back({SDLContext::EventType::WINDOW_FOCUS_GAIN, {}, {}, {}});
- break;
- case SDL_WINDOWEVENT_FOCUS_LOST:
- event_list.push_back({SDLContext::EventType::WINDOW_FOCUS_LOST, {}, {}, {}});
- break;
- }
+ // switch (window_event.event) {
+ // case SDL_WINDOWEVENT_EXPOSED:
+ // event_list.push_back({SDLContext::EventType::WINDOW_EXPOSE, {}, {}, {}});
+ // break;
+ // case SDL_WINDOWEVENT_RESIZED:
+ // event_list.push_back({SDLContext::EventType::WINDOW_RESIZE,
+ // {},
+ // {},
+ // {{}, {window_event.data1, window_event.data2}}});
+ // break;
+ // case SDL_WINDOWEVENT_MOVED:
+ // event_list.push_back({SDLContext::EventType::WINDOW_MOVE,
+ // {},
+ // {},
+ // {{window_event.data1, window_event.data2}, {}}});
+ // break;
+ // case SDL_WINDOWEVENT_MINIMIZED:
+ // event_list.push_back({SDLContext::EventType::WINDOW_MINIMIZE, {}, {}, {}});
+ // break;
+ // case SDL_WINDOWEVENT_MAXIMIZED:
+ // event_list.push_back({SDLContext::EventType::WINDOW_MAXIMIZE, {}, {}, {}});
+ // break;
+ // case SDL_WINDOWEVENT_FOCUS_GAINED:
+ // event_list.push_back({SDLContext::EventType::WINDOW_FOCUS_GAIN, {}, {}, {}});
+ // break;
+ // case SDL_WINDOWEVENT_FOCUS_LOST:
+ // event_list.push_back({SDLContext::EventType::WINDOW_FOCUS_LOST, {}, {}, {}});
+ // break;
+ // }
}
void SDLContext::set_color_texture(const Texture & texture, const Color & color) {
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 0a2456d..d7af226 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -105,10 +105,21 @@ public:
};
//! EventData struct for passing event data from facade
struct EventData {
- SDLContext::EventType event_type = SDLContext::EventType::NONE;
- KeyData key_data;
- MouseData mouse_data;
- WindowData window_data;
+ SDLContext::EventType event_type = SDLContext::EventType::NONE;
+
+ union EventDataUnion {
+ KeyData key_data;
+ MouseData mouse_data;
+ WindowData window_data;
+
+ EventDataUnion() {}
+ ~EventDataUnion() {}
+ } data;
+
+ // Helper functions
+ // bool isKeyEvent() const { return event_type == SDLContext::EventType::KEYDOWN || event_type == SDLContext::EventType::KEYUP; }
+ // bool isMouseEvent() const { return event_type == SDLContext::EventType::MOUSEDOWN || event_type == SDLContext::EventType::MOUSEUP || event_type == SDLContext::EventType::MOUSEMOVE; }
+ // bool isWindowEvent() const { return event_type == SDLContext::EventType::WINDOW_MINIMIZE || event_type == SDLContext::EventType::WINDOW_RESIZE; }
};
/**
* \brief Retrieves the current state of the keyboard.
diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp
index 459feb3..32538e8 100644
--- a/src/crepe/system/InputSystem.cpp
+++ b/src/crepe/system/InputSystem.cpp
@@ -1,4 +1,4 @@
-
+#include <iostream>
#include "../api/Button.h"
#include "../manager/ComponentManager.h"
#include "../manager/EventManager.h"
@@ -32,10 +32,10 @@ void InputSystem::update() {
Transform & cam_transform = transform_vec.front().get();
ivec2 camera_origin;
- camera_origin.y = cam_transform.position.y + current_cam.data.postion_offset.y
- - (current_cam.viewport_size.y / 2);
- camera_origin.x = cam_transform.position.x + current_cam.data.postion_offset.x
- - (current_cam.viewport_size.x / 2);
+ int camera_origin_x = cam_transform.position.x + current_cam.data.postion_offset.x
+ - (current_cam.viewport_size.x / 2);
+ int camera_origin_y = cam_transform.position.y + current_cam.data.postion_offset.y
+ - (current_cam.viewport_size.y / 2);
for (const SDLContext::EventData & event : event_list) {
// Only calculate mouse coordinates for relevant events
@@ -45,15 +45,17 @@ void InputSystem::update() {
|| event.event_type == SDLContext::EventType::MOUSEWHEEL) {
ivec2 adjusted_mouse;
- adjusted_mouse.x = event.mouse_data.mouse_position.x + camera_origin.x;
- adjusted_mouse.y = event.mouse_data.mouse_position.y + camera_origin.y;
+ std::cout << "mousePos X: " << event.data.mouse_data.mouse_position.x << std::endl;
+ std::cout << "mousePos Y: " << event.data.mouse_data.mouse_position.y << std::endl;
+ int world_mouse_x = event.data.mouse_data.mouse_position.x + camera_origin_x;
+ int world_mouse_y = event.data.mouse_data.mouse_position.y + camera_origin_y;
// Check if the mouse is within the viewport
bool mouse_in_viewport
- = !(adjusted_mouse.x < camera_origin.x
- || adjusted_mouse.x > camera_origin.x + current_cam.viewport_size.x
- || adjusted_mouse.y < camera_origin.y
- || adjusted_mouse.y > camera_origin.y + current_cam.viewport_size.y);
-
+ = !(world_mouse_x < camera_origin_x
+ || world_mouse_x > camera_origin_x + current_cam.viewport_size.x
+ || world_mouse_y < camera_origin_y
+ || world_mouse_y > camera_origin_y + current_cam.viewport_size.y);
+ std::cout << "in view " << mouse_in_viewport << std::endl;
if (!mouse_in_viewport) continue;
// Handle mouse-specific events
@@ -61,28 +63,28 @@ void InputSystem::update() {
case SDLContext::EventType::MOUSEDOWN:
event_mgr.queue_event<MousePressEvent>({
.mouse_pos = {adjusted_mouse.x, adjusted_mouse.y},
- .button = event.mouse_data.mouse_button,
+ .button = event.data.mouse_data.mouse_button,
});
this->last_mouse_down_position = {adjusted_mouse.x, adjusted_mouse.y};
- this->last_mouse_button = event.mouse_data.mouse_button;
+ this->last_mouse_button = event.data.mouse_data.mouse_button;
break;
case SDLContext::EventType::MOUSEUP: {
event_mgr.queue_event<MouseReleaseEvent>({
.mouse_pos = {adjusted_mouse.x, adjusted_mouse.y},
- .button = event.mouse_data.mouse_button,
+ .button = event.data.mouse_data.mouse_button,
});
int delta_x = adjusted_mouse.x - this->last_mouse_down_position.x;
int delta_y = adjusted_mouse.y - this->last_mouse_down_position.y;
- if (this->last_mouse_button == event.mouse_data.mouse_button
+ if (this->last_mouse_button == event.data.mouse_data.mouse_button
&& std::abs(delta_x) <= click_tolerance
&& std::abs(delta_y) <= click_tolerance) {
event_mgr.queue_event<MouseClickEvent>({
.mouse_pos = {adjusted_mouse.x, adjusted_mouse.y},
- .button = event.mouse_data.mouse_button,
+ .button = event.data.mouse_data.mouse_button,
});
- this->handle_click(event.mouse_data.mouse_button, adjusted_mouse);
+ this->handle_click(event.data.mouse_data.mouse_button, adjusted_mouse);
}
break;
}
@@ -90,7 +92,7 @@ void InputSystem::update() {
case SDLContext::EventType::MOUSEMOVE:
event_mgr.queue_event<MouseMoveEvent>({
.mouse_pos = {adjusted_mouse.x, adjusted_mouse.y},
- .mouse_delta = event.mouse_data.rel_mouse_move,
+ .mouse_delta = event.data.mouse_data.rel_mouse_move,
});
this->handle_move(event, adjusted_mouse);
break;
@@ -98,8 +100,8 @@ void InputSystem::update() {
case SDLContext::EventType::MOUSEWHEEL:
event_mgr.queue_event<MouseScrollEvent>({
.mouse_pos = {adjusted_mouse.x, adjusted_mouse.y},
- .scroll_direction = event.mouse_data.scroll_direction,
- .scroll_delta = event.mouse_data.scroll_delta,
+ .scroll_direction = event.data.mouse_data.scroll_direction,
+ .scroll_delta = event.data.mouse_data.scroll_delta,
});
break;
@@ -110,11 +112,12 @@ void InputSystem::update() {
// Handle non-mouse events
switch (event.event_type) {
case SDLContext::EventType::KEYDOWN:
+
event_mgr.queue_event<KeyPressEvent>(
- {.repeat = event.key_data.key_repeat, .key = event.key_data.key});
+ {.repeat = event.data.key_data.key_repeat, .key = event.data.key_data.key});
break;
case SDLContext::EventType::KEYUP:
- event_mgr.queue_event<KeyReleaseEvent>({.key = event.key_data.key});
+ event_mgr.queue_event<KeyReleaseEvent>({.key = event.data.key_data.key});
break;
case SDLContext::EventType::SHUTDOWN:
event_mgr.queue_event<ShutDownEvent>({});
@@ -124,11 +127,11 @@ void InputSystem::update() {
break;
case SDLContext::EventType::WINDOW_RESIZE:
event_mgr.queue_event<WindowResizeEvent>(
- WindowResizeEvent{.dimensions = event.window_data.resize_dimension});
+ WindowResizeEvent{.dimensions = event.data.window_data.resize_dimension});
break;
case SDLContext::EventType::WINDOW_MOVE:
event_mgr.queue_event<WindowMoveEvent>(
- {.delta_move = event.window_data.move_delta});
+ {.delta_move = event.data.window_data.move_delta});
break;
case SDLContext::EventType::WINDOW_MINIMIZE:
event_mgr.queue_event<WindowMinimizeEvent>({});
diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp
index f57a6de..f30f15a 100644
--- a/src/test/EventTest.cpp
+++ b/src/test/EventTest.cpp
@@ -56,7 +56,7 @@ TEST_F(EventManagerTest, EventManagerTest_trigger_all_channels) {
event_mgr.subscribe<MouseClickEvent>(mouse_handler, EventManager::CHANNEL_ALL);
MouseClickEvent click_event{.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE};
- EventManager::get_instance().trigger_event<MouseClickEvent>(click_event,
+ event_mgr.trigger_event<MouseClickEvent>(click_event,
EventManager::CHANNEL_ALL);
EXPECT_TRUE(triggered);
@@ -74,7 +74,7 @@ TEST_F(EventManagerTest, EventManagerTest_trigger_one_channel) {
event_mgr.subscribe<MouseClickEvent>(mouse_handler, test_channel);
MouseClickEvent click_event{.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE};
- EventManager::get_instance().trigger_event<MouseClickEvent>(click_event,
+ event_mgr.trigger_event<MouseClickEvent>(click_event,
EventManager::CHANNEL_ALL);
EXPECT_FALSE(triggered);
@@ -106,8 +106,8 @@ TEST_F(EventManagerTest, EventManagerTest_callback_propagation) {
// Test event
MouseClickEvent click_event{.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE};
- event_manager.subscribe<MouseClickEvent>(mouse_handler_true, EventManager::CHANNEL_ALL);
- event_manager.subscribe<MouseClickEvent>(mouse_handler_false, EventManager::CHANNEL_ALL);
+ event_mgr.subscribe<MouseClickEvent>(mouse_handler_true, EventManager::CHANNEL_ALL);
+ event_mgr.subscribe<MouseClickEvent>(mouse_handler_false, EventManager::CHANNEL_ALL);
// Trigger event
event_mgr.trigger_event<MouseClickEvent>(click_event, EventManager::CHANNEL_ALL);
@@ -137,15 +137,15 @@ TEST_F(EventManagerTest, EventManagerTest_queue_dispatch) {
int test_channel = 1;
EventHandler<MouseClickEvent> mouse_handler1 = [&](const MouseClickEvent & e) {
triggered1 = true;
- EXPECT_EQ(e.mouse_x, 100);
- EXPECT_EQ(e.mouse_y, 200);
+ EXPECT_EQ(e.mouse_pos.x, 100);
+ EXPECT_EQ(e.mouse_pos.y, 200);
EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE);
return false; // Allows propagation
};
EventHandler<MouseClickEvent> mouse_handler2 = [&](const MouseClickEvent & e) {
triggered2 = true;
- EXPECT_EQ(e.mouse_x, 100);
- EXPECT_EQ(e.mouse_y, 200);
+ EXPECT_EQ(e.mouse_pos.x, 100);
+ EXPECT_EQ(e.mouse_pos.y, 200);
EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE);
return false; // Allows propagation
};
@@ -153,9 +153,9 @@ TEST_F(EventManagerTest, EventManagerTest_queue_dispatch) {
event_mgr.subscribe<MouseClickEvent>(mouse_handler2, test_channel);
event_mgr.queue_event<MouseClickEvent>(
- MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE});
+ MouseClickEvent{.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE});
event_mgr.queue_event<MouseClickEvent>(
- MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE},
+ MouseClickEvent{.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE},
test_channel);
event_mgr.dispatch_events();
EXPECT_TRUE(triggered1);
@@ -189,7 +189,7 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) {
subscription_t handler2_id = event_mgr.subscribe<MouseClickEvent>(mouse_handler2);
// Queue events
- event_manager.queue_event<MouseClickEvent>(
+ event_mgr.queue_event<MouseClickEvent>(
MouseClickEvent{.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE});
// Dispatch events - both handlers should be triggered
@@ -205,7 +205,7 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) {
event_mgr.unsubscribe(handler1_id);
// Queue the same event again
- event_manager.queue_event<MouseClickEvent>(
+ event_mgr.queue_event<MouseClickEvent>(
MouseClickEvent{.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE});
// Dispatch events - only handler 2 should be triggered, handler 1 should NOT
@@ -220,7 +220,7 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) {
event_mgr.unsubscribe(handler2_id);
// Queue the event again
- event_manager.queue_event<MouseClickEvent>(
+ event_mgr.queue_event<MouseClickEvent>(
MouseClickEvent{.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE});
// Dispatch events - no handler should be triggered