aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/facade')
-rw-r--r--src/crepe/facade/SDLContext.cpp162
-rw-r--r--src/crepe/facade/SDLContext.h78
2 files changed, 200 insertions, 40 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index e8be7ca..9b4595e 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -6,6 +6,7 @@
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_video.h>
+#include <array>
#include <cmath>
#include <cstddef>
#include <cstdint>
@@ -15,7 +16,6 @@
#include <stdexcept>
#include "../api/Camera.h"
-#include "../api/Config.h"
#include "../api/Sprite.h"
#include "../api/Texture.h"
#include "../util/Log.h"
@@ -76,31 +76,144 @@ SDLContext::~SDLContext() {
IMG_Quit();
SDL_Quit();
}
-void SDLContext::handle_events(bool & running) {
- //TODO: wouter i need events
- /*
- SDL_Event event;
- SDL_PollEvent(&event);
- switch (event.type) {
- case SDL_QUIT:
- running = false;
- break;
- case SDL_KEYDOWN:
- triggerEvent(KeyPressedEvent(getCustomKey(event.key.keysym.sym)));
- break;
- case SDL_MOUSEBUTTONDOWN:
- int x, y;
- SDL_GetMouseState(&x, &y);
- triggerEvent(MousePressedEvent(x, y));
- break;
+
+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);
+
+ table[SDL_SCANCODE_SPACE] = Keycode::SPACE;
+ table[SDL_SCANCODE_APOSTROPHE] = Keycode::APOSTROPHE;
+ table[SDL_SCANCODE_COMMA] = Keycode::COMMA;
+ table[SDL_SCANCODE_MINUS] = Keycode::MINUS;
+ table[SDL_SCANCODE_PERIOD] = Keycode::PERIOD;
+ table[SDL_SCANCODE_SLASH] = Keycode::SLASH;
+ table[SDL_SCANCODE_0] = Keycode::D0;
+ table[SDL_SCANCODE_1] = Keycode::D1;
+ table[SDL_SCANCODE_2] = Keycode::D2;
+ table[SDL_SCANCODE_3] = Keycode::D3;
+ table[SDL_SCANCODE_4] = Keycode::D4;
+ table[SDL_SCANCODE_5] = Keycode::D5;
+ table[SDL_SCANCODE_6] = Keycode::D6;
+ table[SDL_SCANCODE_7] = Keycode::D7;
+ table[SDL_SCANCODE_8] = Keycode::D8;
+ table[SDL_SCANCODE_9] = Keycode::D9;
+ table[SDL_SCANCODE_SEMICOLON] = Keycode::SEMICOLON;
+ table[SDL_SCANCODE_EQUALS] = Keycode::EQUAL;
+ table[SDL_SCANCODE_A] = Keycode::A;
+ table[SDL_SCANCODE_B] = Keycode::B;
+ table[SDL_SCANCODE_C] = Keycode::C;
+ table[SDL_SCANCODE_D] = Keycode::D;
+ table[SDL_SCANCODE_E] = Keycode::E;
+ table[SDL_SCANCODE_F] = Keycode::F;
+ table[SDL_SCANCODE_G] = Keycode::G;
+ table[SDL_SCANCODE_H] = Keycode::H;
+ table[SDL_SCANCODE_I] = Keycode::I;
+ table[SDL_SCANCODE_J] = Keycode::J;
+ table[SDL_SCANCODE_K] = Keycode::K;
+ table[SDL_SCANCODE_L] = Keycode::L;
+ table[SDL_SCANCODE_M] = Keycode::M;
+ table[SDL_SCANCODE_N] = Keycode::N;
+ table[SDL_SCANCODE_O] = Keycode::O;
+ table[SDL_SCANCODE_P] = Keycode::P;
+ table[SDL_SCANCODE_Q] = Keycode::Q;
+ table[SDL_SCANCODE_R] = Keycode::R;
+ table[SDL_SCANCODE_S] = Keycode::S;
+ table[SDL_SCANCODE_T] = Keycode::T;
+ table[SDL_SCANCODE_U] = Keycode::U;
+ table[SDL_SCANCODE_V] = Keycode::V;
+ table[SDL_SCANCODE_W] = Keycode::W;
+ table[SDL_SCANCODE_X] = Keycode::X;
+ table[SDL_SCANCODE_Y] = Keycode::Y;
+ table[SDL_SCANCODE_Z] = Keycode::Z;
+ table[SDL_SCANCODE_LEFTBRACKET] = Keycode::LEFT_BRACKET;
+ table[SDL_SCANCODE_BACKSLASH] = Keycode::BACKSLASH;
+ table[SDL_SCANCODE_RIGHTBRACKET] = Keycode::RIGHT_BRACKET;
+ table[SDL_SCANCODE_GRAVE] = Keycode::GRAVE_ACCENT;
+ table[SDL_SCANCODE_ESCAPE] = Keycode::ESCAPE;
+ table[SDL_SCANCODE_RETURN] = Keycode::ENTER;
+ table[SDL_SCANCODE_TAB] = Keycode::TAB;
+ table[SDL_SCANCODE_BACKSPACE] = Keycode::BACKSPACE;
+ table[SDL_SCANCODE_INSERT] = Keycode::INSERT;
+ table[SDL_SCANCODE_DELETE] = Keycode::DELETE;
+ table[SDL_SCANCODE_RIGHT] = Keycode::RIGHT;
+ table[SDL_SCANCODE_LEFT] = Keycode::LEFT;
+ table[SDL_SCANCODE_DOWN] = Keycode::DOWN;
+ table[SDL_SCANCODE_UP] = Keycode::UP;
+ table[SDL_SCANCODE_PAGEUP] = Keycode::PAGE_UP;
+ table[SDL_SCANCODE_PAGEDOWN] = Keycode::PAGE_DOWN;
+ table[SDL_SCANCODE_HOME] = Keycode::HOME;
+ table[SDL_SCANCODE_END] = Keycode::END;
+ table[SDL_SCANCODE_CAPSLOCK] = Keycode::CAPS_LOCK;
+ table[SDL_SCANCODE_SCROLLLOCK] = Keycode::SCROLL_LOCK;
+ table[SDL_SCANCODE_NUMLOCKCLEAR] = Keycode::NUM_LOCK;
+ table[SDL_SCANCODE_PRINTSCREEN] = Keycode::PRINT_SCREEN;
+ table[SDL_SCANCODE_PAUSE] = Keycode::PAUSE;
+ table[SDL_SCANCODE_F1] = Keycode::F1;
+ table[SDL_SCANCODE_F2] = Keycode::F2;
+ table[SDL_SCANCODE_F3] = Keycode::F3;
+ table[SDL_SCANCODE_F4] = Keycode::F4;
+ table[SDL_SCANCODE_F5] = Keycode::F5;
+ table[SDL_SCANCODE_F6] = Keycode::F6;
+ table[SDL_SCANCODE_F7] = Keycode::F7;
+ table[SDL_SCANCODE_F8] = Keycode::F8;
+ table[SDL_SCANCODE_F9] = Keycode::F9;
+ table[SDL_SCANCODE_F10] = Keycode::F10;
+ table[SDL_SCANCODE_F11] = Keycode::F11;
+ table[SDL_SCANCODE_F12] = Keycode::F12;
+ table[SDL_SCANCODE_KP_0] = Keycode::KP0;
+ table[SDL_SCANCODE_KP_1] = Keycode::KP1;
+ table[SDL_SCANCODE_KP_2] = Keycode::KP2;
+ table[SDL_SCANCODE_KP_3] = Keycode::KP3;
+ table[SDL_SCANCODE_KP_4] = Keycode::KP4;
+ table[SDL_SCANCODE_KP_5] = Keycode::KP5;
+ table[SDL_SCANCODE_KP_6] = Keycode::KP6;
+ table[SDL_SCANCODE_KP_7] = Keycode::KP7;
+ table[SDL_SCANCODE_KP_8] = Keycode::KP8;
+ table[SDL_SCANCODE_KP_9] = Keycode::KP9;
+ table[SDL_SCANCODE_LSHIFT] = Keycode::LEFT_SHIFT;
+ table[SDL_SCANCODE_LCTRL] = Keycode::LEFT_CONTROL;
+ table[SDL_SCANCODE_LALT] = Keycode::LEFT_ALT;
+ table[SDL_SCANCODE_LGUI] = Keycode::LEFT_SUPER;
+ table[SDL_SCANCODE_RSHIFT] = Keycode::RIGHT_SHIFT;
+ table[SDL_SCANCODE_RCTRL] = Keycode::RIGHT_CONTROL;
+ table[SDL_SCANCODE_RALT] = Keycode::RIGHT_ALT;
+ table[SDL_SCANCODE_RGUI] = Keycode::RIGHT_SUPER;
+ table[SDL_SCANCODE_MENU] = Keycode::MENU;
+
+ return table;
+ }();
+
+ if (sdl_key < 0 || sdl_key >= SDL_NUM_SCANCODES) {
+ return Keycode::NONE;
}
- */
+
+ return LOOKUP_TABLE[sdl_key];
}
-void SDLContext::clear_screen() {
- SDL_SetRenderDrawColor(this->game_renderer.get(), 0, 0, 0, 255);
- SDL_RenderClear(this->game_renderer.get());
+MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) {
+ static const std::array<MouseButton, 5> MOUSE_BUTTON_LOOKUP_TABLE = [] {
+ std::array<MouseButton, 5> table{};
+ table.fill(MouseButton::NONE);
+
+ table[SDL_BUTTON_LEFT] = MouseButton::LEFT_MOUSE;
+ table[SDL_BUTTON_RIGHT] = MouseButton::RIGHT_MOUSE;
+ table[SDL_BUTTON_MIDDLE] = MouseButton::MIDDLE_MOUSE;
+ table[SDL_BUTTON_X1] = MouseButton::X1_MOUSE;
+ table[SDL_BUTTON_X2] = MouseButton::X2_MOUSE;
+
+ return table;
+ }();
+
+ if (sdl_button >= MOUSE_BUTTON_LOOKUP_TABLE.size()) {
+ // Return NONE for invalid or unmapped button
+ return MouseButton::NONE;
+ }
+
+ return MOUSE_BUTTON_LOOKUP_TABLE[sdl_button];
}
+
+void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); }
void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); }
SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const {
@@ -223,8 +336,3 @@ ivec2 SDLContext::get_size(const Texture & ctx) {
}
void SDLContext::delay(int ms) const { SDL_Delay(ms); }
-
-void SDLContext::set_color_texture(const Texture & texture, const Color & color) {
- SDL_SetTextureColorMod(texture.texture.get(), color.r, color.g, color.b);
- SDL_SetTextureAlphaMod(texture.texture.get(), color.a);
-}
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index e49ca78..a56232f 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -1,5 +1,6 @@
#pragma once
+#include <SDL2/SDL.h>
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
@@ -8,20 +9,22 @@
#include <functional>
#include <memory>
#include <string>
-
-#include "../api/Camera.h"
-#include "../api/Sprite.h"
-
+#include <utility>
+
+#include "api/Camera.h"
+#include "api/Event.h"
+#include "api/KeyCodes.h"
+#include "api/Sprite.h"
+#include "api/Transform.h"
+#include "api/Camera.h"
#include "api/Color.h"
#include "api/Texture.h"
#include "types.h"
namespace crepe {
-// TODO: SDL_Keycode is defined in a header not distributed with crepe, which means this
-// typedef is unusable when crepe is packaged. Wouter will fix this later.
-typedef SDL_Keycode CREPE_KEYCODES;
-
+class LoopManager;
+class InputSystem;
/**
* \class SDLContext
* \brief Facade for the SDL library
@@ -41,6 +44,28 @@ public:
};
public:
+ //! EventType enum for passing eventType
+ enum EventType {
+ NONE = 0,
+ MOUSEDOWN,
+ MOUSEUP,
+ MOUSEMOVE,
+ MOUSEWHEEL,
+ KEYUP,
+ KEYDOWN,
+ SHUTDOWN,
+
+ };
+ //! EventData struct for passing event data from facade
+ struct EventData {
+ SDLContext::EventType event_type = SDLContext::EventType::NONE;
+ Keycode key = Keycode::NONE;
+ bool key_repeat = false;
+ MouseButton mouse_button = MouseButton::NONE;
+ std::pair<int, int> mouse_position = {-1, -1};
+ int wheel_delta = -1;
+ std::pair<int, int> rel_mouse_move = {-1, -1};
+ };
/**
* \brief Gets the singleton instance of SDLContext.
* \return Reference to the SDLContext instance.
@@ -53,13 +78,40 @@ public:
SDLContext & operator=(SDLContext &&) = delete;
private:
- //! will only use handle_events
- friend class LoopManager;
+ //! will only use get_events
+ friend class InputSystem;
+ /**
+ * @brief Retrieves a list of all events from the SDL context.
+ *
+ * This method retrieves all the events from the SDL context that are currently
+ * available. It is primarily used by the InputSystem to process various
+ * input events such as mouse clicks, mouse movements, and keyboard presses.
+ *
+ * @return A vector of `SDLContext::EventData` containing the events.
+ */
+ std::vector<SDLContext::EventData> get_events();
+
+ /**
+ * @brief Converts an SDL key code to the custom Keycode type.
+ *
+ * This method maps an SDL key code to the corresponding `Keycode` enum value,
+ * which is used internally by the system to identify the keys.
+ *
+ * @param sdl_key The SDL key code to convert.
+ * @return The corresponding `Keycode` value.
+ */
+ Keycode sdl_to_keycode(SDL_Keycode sdl_key);
+
/**
- * \brief Handles SDL events such as window close and input.
- * \param running Reference to a boolean flag that controls the main loop.
+ * @brief Converts an SDL mouse button code to the custom MouseButton type.
+ *
+ * This method maps an SDL mouse button code to the corresponding `MouseButton`
+ * enum value, which is used internally by the system to identify mouse buttons.
+ *
+ * @param sdl_button The SDL mouse button code to convert.
+ * @return The corresponding `MouseButton` value.
*/
- void handle_events(bool & running);
+ MouseButton sdl_to_mousebutton(Uint8 sdl_button);
private:
//! Will only use get_ticks