diff options
Diffstat (limited to 'src/crepe/facade')
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 80 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 40 |
2 files changed, 114 insertions, 6 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index b3298a7..ff7c9f0 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -11,14 +11,15 @@ #include <memory> #include <stdexcept> #include <string> +#include <array> #include "../api/Camera.h" #include "../api/Sprite.h" #include "../api/Texture.h" #include "../api/Transform.h" -#include "../api/Vector2.h" #include "../util/Log.h" + #include "SDLContext.h" using namespace crepe; @@ -72,6 +73,7 @@ SDLContext::~SDLContext() { IMG_Quit(); SDL_Quit(); } + void SDLContext::handle_events(bool & running) { //TODO: wouter i need events /* @@ -192,3 +194,79 @@ int SDLContext::get_height(const Texture & ctx) const { return h; } void SDLContext::delay(int ms) const { SDL_Delay(ms); } + +std::vector<SDLContext::EventData> SDLContext::get_events(){ + std::vector<SDLContext::EventData> event_list; + SDL_Event event; + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + event_list.push_back(EventData{ + .event_type = SDLContext::Event::SHUTDOWN, + }); + break; + case SDL_KEYDOWN: + event_list.push_back(EventData{ + .event_type = SDLContext::Event::KEYDOWN, + .key = sdl_to_keycode(event.key.keysym.scancode), + .key_repeat = (event.key.repeat != 0), + }); + break; + case SDL_KEYUP: + event_list.push_back(EventData{ + .event_type = SDLContext::Event::KEYUP, + .key = sdl_to_keycode(event.key.keysym.scancode), + }); + break; + case SDL_MOUSEBUTTONDOWN: + { + int x,y; + SDL_GetMouseState(&x, &y); + event_list.push_back(EventData{ + .event_type = SDLContext::Event::MOUSEDOWN, + .mouse_button = sdl_to_mousebutton(event.button.button), + .mouse_position = {x,y}, + }); + } + break; + case SDL_MOUSEBUTTONUP: + { + int x,y; + SDL_GetMouseState(&x, &y); + event_list.push_back(EventData{ + .event_type = SDLContext::Event::MOUSEUP, + .mouse_button = sdl_to_mousebutton(event.button.button), + .mouse_position = {x,y}, + }); + } + break; + + case SDL_MOUSEMOTION: + { + int x,y; + SDL_GetMouseState(&x, &y); + event_list.push_back(EventData{ + .event_type = SDLContext::Event::MOUSEMOVE, + .mouse_position = {x,y}, + }); + } + break; + + case SDL_MOUSEWHEEL: + { + int x, y; + SDL_GetMouseState(&x, &y); + + event_list.push_back(EventData{ + .event_type = SDLContext::Event::MOUSEWHEEL, + .mouse_position = {event.motion.x,event.motion.y}, + .wheel_delta = event.wheel.y, + .rel_mouse_move {event.motion.yrel,event.motion.xrel}, + }); + } + break; + } + } + return event_list; +} + diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 20e30b3..5b6b093 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -1,5 +1,5 @@ #pragma once - +#include <SDL2/SDL.h> #include <SDL2/SDL_keycode.h> #include <SDL2/SDL_rect.h> #include <SDL2/SDL_render.h> @@ -10,7 +10,10 @@ #include <string> #include "../api/Sprite.h" +#include "../api/KeyCodes.h" #include "../api/Transform.h" +#include "../api/Vector2.h" +#include "../api/Event.h" #include "api/Camera.h" #include "types.h" @@ -19,8 +22,9 @@ 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; - +//typedef SDL_Keycode CREPE_KEYCODES; +class LoopManager; +class InputSystem; /** * \class SDLContext * \brief Facade for the SDL library @@ -31,6 +35,26 @@ typedef SDL_Keycode CREPE_KEYCODES; class SDLContext { public: + enum Event{ + NONE = 0, + MOUSEDOWN, + MOUSEUP, + MOUSEMOVE, + MOUSEWHEEL, + KEYUP, + KEYDOWN, + SHUTDOWN + + }; + struct EventData { + SDLContext::Event event_type = SDLContext::Event::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. @@ -44,13 +68,18 @@ public: private: //! will only use handle_events - friend class LoopManager; + friend class InputSystem; /** * \brief Handles SDL events such as window close and input. * \param running Reference to a boolean flag that controls the main loop. */ void handle_events(bool & running); - + std::vector<SDLContext::EventData> get_events(); + + Keycode get_key(); + Keycode get_mouse(); + Keycode sdl_to_keycode(SDL_Keycode sdlKey); + MouseButton sdl_to_mousebutton(Uint8 sdl_button); private: //! Will only use get_ticks friend class AnimatorSystem; @@ -168,4 +197,5 @@ private: SDL_Rect viewport = {0, 0, 640, 480}; }; + } // namespace crepe |