aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/facade')
-rw-r--r--src/crepe/facade/SDLContext.cpp85
-rw-r--r--src/crepe/facade/SDLContext.h32
2 files changed, 66 insertions, 51 deletions
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.
*