aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade/SDLContext.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/facade/SDLContext.cpp')
-rw-r--r--src/crepe/facade/SDLContext.cpp88
1 files changed, 68 insertions, 20 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index 20bb030..4552605 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -75,11 +75,12 @@ SDLContext::~SDLContext() {
SDL_Quit();
}
-Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) {
+Keycode SDLContext::sdl_to_keycode(SDL_Scancode sdl_key) {
static const std::array<Keycode, SDL_NUM_SCANCODES> LOOKUP_TABLE = [] {
std::array<Keycode, SDL_NUM_SCANCODES> table{};
table.fill(Keycode::NONE);
+ // Map all SDL scancodes to Keycodes
table[SDL_SCANCODE_SPACE] = Keycode::SPACE;
table[SDL_SCANCODE_APOSTROPHE] = Keycode::APOSTROPHE;
table[SDL_SCANCODE_COMMA] = Keycode::COMMA;
@@ -181,13 +182,27 @@ Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) {
return table;
}();
-
if (sdl_key < 0 || sdl_key >= SDL_NUM_SCANCODES) {
return Keycode::NONE;
}
-
return LOOKUP_TABLE[sdl_key];
}
+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, Keycode::NUM_KEYCODES> keyState{};
+ SDL_PumpEvents();
+ const Uint8 * current_state = SDL_GetKeyboardState(nullptr);
+
+ for (int i = 0; i < SDL_NUM_SCANCODES; ++i) {
+ Keycode key = sdl_to_keycode(static_cast<SDL_Scancode>(i));
+
+ if (key != Keycode::NONE) {
+ keyState[key] = current_state[i] != 0;
+ }
+ }
+
+ return keyState;
+}
MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) {
static const std::array<MouseButton, 5> MOUSE_BUTTON_LOOKUP_TABLE = [] {
@@ -368,35 +383,32 @@ 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;
switch (event.type) {
case SDL_QUIT:
- event_list.push_back(EventData{
- .event_type = SDLContext::EventType::SHUTDOWN,
- });
+ event_list.push_back({SDLContext::EventType::SHUTDOWN, {}, {}, {}});
break;
case SDL_KEYDOWN:
- event_list.push_back(EventData{
- .event_type = SDLContext::EventType::KEYDOWN,
- .key = sdl_to_keycode(event.key.keysym.scancode),
- .key_repeat = (event.key.repeat != 0),
- });
+ event_list.push_back(
+ {SDLContext::EventType::KEYDOWN,
+ {sdl_to_keycode(event.key.keysym.scancode), event.key.repeat != 0},
+ {},
+ {}});
break;
case SDL_KEYUP:
- event_list.push_back(EventData{
- .event_type = SDLContext::EventType::KEYUP,
- .key = sdl_to_keycode(event.key.keysym.scancode),
- });
+ event_list.push_back({SDLContext::EventType::KEYUP,
+ {sdl_to_keycode(event.key.keysym.scancode), false},
+ {},
+ {}});
break;
case SDL_MOUSEBUTTONDOWN:
event_list.push_back(EventData{
.event_type = SDLContext::EventType::MOUSEDOWN,
.mouse_button = sdl_to_mousebutton(event.button.button),
- .mouse_position = mouse_pos,
+ .mouse_position = {event.button.x, event.button.y},
});
break;
case SDL_MOUSEBUTTONUP: {
@@ -405,21 +417,21 @@ std::vector<SDLContext::EventData> SDLContext::get_events() {
event_list.push_back(EventData{
.event_type = SDLContext::EventType::MOUSEUP,
.mouse_button = sdl_to_mousebutton(event.button.button),
- .mouse_position = mouse_pos,
+ .mouse_position = {event.button.x, event.button.y},
});
} break;
case SDL_MOUSEMOTION: {
event_list.push_back(
EventData{.event_type = SDLContext::EventType::MOUSEMOVE,
- .mouse_position = mouse_pos,
+ .mouse_position = {event.motion.x, event.motion.y},
.rel_mouse_move = {event.motion.xrel, event.motion.yrel}});
} break;
case SDL_MOUSEWHEEL: {
event_list.push_back(EventData{
.event_type = SDLContext::EventType::MOUSEWHEEL,
- .mouse_position = mouse_pos,
+ .mouse_position = {event.motion.x, event.motion.y},
// TODO: why is this needed?
.scroll_direction = event.wheel.y < 0 ? -1 : 1,
.scroll_delta = event.wheel.preciseY,
@@ -427,8 +439,44 @@ std::vector<SDLContext::EventData> SDLContext::get_events() {
} break;
}
}
+
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;
+ }
+}
+
void SDLContext::set_color_texture(const Texture & texture, const Color & color) {
SDL_SetTextureColorMod(texture.get_img(), color.r, color.g, color.b);
SDL_SetTextureAlphaMod(texture.get_img(), color.a);