aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/KeyCodes.h6
-rw-r--r--src/crepe/facade/SDLContext.cpp85
-rw-r--r--src/crepe/facade/SDLContext.h32
-rw-r--r--src/crepe/system/InputSystem.cpp8
-rw-r--r--src/test/InputTest.cpp27
5 files changed, 101 insertions, 57 deletions
diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h
index 6204cf2..f704fbb 100644
--- a/src/crepe/api/KeyCodes.h
+++ b/src/crepe/api/KeyCodes.h
@@ -13,7 +13,7 @@ enum class MouseButton {
};
//! Enumeration for keyboard key inputs, including printable characters, function keys, and keypad keys.
-enum class Keycode {
+typedef enum {
NONE = 0, //!< No key input.
SPACE = 32, //!< Spacebar.
APOSTROPHE = 39, //!< Apostrophe (').
@@ -151,6 +151,6 @@ enum class Keycode {
/// \}
MENU = 348, //!< Menu key.
//! Not actually a key instead its the amount of keycodes there are for array indexing
- NUM_KEYCODES = 113,
-};
+ NUM_KEYCODES = 512,
+} Keycode;
} // namespace crepe
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index 9e10803..fdc83b2 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -6,6 +6,9 @@
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_video.h>
+
+#include <iostream>
+
#include <array>
#include <cmath>
#include <cstddef>
@@ -77,8 +80,9 @@ SDLContext::~SDLContext() {
SDL_Quit();
}
-static const std::array<Keycode, SDL_NUM_SCANCODES>& get_lookup_table() {
- static const std::array<Keycode, SDL_NUM_SCANCODES> LOOKUP_TABLE = [] {
+// Function that uses the shared table
+Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) {
+ static const std::array<Keycode, SDL_NUM_SCANCODES> LOOKUP_TABLE = [] {
std::array<Keycode, SDL_NUM_SCANCODES> table{};
table.fill(Keycode::NONE);
@@ -184,25 +188,20 @@ static const std::array<Keycode, SDL_NUM_SCANCODES>& get_lookup_table() {
return table;
}();
- return LOOKUP_TABLE;
-}
-
-// Function that uses the shared table
-Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) {
if (sdl_key < 0 || sdl_key >= SDL_NUM_SCANCODES) {
return Keycode::NONE;
}
- return get_lookup_table()[sdl_key];
+ return LOOKUP_TABLE[sdl_key];
}
-std::array<bool, SDL_NUM_SCANCODES> SDLContext::get_keyboard_state() {
+std::array<bool, Keycode::NUM_KEYCODES> SDLContext::get_keyboard_state() {
// Array to hold the key states (true if pressed, false if not)
std::array<bool, SDL_NUM_SCANCODES> keyState;
const Uint8* current_state = SDL_GetKeyboardState(nullptr);
-
+
for (int i = 0; i < SDL_NUM_SCANCODES; ++i) {
// Set true if the key is pressed, false if not
- keyState[i] = currentState[i] != 0;
+ keyState[i] = current_state[i] != 0;
}
return keyState;
@@ -428,7 +427,36 @@ std::vector<SDLContext::EventData> SDLContext::get_events() {
// Forward window events for further processing
case SDL_WINDOWEVENT:
- handle_window_event(event.window, event_list);
+ switch (event.window.event) {
+ case SDL_WINDOWEVENT_EXPOSED:
+ event_list.push_back({SDLContext::EventType::WINDOW_EXPOSE, {}, {}, {}});
+ break;
+ case SDL_WINDOWEVENT_RESIZED:
+ {
+ std::cout << "window resize" << std::endl;
+ SDLContext::EventData event_data;
+ event_data.event_type = SDLContext::EventType::WINDOW_RESIZE;
+ event_data.window_data.resize_dimension = {event.window.data1,event.window.data2};
+ event_list.push_back(event_data);
+ break;
+ }
+ case SDL_WINDOWEVENT_MOVED:
+ event_list.push_back({SDLContext::EventType::WINDOW_MOVE, {}, {},
+ {{event.window.data1, event.window.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;
+ }
break;
}
}
@@ -436,35 +464,10 @@ std::vector<SDLContext::EventData> SDLContext::get_events() {
return event_list;
}
-// 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;
- }
-}
+// // Separate function for SDL_WINDOWEVENT subtypes
+// void SDLContext::handle_event(const SDL_WindowEvent& event,
+// std::vector<SDLContext::EventData>& event_list) {
+// }
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 0f503f3..98be988 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -9,6 +9,7 @@
#include <functional>
#include <memory>
#include <string>
+#include <array>
#include "api/Camera.h"
#include "api/Color.h"
@@ -134,14 +135,14 @@ private:
* \return Events that occurred since last call to `get_events()`
*/
std::vector<SDLContext::EventData> get_events();
- /**
- * \brief Fills event_list with triggered window events
- *
- * This method checks if any window events are triggered and adds them to the event_list.
- *
- */
- void handle_window_event(const SDL_WindowEvent& window_event,
- std::vector<SDLContext::EventData>& event_list);
+ // /**
+ // * \brief Fills event_list with triggered window events
+ // *
+ // * This method checks if any window events are triggered and adds them to the event_list.
+ // *
+ // */
+ // void handle_window_event(const SDL_WindowEvent& window_event,
+ // std::vector<SDLContext::EventData>& event_list);
/**
* \brief Converts an SDL key code to the custom Keycode type.
*
@@ -152,8 +153,19 @@ private:
* \return The corresponding `Keycode` value or `Keycode::NONE` if the key is unrecognized.
*/
Keycode sdl_to_keycode(SDL_Keycode sdl_key);
- static const std::array<Keycode, SDL_NUM_SCANCODES>& get_lookup_table();
- std::array<Keycode, SDL_NUM_SCANCODES> get_keyboard_state();
+ /**
+ * \brief Retrieves the current state of the keyboard.
+ *
+ * This method returns the state of all keys on the keyboard, represented as a
+ * `std::array` of boolean values. Each element of the array corresponds to a
+ * specific key defined in the `Keycode` enum, and the value indicates whether
+ * the key is currently pressed (true) or not pressed (false).
+ *
+ * \return A `std::array<bool, Keycode::NUM_KEYCODES>` representing the state of
+ * each key on the keyboard, where `true` means the key is pressed, and
+ * `false` means it is not pressed.
+ */
+ std::array<bool, Keycode::NUM_KEYCODES> get_keyboard_state();
/**
* \brief Converts an SDL mouse button code to the custom MouseButton type.
*
diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp
index c3bf4d4..7c33308 100644
--- a/src/crepe/system/InputSystem.cpp
+++ b/src/crepe/system/InputSystem.cpp
@@ -1,3 +1,6 @@
+
+#include <iostream>
+
#include "../api/Button.h"
#include "../manager/ComponentManager.h"
#include "../manager/EventManager.h"
@@ -34,6 +37,7 @@ void InputSystem::update() {
- (current_cam.viewport_size.x / 2);
for (const SDLContext::EventData & event : event_list) {
+ std::cout << "event type: " << event.event_type << std::endl;
// Only calculate mouse coordinates for relevant events
if (event.event_type == SDLContext::EventType::MOUSEDOWN
|| event.event_type == SDLContext::EventType::MOUSEUP
@@ -102,6 +106,7 @@ void InputSystem::update() {
break;
}
} else {
+ std::cout << "non mouse event" << std::endl;
// Handle non-mouse events
switch (event.event_type) {
case SDLContext::EventType::KEYDOWN:
@@ -117,7 +122,8 @@ void InputSystem::update() {
event_mgr.queue_event<WindowExposeEvent>({});
break;
case SDLContext::EventType::WINDOW_RESIZE:
- event_mgr.queue_event<WindowResizeEvent>({.dimensions = event.window_data.resize_dimension});
+ std::cout << "input system queue" << std::endl;
+ event_mgr.queue_event<WindowResizeEvent>(WindowResizeEvent{.dimensions = event.window_data.resize_dimension});
break;
case SDLContext::EventType::WINDOW_MOVE:
event_mgr.queue_event<WindowMoveEvent>({.delta_move = event.window_data.move_delta});
diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp
index 4d35eef..7d4a017 100644
--- a/src/test/InputTest.cpp
+++ b/src/test/InputTest.cpp
@@ -1,4 +1,7 @@
#include <gtest/gtest.h>
+
+#include <iostream>
+
#define protected public
#define private public
#include "api/KeyCodes.h"
@@ -32,8 +35,6 @@ public:
protected:
void SetUp() override {
- mediator.event_manager = event_manager;
- mediator.component_manager = mgr;
event_manager.clear();
}
@@ -291,3 +292,25 @@ TEST_F(InputTest, testButtonHover) {
event_manager.dispatch_events();
EXPECT_TRUE(button.hover);
}
+
+TEST_F(InputTest, WindowResizeTest) {
+ bool callback_triggered = false;
+ EventHandler<WindowResizeEvent> on_window_resize = [&](const WindowResizeEvent & event) {
+ std::cout << "test callback" << std::endl;
+ callback_triggered = true;
+ EXPECT_EQ(event.dimensions.x, 0);
+ EXPECT_EQ(event.dimensions.y, 0);
+ return false;
+ };
+ event_manager.subscribe<WindowResizeEvent>(on_window_resize);
+ SDL_Event resize_event;
+ SDL_zero(resize_event);
+ resize_event.type = SDL_WINDOWEVENT;
+ resize_event.window.event = SDL_WINDOWEVENT_RESIZED;
+ resize_event.window.data1 = 800; // new width
+ resize_event.window.data2 = 600; // new height
+ SDL_PushEvent(&resize_event);
+ input_system.update();
+ event_manager.dispatch_events();
+ EXPECT_TRUE(callback_triggered);
+}