From 3ba5aadd78bd8b48627c1efa51886d4b383e753f Mon Sep 17 00:00:00 2001 From: Derrick Timmermans Date: Wed, 10 Oct 2018 20:41:13 +0800 Subject: [PATCH 01/18] Implemented InputManager --- BloomFramework/BloomFramework.vcxproj | 3 + BloomFramework/BloomFramework.vcxproj.filters | 9 + BloomFramework/include/Framework.h | 3 +- BloomFramework/include/Game.h | 3 +- .../include/Input/InputDefinitions.h | 257 ++++++++++++++++++ BloomFramework/include/Input/InputManager.h | 48 ++++ BloomFramework/src/Game.cpp | 5 +- BloomFramework/src/Input/InputManager.cpp | 221 +++++++++++++++ Test Bench/GameObjectTest/RandomizerSystem.h | 2 +- 9 files changed, 545 insertions(+), 6 deletions(-) create mode 100644 BloomFramework/include/Input/InputDefinitions.h create mode 100644 BloomFramework/include/Input/InputManager.h create mode 100644 BloomFramework/src/Input/InputManager.cpp diff --git a/BloomFramework/BloomFramework.vcxproj b/BloomFramework/BloomFramework.vcxproj index 5998e042..741bdaf7 100644 --- a/BloomFramework/BloomFramework.vcxproj +++ b/BloomFramework/BloomFramework.vcxproj @@ -163,6 +163,8 @@ + + @@ -175,6 +177,7 @@ + diff --git a/BloomFramework/BloomFramework.vcxproj.filters b/BloomFramework/BloomFramework.vcxproj.filters index 3ceb7498..59ceb913 100644 --- a/BloomFramework/BloomFramework.vcxproj.filters +++ b/BloomFramework/BloomFramework.vcxproj.filters @@ -39,6 +39,9 @@ Source Files + + Source Files + @@ -86,6 +89,12 @@ Header Files\Systems + + Header Files + + + Header Files + diff --git a/BloomFramework/include/Framework.h b/BloomFramework/include/Framework.h index e72fb91e..aa0a8280 100644 --- a/BloomFramework/include/Framework.h +++ b/BloomFramework/include/Framework.h @@ -7,4 +7,5 @@ #include "Game.h" #include "GameObject.h" #include "Components/Components.h" -#include "Systems/Systems.h" \ No newline at end of file +#include "Systems/Systems.h" +#include "Input/InputManager.h" \ No newline at end of file diff --git a/BloomFramework/include/Game.h b/BloomFramework/include/Game.h index 190a4719..4c8905ec 100644 --- a/BloomFramework/include/Game.h +++ b/BloomFramework/include/Game.h @@ -4,7 +4,7 @@ #include "stdIncludes.h" #include "TextureStore.h" #include "Timer.h" - +#include "Input/InputManager.h" namespace bloom { class BLOOMFRAMEWORK_API Game { friend TextureStore::TextureStore(Game & object); @@ -48,5 +48,6 @@ namespace bloom { SDL_Color m_color; SDL_Event m_event; bool m_isRunning; + InputManager input; }; } \ No newline at end of file diff --git a/BloomFramework/include/Input/InputDefinitions.h b/BloomFramework/include/Input/InputDefinitions.h new file mode 100644 index 00000000..0468b079 --- /dev/null +++ b/BloomFramework/include/Input/InputDefinitions.h @@ -0,0 +1,257 @@ +#pragma once + +#include "stdIncludes.h" + +const int KEYBOARD_SIZE = 282; +namespace bloom { + enum KeyboardKey{ + KEY_UNKNOWN = SDL_SCANCODE_UNKNOWN, + KEY_A = SDL_SCANCODE_A, + KEY_B = SDL_SCANCODE_B, + KEY_C = SDL_SCANCODE_C, + KEY_D = SDL_SCANCODE_D, + KEY_E = SDL_SCANCODE_E, + KEY_F = SDL_SCANCODE_F, + KEY_G = SDL_SCANCODE_G, + KEY_H = SDL_SCANCODE_H, + KEY_I = SDL_SCANCODE_I, + KEY_J = SDL_SCANCODE_J, + KEY_K = SDL_SCANCODE_K, + KEY_L = SDL_SCANCODE_L, + KEY_M = SDL_SCANCODE_M, + KEY_N = SDL_SCANCODE_N, + KEY_O = SDL_SCANCODE_O, + KEY_P = SDL_SCANCODE_P, + KEY_Q = SDL_SCANCODE_Q, + KEY_R = SDL_SCANCODE_R, + KEY_S = SDL_SCANCODE_S, + KEY_T = SDL_SCANCODE_T, + KEY_U = SDL_SCANCODE_U, + KEY_V = SDL_SCANCODE_V, + KEY_W = SDL_SCANCODE_W, + KEY_X = SDL_SCANCODE_X, + KEY_Y = SDL_SCANCODE_Y, + KEY_Z = SDL_SCANCODE_Z, + KEY_1 = SDL_SCANCODE_1, + KEY_2 = SDL_SCANCODE_2, + KEY_3 = SDL_SCANCODE_3, + KEY_4 = SDL_SCANCODE_4, + KEY_5 = SDL_SCANCODE_5, + KEY_6 = SDL_SCANCODE_6, + KEY_7 = SDL_SCANCODE_7, + KEY_8 = SDL_SCANCODE_8, + KEY_9 = SDL_SCANCODE_9, + KEY_0 = SDL_SCANCODE_0, + KEY_RETURN = SDL_SCANCODE_RETURN, + KEY_ESCAPE = SDL_SCANCODE_ESCAPE, + KEY_BACKSPACE = SDL_SCANCODE_BACKSPACE, + KEY_TAB = SDL_SCANCODE_TAB, + KEY_SPACE = SDL_SCANCODE_SPACE, + KEY_MINUS = SDL_SCANCODE_MINUS, + KEY_EQUALS = SDL_SCANCODE_EQUALS, + KEY_LEFTBRACKET = SDL_SCANCODE_LEFTBRACKET, + KEY_RIGHTBRACKET = SDL_SCANCODE_RIGHTBRACKET, + KEY_BACKSLASH = SDL_SCANCODE_BACKSLASH, + KEY_NONUSHASH = SDL_SCANCODE_NONUSHASH, + KEY_SEMICOLON = SDL_SCANCODE_SEMICOLON, + KEY_APOSTROPHE = SDL_SCANCODE_APOSTROPHE, + KEY_GRAVE = SDL_SCANCODE_GRAVE, + KEY_COMMA = SDL_SCANCODE_COMMA, + KEY_PERIOD = SDL_SCANCODE_PERIOD, + KEY_SLASH = SDL_SCANCODE_SLASH, + KEY_CAPSLOCK = SDL_SCANCODE_CAPSLOCK, + KEY_F1 = SDL_SCANCODE_F1, + KEY_F2 = SDL_SCANCODE_F2, + KEY_F3 = SDL_SCANCODE_F3, + KEY_F4 = SDL_SCANCODE_F4, + KEY_F5 = SDL_SCANCODE_F5, + KEY_F6 = SDL_SCANCODE_F6, + KEY_F7 = SDL_SCANCODE_F7, + KEY_F8 = SDL_SCANCODE_F8, + KEY_F9 = SDL_SCANCODE_F9, + KEY_F10 = SDL_SCANCODE_F10, + KEY_F11 = SDL_SCANCODE_F11, + KEY_F12 = SDL_SCANCODE_F12, + KEY_PRINTSCREEN = SDL_SCANCODE_PRINTSCREEN, + KEY_SCROLLLOCK = SDL_SCANCODE_SCROLLLOCK, + KEY_PAUSE = SDL_SCANCODE_PAUSE, + KEY_INSERT = SDL_SCANCODE_INSERT, + KEY_HOME = SDL_SCANCODE_HOME, + KEY_PAGEUP = SDL_SCANCODE_PAGEUP, + KEY_DELETE = SDL_SCANCODE_DELETE, + KEY_END = SDL_SCANCODE_END, + KEY_PAGEDOWN = SDL_SCANCODE_PAGEDOWN, + KEY_RIGHT = SDL_SCANCODE_RIGHT, + KEY_LEFT = SDL_SCANCODE_LEFT, + KEY_DOWN = SDL_SCANCODE_DOWN, + KEY_UP = SDL_SCANCODE_UP, + KEY_NUMLOCKCLEAR = SDL_SCANCODE_NUMLOCKCLEAR, + KEY_KEYPAD_DIVIDE = SDL_SCANCODE_KP_DIVIDE, + KEY_KEYPAD_MULTIPLY = SDL_SCANCODE_KP_MULTIPLY, + KEY_KEYPAD_MINUS = SDL_SCANCODE_KP_MINUS, + KEY_KEYPAD_PLUS = SDL_SCANCODE_KP_PLUS, + KEY_KEYPAD_ENTER = SDL_SCANCODE_KP_ENTER, + KEY_KEYPAD_1 = SDL_SCANCODE_KP_1, + KEY_KEYPAD_2 = SDL_SCANCODE_KP_2, + KEY_KEYPAD_3 = SDL_SCANCODE_KP_3, + KEY_KEYPAD_4 = SDL_SCANCODE_KP_4, + KEY_KEYPAD_5 = SDL_SCANCODE_KP_5, + KEY_KEYPAD_6 = SDL_SCANCODE_KP_6, + KEY_KEYPAD_7 = SDL_SCANCODE_KP_7, + KEY_KEYPAD_8 = SDL_SCANCODE_KP_8, + KEY_KEYPAD_9 = SDL_SCANCODE_KP_9, + KEY_KEYPAD_0 = SDL_SCANCODE_KP_0, + KEY_KEYPAD_PERIOD = SDL_SCANCODE_KP_PERIOD, + KEY_NONUSBACKSLASH = SDL_SCANCODE_NONUSBACKSLASH, + KEY_APPLICATION = SDL_SCANCODE_APPLICATION, + KEY_POWER = SDL_SCANCODE_POWER, + KEY_KEYPAD_EQUALS = SDL_SCANCODE_KP_EQUALS, + KEY_F13 = SDL_SCANCODE_F13, + KEY_F14 = SDL_SCANCODE_F14, + KEY_F15 = SDL_SCANCODE_F15, + KEY_F16 = SDL_SCANCODE_F16, + KEY_F17 = SDL_SCANCODE_F17, + KEY_F18 = SDL_SCANCODE_F18, + KEY_F19 = SDL_SCANCODE_F19, + KEY_F20 = SDL_SCANCODE_F20, + KEY_F21 = SDL_SCANCODE_F21, + KEY_F22 = SDL_SCANCODE_F22, + KEY_F23 = SDL_SCANCODE_F23, + KEY_F24 = SDL_SCANCODE_F24, + KEY_EXECUTE = SDL_SCANCODE_EXECUTE, + KEY_HELP = SDL_SCANCODE_HELP, + KEY_MENU = SDL_SCANCODE_MENU, + KEY_SELECT = SDL_SCANCODE_SELECT, + KEY_STOP = SDL_SCANCODE_STOP, + KEY_AGAIN = SDL_SCANCODE_AGAIN, + KEY_UNDO = SDL_SCANCODE_UNDO, + KEY_CUT = SDL_SCANCODE_CUT, + KEY_COPY = SDL_SCANCODE_COPY, + KEY_PASTE = SDL_SCANCODE_PASTE, + KEY_FIND = SDL_SCANCODE_FIND, + KEY_MUTE = SDL_SCANCODE_MUTE, + KEY_VOLUMEUP = SDL_SCANCODE_VOLUMEUP, + KEY_VOLUMEDOWN = SDL_SCANCODE_VOLUMEDOWN, + KEY_KEYPAD_COMMA = SDL_SCANCODE_KP_COMMA, + KEY_KEYPAD_EQUALSAS400 = SDL_SCANCODE_KP_EQUALSAS400, + KEY_INTERNATIONAL1 = SDL_SCANCODE_INTERNATIONAL1, + KEY_INTERNATIONAL2 = SDL_SCANCODE_INTERNATIONAL2, + KEY_INTERNATIONAL3 = SDL_SCANCODE_INTERNATIONAL3, + KEY_INTERNATIONAL4 = SDL_SCANCODE_INTERNATIONAL4, + KEY_INTERNATIONAL5 = SDL_SCANCODE_INTERNATIONAL5, + KEY_INTERNATIONAL6 = SDL_SCANCODE_INTERNATIONAL6, + KEY_INTERNATIONAL7 = SDL_SCANCODE_INTERNATIONAL7, + KEY_INTERNATIONAL8 = SDL_SCANCODE_INTERNATIONAL8, + KEY_INTERNATIONAL9 = SDL_SCANCODE_INTERNATIONAL9, + KEY_LANG1 = SDL_SCANCODE_LANG1, + KEY_LANG2 = SDL_SCANCODE_LANG2, + KEY_LANG3 = SDL_SCANCODE_LANG3, + KEY_LANG4 = SDL_SCANCODE_LANG4, + KEY_LANG5 = SDL_SCANCODE_LANG5, + KEY_LANG6 = SDL_SCANCODE_LANG6, + KEY_LANG7 = SDL_SCANCODE_LANG7, + KEY_LANG8 = SDL_SCANCODE_LANG8, + KEY_LANG9 = SDL_SCANCODE_LANG9, + KEY_ALTERASE = SDL_SCANCODE_ALTERASE, + KEY_SYSREQ = SDL_SCANCODE_SYSREQ, + KEY_CANCEL = SDL_SCANCODE_CANCEL, + KEY_CLEAR = SDL_SCANCODE_CLEAR, + KEY_PRIOR = SDL_SCANCODE_PRIOR, + KEY_RETURN2 = SDL_SCANCODE_RETURN2, + KEY_SEPARATOR = SDL_SCANCODE_SEPARATOR, + KEY_OUT = SDL_SCANCODE_OUT, + KEY_OPER = SDL_SCANCODE_OPER, + KEY_CLEARAGAIN = SDL_SCANCODE_CLEARAGAIN, + KEY_CRSEL = SDL_SCANCODE_CRSEL, + KEY_EXSEL = SDL_SCANCODE_EXSEL, + KEY_KEYPAD_00 = SDL_SCANCODE_KP_00, + KEY_KEYPAD_000 = SDL_SCANCODE_KP_000, + KEY_THOUSANDSSEPARATOR = SDL_SCANCODE_THOUSANDSSEPARATOR, + KEY_DECIMALSEPARATOR = SDL_SCANCODE_DECIMALSEPARATOR, + KEY_CURRENCYUNIT = SDL_SCANCODE_CURRENCYUNIT, + KEY_CURRENCYSUBUNIT = SDL_SCANCODE_CURRENCYSUBUNIT, + KEY_KEYPAD_LEFTPAREN = SDL_SCANCODE_KP_LEFTPAREN, + KEY_KEYPAD_RIGHTPAREN = SDL_SCANCODE_KP_RIGHTPAREN, + KEY_KEYPAD_LEFTBRACE = SDL_SCANCODE_KP_LEFTBRACE, + KEY_KEYPAD_RIGHTBRACE = SDL_SCANCODE_KP_RIGHTBRACE, + KEY_KEYPAD_TAB = SDL_SCANCODE_KP_TAB, + KEY_KEYPAD_BACKSPACE = SDL_SCANCODE_KP_BACKSPACE, + KEY_KEYPAD_A = SDL_SCANCODE_KP_A, + KEY_KEYPAD_B = SDL_SCANCODE_KP_B, + KEY_KEYPAD_C = SDL_SCANCODE_KP_C, + KEY_KEYPAD_D = SDL_SCANCODE_KP_D, + KEY_KEYPAD_E = SDL_SCANCODE_KP_E, + KEY_KEYPAD_F = SDL_SCANCODE_KP_F, + KEY_KEYPAD_XOR = SDL_SCANCODE_KP_XOR, + KEY_KEYPAD_POWER = SDL_SCANCODE_KP_POWER, + KEY_KEYPAD_PERCENT = SDL_SCANCODE_KP_PERCENT, + KEY_KEYPAD_LESS = SDL_SCANCODE_KP_LESS, + KEY_KEYPAD_GREATER = SDL_SCANCODE_KP_GREATER, + KEY_KEYPAD_AMPERSAND = SDL_SCANCODE_KP_AMPERSAND, + KEY_KEYPAD_DBLAMPERSAND = SDL_SCANCODE_KP_DBLAMPERSAND, + KEY_KEYPAD_VERTICALBAR = SDL_SCANCODE_KP_VERTICALBAR, + KEY_KEYPAD_DBLVERTICALBAR = SDL_SCANCODE_KP_DBLVERTICALBAR, + KEY_KEYPAD_COLON = SDL_SCANCODE_KP_COLON, + KEY_KEYPAD_HASH = SDL_SCANCODE_KP_HASH, + KEY_KEYPAD_SPACE = SDL_SCANCODE_KP_SPACE, + KEY_KEYPAD_AT = SDL_SCANCODE_KP_AT, + KEY_KEYPAD_EXCLAM = SDL_SCANCODE_KP_EXCLAM, + KEY_KEYPAD_MEMSTORE = SDL_SCANCODE_KP_MEMSTORE, + KEY_KEYPAD_MEMRECALL = SDL_SCANCODE_KP_MEMRECALL, + KEY_KEYPAD_MEMCLEAR = SDL_SCANCODE_KP_MEMCLEAR, + KEY_KEYPAD_MEMADD = SDL_SCANCODE_KP_MEMADD, + KEY_KEYPAD_MEMSUBTRACT = SDL_SCANCODE_KP_MEMSUBTRACT, + KEY_KEYPAD_MEMMULTIPLY = SDL_SCANCODE_KP_MEMMULTIPLY, + KEY_KEYPAD_MEMDIVIDE = SDL_SCANCODE_KP_MEMDIVIDE, + KEY_KEYPAD_PLUSMINUS = SDL_SCANCODE_KP_PLUSMINUS, + KEY_KEYPAD_CLEAR = SDL_SCANCODE_KP_CLEAR, + KEY_KEYPAD_CLEARENTRY = SDL_SCANCODE_KP_CLEARENTRY, + KEY_KEYPAD_BINARY = SDL_SCANCODE_KP_BINARY, + KEY_KEYPAD_OCTAL = SDL_SCANCODE_KP_OCTAL, + KEY_KEYPAD_DECIMAL = SDL_SCANCODE_KP_DECIMAL, + KEY_KEYPAD_HEXADECIMAL = SDL_SCANCODE_KP_HEXADECIMAL, + KEY_LEFT_CTRL = SDL_SCANCODE_LCTRL, + KEY_LEFT_SHIFT = SDL_SCANCODE_LSHIFT, + KEY_LEFT_ALT = SDL_SCANCODE_LALT, + KEY_LEFT_GUI = SDL_SCANCODE_LGUI, + KEY_RIGHT_CTRL = SDL_SCANCODE_RCTRL, + KEY_RIGHT_SHIFT = SDL_SCANCODE_RSHIFT, + KEY_RIGHT_ALT = SDL_SCANCODE_RALT, + KEY_RIGHT_GUI = SDL_SCANCODE_RGUI, + KEY_MODE = SDL_SCANCODE_MODE, + KEY_AUDIONEXT = SDL_SCANCODE_AUDIONEXT, + KEY_AUDIOPREV = SDL_SCANCODE_AUDIOPREV, + KEY_AUDIOSTOP = SDL_SCANCODE_AUDIOSTOP, + KEY_AUDIOPLAY = SDL_SCANCODE_AUDIOPLAY, + KEY_AUDIOMUTE = SDL_SCANCODE_AUDIOMUTE, + KEY_MEDIASELECT = SDL_SCANCODE_MEDIASELECT, + KEY_WWW = SDL_SCANCODE_WWW, + KEY_MAIL = SDL_SCANCODE_MAIL, + KEY_CALCULATOR = SDL_SCANCODE_CALCULATOR, + KEY_COMPUTER = SDL_SCANCODE_COMPUTER, + KEY_AC_SEARCH = SDL_SCANCODE_AC_SEARCH, + KEY_AC_HOME = SDL_SCANCODE_AC_HOME, + KEY_AC_BACK = SDL_SCANCODE_AC_BACK, + KEY_AC_FORWARD = SDL_SCANCODE_AC_FORWARD, + KEY_AC_STOP = SDL_SCANCODE_AC_STOP, + KEY_AC_REFRESH = SDL_SCANCODE_AC_REFRESH, + KEY_AC_BOOKMARKS = SDL_SCANCODE_AC_BOOKMARKS, + KEY_BRIGHTNESSDOWN = SDL_SCANCODE_BRIGHTNESSDOWN, + KEY_BRIGHTNESSUP = SDL_SCANCODE_BRIGHTNESSUP, + KEY_DISPLAYSWITCH = SDL_SCANCODE_DISPLAYSWITCH, + KEY_KBDILLUMTOGGLE = SDL_SCANCODE_KBDILLUMTOGGLE, + KEY_KBDILLUMDOWN = SDL_SCANCODE_KBDILLUMDOWN, + KEY_KBDILLUMUP = SDL_SCANCODE_KBDILLUMUP, + KEY_EJECT = SDL_SCANCODE_EJECT, + KEY_SLEEP = SDL_SCANCODE_SLEEP + }; + + enum MouseButton{ + MOUSE_LEFT = SDL_BUTTON_LEFT, + MOUSE_MIDDLE = SDL_BUTTON_MIDDLE, + MOUSE_RIGHT = SDL_BUTTON_RIGHT, + + MOUSE_MAX // No button, just to define max + // array size. + }; +} \ No newline at end of file diff --git a/BloomFramework/include/Input/InputManager.h b/BloomFramework/include/Input/InputManager.h new file mode 100644 index 00000000..aa58c8c6 --- /dev/null +++ b/BloomFramework/include/Input/InputManager.h @@ -0,0 +1,48 @@ +#pragma once +#include "stdIncludes.h" +/// All keys are defined here. +#include "InputDefinitions.h" + +namespace bloom { + class BLOOMFRAMEWORK_API InputManager { + public: + InputManager(); + + void update(); + bool isKeyDown(int key); + bool isKeyUp(int key); + bool shift(); + bool ctrl(); + bool alt(); + bool isMouseDown(MouseButton button); + bool isMouseUp(MouseButton button); + bool isKeyPressed(KeyboardKey key); + bool isMousePressed(MouseButton button); + bool quitRequested(); + int getMouseX(); + int getMouseY(); + void lock(); + void unlock(); + bool isMouseInside(SDL_Rect rectangle); + static bool isPrintable(SDL_Keycode key); + + bool isPrintableKeyDown(); + std::string getCurPrintableKey(); + + private: + const uint8_t* m_keyboard; + + uint32_t m_mouse; + + int m_mouseX, m_mouseY; + int m_mouseMoveX, m_mouseMoveY; + int m_scrollX, m_scrollY; + bool m_keyDown[KEYBOARD_SIZE]; + bool m_keyUp[KEYBOARD_SIZE]; + bool m_mouseDown[MOUSE_MAX]; + bool m_mouseUp[MOUSE_MAX]; + bool m_will_quit; + int m_curPrintableKey; + bool m_isLocked; + }; +} \ No newline at end of file diff --git a/BloomFramework/src/Game.cpp b/BloomFramework/src/Game.cpp index 7a5030d9..d6108a6d 100644 --- a/BloomFramework/src/Game.cpp +++ b/BloomFramework/src/Game.cpp @@ -108,9 +108,8 @@ namespace bloom { } void Game::handleEvents() { - SDL_PollEvent(&m_event); - - if (m_event.type == SDL_QUIT) + input.update(); + if (input.quitRequested()) m_isRunning = false; } diff --git a/BloomFramework/src/Input/InputManager.cpp b/BloomFramework/src/Input/InputManager.cpp new file mode 100644 index 00000000..94d9f4a5 --- /dev/null +++ b/BloomFramework/src/Input/InputManager.cpp @@ -0,0 +1,221 @@ +#include "Input/InputManager.h" + +namespace bloom { + InputManager::InputManager() : + m_keyboard(nullptr), + m_mouse(0), + m_mouseX(0), + m_mouseY(0), + m_will_quit(false), + m_isLocked(false) {} + + void InputManager::update() { + for (int i = 0; i < KEYBOARD_SIZE; i++) { + m_keyDown[i] = false; + m_keyUp[i] = false; + } + for (int i = 0; i < MOUSE_MAX; i++) { + m_mouseDown[i] = false; + m_mouseUp[i] = false; + } + m_mouseMoveX = 0; + m_mouseMoveY = 0; + m_curPrintableKey = 0; + + // Get key events from the OS + SDL_Event event; + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + m_will_quit = true; + break; + + case SDL_KEYDOWN: { + m_keyboard = SDL_GetKeyboardState(nullptr); + + int index = event.key.keysym.scancode; + + m_keyDown[index] = true; + + if (InputManager::isPrintable(event.key.keysym.sym)) + m_curPrintableKey = event.key.keysym.sym; + } + break; + + case SDL_KEYUP: { + m_keyboard = SDL_GetKeyboardState(nullptr); + + int index = event.key.keysym.scancode; + m_keyUp[index] = true; + } + break; + + case SDL_MOUSEMOTION: + m_mouseX = event.motion.x; + m_mouseY = event.motion.y; + m_mouseMoveX = event.motion.xrel; + m_mouseMoveY = event.motion.yrel; + break; + + case SDL_MOUSEBUTTONDOWN: + m_mouse = SDL_GetMouseState(&(m_mouseX), + &(m_mouseY)); + + m_mouseDown[event.button.button] = true; + break; + + case SDL_MOUSEBUTTONUP: + m_mouse = SDL_GetMouseState(&(m_mouseX), + &(m_mouseY)); + + m_mouseDown[event.button.button] = true; + break; + + // Brand new SDL2 event. + case SDL_MOUSEWHEEL: + m_scrollX = event.wheel.x; + m_scrollY = event.wheel.y; + break; + + default: + break; + } + } + } + + bool InputManager::isKeyDown(int key) { + if (m_isLocked) return false; + + if (key < 0 || key >= KEYBOARD_SIZE) + return false; + + return (m_keyDown[key]); + } + + bool InputManager::isKeyUp(int key) { + if (m_isLocked) return false; + + if (key < 0 || key >= KEYBOARD_SIZE) + return false; + + return (m_keyUp[key]); + } + + bool InputManager::isKeyPressed(KeyboardKey key) { + if (m_isLocked) return false; + + if (!(m_keyboard)) + return false; + + int sdl_key = static_cast(key); + + if (m_keyboard[sdl_key]) + return true; + + return false; + } + + bool InputManager::shift() { + return (isKeyPressed(KEY_LEFT_SHIFT) || + isKeyPressed(KEY_RIGHT_SHIFT)); + } + + bool InputManager::ctrl() { + return (isKeyPressed(KEY_LEFT_CTRL) || + isKeyPressed(KEY_RIGHT_CTRL)); + } + + bool InputManager::alt() { + return (isKeyPressed(KEY_LEFT_ALT) || + isKeyPressed(KEY_RIGHT_ALT)); + } + + bool InputManager::isMouseDown(MouseButton button) { + if (m_isLocked) return false; + + if (button == MOUSE_MAX) + return false; + + return m_mouseDown[button]; + } + + bool InputManager::isMouseUp(MouseButton button) { + if (m_isLocked) return false; + + if (button == MOUSE_MAX) + return false; + + return m_mouseUp[button]; + } + + bool InputManager::isMousePressed(MouseButton button) { + if (m_isLocked) return false; + + switch (button) + { + case MOUSE_LEFT: + if (m_mouse & SDL_BUTTON(1)) + return true; + break; + case MOUSE_MIDDLE: + if (m_mouse & SDL_BUTTON(2)) + return true; + break; + case MOUSE_RIGHT: + if (m_mouse & SDL_BUTTON(3)) + return true; + break; + + default: + break; + } + + return false; + } + + int InputManager::getMouseX() { + return m_mouseX; + } + + int InputManager::getMouseY() { + return m_mouseY; + } + + bool InputManager::quitRequested() { + bool curr = m_will_quit; + m_will_quit = false; + return curr; + } + + bool InputManager::isMouseInside(SDL_Rect rectangle) { + if ((m_mouseX >= rectangle.x) && + (m_mouseX <= rectangle.x + rectangle.w) + && + (m_mouseY >= rectangle.y) && + (m_mouseY <= rectangle.y + rectangle.h)) + return true; + + return false; + } + + bool InputManager::isPrintable(SDL_Keycode key) { + return ((key > SDLK_SPACE) && (key < SDLK_z)); + } + + bool InputManager::isPrintableKeyDown() { + return (InputManager::isPrintable(m_curPrintableKey)); + } + + std::string InputManager::getCurPrintableKey() { + char c = static_cast(m_curPrintableKey);; + return (static_cast(&c)); + } + + void InputManager::lock() { + m_isLocked = true; + } + + void InputManager::unlock() { + m_isLocked = false; + } +} \ No newline at end of file diff --git a/Test Bench/GameObjectTest/RandomizerSystem.h b/Test Bench/GameObjectTest/RandomizerSystem.h index c94b3e68..bb10469e 100644 --- a/Test Bench/GameObjectTest/RandomizerSystem.h +++ b/Test Bench/GameObjectTest/RandomizerSystem.h @@ -11,7 +11,7 @@ class RandomPositionSystem : bloom::System { void bloom::System::update(std::optional dt = std::nullopt) { m_registry.view().each( - [this](auto entity, Position & pos) { + [=](auto entity, Position & pos) { if (!m_registry.has(entity)) { pos.x = rand() % 672; pos.y = rand() % 472; From 24cc6d80bfcfc2406633c79f0687f2677d5b17d6 Mon Sep 17 00:00:00 2001 From: Derrick Timmermans Date: Wed, 10 Oct 2018 20:54:27 +0800 Subject: [PATCH 02/18] Improve some odd code formatting --- BloomFramework/src/Input/InputManager.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/BloomFramework/src/Input/InputManager.cpp b/BloomFramework/src/Input/InputManager.cpp index 94d9f4a5..21241080 100644 --- a/BloomFramework/src/Input/InputManager.cpp +++ b/BloomFramework/src/Input/InputManager.cpp @@ -39,16 +39,17 @@ namespace bloom { if (InputManager::isPrintable(event.key.keysym.sym)) m_curPrintableKey = event.key.keysym.sym; + break; } - break; case SDL_KEYUP: { m_keyboard = SDL_GetKeyboardState(nullptr); int index = event.key.keysym.scancode; m_keyUp[index] = true; + + break; } - break; case SDL_MOUSEMOTION: m_mouseX = event.motion.x; From 140cbc965a3da48247da1931efd7d578131a4b08 Mon Sep 17 00:00:00 2001 From: Derrick Timmermans Date: Thu, 11 Oct 2018 22:43:00 +0800 Subject: [PATCH 03/18] Add mouse testing in Test Bench --- BloomFramework/include/Game.h | 4 ++-- BloomFramework/include/stdIncludes.h | 1 - Test Bench/Assets/testCursor.png | Bin 0 -> 4584 bytes Test Bench/main.cpp | 10 +++++++++- 4 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 Test Bench/Assets/testCursor.png diff --git a/BloomFramework/include/Game.h b/BloomFramework/include/Game.h index 4c8905ec..d12f93c1 100644 --- a/BloomFramework/include/Game.h +++ b/BloomFramework/include/Game.h @@ -39,7 +39,7 @@ namespace bloom { TextureStore textures = TextureStore(m_renderer); Timer timer; - + InputManager input; protected: SDL_Renderer * m_renderer = nullptr; SDL_Window * m_window = nullptr; @@ -48,6 +48,6 @@ namespace bloom { SDL_Color m_color; SDL_Event m_event; bool m_isRunning; - InputManager input; + }; } \ No newline at end of file diff --git a/BloomFramework/include/stdIncludes.h b/BloomFramework/include/stdIncludes.h index 042ae61d..ab6c0079 100644 --- a/BloomFramework/include/stdIncludes.h +++ b/BloomFramework/include/stdIncludes.h @@ -6,7 +6,6 @@ #define BLOOMFRAMEWORK_API __declspec(dllimport) #endif - #include "SDL.h" #include "SDL_image.h" #include "SDL_ttf.h" diff --git a/Test Bench/Assets/testCursor.png b/Test Bench/Assets/testCursor.png new file mode 100644 index 0000000000000000000000000000000000000000..b483f525d3dc938d2d95122c336975ed5117e6fe GIT binary patch literal 4584 zcmbW5c|4SB*vFq)m>F49vSrAgy@jzfD3XLyqwGuMD9g+sS;vwhyDXJKi>N_JVRFhS zl9r()ikMAipIq&I@_n&v>GxN-SU%%^n{qE~|?&tY@Cgq@=m5_k600coo z*840RAP9y7p9g4uFhfN-)4>7hXKrf_LA9Cdmc01DnBUvR$^u&Dy{|qjzX(BM{MHuc zN6x^;KPnuMHHY}RvY*W+P%A4fijQh&RP6KhC217O-wH^$dbj0Zc-WDwBXV~z@C~a)2gnLL{GU&ZkB>YYu|fak_ulk9=4Pv;pL%NUijGt1%f}m_R!NS!m3qa(RFE9y zgdB8oc45b9Igd#bU$MCTXS|J5`@=sE9rb)k9&>m{!WpPnBg{UL$Br8YBh0RmjZ@g0 z`Qp6ogV;q8ItEJ9%)kH*Wt1T4kMPZ7HTw8m#9&iigFYVWHym@U#zRZxi*s{x+wvV) z2Xz^m>{pYMlkGp0Bo2Bxr!z z!-#vgWtv!IJ9~Rdnh9PL-jy4^bAv1M)2B}cpq5sQEnh+V`GS1v46N^VEvsG+-`K~0 zsn?lEAK5C29fkGj^BsM$;OVtl5a98xmGLvMgG5A{NZv=9h*~4{;lqdaPf=Gp5?%~H zVYAsfz}gj!Mk<@U*u4Pla3V%T6D#mtJ}rS6B0)VU5PCJ!QpGh3;T5f@sE`>(bh)+& ztp*wnBLuBlq_7ZQ!Rq9XQo73=jIUtRyV~~ikXobcFybnUwi!D>6A=hHD~rWa!Yg7! z%v#tZE_16j;V22nLQmA<#}pU36JJC5)5=*Atv??PA(^yT2{e_kyh9XM!Htn8HgggQ z%gA2?$GJ~paLyvc6X1Ds(@^~Ef08ss8)peIohZ@9_&Qn%1o!JJ6G3V>(L{I&!lJk* z`tn4$AAy2JQ#lBEqT}3QGgjqa2Jwstd>jKqCB%s0K%xj-6@<)LG(Q3ssZBf=XvT7l z06&>dJ*fC~R4*CPMh5R^NU?}KaZS&u{40^c$DD=z)VP>baOH_i8XisM(G1oQYjhql zh$Yw`$1q%z{FBjS`r9zXacO7FY#`S8_hujJ3uGy@3NW zA@(4YNQn7q2T11?h|HRF4uTg<u6Jp{}`jiu7UifYr|1W zQb?~2mKsZk^b^F=JU7TlcA)q6p&f~^G~MY&s)$|EI$d3R*7SlWVF)A3aq0<&TV)br zREY~N%;7|F9K7-`<9I~|?U6Xn1TR@nf?LJ$qR3-Fh;p1OVu-CJ@`eq96}H4k+>BxL;Hh&wA|FenO zfDw=>=i|0yw2q^8^2@1R+`0%qLYD z9AkKoAu-OYutFL*SaI38Ryoo1?@7`6Y^8M!VvE@m@ZR%;!N|&k3n377n$!DsJdw~m zcBWjR!n{tiU06l>?wc&Ow0izF*O=HGmv2t5?M4TdX-&(^Z}waajT&c8MG~srQzor^ zXTq^!TFe_o4l=b#8b0s#cB*e}!OTS7ayg~EQ4Gbrmve#8e5;V30;cLXjgb4i99b@`UfiZ98(+S-fBY}aSRFeH-5Z@c0=}dl|JcFN_P%_OSa47FPbWY zss(a61epSbJ4KfovbSzeP;3}~P<06*GyY?XqcQ2e>g~4TTjxZ8TH?@9-ZpP>gQ)8t z^4^l|9qQmf`{YaXTNSo(zs;falnhZMrQTH)ID0e4&y~-0U!9=s$@pQm`bib zN_Ky)s60;9Q+ax#zky;G*vc;|OXq9e==m8OVHXoYYvgeWiUx5kbjARbpxF2&xqsx_ zu{g|bq8;Nx_UB5d`}}X3I9Hw!Bnx5VShDNu23xN~@;GvZfEjUQZx95`0?Pm5z#|*) z6x0#{Gb4%{!!(Xui_T4JUk?UWSly5Ad ztSoxiYEw2}9&mg$Q@a%~sc{64ASD#&2U*b&$g^cS;#tqMNanC8O#x6t<&ePLpqfr+ z45lT!eC0Z<$vGvqDq$*a+W*#9i=d1n%ivSbll$>_Dx6Z(Ji=M;M^rt7y!?P3LoKj9XN+xOZ%$-wjNPZcrv-d(( zA6I%tHFGbPH_NZq@VDVUd3>j&`iXb@Rq%T_e7shg6Q!p}qhGY}ou}XCOOZND{k~oU zr=-fL&UWBL#$fF7T(9ZLc}!Zb=Y|5XSQor%VIwoT`9|sSZGq#vY{02X9Q8JhvBA=9 zDM|(!pwTuQ&DqRd_)f)L$fYuX(#pH|F`_}A>+F>_Hmaj0cp_<~R$jhwf6r`l0W0}% zcQUxr9(_%z-{5mA`(U4m4)3DBNxZT+*lw4=g}+!N6YQ=3n(%how%M16 zyC@floSF;OQYyTq6kPUq+8460%|<+ycr*i%kox1xy(`bHe+>F*>9&To%SuQO3V{1! z1^&fy=e5pB1=7Pqn2ES~i*PY;58%4`WjJ0h*7Vr_q&h{T8|+8Wv++>P=HvTr+9@Kf zfaM~~de&z{>7-}hRwc@Kx1O*&=nPh50#DlHw&#K7H)XJcapzkFqgJ%GR7Ig%z(rPd zzAWG22-wdHqsk4|Bs(HN8S6RGojeb!Tt9G@bvEX`w>QS*z{CUw5pp_T|1$My<%dUT$Xd>3OG`mR%YcEPgP! z3uiN)sK1o$8e7DPFy~@84KWAaJZ{ey^N%U48y!A+}r^R2nqiVm(C3dizQFQENRxEj@iIiR@JoWtb zZn8qd#T=nKR`g8~eqLK8YQ2M|@AtW|XdRK4!h)5;wY2Wngc7$O5L}QlPz`^5u`j4$ z)@+W^q1zNNqPVliAgE{NR-mIUQah?YEu($rW|>8J7$Z4wi+A_;(1xNn6|4{lfwQZ-Mh)U`F$qqL#0z6lHCqhH2x_M}d4xYLYh8|_0mFUd zn%w@}PHwWq21si>+-9GOyXyR{4M<7srbVrkr)@7@ywJIDCZ0roGlw7PYcfj8-MJz% zKR-XU^E~wQ)i{MSoHqLTekW(vxQPVaRHQUPuj_TC5i`!=&bLbPzaqXY6;fXMJnH+r zbMntoOPCqibnAoq?n%+)-ZvN02cVp>fDCP>a@8T{_48>_4SuFsGG^q5mT$Y-GLms- ze`fadHlA=bo)nuuIaR1_00}D#U@UK2Q_pSm=4R8I7TzxuEqrWz8h-ZO(+QHc8g%J? z7+E}hY+_X`Zq`U;textures.load("Assets/testCursor.png"); game->textures.load("Assets/OverworldTestSpritesheet.png", SDL_Color{ 64, 176, 104, 113 }); game->textures.load("Assets/TestChar.png", SDL_Color{ 144,168,0,0 }); + TestChar cursor = TestChar(testRegistry, game); + TestChar testSprite = TestChar(testRegistry, game); testSprite.init(SDL_Rect{ 0,0,128,128 }, "Assets/OverworldTestSpritesheet.png", SDL_Rect{ 0,0,32,32 }); renderSysTest.update(); @@ -61,13 +64,18 @@ int main() { // Randomizes position of entities(excluding those with `NoRandomPos` Component. RandomPositionSystem randomizer(testRegistry); - + cursor.init(SDL_Rect{ 0,0,39,55 }, "Assets/testCursor.png"); + cursor.disableRandomPos(); + int testX = 0, testY = 0; while (game->isRunning()) { // If manual control of entities is required, this is the method to do so. auto & testGOpos = testRegistry.get(testGO.getEntityID()); testGOpos.x = testX++; testGOpos.y = testY++; + auto & cursorPos = testRegistry.get(cursor.getEntityID()); + cursorPos.x = game->input.getMouseX(); + cursorPos.y = game->input.getMouseY(); // Demo ends here. framestart = SDL_GetTicks(); game->handleEvents(); From 9e5ad2c9b27ef934d47b6a2d234585f803e55a68 Mon Sep 17 00:00:00 2001 From: Derrick Timmermans Date: Tue, 13 Nov 2018 00:09:44 +0800 Subject: [PATCH 04/18] Some tidying --- .../include/Input/InputDefinitions.h | 5 +- BloomFramework/src/Input/InputManager.cpp | 61 ++++++++----------- 2 files changed, 30 insertions(+), 36 deletions(-) diff --git a/BloomFramework/include/Input/InputDefinitions.h b/BloomFramework/include/Input/InputDefinitions.h index 0468b079..2216d7f0 100644 --- a/BloomFramework/include/Input/InputDefinitions.h +++ b/BloomFramework/include/Input/InputDefinitions.h @@ -2,7 +2,6 @@ #include "stdIncludes.h" -const int KEYBOARD_SIZE = 282; namespace bloom { enum KeyboardKey{ KEY_UNKNOWN = SDL_SCANCODE_UNKNOWN, @@ -243,7 +242,9 @@ namespace bloom { KEY_KBDILLUMDOWN = SDL_SCANCODE_KBDILLUMDOWN, KEY_KBDILLUMUP = SDL_SCANCODE_KBDILLUMUP, KEY_EJECT = SDL_SCANCODE_EJECT, - KEY_SLEEP = SDL_SCANCODE_SLEEP + KEY_SLEEP = SDL_SCANCODE_SLEEP, + + KEYBOARD_SIZE // This defines the keyboard size }; enum MouseButton{ diff --git a/BloomFramework/src/Input/InputManager.cpp b/BloomFramework/src/Input/InputManager.cpp index 21241080..be8b9bae 100644 --- a/BloomFramework/src/Input/InputManager.cpp +++ b/BloomFramework/src/Input/InputManager.cpp @@ -26,58 +26,55 @@ namespace bloom { SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { - case SDL_QUIT: + case SDL_QUIT: { m_will_quit = true; break; - + } case SDL_KEYDOWN: { m_keyboard = SDL_GetKeyboardState(nullptr); + SDL_Keysym pressedKey = event.key.keysym; - int index = event.key.keysym.scancode; + m_keyDown[pressedKey.scancode] = true; - m_keyDown[index] = true; + if (InputManager::isPrintable(pressedKey.sym)) + m_curPrintableKey = pressedKey.sym; - if (InputManager::isPrintable(event.key.keysym.sym)) - m_curPrintableKey = event.key.keysym.sym; - break; + break; } - case SDL_KEYUP: { m_keyboard = SDL_GetKeyboardState(nullptr); + SDL_Keysym releasedKey = event.key.keysym; - int index = event.key.keysym.scancode; - m_keyUp[index] = true; + m_keyUp[releasedKey.scancode] = true; break; } - - case SDL_MOUSEMOTION: + case SDL_MOUSEMOTION: { m_mouseX = event.motion.x; m_mouseY = event.motion.y; m_mouseMoveX = event.motion.xrel; m_mouseMoveY = event.motion.yrel; break; - - case SDL_MOUSEBUTTONDOWN: + } + case SDL_MOUSEBUTTONDOWN: { m_mouse = SDL_GetMouseState(&(m_mouseX), &(m_mouseY)); m_mouseDown[event.button.button] = true; break; - - case SDL_MOUSEBUTTONUP: + } + case SDL_MOUSEBUTTONUP: { m_mouse = SDL_GetMouseState(&(m_mouseX), &(m_mouseY)); m_mouseDown[event.button.button] = true; break; - - // Brand new SDL2 event. - case SDL_MOUSEWHEEL: + } + case SDL_MOUSEWHEEL: { m_scrollX = event.wheel.x; m_scrollY = event.wheel.y; break; - + } default: break; } @@ -108,27 +105,22 @@ namespace bloom { if (!(m_keyboard)) return false; - int sdl_key = static_cast(key); - - if (m_keyboard[sdl_key]) + if (m_keyboard[key]) return true; return false; } bool InputManager::shift() { - return (isKeyPressed(KEY_LEFT_SHIFT) || - isKeyPressed(KEY_RIGHT_SHIFT)); + return (isKeyPressed(KEY_LEFT_SHIFT) || isKeyPressed(KEY_RIGHT_SHIFT)); } bool InputManager::ctrl() { - return (isKeyPressed(KEY_LEFT_CTRL) || - isKeyPressed(KEY_RIGHT_CTRL)); + return (isKeyPressed(KEY_LEFT_CTRL) || isKeyPressed(KEY_RIGHT_CTRL)); } bool InputManager::alt() { - return (isKeyPressed(KEY_LEFT_ALT) || - isKeyPressed(KEY_RIGHT_ALT)); + return (isKeyPressed(KEY_LEFT_ALT) || isKeyPressed(KEY_RIGHT_ALT)); } bool InputManager::isMouseDown(MouseButton button) { @@ -158,10 +150,12 @@ namespace bloom { if (m_mouse & SDL_BUTTON(1)) return true; break; + case MOUSE_MIDDLE: if (m_mouse & SDL_BUTTON(2)) return true; break; + case MOUSE_RIGHT: if (m_mouse & SDL_BUTTON(3)) return true; @@ -189,11 +183,10 @@ namespace bloom { } bool InputManager::isMouseInside(SDL_Rect rectangle) { - if ((m_mouseX >= rectangle.x) && - (m_mouseX <= rectangle.x + rectangle.w) - && - (m_mouseY >= rectangle.y) && - (m_mouseY <= rectangle.y + rectangle.h)) + if ((m_mouseX >= rectangle.x) + && (m_mouseX <= rectangle.x + rectangle.w) + && (m_mouseY >= rectangle.y) + && (m_mouseY <= rectangle.y + rectangle.h)) return true; return false; From 9c8161724db8c993821fc282e1616b019760a428 Mon Sep 17 00:00:00 2001 From: Derrick Timmermans Date: Fri, 1 Mar 2019 22:46:19 +0800 Subject: [PATCH 05/18] Optimizations --- BloomFramework/include/Input/InputManager.h | 26 +++---- BloomFramework/src/Input/InputManager.cpp | 86 ++++++++------------- Test Bench/main.cpp | 4 +- 3 files changed, 47 insertions(+), 69 deletions(-) diff --git a/BloomFramework/include/Input/InputManager.h b/BloomFramework/include/Input/InputManager.h index aa58c8c6..3b295df1 100644 --- a/BloomFramework/include/Input/InputManager.h +++ b/BloomFramework/include/Input/InputManager.h @@ -1,4 +1,6 @@ #pragma once + +#include #include "stdIncludes.h" /// All keys are defined here. #include "InputDefinitions.h" @@ -25,24 +27,20 @@ namespace bloom { void unlock(); bool isMouseInside(SDL_Rect rectangle); static bool isPrintable(SDL_Keycode key); - - bool isPrintableKeyDown(); - std::string getCurPrintableKey(); + std::string getPrintable(); private: - const uint8_t* m_keyboard; + const uint8_t* m_keyboard = nullptr; uint32_t m_mouse; - int m_mouseX, m_mouseY; - int m_mouseMoveX, m_mouseMoveY; - int m_scrollX, m_scrollY; - bool m_keyDown[KEYBOARD_SIZE]; - bool m_keyUp[KEYBOARD_SIZE]; - bool m_mouseDown[MOUSE_MAX]; - bool m_mouseUp[MOUSE_MAX]; - bool m_will_quit; - int m_curPrintableKey; - bool m_isLocked; + int m_mouseX = 0, m_mouseY = 0; + int m_mouseMoveX = 0, m_mouseMoveY = 0; + int m_scrollX = 0, m_scrollY = 0; + std::array m_keyState; + std::array m_mouseState; + bool m_will_quit = false; + bool m_isLocked = false; + std::string m_printable = ""; }; } \ No newline at end of file diff --git a/BloomFramework/src/Input/InputManager.cpp b/BloomFramework/src/Input/InputManager.cpp index be8b9bae..c6de4427 100644 --- a/BloomFramework/src/Input/InputManager.cpp +++ b/BloomFramework/src/Input/InputManager.cpp @@ -7,20 +7,22 @@ namespace bloom { m_mouseX(0), m_mouseY(0), m_will_quit(false), - m_isLocked(false) {} + m_isLocked(false), + m_keyState(), + m_mouseState() {} void InputManager::update() { - for (int i = 0; i < KEYBOARD_SIZE; i++) { - m_keyDown[i] = false; - m_keyUp[i] = false; - } - for (int i = 0; i < MOUSE_MAX; i++) { - m_mouseDown[i] = false; - m_mouseUp[i] = false; - } + for (int i = 0; i < KEYBOARD_SIZE; i++) + m_keyState[i] = false; + + for (int i = 0; i < MOUSE_MAX; i++) + m_mouseState[i] = false; + m_mouseMoveX = 0; m_mouseMoveY = 0; - m_curPrintableKey = 0; + m_printable = ""; + m_keyboard = SDL_GetKeyboardState(nullptr); + m_mouse = SDL_GetMouseState(&m_mouseX, &m_mouseY); // Get key events from the OS SDL_Event event; @@ -31,21 +33,19 @@ namespace bloom { break; } case SDL_KEYDOWN: { - m_keyboard = SDL_GetKeyboardState(nullptr); SDL_Keysym pressedKey = event.key.keysym; - m_keyDown[pressedKey.scancode] = true; + m_keyState[pressedKey.scancode] = 1; - if (InputManager::isPrintable(pressedKey.sym)) - m_curPrintableKey = pressedKey.sym; + if (isPrintable(pressedKey.sym)) + m_printable = pressedKey.sym; break; } case SDL_KEYUP: { - m_keyboard = SDL_GetKeyboardState(nullptr); SDL_Keysym releasedKey = event.key.keysym; - m_keyUp[releasedKey.scancode] = true; + m_keyState[releasedKey.scancode] = -1; break; } @@ -57,17 +57,11 @@ namespace bloom { break; } case SDL_MOUSEBUTTONDOWN: { - m_mouse = SDL_GetMouseState(&(m_mouseX), - &(m_mouseY)); - - m_mouseDown[event.button.button] = true; + m_mouseState[event.button.button] = 1; break; } case SDL_MOUSEBUTTONUP: { - m_mouse = SDL_GetMouseState(&(m_mouseX), - &(m_mouseY)); - - m_mouseDown[event.button.button] = true; + m_mouseState[event.button.button] = -1; break; } case SDL_MOUSEWHEEL: { @@ -87,7 +81,7 @@ namespace bloom { if (key < 0 || key >= KEYBOARD_SIZE) return false; - return (m_keyDown[key]); + return (m_keyState[key] == 1); } bool InputManager::isKeyUp(int key) { @@ -96,7 +90,7 @@ namespace bloom { if (key < 0 || key >= KEYBOARD_SIZE) return false; - return (m_keyUp[key]); + return (m_keyState[key] == -1); } bool InputManager::isKeyPressed(KeyboardKey key) { @@ -129,7 +123,7 @@ namespace bloom { if (button == MOUSE_MAX) return false; - return m_mouseDown[button]; + return (m_mouseState[button] == 1); } bool InputManager::isMouseUp(MouseButton button) { @@ -138,7 +132,7 @@ namespace bloom { if (button == MOUSE_MAX) return false; - return m_mouseUp[button]; + return (m_mouseState[button] == -1); } bool InputManager::isMousePressed(MouseButton button) { @@ -147,25 +141,17 @@ namespace bloom { switch (button) { case MOUSE_LEFT: - if (m_mouse & SDL_BUTTON(1)) - return true; - break; + return (m_mouse & SDL_BUTTON(1)); case MOUSE_MIDDLE: - if (m_mouse & SDL_BUTTON(2)) - return true; - break; + return (m_mouse & SDL_BUTTON(2)); case MOUSE_RIGHT: - if (m_mouse & SDL_BUTTON(3)) - return true; - break; + return (m_mouse & SDL_BUTTON(3)); default: - break; + return false; } - - return false; } int InputManager::getMouseX() { @@ -183,26 +169,18 @@ namespace bloom { } bool InputManager::isMouseInside(SDL_Rect rectangle) { - if ((m_mouseX >= rectangle.x) + return ((m_mouseX >= rectangle.x) && (m_mouseX <= rectangle.x + rectangle.w) - && (m_mouseY >= rectangle.y) - && (m_mouseY <= rectangle.y + rectangle.h)) - return true; - - return false; + && (m_mouseY >= rectangle.y) + && (m_mouseY <= rectangle.y + rectangle.h)); } bool InputManager::isPrintable(SDL_Keycode key) { - return ((key > SDLK_SPACE) && (key < SDLK_z)); - } - - bool InputManager::isPrintableKeyDown() { - return (InputManager::isPrintable(m_curPrintableKey)); + return ((key >= SDLK_SPACE) && (key <= SDLK_z)); } - std::string InputManager::getCurPrintableKey() { - char c = static_cast(m_curPrintableKey);; - return (static_cast(&c)); + std::string InputManager::getPrintable() { + return m_printable; } void InputManager::lock() { diff --git a/Test Bench/main.cpp b/Test Bench/main.cpp index 1b09d02f..9018815d 100644 --- a/Test Bench/main.cpp +++ b/Test Bench/main.cpp @@ -29,7 +29,7 @@ inline int rstep(int n) { void test_player(const std::filesystem::path& musicPath, const std::filesystem::path& soundsPath) { //MusicTrack track1{ musicPath / L"music_007.mp3" }; - + music.queue.setVolume(0.0); music.push(musicPath / L"music_001.mp3"); music.push(musicPath / L"music_002.mp3"); music.push(musicPath / L"music_003.mp3"); @@ -124,6 +124,7 @@ void test_drawer(const std::filesystem::path& assetsPath) { cursorPos.x = game->input.getMouseX(); cursorPos.y = game->input.getMouseY(); + std::cout << game->input.getPrintable(); if (testGOpos.x >= WINDOW_WIDTH) { testGOpos.x = -testGOsize.w; testX = rstep(10); testY = rstep(10); } @@ -147,6 +148,7 @@ void test_drawer(const std::filesystem::path& assetsPath) { game->delay(framedelay - frametime); } } + std::cout << std::endl; game->destroy(); } From 4f8c3bb9bbb7b901df69b77cd34dadba0d949c58 Mon Sep 17 00:00:00 2001 From: Derrick Timmermans Date: Fri, 1 Mar 2019 23:06:51 +0800 Subject: [PATCH 06/18] More optimizations --- BloomFramework/src/Input/InputManager.cpp | 38 +++-------------------- 1 file changed, 5 insertions(+), 33 deletions(-) diff --git a/BloomFramework/src/Input/InputManager.cpp b/BloomFramework/src/Input/InputManager.cpp index c6de4427..a095f7a3 100644 --- a/BloomFramework/src/Input/InputManager.cpp +++ b/BloomFramework/src/Input/InputManager.cpp @@ -76,33 +76,15 @@ namespace bloom { } bool InputManager::isKeyDown(int key) { - if (m_isLocked) return false; - - if (key < 0 || key >= KEYBOARD_SIZE) - return false; - - return (m_keyState[key] == 1); + return (!m_isLocked && (key < 0 || key >= KEYBOARD_SIZE) && m_keyState[key] == 1); } bool InputManager::isKeyUp(int key) { - if (m_isLocked) return false; - - if (key < 0 || key >= KEYBOARD_SIZE) - return false; - - return (m_keyState[key] == -1); + return (!m_isLocked && (key < 0 || key >= KEYBOARD_SIZE) && m_keyState[key] == -1); } bool InputManager::isKeyPressed(KeyboardKey key) { - if (m_isLocked) return false; - - if (!(m_keyboard)) - return false; - - if (m_keyboard[key]) - return true; - - return false; + return (!m_isLocked && m_keyboard && static_cast(m_keyboard[key])); } bool InputManager::shift() { @@ -118,21 +100,11 @@ namespace bloom { } bool InputManager::isMouseDown(MouseButton button) { - if (m_isLocked) return false; - - if (button == MOUSE_MAX) - return false; - - return (m_mouseState[button] == 1); + return (!m_isLocked && button < MOUSE_MAX && m_mouseState[button] == 1); } bool InputManager::isMouseUp(MouseButton button) { - if (m_isLocked) return false; - - if (button == MOUSE_MAX) - return false; - - return (m_mouseState[button] == -1); + return (!m_isLocked && button < MOUSE_MAX && m_mouseState[button] == -1); } bool InputManager::isMousePressed(MouseButton button) { From 779507700695e8ed27ae679b400992e933e1077f Mon Sep 17 00:00:00 2001 From: Anatoly Pitikin Date: Sat, 2 Mar 2019 00:54:59 +0300 Subject: [PATCH 07/18] Much more optimizations - Use enum class instead of enum - Add const and noexcept qualifiers - Make isPrintable private - Rename m_will_quit and m_isLocked to m_quitState and m_lockState - Simplify clearing m_printable in update() --- .../include/Input/InputDefinitions.h | 4 +- BloomFramework/include/Input/InputManager.h | 49 +++++----- BloomFramework/src/Input/InputManager.cpp | 95 +++++++++---------- 3 files changed, 73 insertions(+), 75 deletions(-) diff --git a/BloomFramework/include/Input/InputDefinitions.h b/BloomFramework/include/Input/InputDefinitions.h index 2216d7f0..0d36a7f9 100644 --- a/BloomFramework/include/Input/InputDefinitions.h +++ b/BloomFramework/include/Input/InputDefinitions.h @@ -3,7 +3,7 @@ #include "stdIncludes.h" namespace bloom { - enum KeyboardKey{ + enum class KeyboardKey{ KEY_UNKNOWN = SDL_SCANCODE_UNKNOWN, KEY_A = SDL_SCANCODE_A, KEY_B = SDL_SCANCODE_B, @@ -247,7 +247,7 @@ namespace bloom { KEYBOARD_SIZE // This defines the keyboard size }; - enum MouseButton{ + enum class MouseButton{ MOUSE_LEFT = SDL_BUTTON_LEFT, MOUSE_MIDDLE = SDL_BUTTON_MIDDLE, MOUSE_RIGHT = SDL_BUTTON_RIGHT, diff --git a/BloomFramework/include/Input/InputManager.h b/BloomFramework/include/Input/InputManager.h index 3b295df1..a07015a2 100644 --- a/BloomFramework/include/Input/InputManager.h +++ b/BloomFramework/include/Input/InputManager.h @@ -8,39 +8,40 @@ namespace bloom { class BLOOMFRAMEWORK_API InputManager { public: - InputManager(); + InputManager() noexcept; void update(); - bool isKeyDown(int key); - bool isKeyUp(int key); - bool shift(); - bool ctrl(); - bool alt(); - bool isMouseDown(MouseButton button); - bool isMouseUp(MouseButton button); - bool isKeyPressed(KeyboardKey key); - bool isMousePressed(MouseButton button); - bool quitRequested(); - int getMouseX(); - int getMouseY(); - void lock(); - void unlock(); - bool isMouseInside(SDL_Rect rectangle); - static bool isPrintable(SDL_Keycode key); - std::string getPrintable(); + bool isKeyDown(KeyboardKey key) const noexcept; + bool isKeyUp(KeyboardKey key) const noexcept; + bool shift() const noexcept; + bool ctrl() const noexcept; + bool alt() const noexcept; + bool isMouseDown(MouseButton button) const noexcept; + bool isMouseUp(MouseButton button) const noexcept; + bool isKeyPressed(KeyboardKey key) const noexcept; + bool isMousePressed(MouseButton button) const noexcept; + bool quitRequested() noexcept; + int getMouseX() const noexcept; + int getMouseY() const noexcept; + void lock() noexcept; + void unlock() noexcept; + bool isMouseInside(SDL_Rect rectangle) const noexcept; + std::string getPrintable() const noexcept; private: + static bool isPrintable(SDL_Keycode key) noexcept; + const uint8_t* m_keyboard = nullptr; - uint32_t m_mouse; + uint32_t m_mouse = 0; int m_mouseX = 0, m_mouseY = 0; int m_mouseMoveX = 0, m_mouseMoveY = 0; int m_scrollX = 0, m_scrollY = 0; - std::array m_keyState; - std::array m_mouseState; - bool m_will_quit = false; - bool m_isLocked = false; - std::string m_printable = ""; + std::array(KeyboardKey::KEYBOARD_SIZE)> m_keyState; + std::array(MouseButton::MOUSE_MAX)> m_mouseState; + bool m_quitState = false; + bool m_lockState = false; + std::string m_printable{ "" }; }; } \ No newline at end of file diff --git a/BloomFramework/src/Input/InputManager.cpp b/BloomFramework/src/Input/InputManager.cpp index a095f7a3..072ccddb 100644 --- a/BloomFramework/src/Input/InputManager.cpp +++ b/BloomFramework/src/Input/InputManager.cpp @@ -1,26 +1,21 @@ #include "Input/InputManager.h" namespace bloom { - InputManager::InputManager() : - m_keyboard(nullptr), - m_mouse(0), - m_mouseX(0), - m_mouseY(0), - m_will_quit(false), - m_isLocked(false), + InputManager::InputManager() noexcept : m_keyState(), - m_mouseState() {} + m_mouseState() + {} void InputManager::update() { - for (int i = 0; i < KEYBOARD_SIZE; i++) - m_keyState[i] = false; + for (int i = 0; i < static_cast(KeyboardKey::KEYBOARD_SIZE); i++) + m_keyState[i] = 0; - for (int i = 0; i < MOUSE_MAX; i++) - m_mouseState[i] = false; + for (int i = 0; i < static_cast(MouseButton::MOUSE_MAX); i++) + m_mouseState[i] = 0; m_mouseMoveX = 0; m_mouseMoveY = 0; - m_printable = ""; + m_printable.clear(); m_keyboard = SDL_GetKeyboardState(nullptr); m_mouse = SDL_GetMouseState(&m_mouseX, &m_mouseY); @@ -29,7 +24,7 @@ namespace bloom { while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: { - m_will_quit = true; + m_quitState = true; break; } case SDL_KEYDOWN: { @@ -38,7 +33,7 @@ namespace bloom { m_keyState[pressedKey.scancode] = 1; if (isPrintable(pressedKey.sym)) - m_printable = pressedKey.sym; + m_printable = static_cast(pressedKey.sym); break; } @@ -75,50 +70,52 @@ namespace bloom { } } - bool InputManager::isKeyDown(int key) { - return (!m_isLocked && (key < 0 || key >= KEYBOARD_SIZE) && m_keyState[key] == 1); + bool InputManager::isKeyDown(KeyboardKey key) const noexcept { + return (!m_lockState && key != KeyboardKey::KEYBOARD_SIZE && m_keyState[static_cast(key)] == 1); } - bool InputManager::isKeyUp(int key) { - return (!m_isLocked && (key < 0 || key >= KEYBOARD_SIZE) && m_keyState[key] == -1); + bool InputManager::isKeyUp(KeyboardKey key) const noexcept { + return (!m_lockState && key != KeyboardKey::KEYBOARD_SIZE && m_keyState[static_cast(key)] == -1); } - bool InputManager::isKeyPressed(KeyboardKey key) { - return (!m_isLocked && m_keyboard && static_cast(m_keyboard[key])); + bool InputManager::isKeyPressed(KeyboardKey key) const noexcept { + return (!m_lockState && m_keyboard && key != KeyboardKey::KEYBOARD_SIZE + && static_cast(m_keyboard[static_cast(key)])); } - bool InputManager::shift() { - return (isKeyPressed(KEY_LEFT_SHIFT) || isKeyPressed(KEY_RIGHT_SHIFT)); + bool InputManager::shift() const noexcept { + return (isKeyPressed(KeyboardKey::KEY_LEFT_SHIFT) || isKeyPressed(KeyboardKey::KEY_RIGHT_SHIFT)); } - bool InputManager::ctrl() { - return (isKeyPressed(KEY_LEFT_CTRL) || isKeyPressed(KEY_RIGHT_CTRL)); + bool InputManager::ctrl() const noexcept { + return (isKeyPressed(KeyboardKey::KEY_LEFT_CTRL) || isKeyPressed(KeyboardKey::KEY_RIGHT_CTRL)); } - bool InputManager::alt() { - return (isKeyPressed(KEY_LEFT_ALT) || isKeyPressed(KEY_RIGHT_ALT)); + bool InputManager::alt() const noexcept { + return (isKeyPressed(KeyboardKey::KEY_LEFT_ALT) || isKeyPressed(KeyboardKey::KEY_RIGHT_ALT)); } - bool InputManager::isMouseDown(MouseButton button) { - return (!m_isLocked && button < MOUSE_MAX && m_mouseState[button] == 1); + bool InputManager::isMouseDown(MouseButton button) const noexcept { + return (!m_lockState && button != MouseButton::MOUSE_MAX && m_mouseState[static_cast(button)] == 1); } - bool InputManager::isMouseUp(MouseButton button) { - return (!m_isLocked && button < MOUSE_MAX && m_mouseState[button] == -1); + bool InputManager::isMouseUp(MouseButton button) const noexcept { + return (!m_lockState && button != MouseButton::MOUSE_MAX && m_mouseState[static_cast(button)] == -1); } - bool InputManager::isMousePressed(MouseButton button) { - if (m_isLocked) return false; + bool InputManager::isMousePressed(MouseButton button) const noexcept { + if (m_lockState) + return false; switch (button) { - case MOUSE_LEFT: + case MouseButton::MOUSE_LEFT: return (m_mouse & SDL_BUTTON(1)); - case MOUSE_MIDDLE: + case MouseButton::MOUSE_MIDDLE: return (m_mouse & SDL_BUTTON(2)); - case MOUSE_RIGHT: + case MouseButton::MOUSE_RIGHT: return (m_mouse & SDL_BUTTON(3)); default: @@ -126,40 +123,40 @@ namespace bloom { } } - int InputManager::getMouseX() { + int InputManager::getMouseX() const noexcept { return m_mouseX; } - int InputManager::getMouseY() { + int InputManager::getMouseY() const noexcept { return m_mouseY; } - bool InputManager::quitRequested() { - bool curr = m_will_quit; - m_will_quit = false; + bool InputManager::quitRequested() noexcept { + bool curr = m_quitState; + m_quitState = false; return curr; } - bool InputManager::isMouseInside(SDL_Rect rectangle) { + bool InputManager::isMouseInside(SDL_Rect rectangle) const noexcept { return ((m_mouseX >= rectangle.x) && (m_mouseX <= rectangle.x + rectangle.w) && (m_mouseY >= rectangle.y) && (m_mouseY <= rectangle.y + rectangle.h)); } - bool InputManager::isPrintable(SDL_Keycode key) { - return ((key >= SDLK_SPACE) && (key <= SDLK_z)); + bool InputManager::isPrintable(SDL_Keycode key) noexcept { + return (key >= SDLK_SPACE && key <= SDLK_z); } - std::string InputManager::getPrintable() { + std::string InputManager::getPrintable() const noexcept { return m_printable; } - void InputManager::lock() { - m_isLocked = true; + void InputManager::lock() noexcept { + m_lockState = true; } - void InputManager::unlock() { - m_isLocked = false; + void InputManager::unlock() noexcept { + m_lockState = false; } } \ No newline at end of file From 8094157a490ef52a937bc3865a0eb32109120277 Mon Sep 17 00:00:00 2001 From: Anatoly Pitikin Date: Sat, 2 Mar 2019 01:18:40 +0300 Subject: [PATCH 08/18] Use cref for SDL_Rect in isMouseInside --- BloomFramework/include/Input/InputManager.h | 2 +- BloomFramework/src/Input/InputManager.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/BloomFramework/include/Input/InputManager.h b/BloomFramework/include/Input/InputManager.h index a07015a2..05ba0ff9 100644 --- a/BloomFramework/include/Input/InputManager.h +++ b/BloomFramework/include/Input/InputManager.h @@ -25,7 +25,7 @@ namespace bloom { int getMouseY() const noexcept; void lock() noexcept; void unlock() noexcept; - bool isMouseInside(SDL_Rect rectangle) const noexcept; + bool isMouseInside(const SDL_Rect& rectangle) const noexcept; std::string getPrintable() const noexcept; private: diff --git a/BloomFramework/src/Input/InputManager.cpp b/BloomFramework/src/Input/InputManager.cpp index 072ccddb..5f8e2864 100644 --- a/BloomFramework/src/Input/InputManager.cpp +++ b/BloomFramework/src/Input/InputManager.cpp @@ -137,7 +137,7 @@ namespace bloom { return curr; } - bool InputManager::isMouseInside(SDL_Rect rectangle) const noexcept { + bool InputManager::isMouseInside(const SDL_Rect& rectangle) const noexcept { return ((m_mouseX >= rectangle.x) && (m_mouseX <= rectangle.x + rectangle.w) && (m_mouseY >= rectangle.y) From 43e1c71a1a661570a43b75e7a76b39077b4df423 Mon Sep 17 00:00:00 2001 From: Anatoly Pitikin Date: Sat, 2 Mar 2019 01:19:10 +0300 Subject: [PATCH 09/18] Use some internal defines to check keys/buttons --- BloomFramework/src/Input/InputManager.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/BloomFramework/src/Input/InputManager.cpp b/BloomFramework/src/Input/InputManager.cpp index 5f8e2864..1f07d832 100644 --- a/BloomFramework/src/Input/InputManager.cpp +++ b/BloomFramework/src/Input/InputManager.cpp @@ -1,5 +1,10 @@ #include "Input/InputManager.h" +// checks keyboard key +#define checkKey(lockState, key) (!lockState && key != bloom::KeyboardKey::KEYBOARD_SIZE) +// checks mouse button +#define checkBtn(lockState, btn) (!lockState && btn != bloom::MouseButton::MOUSE_MAX) + namespace bloom { InputManager::InputManager() noexcept : m_keyState(), @@ -33,7 +38,6 @@ namespace bloom { m_keyState[pressedKey.scancode] = 1; if (isPrintable(pressedKey.sym)) - m_printable = static_cast(pressedKey.sym); break; } @@ -71,15 +75,15 @@ namespace bloom { } bool InputManager::isKeyDown(KeyboardKey key) const noexcept { - return (!m_lockState && key != KeyboardKey::KEYBOARD_SIZE && m_keyState[static_cast(key)] == 1); + return (checkKey(m_lockState, key) && m_keyState[static_cast(key)] == 1); } bool InputManager::isKeyUp(KeyboardKey key) const noexcept { - return (!m_lockState && key != KeyboardKey::KEYBOARD_SIZE && m_keyState[static_cast(key)] == -1); + return (checkKey(m_lockState, key) && m_keyState[static_cast(key)] == -1); } bool InputManager::isKeyPressed(KeyboardKey key) const noexcept { - return (!m_lockState && m_keyboard && key != KeyboardKey::KEYBOARD_SIZE + return (checkKey(m_lockState, key) && m_keyboard && static_cast(m_keyboard[static_cast(key)])); } @@ -96,11 +100,11 @@ namespace bloom { } bool InputManager::isMouseDown(MouseButton button) const noexcept { - return (!m_lockState && button != MouseButton::MOUSE_MAX && m_mouseState[static_cast(button)] == 1); + return (checkBtn(m_lockState, button) && m_mouseState[static_cast(button)] == 1); } bool InputManager::isMouseUp(MouseButton button) const noexcept { - return (!m_lockState && button != MouseButton::MOUSE_MAX && m_mouseState[static_cast(button)] == -1); + return (checkBtn(m_lockState, button) && m_mouseState[static_cast(button)] == -1); } bool InputManager::isMousePressed(MouseButton button) const noexcept { From d12e5ba2be7e9e51226e5ad79bae189a8b3826d3 Mon Sep 17 00:00:00 2001 From: Derrick Timmermans Date: Sat, 2 Mar 2019 22:50:33 +0800 Subject: [PATCH 10/18] Fit all keyboard and mouse stuff into their respective nested class This is absolutely NOT complete. Still a lot of work to be done, but this should act as a starting point. --- BloomFramework/include/Input/InputManager.h | 72 ++++++++++++------- BloomFramework/src/Input/InputManager.cpp | 80 +++++++++++---------- Test Bench/main.cpp | 6 +- 3 files changed, 90 insertions(+), 68 deletions(-) diff --git a/BloomFramework/include/Input/InputManager.h b/BloomFramework/include/Input/InputManager.h index 05ba0ff9..2c8fc4cb 100644 --- a/BloomFramework/include/Input/InputManager.h +++ b/BloomFramework/include/Input/InputManager.h @@ -10,38 +10,58 @@ namespace bloom { public: InputManager() noexcept; + class BLOOMFRAMEWORK_API Keyboard { + friend class InputManager; + + public: + bool isPressed(KeyboardKey key) const noexcept; + bool isDown(KeyboardKey key) const noexcept; + bool isUp(KeyboardKey key) const noexcept; + bool shift() const noexcept; + bool ctrl() const noexcept; + bool alt() const noexcept; + std::string getPrintable() const noexcept; + + private: + const uint8_t* m_keyboard = nullptr; + + static bool isPrintable(SDL_Keycode key) noexcept; + std::array(KeyboardKey::KEYBOARD_SIZE)> m_keyState = std::array(KeyboardKey::KEYBOARD_SIZE)>(); + std::string m_printable{ "" }; + + bool m_lockState = false; + } keyboard; + + class BLOOMFRAMEWORK_API Mouse { + friend class InputManager; + + public: + bool isDown(MouseButton button) const noexcept; + bool isUp(MouseButton button) const noexcept; + bool isPressed(MouseButton button) const noexcept; + int getX() const noexcept; + int getY() const noexcept; + bool isInside(const SDL_Rect& rectangle) const noexcept; + + private: + uint32_t m_mouse = 0; + + int m_mouseX = 0, m_mouseY = 0; + int m_mouseMoveX = 0, m_mouseMoveY = 0; + int m_scrollX = 0, m_scrollY = 0; + + std::array(MouseButton::MOUSE_MAX)> m_mouseState; + + bool m_lockState = false; + } mouse; + void update(); - bool isKeyDown(KeyboardKey key) const noexcept; - bool isKeyUp(KeyboardKey key) const noexcept; - bool shift() const noexcept; - bool ctrl() const noexcept; - bool alt() const noexcept; - bool isMouseDown(MouseButton button) const noexcept; - bool isMouseUp(MouseButton button) const noexcept; - bool isKeyPressed(KeyboardKey key) const noexcept; - bool isMousePressed(MouseButton button) const noexcept; bool quitRequested() noexcept; - int getMouseX() const noexcept; - int getMouseY() const noexcept; void lock() noexcept; void unlock() noexcept; - bool isMouseInside(const SDL_Rect& rectangle) const noexcept; - std::string getPrintable() const noexcept; - + private: - static bool isPrintable(SDL_Keycode key) noexcept; - - const uint8_t* m_keyboard = nullptr; - - uint32_t m_mouse = 0; - - int m_mouseX = 0, m_mouseY = 0; - int m_mouseMoveX = 0, m_mouseMoveY = 0; - int m_scrollX = 0, m_scrollY = 0; - std::array(KeyboardKey::KEYBOARD_SIZE)> m_keyState; - std::array(MouseButton::MOUSE_MAX)> m_mouseState; bool m_quitState = false; bool m_lockState = false; - std::string m_printable{ "" }; }; } \ No newline at end of file diff --git a/BloomFramework/src/Input/InputManager.cpp b/BloomFramework/src/Input/InputManager.cpp index 1f07d832..28fe6acd 100644 --- a/BloomFramework/src/Input/InputManager.cpp +++ b/BloomFramework/src/Input/InputManager.cpp @@ -7,22 +7,22 @@ namespace bloom { InputManager::InputManager() noexcept : - m_keyState(), - m_mouseState() + keyboard(), + mouse() {} void InputManager::update() { for (int i = 0; i < static_cast(KeyboardKey::KEYBOARD_SIZE); i++) - m_keyState[i] = 0; + keyboard.m_keyState[i] = 0; for (int i = 0; i < static_cast(MouseButton::MOUSE_MAX); i++) - m_mouseState[i] = 0; + mouse.m_mouseState[i] = 0; - m_mouseMoveX = 0; - m_mouseMoveY = 0; - m_printable.clear(); - m_keyboard = SDL_GetKeyboardState(nullptr); - m_mouse = SDL_GetMouseState(&m_mouseX, &m_mouseY); + mouse.m_mouseMoveX = 0; + mouse.m_mouseMoveY = 0; + keyboard.m_printable.clear(); + keyboard.m_keyboard = SDL_GetKeyboardState(nullptr); + mouse.m_mouse = SDL_GetMouseState(&mouse.m_mouseX, &mouse.m_mouseY); // Get key events from the OS SDL_Event event; @@ -35,37 +35,37 @@ namespace bloom { case SDL_KEYDOWN: { SDL_Keysym pressedKey = event.key.keysym; - m_keyState[pressedKey.scancode] = 1; + keyboard.m_keyState[pressedKey.scancode] = 1; - if (isPrintable(pressedKey.sym)) + if (keyboard.isPrintable(pressedKey.sym)) break; } case SDL_KEYUP: { SDL_Keysym releasedKey = event.key.keysym; - m_keyState[releasedKey.scancode] = -1; + keyboard.m_keyState[releasedKey.scancode] = -1; break; } case SDL_MOUSEMOTION: { - m_mouseX = event.motion.x; - m_mouseY = event.motion.y; - m_mouseMoveX = event.motion.xrel; - m_mouseMoveY = event.motion.yrel; + mouse.m_mouseX = event.motion.x; + mouse.m_mouseY = event.motion.y; + mouse.m_mouseMoveX = event.motion.xrel; + mouse.m_mouseMoveY = event.motion.yrel; break; } case SDL_MOUSEBUTTONDOWN: { - m_mouseState[event.button.button] = 1; + mouse.m_mouseState[event.button.button] = 1; break; } case SDL_MOUSEBUTTONUP: { - m_mouseState[event.button.button] = -1; + mouse.m_mouseState[event.button.button] = -1; break; } case SDL_MOUSEWHEEL: { - m_scrollX = event.wheel.x; - m_scrollY = event.wheel.y; + mouse.m_scrollX = event.wheel.x; + mouse.m_scrollY = event.wheel.y; break; } default: @@ -74,40 +74,40 @@ namespace bloom { } } - bool InputManager::isKeyDown(KeyboardKey key) const noexcept { + bool InputManager::Keyboard::isDown(KeyboardKey key) const noexcept { return (checkKey(m_lockState, key) && m_keyState[static_cast(key)] == 1); } - bool InputManager::isKeyUp(KeyboardKey key) const noexcept { + bool InputManager::Keyboard::isUp(KeyboardKey key) const noexcept { return (checkKey(m_lockState, key) && m_keyState[static_cast(key)] == -1); } - bool InputManager::isKeyPressed(KeyboardKey key) const noexcept { + bool InputManager::Keyboard::isPressed(KeyboardKey key) const noexcept { return (checkKey(m_lockState, key) && m_keyboard && static_cast(m_keyboard[static_cast(key)])); } - bool InputManager::shift() const noexcept { - return (isKeyPressed(KeyboardKey::KEY_LEFT_SHIFT) || isKeyPressed(KeyboardKey::KEY_RIGHT_SHIFT)); + bool InputManager::Keyboard::shift() const noexcept { + return (isPressed(KeyboardKey::KEY_LEFT_SHIFT) || isPressed(KeyboardKey::KEY_RIGHT_SHIFT)); } - bool InputManager::ctrl() const noexcept { - return (isKeyPressed(KeyboardKey::KEY_LEFT_CTRL) || isKeyPressed(KeyboardKey::KEY_RIGHT_CTRL)); + bool InputManager::Keyboard::ctrl() const noexcept { + return (isPressed(KeyboardKey::KEY_LEFT_CTRL) || isPressed(KeyboardKey::KEY_RIGHT_CTRL)); } - bool InputManager::alt() const noexcept { - return (isKeyPressed(KeyboardKey::KEY_LEFT_ALT) || isKeyPressed(KeyboardKey::KEY_RIGHT_ALT)); + bool InputManager::Keyboard::alt() const noexcept { + return (isPressed(KeyboardKey::KEY_LEFT_ALT) || isPressed(KeyboardKey::KEY_RIGHT_ALT)); } - bool InputManager::isMouseDown(MouseButton button) const noexcept { + bool InputManager::Mouse::isDown(MouseButton button) const noexcept { return (checkBtn(m_lockState, button) && m_mouseState[static_cast(button)] == 1); } - bool InputManager::isMouseUp(MouseButton button) const noexcept { + bool InputManager::Mouse::isUp(MouseButton button) const noexcept { return (checkBtn(m_lockState, button) && m_mouseState[static_cast(button)] == -1); } - bool InputManager::isMousePressed(MouseButton button) const noexcept { + bool InputManager::Mouse::isPressed(MouseButton button) const noexcept { if (m_lockState) return false; @@ -127,11 +127,11 @@ namespace bloom { } } - int InputManager::getMouseX() const noexcept { + int InputManager::Mouse::getX() const noexcept { return m_mouseX; } - int InputManager::getMouseY() const noexcept { + int InputManager::Mouse::getY() const noexcept { return m_mouseY; } @@ -141,26 +141,28 @@ namespace bloom { return curr; } - bool InputManager::isMouseInside(const SDL_Rect& rectangle) const noexcept { + bool InputManager::Mouse::isInside(const SDL_Rect& rectangle) const noexcept { return ((m_mouseX >= rectangle.x) && (m_mouseX <= rectangle.x + rectangle.w) && (m_mouseY >= rectangle.y) && (m_mouseY <= rectangle.y + rectangle.h)); } - bool InputManager::isPrintable(SDL_Keycode key) noexcept { + bool InputManager::Keyboard::isPrintable(SDL_Keycode key) noexcept { return (key >= SDLK_SPACE && key <= SDLK_z); } - std::string InputManager::getPrintable() const noexcept { + std::string InputManager::Keyboard::getPrintable() const noexcept { return m_printable; } void InputManager::lock() noexcept { - m_lockState = true; + keyboard.m_lockState = true; + mouse.m_lockState = true; } void InputManager::unlock() noexcept { - m_lockState = false; + keyboard.m_lockState = false; + mouse.m_lockState = false; } } \ No newline at end of file diff --git a/Test Bench/main.cpp b/Test Bench/main.cpp index 9018815d..dbb48edb 100644 --- a/Test Bench/main.cpp +++ b/Test Bench/main.cpp @@ -122,9 +122,9 @@ void test_drawer(const std::filesystem::path& assetsPath) { testGOpos.x += testX; testGOpos.y += testY; - cursorPos.x = game->input.getMouseX(); - cursorPos.y = game->input.getMouseY(); - std::cout << game->input.getPrintable(); + cursorPos.x = game->input.mouse.getX(); + cursorPos.y = game->input.mouse.getY(); + std::cout << game->input.keyboard.getPrintable(); if (testGOpos.x >= WINDOW_WIDTH) { testGOpos.x = -testGOsize.w; testX = rstep(10); testY = rstep(10); } From 38afcf2510e8ca5d634f562ca186bd535b620765 Mon Sep 17 00:00:00 2001 From: Derrick Timmermans Date: Sat, 2 Mar 2019 23:04:46 +0800 Subject: [PATCH 11/18] Use usings to keep some lines short and readable --- BloomFramework/include/Input/InputManager.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/BloomFramework/include/Input/InputManager.h b/BloomFramework/include/Input/InputManager.h index 2c8fc4cb..634c5515 100644 --- a/BloomFramework/include/Input/InputManager.h +++ b/BloomFramework/include/Input/InputManager.h @@ -12,6 +12,7 @@ namespace bloom { class BLOOMFRAMEWORK_API Keyboard { friend class InputManager; + using KeyboardKeys = std::array(KeyboardKey::KEYBOARD_SIZE)>; public: bool isPressed(KeyboardKey key) const noexcept; @@ -26,7 +27,7 @@ namespace bloom { const uint8_t* m_keyboard = nullptr; static bool isPrintable(SDL_Keycode key) noexcept; - std::array(KeyboardKey::KEYBOARD_SIZE)> m_keyState = std::array(KeyboardKey::KEYBOARD_SIZE)>(); + KeyboardKeys m_keyState = KeyboardKeys(); std::string m_printable{ "" }; bool m_lockState = false; @@ -34,6 +35,7 @@ namespace bloom { class BLOOMFRAMEWORK_API Mouse { friend class InputManager; + using MouseButtons = std::array(MouseButton::MOUSE_MAX)>; public: bool isDown(MouseButton button) const noexcept; @@ -49,8 +51,7 @@ namespace bloom { int m_mouseX = 0, m_mouseY = 0; int m_mouseMoveX = 0, m_mouseMoveY = 0; int m_scrollX = 0, m_scrollY = 0; - - std::array(MouseButton::MOUSE_MAX)> m_mouseState; + MouseButtons m_mouseState = MouseButtons(); bool m_lockState = false; } mouse; From 544fdfeead8673d8a00be314908a636cf2381d61 Mon Sep 17 00:00:00 2001 From: Derrick Timmermans Date: Sat, 2 Mar 2019 23:10:18 +0800 Subject: [PATCH 12/18] Fix printable character not being updated --- BloomFramework/src/Input/InputManager.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/BloomFramework/src/Input/InputManager.cpp b/BloomFramework/src/Input/InputManager.cpp index 28fe6acd..69e34cfd 100644 --- a/BloomFramework/src/Input/InputManager.cpp +++ b/BloomFramework/src/Input/InputManager.cpp @@ -12,11 +12,8 @@ namespace bloom { {} void InputManager::update() { - for (int i = 0; i < static_cast(KeyboardKey::KEYBOARD_SIZE); i++) - keyboard.m_keyState[i] = 0; - - for (int i = 0; i < static_cast(MouseButton::MOUSE_MAX); i++) - mouse.m_mouseState[i] = 0; + keyboard.m_keyState.fill(0); + mouse.m_mouseState.fill(0); mouse.m_mouseMoveX = 0; mouse.m_mouseMoveY = 0; @@ -38,6 +35,7 @@ namespace bloom { keyboard.m_keyState[pressedKey.scancode] = 1; if (keyboard.isPrintable(pressedKey.sym)) + keyboard.m_printable = pressedKey.sym; break; } @@ -141,7 +139,7 @@ namespace bloom { return curr; } - bool InputManager::Mouse::isInside(const SDL_Rect& rectangle) const noexcept { + bool InputManager::Mouse::isInside(const SDL_Rect & rectangle) const noexcept { return ((m_mouseX >= rectangle.x) && (m_mouseX <= rectangle.x + rectangle.w) && (m_mouseY >= rectangle.y) From 2177f7135e7c4ee23e84bc8c2feee40c84987f8a Mon Sep 17 00:00:00 2001 From: Anatoly Pitikin Date: Mon, 4 Mar 2019 21:59:02 +0300 Subject: [PATCH 13/18] [WIP] Small reorganization - Move all input staff to `bloom::input` namespace - Move Keyboard and Mouse classes to separate file - Rename Keyboard to KeyboardEvent and Mouse to MouseEvent - Add set and clear methods for event classes (private methods) - Add enum for event types - Add reset() for InputManager to reset all events (private method) - Mark isUp and isDown as deprecated (check comments for them) --- BloomFramework/BloomFramework.vcxproj | 2 + BloomFramework/BloomFramework.vcxproj.filters | 6 + BloomFramework/include/Game.h | 2 +- .../include/Input/InputDefinitions.h | 2 +- BloomFramework/include/Input/InputEvents.h | 79 ++++++++ BloomFramework/include/Input/InputManager.h | 62 ++----- BloomFramework/src/Input/InputEvents.cpp | 154 ++++++++++++++++ BloomFramework/src/Input/InputManager.cpp | 172 ++++++------------ 8 files changed, 312 insertions(+), 167 deletions(-) create mode 100644 BloomFramework/include/Input/InputEvents.h create mode 100644 BloomFramework/src/Input/InputEvents.cpp diff --git a/BloomFramework/BloomFramework.vcxproj b/BloomFramework/BloomFramework.vcxproj index 5c73f997..2d83d453 100644 --- a/BloomFramework/BloomFramework.vcxproj +++ b/BloomFramework/BloomFramework.vcxproj @@ -185,6 +185,7 @@ + @@ -211,6 +212,7 @@ + diff --git a/BloomFramework/BloomFramework.vcxproj.filters b/BloomFramework/BloomFramework.vcxproj.filters index 0d19e5bc..d01fa7d1 100644 --- a/BloomFramework/BloomFramework.vcxproj.filters +++ b/BloomFramework/BloomFramework.vcxproj.filters @@ -87,6 +87,9 @@ Source Files\Graphics + + Source Files\Input + @@ -188,6 +191,9 @@ Header Files\Components + + Header Files\Input + diff --git a/BloomFramework/include/Game.h b/BloomFramework/include/Game.h index be7f928c..f11dd288 100644 --- a/BloomFramework/include/Game.h +++ b/BloomFramework/include/Game.h @@ -42,7 +42,7 @@ namespace bloom { TextureStore textures = TextureStore(m_renderer); Timer timer; - InputManager input; + input::InputManager input; protected: SDL_Renderer * m_renderer = nullptr; diff --git a/BloomFramework/include/Input/InputDefinitions.h b/BloomFramework/include/Input/InputDefinitions.h index 0d36a7f9..6556d135 100644 --- a/BloomFramework/include/Input/InputDefinitions.h +++ b/BloomFramework/include/Input/InputDefinitions.h @@ -2,7 +2,7 @@ #include "stdIncludes.h" -namespace bloom { +namespace bloom::input { enum class KeyboardKey{ KEY_UNKNOWN = SDL_SCANCODE_UNKNOWN, KEY_A = SDL_SCANCODE_A, diff --git a/BloomFramework/include/Input/InputEvents.h b/BloomFramework/include/Input/InputEvents.h new file mode 100644 index 00000000..a8073c72 --- /dev/null +++ b/BloomFramework/include/Input/InputEvents.h @@ -0,0 +1,79 @@ +#pragma once + +#include "stdIncludes.h" +#include "InputDefinitions.h" + +namespace bloom::input { + class BLOOMFRAMEWORK_API InputManager; + + enum class EventType { + QuitEvent, + KeyboardEvent, + MouseEvent, + UnknownEvent + }; + + class BLOOMFRAMEWORK_API KeyboardEvent { + friend class InputManager; + using KeyboardKeys = std::array(KeyboardKey::KEYBOARD_SIZE)>; + + public: + // TODO: what's the point of this function? + [[deprecated]] bool isDown(KeyboardKey key) const noexcept; + // TODO: what's the point of this function? + [[deprecated]] bool isUp(KeyboardKey key) const noexcept; + + bool isPressed(KeyboardKey key) const noexcept; + void lock(); + void unlock(); + bool shift() const noexcept; + bool ctrl() const noexcept; + bool alt() const noexcept; + std::string getPrintable() const noexcept; + + private: + void clear(); + void set(SDL_Keysym key, int8_t keyState); + static bool isPrintable(SDL_Keycode key) noexcept; + + const uint8_t* m_keyboard = nullptr; + + KeyboardKeys m_keyState{}; + std::string m_printable{ "" }; + + bool m_lockState = false; + }; + + class BLOOMFRAMEWORK_API MouseEvent { + friend class InputManager; + using MouseButtons = std::array(MouseButton::MOUSE_MAX)>; + + public: + // TODO: what's the point of this function? + [[deprecated]] bool isDown(MouseButton button) const noexcept; + // TODO: what's the point of this function? + [[deprecated]] bool isUp(MouseButton button) const noexcept; + + bool isPressed(MouseButton button) const noexcept; + void lock(); + void unlock(); + int getX() const noexcept; + int getY() const noexcept; + bool isInside(const SDL_Rect& rectangle) const noexcept; + + private: + void clear(); + void set(int32_t x, int32_t y, int32_t moveX, int32_t moveY); + void set(uint8_t button, int32_t state); + void set(int32_t scrollX, int32_t scrollY); + + uint32_t m_mouse = 0; + + int32_t m_mouseX = 0, m_mouseY = 0, + m_mouseMoveX = 0, m_mouseMoveY = 0, + m_scrollX = 0, m_scrollY = 0; + MouseButtons m_mouseState{}; + + bool m_lockState = false; + }; +} \ No newline at end of file diff --git a/BloomFramework/include/Input/InputManager.h b/BloomFramework/include/Input/InputManager.h index 634c5515..d243fb28 100644 --- a/BloomFramework/include/Input/InputManager.h +++ b/BloomFramework/include/Input/InputManager.h @@ -3,66 +3,26 @@ #include #include "stdIncludes.h" /// All keys are defined here. -#include "InputDefinitions.h" +#include "InputEvents.h" -namespace bloom { +namespace bloom::input { class BLOOMFRAMEWORK_API InputManager { public: - InputManager() noexcept; - - class BLOOMFRAMEWORK_API Keyboard { - friend class InputManager; - using KeyboardKeys = std::array(KeyboardKey::KEYBOARD_SIZE)>; - - public: - bool isPressed(KeyboardKey key) const noexcept; - bool isDown(KeyboardKey key) const noexcept; - bool isUp(KeyboardKey key) const noexcept; - bool shift() const noexcept; - bool ctrl() const noexcept; - bool alt() const noexcept; - std::string getPrintable() const noexcept; - - private: - const uint8_t* m_keyboard = nullptr; - - static bool isPrintable(SDL_Keycode key) noexcept; - KeyboardKeys m_keyState = KeyboardKeys(); - std::string m_printable{ "" }; - - bool m_lockState = false; - } keyboard; - - class BLOOMFRAMEWORK_API Mouse { - friend class InputManager; - using MouseButtons = std::array(MouseButton::MOUSE_MAX)>; - - public: - bool isDown(MouseButton button) const noexcept; - bool isUp(MouseButton button) const noexcept; - bool isPressed(MouseButton button) const noexcept; - int getX() const noexcept; - int getY() const noexcept; - bool isInside(const SDL_Rect& rectangle) const noexcept; - - private: - uint32_t m_mouse = 0; - - int m_mouseX = 0, m_mouseY = 0; - int m_mouseMoveX = 0, m_mouseMoveY = 0; - int m_scrollX = 0, m_scrollY = 0; - MouseButtons m_mouseState = MouseButtons(); - - bool m_lockState = false; - } mouse; + InputManager() noexcept = default; void update(); bool quitRequested() noexcept; void lock() noexcept; void unlock() noexcept; + EventType getType() const noexcept; + + KeyboardEvent keyboard{}; + MouseEvent mouse{}; private: - bool m_quitState = false; - bool m_lockState = false; + void reset(); + + EventType m_type = EventType::UnknownEvent; + //bool m_lockState = false; }; } \ No newline at end of file diff --git a/BloomFramework/src/Input/InputEvents.cpp b/BloomFramework/src/Input/InputEvents.cpp new file mode 100644 index 00000000..c3feefa5 --- /dev/null +++ b/BloomFramework/src/Input/InputEvents.cpp @@ -0,0 +1,154 @@ +#include "Input/InputEvents.h" + +// checks keyboard key +#define checkKey(lockState, key) (!(lockState) && (key) != bloom::input::KeyboardKey::KEYBOARD_SIZE) +// checks mouse button +#define checkBtn(lockState, btn) (!(lockState) && (btn) != bloom::input::MouseButton::MOUSE_MAX) + +// TODO: do we need this? +//#define isLetter(key) ((key) >= SDLK_a && (key) <= SDLK_z) +//#define isDigit(key) ((key) >= SDLK_0 && (key) <= SDLK_9) +//#define isSpecialChar(key) ((key) == SDLK_MINUS || (key) == SDLK_EQUALS (key) == SDLK_COMMA || (key) == SDLK_SPACE \ +// || (key) == SDLK_SEMICOLON || (key) == SDLK_QUOTE || (key) == SDLK_SLASH || (key) == SDLK_PERIOD \ +// || (key) == SDLK_LEFTBRACKET || (key) == SDLK_RIGHTBRACKET || (key) == SDLK_BACKSLASH || (key) == SDLK_BACKQUOTE) + +namespace bloom::input { + + // TODO: what's the point of this function? + bool KeyboardEvent::isDown(KeyboardKey key) const noexcept { + return (checkKey(m_lockState, key) && m_keyState[static_cast(key)] == 1); + } + + // TODO: what's the point of this function? + bool KeyboardEvent::isUp(KeyboardKey key) const noexcept { + return (checkKey(m_lockState, key) && m_keyState[static_cast(key)] == -1); + } + + bool KeyboardEvent::isPressed(KeyboardKey key) const noexcept { + return (checkKey(m_lockState, key) && m_keyboard + && static_cast(m_keyboard[static_cast(key)])); + } + + void KeyboardEvent::lock() { + m_lockState = true; + } + + void KeyboardEvent::unlock() { + m_lockState = false; + } + + bool KeyboardEvent::shift() const noexcept { + return (isPressed(KeyboardKey::KEY_LEFT_SHIFT) || isPressed(KeyboardKey::KEY_RIGHT_SHIFT)); + } + + bool KeyboardEvent::ctrl() const noexcept { + return (isPressed(KeyboardKey::KEY_LEFT_CTRL) || isPressed(KeyboardKey::KEY_RIGHT_CTRL)); + } + + bool KeyboardEvent::alt() const noexcept { + return (isPressed(KeyboardKey::KEY_LEFT_ALT) || isPressed(KeyboardKey::KEY_RIGHT_ALT)); + } + + bool KeyboardEvent::isPrintable(SDL_Keycode key) noexcept { + return (key == SDLK_BACKSPACE || (key >= SDLK_SPACE && key <= SDLK_z)); + } + + std::string KeyboardEvent::getPrintable() const noexcept { + return m_printable; + } + + void KeyboardEvent::clear() { + m_keyState.fill(0); + m_printable.clear(); + m_keyboard = SDL_GetKeyboardState(nullptr); + } + + void KeyboardEvent::set(SDL_Keysym key, int8_t keyState) { + //clear(); + m_keyState[key.scancode] = keyState; + + if (keyState == 1 && isPrintable(key.sym)) + m_printable = static_cast(key.sym); + } + + + + // TODO: what's the point of this function? + bool MouseEvent::isDown(MouseButton button) const noexcept { + return (checkBtn(m_lockState, button) && m_mouseState[static_cast(button)] == 1); + } + + // TODO: what's the point of this function? + bool MouseEvent::isUp(MouseButton button) const noexcept { + return (checkBtn(m_lockState, button) && m_mouseState[static_cast(button)] == -1); + } + + bool MouseEvent::isPressed(MouseButton button) const noexcept { + if (m_lockState) + return false; + + switch (button) + { + case MouseButton::MOUSE_LEFT: + return (m_mouse & SDL_BUTTON(1)); + + case MouseButton::MOUSE_MIDDLE: + return (m_mouse & SDL_BUTTON(2)); + + case MouseButton::MOUSE_RIGHT: + return (m_mouse & SDL_BUTTON(3)); + + default: + return false; + } + } + + int MouseEvent::getX() const noexcept { + return m_mouseX; + } + + int MouseEvent::getY() const noexcept { + return m_mouseY; + } + + bool MouseEvent::isInside(const SDL_Rect & rectangle) const noexcept { + return ((m_mouseX >= rectangle.x) + && (m_mouseX <= rectangle.x + rectangle.w) + && (m_mouseY >= rectangle.y) + && (m_mouseY <= rectangle.y + rectangle.h)); + } + + void MouseEvent::lock() { + m_lockState = true; + } + + void MouseEvent::unlock() { + m_lockState = false; + } + + void MouseEvent::clear() { + m_mouseState.fill(0); + m_scrollX = m_scrollY = 0; + m_mouseMoveX = m_mouseMoveY = 0; + m_mouse = SDL_GetMouseState(&m_mouseX, &m_mouseY); + } + + void MouseEvent::set(int32_t x, int32_t y, int32_t moveX, int32_t moveY) { + //clear(); + m_mouseX = x; + m_mouseY = y; + m_mouseMoveX = moveX; + m_mouseMoveY = moveY; + } + + void MouseEvent::set(uint8_t button, int32_t state) { + //clear(); + m_mouseState[button] = static_cast(state); + } + + void MouseEvent::set(int32_t scrollX, int32_t scrollY) { + //clear(); + m_scrollX = scrollX; + m_scrollY = scrollY; + } +} \ No newline at end of file diff --git a/BloomFramework/src/Input/InputManager.cpp b/BloomFramework/src/Input/InputManager.cpp index 69e34cfd..bd3beff0 100644 --- a/BloomFramework/src/Input/InputManager.cpp +++ b/BloomFramework/src/Input/InputManager.cpp @@ -1,166 +1,110 @@ #include "Input/InputManager.h" -// checks keyboard key -#define checkKey(lockState, key) (!lockState && key != bloom::KeyboardKey::KEYBOARD_SIZE) -// checks mouse button -#define checkBtn(lockState, btn) (!lockState && btn != bloom::MouseButton::MOUSE_MAX) - -namespace bloom { - InputManager::InputManager() noexcept : - keyboard(), - mouse() - {} - +namespace bloom::input { void InputManager::update() { - keyboard.m_keyState.fill(0); - mouse.m_mouseState.fill(0); + reset(); + + //keyboard.m_keyState.fill(0); + //mouse.m_mouseState.fill(0); - mouse.m_mouseMoveX = 0; - mouse.m_mouseMoveY = 0; - keyboard.m_printable.clear(); - keyboard.m_keyboard = SDL_GetKeyboardState(nullptr); - mouse.m_mouse = SDL_GetMouseState(&mouse.m_mouseX, &mouse.m_mouseY); + //mouse.m_mouseMoveX = 0; + //mouse.m_mouseMoveY = 0; + //keyboard.m_printable.clear(); + //keyboard.m_keyboard = SDL_GetKeyboardState(nullptr); + //mouse.m_mouse = SDL_GetMouseState(&mouse.m_mouseX, &mouse.m_mouseY); // Get key events from the OS SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: { - m_quitState = true; + m_type = EventType::QuitEvent; break; } case SDL_KEYDOWN: { - SDL_Keysym pressedKey = event.key.keysym; + m_type = EventType::KeyboardEvent; + keyboard.set(event.key.keysym, 1); + //SDL_Keysym pressedKey = event.key.keysym; - keyboard.m_keyState[pressedKey.scancode] = 1; + //keyboard.m_keyState[pressedKey.scancode] = 1; - if (keyboard.isPrintable(pressedKey.sym)) - keyboard.m_printable = pressedKey.sym; + //if (keyboard.isPrintable(pressedKey.sym)) { + // if (pressedKey.sym == SDLK_BACKSPACE) + // keyboard.m_printable = "\b \b"; + // else + // keyboard.m_printable = pressedKey.sym; + //} break; } case SDL_KEYUP: { - SDL_Keysym releasedKey = event.key.keysym; + m_type = EventType::KeyboardEvent; + keyboard.set(event.key.keysym, -1); + //SDL_Keysym releasedKey = event.key.keysym; - keyboard.m_keyState[releasedKey.scancode] = -1; + //keyboard.m_keyState[releasedKey.scancode] = -1; break; } case SDL_MOUSEMOTION: { - mouse.m_mouseX = event.motion.x; - mouse.m_mouseY = event.motion.y; - mouse.m_mouseMoveX = event.motion.xrel; - mouse.m_mouseMoveY = event.motion.yrel; + m_type = EventType::MouseEvent; + mouse.set(event.motion.x, event.motion.y, event.motion.xrel, event.motion.yrel); + //mouse.m_mouseX = event.motion.x; + //mouse.m_mouseY = event.motion.y; + //mouse.m_mouseMoveX = event.motion.xrel; + //mouse.m_mouseMoveY = event.motion.yrel; break; } case SDL_MOUSEBUTTONDOWN: { - mouse.m_mouseState[event.button.button] = 1; + m_type = EventType::MouseEvent; + mouse.set(event.button.button, 1); + //mouse.m_mouseState[event.button.button] = 1; break; } case SDL_MOUSEBUTTONUP: { - mouse.m_mouseState[event.button.button] = -1; + m_type = EventType::MouseEvent; + mouse.set(event.button.button, -1); + //mouse.m_mouseState[event.button.button] = -1; break; } case SDL_MOUSEWHEEL: { - mouse.m_scrollX = event.wheel.x; - mouse.m_scrollY = event.wheel.y; + m_type = EventType::MouseEvent; + mouse.set(event.wheel.x, event.wheel.y); + //mouse.m_scrollX = event.wheel.x; + //mouse.m_scrollY = event.wheel.y; break; } default: + m_type = EventType::UnknownEvent; break; } } } - bool InputManager::Keyboard::isDown(KeyboardKey key) const noexcept { - return (checkKey(m_lockState, key) && m_keyState[static_cast(key)] == 1); - } - - bool InputManager::Keyboard::isUp(KeyboardKey key) const noexcept { - return (checkKey(m_lockState, key) && m_keyState[static_cast(key)] == -1); + void InputManager::reset() { + keyboard.clear(); + mouse.clear(); } - bool InputManager::Keyboard::isPressed(KeyboardKey key) const noexcept { - return (checkKey(m_lockState, key) && m_keyboard - && static_cast(m_keyboard[static_cast(key)])); - } - - bool InputManager::Keyboard::shift() const noexcept { - return (isPressed(KeyboardKey::KEY_LEFT_SHIFT) || isPressed(KeyboardKey::KEY_RIGHT_SHIFT)); - } - - bool InputManager::Keyboard::ctrl() const noexcept { - return (isPressed(KeyboardKey::KEY_LEFT_CTRL) || isPressed(KeyboardKey::KEY_RIGHT_CTRL)); - } - - bool InputManager::Keyboard::alt() const noexcept { - return (isPressed(KeyboardKey::KEY_LEFT_ALT) || isPressed(KeyboardKey::KEY_RIGHT_ALT)); - } - - bool InputManager::Mouse::isDown(MouseButton button) const noexcept { - return (checkBtn(m_lockState, button) && m_mouseState[static_cast(button)] == 1); - } - - bool InputManager::Mouse::isUp(MouseButton button) const noexcept { - return (checkBtn(m_lockState, button) && m_mouseState[static_cast(button)] == -1); - } - - bool InputManager::Mouse::isPressed(MouseButton button) const noexcept { - if (m_lockState) - return false; - - switch (button) - { - case MouseButton::MOUSE_LEFT: - return (m_mouse & SDL_BUTTON(1)); - - case MouseButton::MOUSE_MIDDLE: - return (m_mouse & SDL_BUTTON(2)); - - case MouseButton::MOUSE_RIGHT: - return (m_mouse & SDL_BUTTON(3)); - - default: - return false; - } - } - - int InputManager::Mouse::getX() const noexcept { - return m_mouseX; + void InputManager::lock() noexcept { + keyboard.lock(); + mouse.lock(); } - int InputManager::Mouse::getY() const noexcept { - return m_mouseY; + void InputManager::unlock() noexcept { + keyboard.unlock(); + mouse.unlock(); } bool InputManager::quitRequested() noexcept { - bool curr = m_quitState; - m_quitState = false; - return curr; - } - - bool InputManager::Mouse::isInside(const SDL_Rect & rectangle) const noexcept { - return ((m_mouseX >= rectangle.x) - && (m_mouseX <= rectangle.x + rectangle.w) - && (m_mouseY >= rectangle.y) - && (m_mouseY <= rectangle.y + rectangle.h)); - } - - bool InputManager::Keyboard::isPrintable(SDL_Keycode key) noexcept { - return (key >= SDLK_SPACE && key <= SDLK_z); - } - - std::string InputManager::Keyboard::getPrintable() const noexcept { - return m_printable; - } - - void InputManager::lock() noexcept { - keyboard.m_lockState = true; - mouse.m_lockState = true; + if (m_type == EventType::QuitEvent) { + reset(); + return true; + } + return false; } - void InputManager::unlock() noexcept { - keyboard.m_lockState = false; - mouse.m_lockState = false; + EventType InputManager::getType() const noexcept { + return m_type; } } \ No newline at end of file From 705f704a7a2a1760520c49927c7ddcbc894e2b99 Mon Sep 17 00:00:00 2001 From: Anatoly Pitikin Date: Fri, 8 Mar 2019 03:10:39 +0300 Subject: [PATCH 14/18] [WIP] Small optimization - Add ctors for KeyboardEvent and MouseEvent - Rename clear to reset in KeyboardEvent and MouseEvent - Add messages for deprecated funcs - Store printable character as char - Use pairs for pos, movement and scroll in MouseEvent - Make enter and backspace printable - Rework the logic of events handing - Add flag for prevention the handling of [unhandled] events if quit event was handled - Now InputManager::update will return current quit state w/o resetting it (to reset use resetQuit) - Turn off InputManager::getType --- BloomFramework/include/Input/InputEvents.h | 57 ++++---- BloomFramework/include/Input/InputManager.h | 9 +- BloomFramework/src/Input/InputEvents.cpp | 139 +++++++++++--------- BloomFramework/src/Input/InputManager.cpp | 80 +++++------ 4 files changed, 151 insertions(+), 134 deletions(-) diff --git a/BloomFramework/include/Input/InputEvents.h b/BloomFramework/include/Input/InputEvents.h index a8073c72..981217af 100644 --- a/BloomFramework/include/Input/InputEvents.h +++ b/BloomFramework/include/Input/InputEvents.h @@ -15,64 +15,69 @@ namespace bloom::input { class BLOOMFRAMEWORK_API KeyboardEvent { friend class InputManager; - using KeyboardKeys = std::array(KeyboardKey::KEYBOARD_SIZE)>; + //using KeyboardKeys = std::array(KeyboardKey::KEYBOARD_SIZE)>; public: + KeyboardEvent() noexcept; + // TODO: what's the point of this function? - [[deprecated]] bool isDown(KeyboardKey key) const noexcept; + [[deprecated("Function 'isDown' is deprecated. Use 'isPressed' instead.")]] + bool isDown(KeyboardKey key) const noexcept; // TODO: what's the point of this function? - [[deprecated]] bool isUp(KeyboardKey key) const noexcept; + [[deprecated("Function 'isUp' is deprecated. Use 'isPressed' instead.")]] + bool isUp(KeyboardKey key) const noexcept; bool isPressed(KeyboardKey key) const noexcept; - void lock(); - void unlock(); + void lock() noexcept; + void unlock() noexcept; bool shift() const noexcept; bool ctrl() const noexcept; bool alt() const noexcept; - std::string getPrintable() const noexcept; + std::string getPrintable() const; private: - void clear(); - void set(SDL_Keysym key, int8_t keyState); + void reset(); + void set(SDL_Keycode key, bool state) noexcept; static bool isPrintable(SDL_Keycode key) noexcept; - const uint8_t* m_keyboard = nullptr; - - KeyboardKeys m_keyState{}; - std::string m_printable{ "" }; + std::array(KeyboardKey::KEYBOARD_SIZE) > m_keyboard{}; + char m_printable = '\0'; bool m_lockState = false; }; class BLOOMFRAMEWORK_API MouseEvent { friend class InputManager; - using MouseButtons = std::array(MouseButton::MOUSE_MAX)>; public: + MouseEvent() noexcept; + // TODO: what's the point of this function? - [[deprecated]] bool isDown(MouseButton button) const noexcept; + [[deprecated("Function 'isDown' is deprecated. Use 'isPressed' instead.")]] + bool isDown(MouseButton button) const noexcept; // TODO: what's the point of this function? - [[deprecated]] bool isUp(MouseButton button) const noexcept; + [[deprecated("Function 'isUp' is deprecated. Use 'isPressed' instead.")]] + bool isUp(MouseButton button) const noexcept; bool isPressed(MouseButton button) const noexcept; - void lock(); - void unlock(); + void lock() noexcept; + void unlock() noexcept; int getX() const noexcept; int getY() const noexcept; bool isInside(const SDL_Rect& rectangle) const noexcept; private: - void clear(); - void set(int32_t x, int32_t y, int32_t moveX, int32_t moveY); - void set(uint8_t button, int32_t state); - void set(int32_t scrollX, int32_t scrollY); + void reset() noexcept; + void set(std::pair pos, std::pair movement) noexcept; + void set(uint8_t button, bool state) noexcept; + void set(std::pair scroll) noexcept; - uint32_t m_mouse = 0; + std::array(MouseButton::MOUSE_MAX)> m_mouse{}; - int32_t m_mouseX = 0, m_mouseY = 0, - m_mouseMoveX = 0, m_mouseMoveY = 0, - m_scrollX = 0, m_scrollY = 0; - MouseButtons m_mouseState{}; + // TODO: ise Point instead of pair here + std::pair m_pos{ 0, 0 }, + m_offset{ 0, 0 }, + m_scroll{ 0, 0 };; bool m_lockState = false; }; diff --git a/BloomFramework/include/Input/InputManager.h b/BloomFramework/include/Input/InputManager.h index d243fb28..cc48cf84 100644 --- a/BloomFramework/include/Input/InputManager.h +++ b/BloomFramework/include/Input/InputManager.h @@ -10,11 +10,12 @@ namespace bloom::input { public: InputManager() noexcept = default; - void update(); + bool update(bool continueOnQuit = false); bool quitRequested() noexcept; + void resetQuit() noexcept; void lock() noexcept; void unlock() noexcept; - EventType getType() const noexcept; + //EventType getType() const noexcept; KeyboardEvent keyboard{}; MouseEvent mouse{}; @@ -22,7 +23,9 @@ namespace bloom::input { private: void reset(); - EventType m_type = EventType::UnknownEvent; + bool m_quitState = false; + EventType m_lastType = EventType::UnknownEvent; + SDL_Event m_intlEvent; //bool m_lockState = false; }; } \ No newline at end of file diff --git a/BloomFramework/src/Input/InputEvents.cpp b/BloomFramework/src/Input/InputEvents.cpp index c3feefa5..de6a3950 100644 --- a/BloomFramework/src/Input/InputEvents.cpp +++ b/BloomFramework/src/Input/InputEvents.cpp @@ -5,7 +5,7 @@ // checks mouse button #define checkBtn(lockState, btn) (!(lockState) && (btn) != bloom::input::MouseButton::MOUSE_MAX) -// TODO: do we need this? +// TODO: do we need this? (may be helpful for input fields) //#define isLetter(key) ((key) >= SDLK_a && (key) <= SDLK_z) //#define isDigit(key) ((key) >= SDLK_0 && (key) <= SDLK_9) //#define isSpecialChar(key) ((key) == SDLK_MINUS || (key) == SDLK_EQUALS (key) == SDLK_COMMA || (key) == SDLK_SPACE \ @@ -13,27 +13,37 @@ // || (key) == SDLK_LEFTBRACKET || (key) == SDLK_RIGHTBRACKET || (key) == SDLK_BACKSLASH || (key) == SDLK_BACKQUOTE) namespace bloom::input { + KeyboardEvent::KeyboardEvent() noexcept { + int numKeys = 0; + auto kb = SDL_GetKeyboardState(&numKeys); + size_t keys = std::min(static_cast(numKeys), m_keyboard.size()); + for (size_t i = 0; i < keys; ++i) { + m_keyboard[i] = kb[i]; + } + } // TODO: what's the point of this function? bool KeyboardEvent::isDown(KeyboardKey key) const noexcept { - return (checkKey(m_lockState, key) && m_keyState[static_cast(key)] == 1); + //return (checkKey(m_lockState, key) && m_keyState[static_cast(key)] == 1); + return isPressed(key); } // TODO: what's the point of this function? bool KeyboardEvent::isUp(KeyboardKey key) const noexcept { - return (checkKey(m_lockState, key) && m_keyState[static_cast(key)] == -1); + //return (checkKey(m_lockState, key) && m_keyState[static_cast(key)] == -1); + return !isPressed(key); } bool KeyboardEvent::isPressed(KeyboardKey key) const noexcept { - return (checkKey(m_lockState, key) && m_keyboard - && static_cast(m_keyboard[static_cast(key)])); + return (checkKey(m_lockState, key) + && m_keyboard[static_cast(key)]); } - void KeyboardEvent::lock() { + void KeyboardEvent::lock() noexcept { m_lockState = true; } - void KeyboardEvent::unlock() { + void KeyboardEvent::unlock() noexcept { m_lockState = false; } @@ -50,105 +60,104 @@ namespace bloom::input { } bool KeyboardEvent::isPrintable(SDL_Keycode key) noexcept { - return (key == SDLK_BACKSPACE || (key >= SDLK_SPACE && key <= SDLK_z)); + return (key >= SDLK_SPACE && key <= SDLK_z || key == SDLK_RETURN || key == SDLK_BACKSPACE); } - std::string KeyboardEvent::getPrintable() const noexcept { - return m_printable; - } + std::string KeyboardEvent::getPrintable() const { + switch (m_printable) { + case '\0': + return std::string{ "" }; + + case '\b': + return std::string{ "\b \b" }; + + case '\r': + return std::string{ "\n" }; - void KeyboardEvent::clear() { - m_keyState.fill(0); - m_printable.clear(); - m_keyboard = SDL_GetKeyboardState(nullptr); + default: + return std::string{ m_printable }; + } } - void KeyboardEvent::set(SDL_Keysym key, int8_t keyState) { - //clear(); - m_keyState[key.scancode] = keyState; + void KeyboardEvent::reset() { + m_printable = '\0'; + } - if (keyState == 1 && isPrintable(key.sym)) - m_printable = static_cast(key.sym); + void KeyboardEvent::set(SDL_Keycode key, bool state) noexcept { + //reset(); + if (state && isPrintable(key)) + m_printable = static_cast(key); + else + m_printable = '\0'; } + MouseEvent::MouseEvent() noexcept { + auto mouseState = SDL_GetMouseState(nullptr, nullptr); + for (size_t i = 1; i <= 3; ++i) { + m_mouse[i] = mouseState & SDL_BUTTON(i); + } + } + // TODO: what's the point of this function? bool MouseEvent::isDown(MouseButton button) const noexcept { - return (checkBtn(m_lockState, button) && m_mouseState[static_cast(button)] == 1); + //return (checkBtn(m_lockState, button) && m_mouseState[static_cast(button)] == 1); + return isPressed(button); } // TODO: what's the point of this function? bool MouseEvent::isUp(MouseButton button) const noexcept { - return (checkBtn(m_lockState, button) && m_mouseState[static_cast(button)] == -1); + //return (checkBtn(m_lockState, button) && m_mouseState[static_cast(button)] == -1); + return !isPressed(button); } bool MouseEvent::isPressed(MouseButton button) const noexcept { - if (m_lockState) - return false; - - switch (button) - { - case MouseButton::MOUSE_LEFT: - return (m_mouse & SDL_BUTTON(1)); - - case MouseButton::MOUSE_MIDDLE: - return (m_mouse & SDL_BUTTON(2)); - - case MouseButton::MOUSE_RIGHT: - return (m_mouse & SDL_BUTTON(3)); - - default: - return false; - } + return (checkBtn(m_lockState, button) + && m_mouse[static_cast(button)]); } int MouseEvent::getX() const noexcept { - return m_mouseX; + return m_pos.first; } int MouseEvent::getY() const noexcept { - return m_mouseY; + return m_pos.second; } bool MouseEvent::isInside(const SDL_Rect & rectangle) const noexcept { - return ((m_mouseX >= rectangle.x) - && (m_mouseX <= rectangle.x + rectangle.w) - && (m_mouseY >= rectangle.y) - && (m_mouseY <= rectangle.y + rectangle.h)); + return ((m_pos.first >= rectangle.x) + && (m_pos.first <= rectangle.x + rectangle.w) + && (m_pos.second >= rectangle.y) + && (m_pos.second <= rectangle.y + rectangle.h)); } - void MouseEvent::lock() { + void MouseEvent::lock() noexcept { m_lockState = true; } - void MouseEvent::unlock() { + void MouseEvent::unlock() noexcept { m_lockState = false; } - void MouseEvent::clear() { - m_mouseState.fill(0); - m_scrollX = m_scrollY = 0; - m_mouseMoveX = m_mouseMoveY = 0; - m_mouse = SDL_GetMouseState(&m_mouseX, &m_mouseY); + void MouseEvent::reset() noexcept { + m_scroll = { 0, 0 }; + m_offset = { 0, 0 }; } - void MouseEvent::set(int32_t x, int32_t y, int32_t moveX, int32_t moveY) { - //clear(); - m_mouseX = x; - m_mouseY = y; - m_mouseMoveX = moveX; - m_mouseMoveY = moveY; + void MouseEvent::set(std::pair pos, std::pair offset) noexcept { + //reset(); + m_pos = std::move(pos); + m_offset = std::move(offset); } - void MouseEvent::set(uint8_t button, int32_t state) { - //clear(); - m_mouseState[button] = static_cast(state); + void MouseEvent::set(uint8_t button, bool state) noexcept { + //reset(); + m_mouse[button] = state; } - void MouseEvent::set(int32_t scrollX, int32_t scrollY) { - //clear(); - m_scrollX = scrollX; - m_scrollY = scrollY; + void MouseEvent::set(std::pair scroll) noexcept { + //reset(); + m_scroll = std::move(scroll); } } \ No newline at end of file diff --git a/BloomFramework/src/Input/InputManager.cpp b/BloomFramework/src/Input/InputManager.cpp index bd3beff0..dd30d7ab 100644 --- a/BloomFramework/src/Input/InputManager.cpp +++ b/BloomFramework/src/Input/InputManager.cpp @@ -1,30 +1,31 @@ #include "Input/InputManager.h" namespace bloom::input { - void InputManager::update() { + bool InputManager::update(bool continueOnQuit) { reset(); - //keyboard.m_keyState.fill(0); //mouse.m_mouseState.fill(0); //mouse.m_mouseMoveX = 0; //mouse.m_mouseMoveY = 0; - //keyboard.m_printable.clear(); + //keyboard.m_printable.reset(); //keyboard.m_keyboard = SDL_GetKeyboardState(nullptr); //mouse.m_mouse = SDL_GetMouseState(&mouse.m_mouseX, &mouse.m_mouseY); // Get key events from the OS - SDL_Event event; - while (SDL_PollEvent(&event)) { - switch (event.type) { + while (SDL_PollEvent(&m_intlEvent)) { + switch (m_intlEvent.type) { case SDL_QUIT: { - m_type = EventType::QuitEvent; + m_lastType = EventType::QuitEvent; + m_quitState = true; + if (!continueOnQuit) + return true; break; } case SDL_KEYDOWN: { - m_type = EventType::KeyboardEvent; - keyboard.set(event.key.keysym, 1); - //SDL_Keysym pressedKey = event.key.keysym; + m_lastType = EventType::KeyboardEvent; + keyboard.set(m_intlEvent.key.keysym.sym, true); + //SDL_Keysym pressedKey = m_intlEvent.key.keysym; //keyboard.m_keyState[pressedKey.scancode] = 1; @@ -38,52 +39,53 @@ namespace bloom::input { break; } case SDL_KEYUP: { - m_type = EventType::KeyboardEvent; - keyboard.set(event.key.keysym, -1); - //SDL_Keysym releasedKey = event.key.keysym; + m_lastType = EventType::KeyboardEvent; + keyboard.set(m_intlEvent.key.keysym.sym, false); + //SDL_Keysym releasedKey = m_intlEvent.key.keysym; //keyboard.m_keyState[releasedKey.scancode] = -1; break; } case SDL_MOUSEMOTION: { - m_type = EventType::MouseEvent; - mouse.set(event.motion.x, event.motion.y, event.motion.xrel, event.motion.yrel); - //mouse.m_mouseX = event.motion.x; - //mouse.m_mouseY = event.motion.y; - //mouse.m_mouseMoveX = event.motion.xrel; - //mouse.m_mouseMoveY = event.motion.yrel; + m_lastType = EventType::MouseEvent; + mouse.set({ m_intlEvent.motion.x, m_intlEvent.motion.y }, { m_intlEvent.motion.xrel, m_intlEvent.motion.yrel }); + //mouse.m_mouseX = m_intlEvent.motion.x; + //mouse.m_mouseY = m_intlEvent.motion.y; + //mouse.m_mouseMoveX = m_intlEvent.motion.xrel; + //mouse.m_mouseMoveY = m_intlEvent.motion.yrel; break; } case SDL_MOUSEBUTTONDOWN: { - m_type = EventType::MouseEvent; - mouse.set(event.button.button, 1); - //mouse.m_mouseState[event.button.button] = 1; + m_lastType = EventType::MouseEvent; + mouse.set(m_intlEvent.button.button, true); + //mouse.m_mouseState[m_intlEvent.button.button] = 1; break; } case SDL_MOUSEBUTTONUP: { - m_type = EventType::MouseEvent; - mouse.set(event.button.button, -1); - //mouse.m_mouseState[event.button.button] = -1; + m_lastType = EventType::MouseEvent; + mouse.set(m_intlEvent.button.button, false); + //mouse.m_mouseState[m_intlEvent.button.button] = -1; break; } case SDL_MOUSEWHEEL: { - m_type = EventType::MouseEvent; - mouse.set(event.wheel.x, event.wheel.y); - //mouse.m_scrollX = event.wheel.x; - //mouse.m_scrollY = event.wheel.y; + m_lastType = EventType::MouseEvent; + mouse.set({ m_intlEvent.wheel.x, m_intlEvent.wheel.y }); + //mouse.m_scrollX = m_intlEvent.wheel.x; + //mouse.m_scrollY = m_intlEvent.wheel.y; break; } default: - m_type = EventType::UnknownEvent; + m_lastType = EventType::UnknownEvent; break; } } + return m_quitState; } void InputManager::reset() { - keyboard.clear(); - mouse.clear(); + keyboard.reset(); + mouse.reset(); } void InputManager::lock() noexcept { @@ -97,14 +99,12 @@ namespace bloom::input { } bool InputManager::quitRequested() noexcept { - if (m_type == EventType::QuitEvent) { - reset(); - return true; - } - return false; + auto tmp = m_quitState; + m_quitState = false; + return tmp; } - EventType InputManager::getType() const noexcept { - return m_type; - } + //EventType InputManager::getType() const noexcept { + // return m_lastType; + //} } \ No newline at end of file From 69b8c16ff79334a990ee8a3a3c22d78a0cf014a9 Mon Sep 17 00:00:00 2001 From: Anatoly Pitikin Date: Sat, 9 Mar 2019 00:39:09 +0300 Subject: [PATCH 15/18] [WIP] Another update - Adjust KeyboardKey::KEYBOARD_SIZE - Add MOUSE_X1 and MOUSE_X2 to MouseButton - Simplify `set` in MouseEvent and KeyboardEvent - Add NoEvent to EventType - Use std::bitset instead of std::array in KeyboardEvent - Record all printable chars (and make m_printable as std::string) - Add `getPos`, `getOffset` and `getScroll` to MouseEvent - Add `wait` to InputManager to wait event (SDL_WaitEvent and SDL_WaitEventTimeout) - Now `InputManager::update ` will return true if at least one event was handled, otherwise false will be returned - Add internal SDL_Event to prevent creating new one each time `update` or `wait` is called - Update getting mod keys (capslock, numlock) state - Add `KeyboardEvent::capsLock` to check caps lock's state - Move main handler from InputManager::update to InputManager::handle --- .../include/Input/InputDefinitions.h | 8 +- BloomFramework/include/Input/InputEvents.h | 41 +++--- BloomFramework/include/Input/InputManager.h | 13 +- BloomFramework/src/Input/InputEvents.cpp | 129 +++++++++++------- BloomFramework/src/Input/InputManager.cpp | 127 +++++++++-------- Test Bench/main.cpp | 2 +- 6 files changed, 186 insertions(+), 134 deletions(-) diff --git a/BloomFramework/include/Input/InputDefinitions.h b/BloomFramework/include/Input/InputDefinitions.h index 6556d135..65b4b493 100644 --- a/BloomFramework/include/Input/InputDefinitions.h +++ b/BloomFramework/include/Input/InputDefinitions.h @@ -3,7 +3,7 @@ #include "stdIncludes.h" namespace bloom::input { - enum class KeyboardKey{ + enum class KeyboardKey { KEY_UNKNOWN = SDL_SCANCODE_UNKNOWN, KEY_A = SDL_SCANCODE_A, KEY_B = SDL_SCANCODE_B, @@ -244,13 +244,15 @@ namespace bloom::input { KEY_EJECT = SDL_SCANCODE_EJECT, KEY_SLEEP = SDL_SCANCODE_SLEEP, - KEYBOARD_SIZE // This defines the keyboard size + KEYBOARD_SIZE = SDL_NUM_SCANCODES // This defines the keyboard size }; - enum class MouseButton{ + enum class MouseButton { MOUSE_LEFT = SDL_BUTTON_LEFT, MOUSE_MIDDLE = SDL_BUTTON_MIDDLE, MOUSE_RIGHT = SDL_BUTTON_RIGHT, + MOUSE_X1 = SDL_BUTTON_X1, + MOUSE_X2 = SDL_BUTTON_X2, MOUSE_MAX // No button, just to define max // array size. diff --git a/BloomFramework/include/Input/InputEvents.h b/BloomFramework/include/Input/InputEvents.h index 981217af..690d0cd7 100644 --- a/BloomFramework/include/Input/InputEvents.h +++ b/BloomFramework/include/Input/InputEvents.h @@ -1,12 +1,15 @@ #pragma once #include "stdIncludes.h" +#include #include "InputDefinitions.h" +#include "Components/Position.h" namespace bloom::input { class BLOOMFRAMEWORK_API InputManager; enum class EventType { + NoEvent, QuitEvent, KeyboardEvent, MouseEvent, @@ -15,16 +18,15 @@ namespace bloom::input { class BLOOMFRAMEWORK_API KeyboardEvent { friend class InputManager; - //using KeyboardKeys = std::array(KeyboardKey::KEYBOARD_SIZE)>; public: KeyboardEvent() noexcept; // TODO: what's the point of this function? - [[deprecated("Function 'isDown' is deprecated. Use 'isPressed' instead.")]] + [[deprecated("Function 'isDown' is deprecated and will be removed soon. Use 'isPressed' instead.")]] bool isDown(KeyboardKey key) const noexcept; // TODO: what's the point of this function? - [[deprecated("Function 'isUp' is deprecated. Use 'isPressed' instead.")]] + [[deprecated("Function 'isUp' is deprecated and will be removed soon. Use 'isPressed' instead.")]] bool isUp(KeyboardKey key) const noexcept; bool isPressed(KeyboardKey key) const noexcept; @@ -33,15 +35,17 @@ namespace bloom::input { bool shift() const noexcept; bool ctrl() const noexcept; bool alt() const noexcept; + bool capsLock() const noexcept; std::string getPrintable() const; private: void reset(); - void set(SDL_Keycode key, bool state) noexcept; + void set(const SDL_KeyboardEvent& kbe) noexcept; static bool isPrintable(SDL_Keycode key) noexcept; + void updateModKeys(); - std::array(KeyboardKey::KEYBOARD_SIZE) > m_keyboard{}; - char m_printable = '\0'; + std::bitset(KeyboardKey::KEYBOARD_SIZE)> m_keyboard{0}; + std::string m_printable{ "" }; bool m_lockState = false; }; @@ -50,35 +54,40 @@ namespace bloom::input { friend class InputManager; public: + using Coordinates = components::Position; + MouseEvent() noexcept; // TODO: what's the point of this function? - [[deprecated("Function 'isDown' is deprecated. Use 'isPressed' instead.")]] + [[deprecated("Function 'isDown' is deprecated and will be removed soon. Use 'isPressed' instead.")]] bool isDown(MouseButton button) const noexcept; // TODO: what's the point of this function? - [[deprecated("Function 'isUp' is deprecated. Use 'isPressed' instead.")]] + [[deprecated("Function 'isUp' is deprecated and will be removed soon. Use 'isPressed' instead.")]] bool isUp(MouseButton button) const noexcept; - bool isPressed(MouseButton button) const noexcept; + uint8_t isPressed(MouseButton button) const noexcept; void lock() noexcept; void unlock() noexcept; int getX() const noexcept; int getY() const noexcept; + Coordinates getPos() const noexcept; + Coordinates getOffset() const noexcept; + Coordinates getScroll() const noexcept; bool isInside(const SDL_Rect& rectangle) const noexcept; private: void reset() noexcept; - void set(std::pair pos, std::pair movement) noexcept; - void set(uint8_t button, bool state) noexcept; - void set(std::pair scroll) noexcept; + void set(const SDL_MouseButtonEvent& mbe) noexcept; + void set(const SDL_MouseMotionEvent& mme) noexcept; + void set(const SDL_MouseWheelEvent& mwe) noexcept; - std::array(MouseButton::MOUSE_MAX)> m_mouse{}; + std::array(MouseButton::MOUSE_MAX)> m_mouse{}; // TODO: ise Point instead of pair here - std::pair m_pos{ 0, 0 }, + Coordinates m_pos{ 0, 0 }, m_offset{ 0, 0 }, - m_scroll{ 0, 0 };; + m_scroll{ 0, 0 }; bool m_lockState = false; }; -} \ No newline at end of file +} diff --git a/BloomFramework/include/Input/InputManager.h b/BloomFramework/include/Input/InputManager.h index cc48cf84..a8320efb 100644 --- a/BloomFramework/include/Input/InputManager.h +++ b/BloomFramework/include/Input/InputManager.h @@ -8,24 +8,23 @@ namespace bloom::input { class BLOOMFRAMEWORK_API InputManager { public: - InputManager() noexcept = default; - bool update(bool continueOnQuit = false); + // If timeout < 0, the function will wait for the event indefinitely + EventType wait(int timeout = -1); bool quitRequested() noexcept; - void resetQuit() noexcept; + void lock() noexcept; void unlock() noexcept; - //EventType getType() const noexcept; KeyboardEvent keyboard{}; MouseEvent mouse{}; private: void reset(); + void handle(); bool m_quitState = false; - EventType m_lastType = EventType::UnknownEvent; - SDL_Event m_intlEvent; - //bool m_lockState = false; + SDL_Event m_intlEvent{}; + EventType m_lastType = EventType::NoEvent; }; } \ No newline at end of file diff --git a/BloomFramework/src/Input/InputEvents.cpp b/BloomFramework/src/Input/InputEvents.cpp index de6a3950..83f2399d 100644 --- a/BloomFramework/src/Input/InputEvents.cpp +++ b/BloomFramework/src/Input/InputEvents.cpp @@ -5,21 +5,17 @@ // checks mouse button #define checkBtn(lockState, btn) (!(lockState) && (btn) != bloom::input::MouseButton::MOUSE_MAX) -// TODO: do we need this? (may be helpful for input fields) -//#define isLetter(key) ((key) >= SDLK_a && (key) <= SDLK_z) -//#define isDigit(key) ((key) >= SDLK_0 && (key) <= SDLK_9) -//#define isSpecialChar(key) ((key) == SDLK_MINUS || (key) == SDLK_EQUALS (key) == SDLK_COMMA || (key) == SDLK_SPACE \ -// || (key) == SDLK_SEMICOLON || (key) == SDLK_QUOTE || (key) == SDLK_SLASH || (key) == SDLK_PERIOD \ -// || (key) == SDLK_LEFTBRACKET || (key) == SDLK_RIGHTBRACKET || (key) == SDLK_BACKSLASH || (key) == SDLK_BACKQUOTE) +#define isLockKey(key) ((key) == SDL_SCANCODE_CAPSLOCK || (key) == SDL_SCANCODE_NUMLOCKCLEAR || (key) == SDL_SCANCODE_SCROLLLOCK) namespace bloom::input { KeyboardEvent::KeyboardEvent() noexcept { int numKeys = 0; - auto kb = SDL_GetKeyboardState(&numKeys); - size_t keys = std::min(static_cast(numKeys), m_keyboard.size()); + const auto kb = SDL_GetKeyboardState(&numKeys); + const size_t keys = std::min(static_cast(numKeys), m_keyboard.size()); for (size_t i = 0; i < keys; ++i) { m_keyboard[i] = kb[i]; } + updateModKeys(); } // TODO: what's the point of this function? @@ -59,42 +55,59 @@ namespace bloom::input { return (isPressed(KeyboardKey::KEY_LEFT_ALT) || isPressed(KeyboardKey::KEY_RIGHT_ALT)); } + bool KeyboardEvent::capsLock() const noexcept { + return isPressed(KeyboardKey::KEY_CAPSLOCK); + } + bool KeyboardEvent::isPrintable(SDL_Keycode key) noexcept { return (key >= SDLK_SPACE && key <= SDLK_z || key == SDLK_RETURN || key == SDLK_BACKSPACE); } - std::string KeyboardEvent::getPrintable() const { - switch (m_printable) { - case '\0': - return std::string{ "" }; - - case '\b': - return std::string{ "\b \b" }; - - case '\r': - return std::string{ "\n" }; + void KeyboardEvent::updateModKeys() { + const auto mod = SDL_GetModState(); + m_keyboard[SDL_SCANCODE_CAPSLOCK] = mod & KMOD_CAPS; + m_keyboard[SDL_SCANCODE_NUMLOCKCLEAR] = mod & KMOD_NUM; + // vvv disable them vvv + //m_keyboard[SDL_SCANCODE_LSHIFT] = mod & KMOD_LSHIFT; + //m_keyboard[SDL_SCANCODE_RSHIFT] = mod & KMOD_RSHIFT; + //m_keyboard[SDL_SCANCODE_LCTRL] = mod & KMOD_LCTRL; + //m_keyboard[SDL_SCANCODE_RCTRL] = mod & KMOD_RCTRL; + //m_keyboard[SDL_SCANCODE_LALT] = mod & KMOD_LALT; + //m_keyboard[SDL_SCANCODE_RALT] = mod & KMOD_RALT; + //m_keyboard[SDL_SCANCODE_LGUI] = mod & KMOD_LGUI; + //m_keyboard[SDL_SCANCODE_RGUI] = mod & KMOD_RGUI; + } - default: - return std::string{ m_printable }; - } + std::string KeyboardEvent::getPrintable() const { + return m_printable; } void KeyboardEvent::reset() { - m_printable = '\0'; + m_printable.clear(); + updateModKeys(); } - void KeyboardEvent::set(SDL_Keycode key, bool state) noexcept { + void KeyboardEvent::set(const SDL_KeyboardEvent& kbe) noexcept { //reset(); - if (state && isPrintable(key)) - m_printable = static_cast(key); + if (isLockKey(kbe.keysym.scancode)) { + if (kbe.state) + m_keyboard[kbe.keysym.scancode].flip(); + } else - m_printable = '\0'; + m_keyboard[kbe.keysym.scancode] = kbe.state; + if (kbe.state && isPrintable(kbe.keysym.sym)) { + if (kbe.keysym.sym == SDLK_BACKSPACE) + m_printable += "\b "; + m_printable += static_cast(kbe.keysym.sym); + if ((shift() || capsLock()) && kbe.keysym.sym >= SDLK_a && kbe.keysym.sym <= SDLK_z) + m_printable.back() -= ('a' - 'A'); + } } MouseEvent::MouseEvent() noexcept { - auto mouseState = SDL_GetMouseState(nullptr, nullptr); + const auto mouseState = SDL_GetMouseState(nullptr, nullptr); for (size_t i = 1; i <= 3; ++i) { m_mouse[i] = mouseState & SDL_BUTTON(i); } @@ -112,24 +125,37 @@ namespace bloom::input { return !isPressed(button); } - bool MouseEvent::isPressed(MouseButton button) const noexcept { - return (checkBtn(m_lockState, button) - && m_mouse[static_cast(button)]); + uint8_t MouseEvent::isPressed(MouseButton button) const noexcept { + if (checkBtn(m_lockState, button)) + return m_mouse[static_cast(button)]; + return 0; } int MouseEvent::getX() const noexcept { - return m_pos.first; + return m_pos.x; } int MouseEvent::getY() const noexcept { - return m_pos.second; + return m_pos.y; + } + + MouseEvent::Coordinates MouseEvent::getPos() const noexcept { + return m_pos; + } + + MouseEvent::Coordinates MouseEvent::getOffset() const noexcept { + return m_offset; + } + + MouseEvent::Coordinates MouseEvent::getScroll() const noexcept { + return m_scroll; } bool MouseEvent::isInside(const SDL_Rect & rectangle) const noexcept { - return ((m_pos.first >= rectangle.x) - && (m_pos.first <= rectangle.x + rectangle.w) - && (m_pos.second >= rectangle.y) - && (m_pos.second <= rectangle.y + rectangle.h)); + return ((m_pos.x >= rectangle.x) + && (m_pos.x <= rectangle.x + rectangle.w) + && (m_pos.y >= rectangle.y) + && (m_pos.y <= rectangle.y + rectangle.h)); } void MouseEvent::lock() noexcept { @@ -141,23 +167,32 @@ namespace bloom::input { } void MouseEvent::reset() noexcept { - m_scroll = { 0, 0 }; - m_offset = { 0, 0 }; + m_scroll.x = 0; m_scroll.y = 0; + m_offset.x = 0; m_offset.y = 0; } - void MouseEvent::set(std::pair pos, std::pair offset) noexcept { + void MouseEvent::set(const SDL_MouseButtonEvent & mbe) noexcept { //reset(); - m_pos = std::move(pos); - m_offset = std::move(offset); - } - - void MouseEvent::set(uint8_t button, bool state) noexcept { + //switch (mbe.state) { + //case SDL_PRESSED: + // m_mouse[mbe.button] = mbe.clicks; + // break; + //case SDL_RELEASED: + // m_mouse[mbe.button] = 0; + // break; + //} + m_mouse[mbe.button] = mbe.state ? mbe.clicks : 0; + m_pos.x = mbe.x; m_pos.y = mbe.y; + } + + void MouseEvent::set(const SDL_MouseMotionEvent & mme) noexcept { //reset(); - m_mouse[button] = state; + m_pos.x = mme.x; m_pos.y = mme.y; + m_offset.x = mme.xrel; m_offset.y = mme.yrel; } - void MouseEvent::set(std::pair scroll) noexcept { + void MouseEvent::set(const SDL_MouseWheelEvent & mwe) noexcept { //reset(); - m_scroll = std::move(scroll); + m_scroll.x = mwe.x; m_scroll.y = mwe.y; } } \ No newline at end of file diff --git a/BloomFramework/src/Input/InputManager.cpp b/BloomFramework/src/Input/InputManager.cpp index dd30d7ab..d3d0b476 100644 --- a/BloomFramework/src/Input/InputManager.cpp +++ b/BloomFramework/src/Input/InputManager.cpp @@ -12,75 +12,82 @@ namespace bloom::input { //keyboard.m_keyboard = SDL_GetKeyboardState(nullptr); //mouse.m_mouse = SDL_GetMouseState(&mouse.m_mouseX, &mouse.m_mouseY); + bool caught = false; + // Get key events from the OS while (SDL_PollEvent(&m_intlEvent)) { - switch (m_intlEvent.type) { - case SDL_QUIT: { - m_lastType = EventType::QuitEvent; - m_quitState = true; - if (!continueOnQuit) - return true; + handle(); + caught = true; + if (m_quitState && !continueOnQuit) break; - } - case SDL_KEYDOWN: { - m_lastType = EventType::KeyboardEvent; - keyboard.set(m_intlEvent.key.keysym.sym, true); - //SDL_Keysym pressedKey = m_intlEvent.key.keysym; + } + if (!caught) + m_lastType = EventType::NoEvent; + return caught; + } - //keyboard.m_keyState[pressedKey.scancode] = 1; + EventType InputManager::wait(int timeout) { + int caught; + if (timeout < 0) + caught = SDL_WaitEvent(&m_intlEvent); + else + caught = SDL_WaitEventTimeout(&m_intlEvent, timeout); + if (caught) + handle(); + else + m_lastType = EventType::NoEvent; + return m_lastType; + } - //if (keyboard.isPrintable(pressedKey.sym)) { - // if (pressedKey.sym == SDLK_BACKSPACE) - // keyboard.m_printable = "\b \b"; - // else - // keyboard.m_printable = pressedKey.sym; - //} + void InputManager::handle() { + switch (m_intlEvent.type) { + case SDL_QUIT: { + m_lastType = EventType::QuitEvent; + m_quitState = true; + break; + } + case SDL_KEYDOWN: case SDL_KEYUP: { + m_lastType = EventType::KeyboardEvent; + keyboard.set(m_intlEvent.key); + //SDL_Keysym pressedKey = m_intlEvent.key.keysym; - break; - } - case SDL_KEYUP: { - m_lastType = EventType::KeyboardEvent; - keyboard.set(m_intlEvent.key.keysym.sym, false); - //SDL_Keysym releasedKey = m_intlEvent.key.keysym; + //keyboard.m_keyState[pressedKey.scancode] = 1; - //keyboard.m_keyState[releasedKey.scancode] = -1; + //if (keyboard.isPrintable(pressedKey.sym)) { + // if (pressedKey.sym == SDLK_BACKSPACE) + // keyboard.m_printable = "\b \b"; + // else + // keyboard.m_printable = pressedKey.sym; + //} - break; - } - case SDL_MOUSEMOTION: { - m_lastType = EventType::MouseEvent; - mouse.set({ m_intlEvent.motion.x, m_intlEvent.motion.y }, { m_intlEvent.motion.xrel, m_intlEvent.motion.yrel }); - //mouse.m_mouseX = m_intlEvent.motion.x; - //mouse.m_mouseY = m_intlEvent.motion.y; - //mouse.m_mouseMoveX = m_intlEvent.motion.xrel; - //mouse.m_mouseMoveY = m_intlEvent.motion.yrel; - break; - } - case SDL_MOUSEBUTTONDOWN: { - m_lastType = EventType::MouseEvent; - mouse.set(m_intlEvent.button.button, true); - //mouse.m_mouseState[m_intlEvent.button.button] = 1; - break; - } - case SDL_MOUSEBUTTONUP: { - m_lastType = EventType::MouseEvent; - mouse.set(m_intlEvent.button.button, false); - //mouse.m_mouseState[m_intlEvent.button.button] = -1; - break; - } - case SDL_MOUSEWHEEL: { - m_lastType = EventType::MouseEvent; - mouse.set({ m_intlEvent.wheel.x, m_intlEvent.wheel.y }); - //mouse.m_scrollX = m_intlEvent.wheel.x; - //mouse.m_scrollY = m_intlEvent.wheel.y; - break; - } - default: - m_lastType = EventType::UnknownEvent; - break; - } + break; + } + case SDL_MOUSEMOTION: { + m_lastType = EventType::MouseEvent; + mouse.set(m_intlEvent.motion); + //mouse.m_mouseX = m_intlEvent.motion.x; + //mouse.m_mouseY = m_intlEvent.motion.y; + //mouse.m_mouseMoveX = m_intlEvent.motion.xrel; + //mouse.m_mouseMoveY = m_intlEvent.motion.yrel; + break; + } + case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: { + m_lastType = EventType::MouseEvent; + mouse.set(m_intlEvent.button); + //mouse.m_mouseState[m_intlEvent.button.button] = 1; + break; + } + case SDL_MOUSEWHEEL: { + m_lastType = EventType::MouseEvent; + mouse.set(m_intlEvent.wheel); + //mouse.m_scrollX = m_intlEvent.wheel.x; + //mouse.m_scrollY = m_intlEvent.wheel.y; + break; + } + default: + m_lastType = EventType::UnknownEvent; + break; } - return m_quitState; } void InputManager::reset() { diff --git a/Test Bench/main.cpp b/Test Bench/main.cpp index dbb48edb..9f8afff1 100644 --- a/Test Bench/main.cpp +++ b/Test Bench/main.cpp @@ -65,7 +65,7 @@ void test_drawer(const std::filesystem::path& assetsPath) { std::cerr << e.what() << std::endl; } - srand(static_cast(time(0))); + srand(static_cast(time(nullptr))); SDL_Color randColor = { static_cast(rand() % 255), static_cast(rand() % 255), static_cast(rand() % 255), static_cast(rand() % 255) }; game->setColor(randColor); From da95825b0f29b6777f6c29e8a18a1aa7d4fedc2e Mon Sep 17 00:00:00 2001 From: Anatoly Pitikin Date: Sun, 10 Mar 2019 22:45:43 +0300 Subject: [PATCH 16/18] Implement state change and reuse wasUp and wasDown - Rename isUp and isDown to wasDown and wasUp - Add control of state change - Now wasDown and wasUp will check if state was changed - Add stateChanged func You may check how wasUp and wasDown work by pressing 0, caps lock and LMB in the Test Bench --- BloomFramework/include/Input/InputEvents.h | 20 +++---- BloomFramework/src/Input/InputEvents.cpp | 69 +++++++++++----------- Test Bench/main.cpp | 16 +++++ 3 files changed, 58 insertions(+), 47 deletions(-) diff --git a/BloomFramework/include/Input/InputEvents.h b/BloomFramework/include/Input/InputEvents.h index 690d0cd7..1828eb7c 100644 --- a/BloomFramework/include/Input/InputEvents.h +++ b/BloomFramework/include/Input/InputEvents.h @@ -22,14 +22,11 @@ namespace bloom::input { public: KeyboardEvent() noexcept; - // TODO: what's the point of this function? - [[deprecated("Function 'isDown' is deprecated and will be removed soon. Use 'isPressed' instead.")]] - bool isDown(KeyboardKey key) const noexcept; - // TODO: what's the point of this function? - [[deprecated("Function 'isUp' is deprecated and will be removed soon. Use 'isPressed' instead.")]] - bool isUp(KeyboardKey key) const noexcept; + bool wasDown(KeyboardKey key) const noexcept; + bool wasUp(KeyboardKey key) const noexcept; bool isPressed(KeyboardKey key) const noexcept; + bool stateChanged(KeyboardKey key) const noexcept; void lock() noexcept; void unlock() noexcept; bool shift() const noexcept; @@ -45,6 +42,7 @@ namespace bloom::input { void updateModKeys(); std::bitset(KeyboardKey::KEYBOARD_SIZE)> m_keyboard{0}; + std::bitset(KeyboardKey::KEYBOARD_SIZE)> m_stateChanged{ 0 }; std::string m_printable{ "" }; bool m_lockState = false; @@ -58,14 +56,11 @@ namespace bloom::input { MouseEvent() noexcept; - // TODO: what's the point of this function? - [[deprecated("Function 'isDown' is deprecated and will be removed soon. Use 'isPressed' instead.")]] - bool isDown(MouseButton button) const noexcept; - // TODO: what's the point of this function? - [[deprecated("Function 'isUp' is deprecated and will be removed soon. Use 'isPressed' instead.")]] - bool isUp(MouseButton button) const noexcept; + bool wasDown(MouseButton button) const noexcept; + bool wasUp(MouseButton button) const noexcept; uint8_t isPressed(MouseButton button) const noexcept; + bool stateChanged(MouseButton button) const noexcept; void lock() noexcept; void unlock() noexcept; int getX() const noexcept; @@ -82,6 +77,7 @@ namespace bloom::input { void set(const SDL_MouseWheelEvent& mwe) noexcept; std::array(MouseButton::MOUSE_MAX)> m_mouse{}; + std::bitset(MouseButton::MOUSE_MAX)> m_stateChanged{ 0 }; // TODO: ise Point instead of pair here Coordinates m_pos{ 0, 0 }, diff --git a/BloomFramework/src/Input/InputEvents.cpp b/BloomFramework/src/Input/InputEvents.cpp index 83f2399d..49cfa642 100644 --- a/BloomFramework/src/Input/InputEvents.cpp +++ b/BloomFramework/src/Input/InputEvents.cpp @@ -13,21 +13,17 @@ namespace bloom::input { const auto kb = SDL_GetKeyboardState(&numKeys); const size_t keys = std::min(static_cast(numKeys), m_keyboard.size()); for (size_t i = 0; i < keys; ++i) { - m_keyboard[i] = kb[i]; + m_keyboard.set(i, kb[i]); } updateModKeys(); } - // TODO: what's the point of this function? - bool KeyboardEvent::isDown(KeyboardKey key) const noexcept { - //return (checkKey(m_lockState, key) && m_keyState[static_cast(key)] == 1); - return isPressed(key); + bool KeyboardEvent::wasDown(KeyboardKey key) const noexcept { + return (isPressed(key) && m_stateChanged[static_cast(key)]); } - // TODO: what's the point of this function? - bool KeyboardEvent::isUp(KeyboardKey key) const noexcept { - //return (checkKey(m_lockState, key) && m_keyState[static_cast(key)] == -1); - return !isPressed(key); + bool KeyboardEvent::wasUp(KeyboardKey key) const noexcept { + return (!isPressed(key) && m_stateChanged[static_cast(key)]); } bool KeyboardEvent::isPressed(KeyboardKey key) const noexcept { @@ -35,6 +31,11 @@ namespace bloom::input { && m_keyboard[static_cast(key)]); } + bool KeyboardEvent::stateChanged(KeyboardKey key) const noexcept { + return (checkKey(m_lockState, key) + && m_stateChanged[static_cast(key)]); + } + void KeyboardEvent::lock() noexcept { m_lockState = true; } @@ -65,17 +66,8 @@ namespace bloom::input { void KeyboardEvent::updateModKeys() { const auto mod = SDL_GetModState(); - m_keyboard[SDL_SCANCODE_CAPSLOCK] = mod & KMOD_CAPS; - m_keyboard[SDL_SCANCODE_NUMLOCKCLEAR] = mod & KMOD_NUM; - // vvv disable them vvv - //m_keyboard[SDL_SCANCODE_LSHIFT] = mod & KMOD_LSHIFT; - //m_keyboard[SDL_SCANCODE_RSHIFT] = mod & KMOD_RSHIFT; - //m_keyboard[SDL_SCANCODE_LCTRL] = mod & KMOD_LCTRL; - //m_keyboard[SDL_SCANCODE_RCTRL] = mod & KMOD_RCTRL; - //m_keyboard[SDL_SCANCODE_LALT] = mod & KMOD_LALT; - //m_keyboard[SDL_SCANCODE_RALT] = mod & KMOD_RALT; - //m_keyboard[SDL_SCANCODE_LGUI] = mod & KMOD_LGUI; - //m_keyboard[SDL_SCANCODE_RGUI] = mod & KMOD_RGUI; + m_keyboard.set(SDL_SCANCODE_CAPSLOCK, mod & KMOD_CAPS); + m_keyboard.set(SDL_SCANCODE_NUMLOCKCLEAR, mod & KMOD_NUM); } std::string KeyboardEvent::getPrintable() const { @@ -85,16 +77,21 @@ namespace bloom::input { void KeyboardEvent::reset() { m_printable.clear(); updateModKeys(); + m_stateChanged.reset(); } void KeyboardEvent::set(const SDL_KeyboardEvent& kbe) noexcept { - //reset(); if (isLockKey(kbe.keysym.scancode)) { - if (kbe.state) - m_keyboard[kbe.keysym.scancode].flip(); + if (kbe.state) { + m_keyboard.flip(kbe.keysym.scancode); + m_stateChanged.set(kbe.keysym.scancode); + } + } + else { + if (static_cast(kbe.state) != m_keyboard[kbe.keysym.scancode]) + m_stateChanged.set(kbe.keysym.scancode); + m_keyboard.set(kbe.keysym.scancode, kbe.state); } - else - m_keyboard[kbe.keysym.scancode] = kbe.state; if (kbe.state && isPrintable(kbe.keysym.sym)) { if (kbe.keysym.sym == SDLK_BACKSPACE) m_printable += "\b "; @@ -113,16 +110,12 @@ namespace bloom::input { } } - // TODO: what's the point of this function? - bool MouseEvent::isDown(MouseButton button) const noexcept { - //return (checkBtn(m_lockState, button) && m_mouseState[static_cast(button)] == 1); - return isPressed(button); + bool MouseEvent::wasDown(MouseButton button) const noexcept { + return (isPressed(button) && m_stateChanged[static_cast(button)]); } - // TODO: what's the point of this function? - bool MouseEvent::isUp(MouseButton button) const noexcept { - //return (checkBtn(m_lockState, button) && m_mouseState[static_cast(button)] == -1); - return !isPressed(button); + bool MouseEvent::wasUp(MouseButton button) const noexcept { + return (!isPressed(button) && m_stateChanged[static_cast(button)]); } uint8_t MouseEvent::isPressed(MouseButton button) const noexcept { @@ -131,6 +124,11 @@ namespace bloom::input { return 0; } + bool MouseEvent::stateChanged(MouseButton button) const noexcept { + return (checkBtn(m_lockState, button) + && m_stateChanged[static_cast(button)]); + } + int MouseEvent::getX() const noexcept { return m_pos.x; } @@ -167,12 +165,12 @@ namespace bloom::input { } void MouseEvent::reset() noexcept { + m_stateChanged.reset(); m_scroll.x = 0; m_scroll.y = 0; m_offset.x = 0; m_offset.y = 0; } void MouseEvent::set(const SDL_MouseButtonEvent & mbe) noexcept { - //reset(); //switch (mbe.state) { //case SDL_PRESSED: // m_mouse[mbe.button] = mbe.clicks; @@ -181,12 +179,13 @@ namespace bloom::input { // m_mouse[mbe.button] = 0; // break; //} + if (static_cast(m_mouse[mbe.button]) != static_cast(mbe.state)) + m_stateChanged.set(mbe.button); m_mouse[mbe.button] = mbe.state ? mbe.clicks : 0; m_pos.x = mbe.x; m_pos.y = mbe.y; } void MouseEvent::set(const SDL_MouseMotionEvent & mme) noexcept { - //reset(); m_pos.x = mme.x; m_pos.y = mme.y; m_offset.x = mme.xrel; m_offset.y = mme.yrel; } diff --git a/Test Bench/main.cpp b/Test Bench/main.cpp index 9f8afff1..34cc8a5f 100644 --- a/Test Bench/main.cpp +++ b/Test Bench/main.cpp @@ -125,6 +125,22 @@ void test_drawer(const std::filesystem::path& assetsPath) { cursorPos.x = game->input.mouse.getX(); cursorPos.y = game->input.mouse.getY(); std::cout << game->input.keyboard.getPrintable(); + + // vvv wasUp and wasDown testing vvv + if (game->input.keyboard.wasDown(input::KeyboardKey::KEY_0)) + std::cout << std::endl << "zero was pressed" << std::endl; + if (game->input.keyboard.wasUp(input::KeyboardKey::KEY_0)) + std::cout << std::endl << "zero was released" << std::endl; + if (game->input.keyboard.wasDown(input::KeyboardKey::KEY_CAPSLOCK)) + std::cout << std::endl << "caps lock was pressed" << std::endl; + if (game->input.keyboard.wasUp(input::KeyboardKey::KEY_CAPSLOCK)) + std::cout << std::endl << "caps lock was released" << std::endl; + if (game->input.mouse.wasDown(input::MouseButton::MOUSE_LEFT)) + std::cout << std::endl << "left mouse button was pressed" << std::endl; + if (game->input.mouse.wasUp(input::MouseButton::MOUSE_LEFT)) + std::cout << std::endl << "left mouse button was released" << std::endl; + // ^^^ wasUp and wasDown testing ^^^ + if (testGOpos.x >= WINDOW_WIDTH) { testGOpos.x = -testGOsize.w; testX = rstep(10); testY = rstep(10); } From 64c732cb836ae099a31af04dd72c9703e4643e03 Mon Sep 17 00:00:00 2001 From: Derrick Timmermans Date: Mon, 11 Mar 2019 22:37:43 +0800 Subject: [PATCH 17/18] Add some simple tests --- .../GameObjectTest/TestControllableObject.h | 30 ++++++++++++++++++ Test Bench/Test Bench.vcxproj | 1 + Test Bench/Test Bench.vcxproj.filters | 3 ++ Test Bench/data/Assets/Box.png | Bin 0 -> 236 bytes Test Bench/main.cpp | 30 +++++++++++++++--- 5 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 Test Bench/GameObjectTest/TestControllableObject.h create mode 100644 Test Bench/data/Assets/Box.png diff --git a/Test Bench/GameObjectTest/TestControllableObject.h b/Test Bench/GameObjectTest/TestControllableObject.h new file mode 100644 index 00000000..1cb059f3 --- /dev/null +++ b/Test Bench/GameObjectTest/TestControllableObject.h @@ -0,0 +1,30 @@ +#pragma once + +#include "Framework.h" +#include "NoRandomComponent.h" + +class TestMovableObject : public bloom::GameObject { + using Position = bloom::components::Position; + using Size = bloom::components::Size; + using Sprite = bloom::components::Sprite; + using bloom::GameObject::GameObject; + +public: + void init() override {} + + void init(SDL_Rect pos_and_size = SDL_Rect{ 0,0, 50, 50 }, const std::filesystem::path texturePath = "Assets/Box.png", std::optional srcRect = std::nullopt) { + pos = &m_registry.replace(m_entity, pos_and_size.x, pos_and_size.y); + m_registry.assign(m_entity, pos_and_size.w, pos_and_size.h); + auto tmp = m_gameInstance->textures.load(texturePath); + m_registry.assign(m_entity, tmp, srcRect); + m_registry.assign(m_entity); + } + + void updatePos(int xOffset, int yOffset) { + pos->x += xOffset; + pos->y += yOffset; + } + +private: + Position* pos; +}; \ No newline at end of file diff --git a/Test Bench/Test Bench.vcxproj b/Test Bench/Test Bench.vcxproj index 0efbc1a5..90694aaa 100644 --- a/Test Bench/Test Bench.vcxproj +++ b/Test Bench/Test Bench.vcxproj @@ -196,6 +196,7 @@ + diff --git a/Test Bench/Test Bench.vcxproj.filters b/Test Bench/Test Bench.vcxproj.filters index 95773fdd..d1d5e3bf 100644 --- a/Test Bench/Test Bench.vcxproj.filters +++ b/Test Bench/Test Bench.vcxproj.filters @@ -52,5 +52,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/Test Bench/data/Assets/Box.png b/Test Bench/data/Assets/Box.png new file mode 100644 index 0000000000000000000000000000000000000000..6846c31409d608cf0c082e0ed67f55c6628b4090 GIT binary patch literal 236 zcmeAS@N?(olHy`uVBq!ia0vp^Mj*_=1SBWM%0B~AjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc!A4IP$B+p3x94_qHZX`B4Pf6`^8aj3j6?VB zSdJf3_5v2Qo8G+s=9uTZ!}Q^`BlowRORjV)J**pWw}3mu{0`Gq-t7*ga}TU7-w8CX eCHni?+pH-&)vUIAz48D$hQZU-&t;ucLK6TRY*u#w literal 0 HcmV?d00001 diff --git a/Test Bench/main.cpp b/Test Bench/main.cpp index 34cc8a5f..d0a453a9 100644 --- a/Test Bench/main.cpp +++ b/Test Bench/main.cpp @@ -9,6 +9,7 @@ #include "GameObjectTest/RandomizerSystem.h" #include "GameObjectTest/TestAnimatedGameObject.h" #include "GameObjectTest/AnimationChangerSystem.h" +#include "GameObjectTest/TestControllableObject.h" #include "getExePath.h" using namespace bloom; @@ -27,7 +28,7 @@ inline int rstep(int n) { return (rand() % n + 1); } -void test_player(const std::filesystem::path& musicPath, const std::filesystem::path& soundsPath) { +void test_player(const std::filesystem::path & musicPath, const std::filesystem::path & soundsPath) { //MusicTrack track1{ musicPath / L"music_007.mp3" }; music.queue.setVolume(0.0); music.push(musicPath / L"music_001.mp3"); @@ -50,7 +51,7 @@ void test_player(const std::filesystem::path& musicPath, const std::filesystem:: music.queue.play(); } -void test_drawer(const std::filesystem::path& assetsPath) { +void test_drawer(const std::filesystem::path & assetsPath) { const int fps = 60; const int framedelay = (1000 / fps); @@ -78,6 +79,7 @@ void test_drawer(const std::filesystem::path& assetsPath) { std::filesystem::path spriteSheetPath = assetsPath / L"OverworldTestSpritesheet.png"; std::filesystem::path testCharPath = assetsPath / L"TestChar.png"; std::filesystem::path testCursorPath = assetsPath / "testCursor.png"; + std::filesystem::path testBoxPath = assetsPath / "Box.png"; // Test Game Object entt::DefaultRegistry testRegistry; @@ -109,14 +111,17 @@ void test_drawer(const std::filesystem::path& assetsPath) { cursor.disableRandomPos(); TestAnimChar testAnim(testRegistry, game); testAnim.init(testCharPath); + TestMovableObject testMovable(testRegistry, game); + testMovable.init(SDL_Rect{ 0,0, 50, 50 }, testBoxPath); + // If manual control of entities is required, this is the method to do so. - auto & testGOpos = testRegistry.get(testGO.getEntityID()); + auto& testGOpos = testRegistry.get(testGO.getEntityID()); - auto & testGOsize = testRegistry.get(testGO.getEntityID()); + auto& testGOsize = testRegistry.get(testGO.getEntityID()); int testX = rstep(10), testY = rstep(10); - auto & cursorPos = testRegistry.get(cursor.getEntityID()); + auto& cursorPos = testRegistry.get(cursor.getEntityID()); while (game->isRunning()) { testGOpos.x += testX; @@ -141,6 +146,21 @@ void test_drawer(const std::filesystem::path& assetsPath) { std::cout << std::endl << "left mouse button was released" << std::endl; // ^^^ wasUp and wasDown testing ^^^ + // vvv isPressed testing vvv + int xOffset = 0, yOffset = 0; + if (game->input.keyboard.isPressed(input::KeyboardKey::KEY_W) || game->input.keyboard.isPressed(input::KeyboardKey::KEY_UP)) + yOffset -= 5; + if (game->input.keyboard.isPressed(input::KeyboardKey::KEY_S) || game->input.keyboard.isPressed(input::KeyboardKey::KEY_DOWN)) + yOffset += 5; + if (game->input.keyboard.isPressed(input::KeyboardKey::KEY_A) || game->input.keyboard.isPressed(input::KeyboardKey::KEY_LEFT)) + xOffset -= 5; + if (game->input.keyboard.isPressed(input::KeyboardKey::KEY_D) || game->input.keyboard.isPressed(input::KeyboardKey::KEY_RIGHT)) + xOffset += 5; + // ^^^ isPressed testing ^^^ + + testMovable.updatePos(xOffset, yOffset); + + if (testGOpos.x >= WINDOW_WIDTH) { testGOpos.x = -testGOsize.w; testX = rstep(10); testY = rstep(10); } From 66f130d4ab12dfa3571987e9359a9d0671e56887 Mon Sep 17 00:00:00 2001 From: Anatoly Pitikin Date: Wed, 2 Oct 2019 16:03:34 +0300 Subject: [PATCH 18/18] Some improvements and optimizations (#4) * Remove old code * [InputEvents] Use constexpr functions instead of macros * Update and simplify keys and buttons naming Use PascalCase (CamelCase) instead of UpperCase. For keys naming was used https://docs.rs/sdl2/0.32.2/sdl2/keyboard/enum.Scancode.html * Add KeyboardEvent::getPrintableRef * Small simplifications * Implement SymRecorder for KeyboardEvent And store only last event char ('\0' if event is not printable) SymRecorder [disabled by default] records all chars from events on demand (after it was activated) to printable user-friendly (means no more '\b' chars and all 'Enter' will be converted to "\r\n") string. 'Backspace' deletes last char. --- .../include/Input/InputDefinitions.h | 490 +++++++++--------- BloomFramework/include/Input/InputEvents.h | 35 +- BloomFramework/src/Input/InputEvents.cpp | 91 ++-- BloomFramework/src/Input/InputManager.cpp | 6 +- Test Bench/main.cpp | 35 +- 5 files changed, 354 insertions(+), 303 deletions(-) diff --git a/BloomFramework/include/Input/InputDefinitions.h b/BloomFramework/include/Input/InputDefinitions.h index 65b4b493..7110ac4f 100644 --- a/BloomFramework/include/Input/InputDefinitions.h +++ b/BloomFramework/include/Input/InputDefinitions.h @@ -4,257 +4,257 @@ namespace bloom::input { enum class KeyboardKey { - KEY_UNKNOWN = SDL_SCANCODE_UNKNOWN, - KEY_A = SDL_SCANCODE_A, - KEY_B = SDL_SCANCODE_B, - KEY_C = SDL_SCANCODE_C, - KEY_D = SDL_SCANCODE_D, - KEY_E = SDL_SCANCODE_E, - KEY_F = SDL_SCANCODE_F, - KEY_G = SDL_SCANCODE_G, - KEY_H = SDL_SCANCODE_H, - KEY_I = SDL_SCANCODE_I, - KEY_J = SDL_SCANCODE_J, - KEY_K = SDL_SCANCODE_K, - KEY_L = SDL_SCANCODE_L, - KEY_M = SDL_SCANCODE_M, - KEY_N = SDL_SCANCODE_N, - KEY_O = SDL_SCANCODE_O, - KEY_P = SDL_SCANCODE_P, - KEY_Q = SDL_SCANCODE_Q, - KEY_R = SDL_SCANCODE_R, - KEY_S = SDL_SCANCODE_S, - KEY_T = SDL_SCANCODE_T, - KEY_U = SDL_SCANCODE_U, - KEY_V = SDL_SCANCODE_V, - KEY_W = SDL_SCANCODE_W, - KEY_X = SDL_SCANCODE_X, - KEY_Y = SDL_SCANCODE_Y, - KEY_Z = SDL_SCANCODE_Z, - KEY_1 = SDL_SCANCODE_1, - KEY_2 = SDL_SCANCODE_2, - KEY_3 = SDL_SCANCODE_3, - KEY_4 = SDL_SCANCODE_4, - KEY_5 = SDL_SCANCODE_5, - KEY_6 = SDL_SCANCODE_6, - KEY_7 = SDL_SCANCODE_7, - KEY_8 = SDL_SCANCODE_8, - KEY_9 = SDL_SCANCODE_9, - KEY_0 = SDL_SCANCODE_0, - KEY_RETURN = SDL_SCANCODE_RETURN, - KEY_ESCAPE = SDL_SCANCODE_ESCAPE, - KEY_BACKSPACE = SDL_SCANCODE_BACKSPACE, - KEY_TAB = SDL_SCANCODE_TAB, - KEY_SPACE = SDL_SCANCODE_SPACE, - KEY_MINUS = SDL_SCANCODE_MINUS, - KEY_EQUALS = SDL_SCANCODE_EQUALS, - KEY_LEFTBRACKET = SDL_SCANCODE_LEFTBRACKET, - KEY_RIGHTBRACKET = SDL_SCANCODE_RIGHTBRACKET, - KEY_BACKSLASH = SDL_SCANCODE_BACKSLASH, - KEY_NONUSHASH = SDL_SCANCODE_NONUSHASH, - KEY_SEMICOLON = SDL_SCANCODE_SEMICOLON, - KEY_APOSTROPHE = SDL_SCANCODE_APOSTROPHE, - KEY_GRAVE = SDL_SCANCODE_GRAVE, - KEY_COMMA = SDL_SCANCODE_COMMA, - KEY_PERIOD = SDL_SCANCODE_PERIOD, - KEY_SLASH = SDL_SCANCODE_SLASH, - KEY_CAPSLOCK = SDL_SCANCODE_CAPSLOCK, - KEY_F1 = SDL_SCANCODE_F1, - KEY_F2 = SDL_SCANCODE_F2, - KEY_F3 = SDL_SCANCODE_F3, - KEY_F4 = SDL_SCANCODE_F4, - KEY_F5 = SDL_SCANCODE_F5, - KEY_F6 = SDL_SCANCODE_F6, - KEY_F7 = SDL_SCANCODE_F7, - KEY_F8 = SDL_SCANCODE_F8, - KEY_F9 = SDL_SCANCODE_F9, - KEY_F10 = SDL_SCANCODE_F10, - KEY_F11 = SDL_SCANCODE_F11, - KEY_F12 = SDL_SCANCODE_F12, - KEY_PRINTSCREEN = SDL_SCANCODE_PRINTSCREEN, - KEY_SCROLLLOCK = SDL_SCANCODE_SCROLLLOCK, - KEY_PAUSE = SDL_SCANCODE_PAUSE, - KEY_INSERT = SDL_SCANCODE_INSERT, - KEY_HOME = SDL_SCANCODE_HOME, - KEY_PAGEUP = SDL_SCANCODE_PAGEUP, - KEY_DELETE = SDL_SCANCODE_DELETE, - KEY_END = SDL_SCANCODE_END, - KEY_PAGEDOWN = SDL_SCANCODE_PAGEDOWN, - KEY_RIGHT = SDL_SCANCODE_RIGHT, - KEY_LEFT = SDL_SCANCODE_LEFT, - KEY_DOWN = SDL_SCANCODE_DOWN, - KEY_UP = SDL_SCANCODE_UP, - KEY_NUMLOCKCLEAR = SDL_SCANCODE_NUMLOCKCLEAR, - KEY_KEYPAD_DIVIDE = SDL_SCANCODE_KP_DIVIDE, - KEY_KEYPAD_MULTIPLY = SDL_SCANCODE_KP_MULTIPLY, - KEY_KEYPAD_MINUS = SDL_SCANCODE_KP_MINUS, - KEY_KEYPAD_PLUS = SDL_SCANCODE_KP_PLUS, - KEY_KEYPAD_ENTER = SDL_SCANCODE_KP_ENTER, - KEY_KEYPAD_1 = SDL_SCANCODE_KP_1, - KEY_KEYPAD_2 = SDL_SCANCODE_KP_2, - KEY_KEYPAD_3 = SDL_SCANCODE_KP_3, - KEY_KEYPAD_4 = SDL_SCANCODE_KP_4, - KEY_KEYPAD_5 = SDL_SCANCODE_KP_5, - KEY_KEYPAD_6 = SDL_SCANCODE_KP_6, - KEY_KEYPAD_7 = SDL_SCANCODE_KP_7, - KEY_KEYPAD_8 = SDL_SCANCODE_KP_8, - KEY_KEYPAD_9 = SDL_SCANCODE_KP_9, - KEY_KEYPAD_0 = SDL_SCANCODE_KP_0, - KEY_KEYPAD_PERIOD = SDL_SCANCODE_KP_PERIOD, - KEY_NONUSBACKSLASH = SDL_SCANCODE_NONUSBACKSLASH, - KEY_APPLICATION = SDL_SCANCODE_APPLICATION, - KEY_POWER = SDL_SCANCODE_POWER, - KEY_KEYPAD_EQUALS = SDL_SCANCODE_KP_EQUALS, - KEY_F13 = SDL_SCANCODE_F13, - KEY_F14 = SDL_SCANCODE_F14, - KEY_F15 = SDL_SCANCODE_F15, - KEY_F16 = SDL_SCANCODE_F16, - KEY_F17 = SDL_SCANCODE_F17, - KEY_F18 = SDL_SCANCODE_F18, - KEY_F19 = SDL_SCANCODE_F19, - KEY_F20 = SDL_SCANCODE_F20, - KEY_F21 = SDL_SCANCODE_F21, - KEY_F22 = SDL_SCANCODE_F22, - KEY_F23 = SDL_SCANCODE_F23, - KEY_F24 = SDL_SCANCODE_F24, - KEY_EXECUTE = SDL_SCANCODE_EXECUTE, - KEY_HELP = SDL_SCANCODE_HELP, - KEY_MENU = SDL_SCANCODE_MENU, - KEY_SELECT = SDL_SCANCODE_SELECT, - KEY_STOP = SDL_SCANCODE_STOP, - KEY_AGAIN = SDL_SCANCODE_AGAIN, - KEY_UNDO = SDL_SCANCODE_UNDO, - KEY_CUT = SDL_SCANCODE_CUT, - KEY_COPY = SDL_SCANCODE_COPY, - KEY_PASTE = SDL_SCANCODE_PASTE, - KEY_FIND = SDL_SCANCODE_FIND, - KEY_MUTE = SDL_SCANCODE_MUTE, - KEY_VOLUMEUP = SDL_SCANCODE_VOLUMEUP, - KEY_VOLUMEDOWN = SDL_SCANCODE_VOLUMEDOWN, - KEY_KEYPAD_COMMA = SDL_SCANCODE_KP_COMMA, - KEY_KEYPAD_EQUALSAS400 = SDL_SCANCODE_KP_EQUALSAS400, - KEY_INTERNATIONAL1 = SDL_SCANCODE_INTERNATIONAL1, - KEY_INTERNATIONAL2 = SDL_SCANCODE_INTERNATIONAL2, - KEY_INTERNATIONAL3 = SDL_SCANCODE_INTERNATIONAL3, - KEY_INTERNATIONAL4 = SDL_SCANCODE_INTERNATIONAL4, - KEY_INTERNATIONAL5 = SDL_SCANCODE_INTERNATIONAL5, - KEY_INTERNATIONAL6 = SDL_SCANCODE_INTERNATIONAL6, - KEY_INTERNATIONAL7 = SDL_SCANCODE_INTERNATIONAL7, - KEY_INTERNATIONAL8 = SDL_SCANCODE_INTERNATIONAL8, - KEY_INTERNATIONAL9 = SDL_SCANCODE_INTERNATIONAL9, - KEY_LANG1 = SDL_SCANCODE_LANG1, - KEY_LANG2 = SDL_SCANCODE_LANG2, - KEY_LANG3 = SDL_SCANCODE_LANG3, - KEY_LANG4 = SDL_SCANCODE_LANG4, - KEY_LANG5 = SDL_SCANCODE_LANG5, - KEY_LANG6 = SDL_SCANCODE_LANG6, - KEY_LANG7 = SDL_SCANCODE_LANG7, - KEY_LANG8 = SDL_SCANCODE_LANG8, - KEY_LANG9 = SDL_SCANCODE_LANG9, - KEY_ALTERASE = SDL_SCANCODE_ALTERASE, - KEY_SYSREQ = SDL_SCANCODE_SYSREQ, - KEY_CANCEL = SDL_SCANCODE_CANCEL, - KEY_CLEAR = SDL_SCANCODE_CLEAR, - KEY_PRIOR = SDL_SCANCODE_PRIOR, - KEY_RETURN2 = SDL_SCANCODE_RETURN2, - KEY_SEPARATOR = SDL_SCANCODE_SEPARATOR, - KEY_OUT = SDL_SCANCODE_OUT, - KEY_OPER = SDL_SCANCODE_OPER, - KEY_CLEARAGAIN = SDL_SCANCODE_CLEARAGAIN, - KEY_CRSEL = SDL_SCANCODE_CRSEL, - KEY_EXSEL = SDL_SCANCODE_EXSEL, - KEY_KEYPAD_00 = SDL_SCANCODE_KP_00, - KEY_KEYPAD_000 = SDL_SCANCODE_KP_000, - KEY_THOUSANDSSEPARATOR = SDL_SCANCODE_THOUSANDSSEPARATOR, - KEY_DECIMALSEPARATOR = SDL_SCANCODE_DECIMALSEPARATOR, - KEY_CURRENCYUNIT = SDL_SCANCODE_CURRENCYUNIT, - KEY_CURRENCYSUBUNIT = SDL_SCANCODE_CURRENCYSUBUNIT, - KEY_KEYPAD_LEFTPAREN = SDL_SCANCODE_KP_LEFTPAREN, - KEY_KEYPAD_RIGHTPAREN = SDL_SCANCODE_KP_RIGHTPAREN, - KEY_KEYPAD_LEFTBRACE = SDL_SCANCODE_KP_LEFTBRACE, - KEY_KEYPAD_RIGHTBRACE = SDL_SCANCODE_KP_RIGHTBRACE, - KEY_KEYPAD_TAB = SDL_SCANCODE_KP_TAB, - KEY_KEYPAD_BACKSPACE = SDL_SCANCODE_KP_BACKSPACE, - KEY_KEYPAD_A = SDL_SCANCODE_KP_A, - KEY_KEYPAD_B = SDL_SCANCODE_KP_B, - KEY_KEYPAD_C = SDL_SCANCODE_KP_C, - KEY_KEYPAD_D = SDL_SCANCODE_KP_D, - KEY_KEYPAD_E = SDL_SCANCODE_KP_E, - KEY_KEYPAD_F = SDL_SCANCODE_KP_F, - KEY_KEYPAD_XOR = SDL_SCANCODE_KP_XOR, - KEY_KEYPAD_POWER = SDL_SCANCODE_KP_POWER, - KEY_KEYPAD_PERCENT = SDL_SCANCODE_KP_PERCENT, - KEY_KEYPAD_LESS = SDL_SCANCODE_KP_LESS, - KEY_KEYPAD_GREATER = SDL_SCANCODE_KP_GREATER, - KEY_KEYPAD_AMPERSAND = SDL_SCANCODE_KP_AMPERSAND, - KEY_KEYPAD_DBLAMPERSAND = SDL_SCANCODE_KP_DBLAMPERSAND, - KEY_KEYPAD_VERTICALBAR = SDL_SCANCODE_KP_VERTICALBAR, - KEY_KEYPAD_DBLVERTICALBAR = SDL_SCANCODE_KP_DBLVERTICALBAR, - KEY_KEYPAD_COLON = SDL_SCANCODE_KP_COLON, - KEY_KEYPAD_HASH = SDL_SCANCODE_KP_HASH, - KEY_KEYPAD_SPACE = SDL_SCANCODE_KP_SPACE, - KEY_KEYPAD_AT = SDL_SCANCODE_KP_AT, - KEY_KEYPAD_EXCLAM = SDL_SCANCODE_KP_EXCLAM, - KEY_KEYPAD_MEMSTORE = SDL_SCANCODE_KP_MEMSTORE, - KEY_KEYPAD_MEMRECALL = SDL_SCANCODE_KP_MEMRECALL, - KEY_KEYPAD_MEMCLEAR = SDL_SCANCODE_KP_MEMCLEAR, - KEY_KEYPAD_MEMADD = SDL_SCANCODE_KP_MEMADD, - KEY_KEYPAD_MEMSUBTRACT = SDL_SCANCODE_KP_MEMSUBTRACT, - KEY_KEYPAD_MEMMULTIPLY = SDL_SCANCODE_KP_MEMMULTIPLY, - KEY_KEYPAD_MEMDIVIDE = SDL_SCANCODE_KP_MEMDIVIDE, - KEY_KEYPAD_PLUSMINUS = SDL_SCANCODE_KP_PLUSMINUS, - KEY_KEYPAD_CLEAR = SDL_SCANCODE_KP_CLEAR, - KEY_KEYPAD_CLEARENTRY = SDL_SCANCODE_KP_CLEARENTRY, - KEY_KEYPAD_BINARY = SDL_SCANCODE_KP_BINARY, - KEY_KEYPAD_OCTAL = SDL_SCANCODE_KP_OCTAL, - KEY_KEYPAD_DECIMAL = SDL_SCANCODE_KP_DECIMAL, - KEY_KEYPAD_HEXADECIMAL = SDL_SCANCODE_KP_HEXADECIMAL, - KEY_LEFT_CTRL = SDL_SCANCODE_LCTRL, - KEY_LEFT_SHIFT = SDL_SCANCODE_LSHIFT, - KEY_LEFT_ALT = SDL_SCANCODE_LALT, - KEY_LEFT_GUI = SDL_SCANCODE_LGUI, - KEY_RIGHT_CTRL = SDL_SCANCODE_RCTRL, - KEY_RIGHT_SHIFT = SDL_SCANCODE_RSHIFT, - KEY_RIGHT_ALT = SDL_SCANCODE_RALT, - KEY_RIGHT_GUI = SDL_SCANCODE_RGUI, - KEY_MODE = SDL_SCANCODE_MODE, - KEY_AUDIONEXT = SDL_SCANCODE_AUDIONEXT, - KEY_AUDIOPREV = SDL_SCANCODE_AUDIOPREV, - KEY_AUDIOSTOP = SDL_SCANCODE_AUDIOSTOP, - KEY_AUDIOPLAY = SDL_SCANCODE_AUDIOPLAY, - KEY_AUDIOMUTE = SDL_SCANCODE_AUDIOMUTE, - KEY_MEDIASELECT = SDL_SCANCODE_MEDIASELECT, - KEY_WWW = SDL_SCANCODE_WWW, - KEY_MAIL = SDL_SCANCODE_MAIL, - KEY_CALCULATOR = SDL_SCANCODE_CALCULATOR, - KEY_COMPUTER = SDL_SCANCODE_COMPUTER, - KEY_AC_SEARCH = SDL_SCANCODE_AC_SEARCH, - KEY_AC_HOME = SDL_SCANCODE_AC_HOME, - KEY_AC_BACK = SDL_SCANCODE_AC_BACK, - KEY_AC_FORWARD = SDL_SCANCODE_AC_FORWARD, - KEY_AC_STOP = SDL_SCANCODE_AC_STOP, - KEY_AC_REFRESH = SDL_SCANCODE_AC_REFRESH, - KEY_AC_BOOKMARKS = SDL_SCANCODE_AC_BOOKMARKS, - KEY_BRIGHTNESSDOWN = SDL_SCANCODE_BRIGHTNESSDOWN, - KEY_BRIGHTNESSUP = SDL_SCANCODE_BRIGHTNESSUP, - KEY_DISPLAYSWITCH = SDL_SCANCODE_DISPLAYSWITCH, - KEY_KBDILLUMTOGGLE = SDL_SCANCODE_KBDILLUMTOGGLE, - KEY_KBDILLUMDOWN = SDL_SCANCODE_KBDILLUMDOWN, - KEY_KBDILLUMUP = SDL_SCANCODE_KBDILLUMUP, - KEY_EJECT = SDL_SCANCODE_EJECT, - KEY_SLEEP = SDL_SCANCODE_SLEEP, + Unknown = SDL_SCANCODE_UNKNOWN, + A = SDL_SCANCODE_A, + B = SDL_SCANCODE_B, + C = SDL_SCANCODE_C, + D = SDL_SCANCODE_D, + E = SDL_SCANCODE_E, + F = SDL_SCANCODE_F, + G = SDL_SCANCODE_G, + H = SDL_SCANCODE_H, + I = SDL_SCANCODE_I, + J = SDL_SCANCODE_J, + K = SDL_SCANCODE_K, + L = SDL_SCANCODE_L, + M = SDL_SCANCODE_M, + N = SDL_SCANCODE_N, + O = SDL_SCANCODE_O, + P = SDL_SCANCODE_P, + Q = SDL_SCANCODE_Q, + R = SDL_SCANCODE_R, + S = SDL_SCANCODE_S, + T = SDL_SCANCODE_T, + U = SDL_SCANCODE_U, + V = SDL_SCANCODE_V, + W = SDL_SCANCODE_W, + X = SDL_SCANCODE_X, + Y = SDL_SCANCODE_Y, + Z = SDL_SCANCODE_Z, + Num1 = SDL_SCANCODE_1, + Num2 = SDL_SCANCODE_2, + Num3 = SDL_SCANCODE_3, + Num4 = SDL_SCANCODE_4, + Num5 = SDL_SCANCODE_5, + Num6 = SDL_SCANCODE_6, + Num7 = SDL_SCANCODE_7, + Num8 = SDL_SCANCODE_8, + Num9 = SDL_SCANCODE_9, + Num0 = SDL_SCANCODE_0, + Return = SDL_SCANCODE_RETURN, + Escape = SDL_SCANCODE_ESCAPE, + Backspace = SDL_SCANCODE_BACKSPACE, + Tab = SDL_SCANCODE_TAB, + Space = SDL_SCANCODE_SPACE, + Minus = SDL_SCANCODE_MINUS, + Equals = SDL_SCANCODE_EQUALS, + LeftBracket = SDL_SCANCODE_LEFTBRACKET, + RightBracket = SDL_SCANCODE_RIGHTBRACKET, + Backslash = SDL_SCANCODE_BACKSLASH, + NonUsHash = SDL_SCANCODE_NONUSHASH, + Semicolon = SDL_SCANCODE_SEMICOLON, + Apostrophe = SDL_SCANCODE_APOSTROPHE, + Grave = SDL_SCANCODE_GRAVE, + Comma = SDL_SCANCODE_COMMA, + Period = SDL_SCANCODE_PERIOD, + Slash = SDL_SCANCODE_SLASH, + CapsLock = SDL_SCANCODE_CAPSLOCK, + F1 = SDL_SCANCODE_F1, + F2 = SDL_SCANCODE_F2, + F3 = SDL_SCANCODE_F3, + F4 = SDL_SCANCODE_F4, + F5 = SDL_SCANCODE_F5, + F6 = SDL_SCANCODE_F6, + F7 = SDL_SCANCODE_F7, + F8 = SDL_SCANCODE_F8, + F9 = SDL_SCANCODE_F9, + F10 = SDL_SCANCODE_F10, + F11 = SDL_SCANCODE_F11, + F12 = SDL_SCANCODE_F12, + PrintScreen = SDL_SCANCODE_PRINTSCREEN, + ScrollLock = SDL_SCANCODE_SCROLLLOCK, + Pause = SDL_SCANCODE_PAUSE, + Insert = SDL_SCANCODE_INSERT, + Home = SDL_SCANCODE_HOME, + PageUp = SDL_SCANCODE_PAGEUP, + Delete = SDL_SCANCODE_DELETE, + End = SDL_SCANCODE_END, + PageDown = SDL_SCANCODE_PAGEDOWN, + Right = SDL_SCANCODE_RIGHT, + Left = SDL_SCANCODE_LEFT, + Down = SDL_SCANCODE_DOWN, + Up = SDL_SCANCODE_UP, + NumLockClear = SDL_SCANCODE_NUMLOCKCLEAR, + KpDivide = SDL_SCANCODE_KP_DIVIDE, + KpMultiply = SDL_SCANCODE_KP_MULTIPLY, + KpMinus = SDL_SCANCODE_KP_MINUS, + KpPlus = SDL_SCANCODE_KP_PLUS, + KpEnter = SDL_SCANCODE_KP_ENTER, + Kp1 = SDL_SCANCODE_KP_1, + Kp2 = SDL_SCANCODE_KP_2, + Kp3 = SDL_SCANCODE_KP_3, + Kp4 = SDL_SCANCODE_KP_4, + Kp5 = SDL_SCANCODE_KP_5, + Kp6 = SDL_SCANCODE_KP_6, + Kp7 = SDL_SCANCODE_KP_7, + Kp8 = SDL_SCANCODE_KP_8, + Kp9 = SDL_SCANCODE_KP_9, + Kp0 = SDL_SCANCODE_KP_0, + KpPeriod = SDL_SCANCODE_KP_PERIOD, + NonUsBackslash = SDL_SCANCODE_NONUSBACKSLASH, + Application = SDL_SCANCODE_APPLICATION, + Power = SDL_SCANCODE_POWER, + KpEquals = SDL_SCANCODE_KP_EQUALS, + F13 = SDL_SCANCODE_F13, + F14 = SDL_SCANCODE_F14, + F15 = SDL_SCANCODE_F15, + F16 = SDL_SCANCODE_F16, + F17 = SDL_SCANCODE_F17, + F18 = SDL_SCANCODE_F18, + F19 = SDL_SCANCODE_F19, + F20 = SDL_SCANCODE_F20, + F21 = SDL_SCANCODE_F21, + F22 = SDL_SCANCODE_F22, + F23 = SDL_SCANCODE_F23, + F24 = SDL_SCANCODE_F24, + Execute = SDL_SCANCODE_EXECUTE, + Help = SDL_SCANCODE_HELP, + Menu = SDL_SCANCODE_MENU, + Select = SDL_SCANCODE_SELECT, + Stop = SDL_SCANCODE_STOP, + Again = SDL_SCANCODE_AGAIN, + Undo = SDL_SCANCODE_UNDO, + Cut = SDL_SCANCODE_CUT, + Copy = SDL_SCANCODE_COPY, + Paste = SDL_SCANCODE_PASTE, + Find = SDL_SCANCODE_FIND, + Mute = SDL_SCANCODE_MUTE, + VolumeUp = SDL_SCANCODE_VOLUMEUP, + VolumeDown = SDL_SCANCODE_VOLUMEDOWN, + KpComma = SDL_SCANCODE_KP_COMMA, + KpEqualsAS400 = SDL_SCANCODE_KP_EQUALSAS400, + International1 = SDL_SCANCODE_INTERNATIONAL1, + International2 = SDL_SCANCODE_INTERNATIONAL2, + International3 = SDL_SCANCODE_INTERNATIONAL3, + International4 = SDL_SCANCODE_INTERNATIONAL4, + International5 = SDL_SCANCODE_INTERNATIONAL5, + International6 = SDL_SCANCODE_INTERNATIONAL6, + International7 = SDL_SCANCODE_INTERNATIONAL7, + International8 = SDL_SCANCODE_INTERNATIONAL8, + International9 = SDL_SCANCODE_INTERNATIONAL9, + Lang1 = SDL_SCANCODE_LANG1, + Lang2 = SDL_SCANCODE_LANG2, + Lang3 = SDL_SCANCODE_LANG3, + Lang4 = SDL_SCANCODE_LANG4, + Lang5 = SDL_SCANCODE_LANG5, + Lang6 = SDL_SCANCODE_LANG6, + Lang7 = SDL_SCANCODE_LANG7, + Lang8 = SDL_SCANCODE_LANG8, + Lang9 = SDL_SCANCODE_LANG9, + AltErase = SDL_SCANCODE_ALTERASE, + SysReq = SDL_SCANCODE_SYSREQ, + Cancel = SDL_SCANCODE_CANCEL, + Clear = SDL_SCANCODE_CLEAR, + Prior = SDL_SCANCODE_PRIOR, + Return2 = SDL_SCANCODE_RETURN2, + Separator = SDL_SCANCODE_SEPARATOR, + Out = SDL_SCANCODE_OUT, + Oper = SDL_SCANCODE_OPER, + ClearAgain = SDL_SCANCODE_CLEARAGAIN, + CrSel = SDL_SCANCODE_CRSEL, + ExSel = SDL_SCANCODE_EXSEL, + Kp00 = SDL_SCANCODE_KP_00, + Kp000 = SDL_SCANCODE_KP_000, + ThousandsSeparator = SDL_SCANCODE_THOUSANDSSEPARATOR, + DecimalSeparator = SDL_SCANCODE_DECIMALSEPARATOR, + CurrencyUnit = SDL_SCANCODE_CURRENCYUNIT, + CurrencySubUnit = SDL_SCANCODE_CURRENCYSUBUNIT, + KpLeftParen = SDL_SCANCODE_KP_LEFTPAREN, + KpRightParen = SDL_SCANCODE_KP_RIGHTPAREN, + KpLeftBrace = SDL_SCANCODE_KP_LEFTBRACE, + KpRightBrace = SDL_SCANCODE_KP_RIGHTBRACE, + KpTab = SDL_SCANCODE_KP_TAB, + KpBackspace = SDL_SCANCODE_KP_BACKSPACE, + KpA = SDL_SCANCODE_KP_A, + KpB = SDL_SCANCODE_KP_B, + KpC = SDL_SCANCODE_KP_C, + KpD = SDL_SCANCODE_KP_D, + KpE = SDL_SCANCODE_KP_E, + KpF = SDL_SCANCODE_KP_F, + KpXor = SDL_SCANCODE_KP_XOR, + KpPower = SDL_SCANCODE_KP_POWER, + KpPercent = SDL_SCANCODE_KP_PERCENT, + KpLess = SDL_SCANCODE_KP_LESS, + KpGreater = SDL_SCANCODE_KP_GREATER, + KpAmpersand = SDL_SCANCODE_KP_AMPERSAND, + KpDblAmpersand = SDL_SCANCODE_KP_DBLAMPERSAND, + KpVerticalBar = SDL_SCANCODE_KP_VERTICALBAR, + KpDblVerticalBar = SDL_SCANCODE_KP_DBLVERTICALBAR, + KpColon = SDL_SCANCODE_KP_COLON, + KpHash = SDL_SCANCODE_KP_HASH, + KpSpace = SDL_SCANCODE_KP_SPACE, + KpAt = SDL_SCANCODE_KP_AT, + KpExclam = SDL_SCANCODE_KP_EXCLAM, + KpMemStore = SDL_SCANCODE_KP_MEMSTORE, + KpMemRecall = SDL_SCANCODE_KP_MEMRECALL, + KpMemClear = SDL_SCANCODE_KP_MEMCLEAR, + KpMemAdd = SDL_SCANCODE_KP_MEMADD, + KpMemSubtract = SDL_SCANCODE_KP_MEMSUBTRACT, + KpMemMultiply = SDL_SCANCODE_KP_MEMMULTIPLY, + KpMemDivide = SDL_SCANCODE_KP_MEMDIVIDE, + KpPlusMinus = SDL_SCANCODE_KP_PLUSMINUS, + KpClear = SDL_SCANCODE_KP_CLEAR, + KpClearEntry = SDL_SCANCODE_KP_CLEARENTRY, + KpBinary = SDL_SCANCODE_KP_BINARY, + KpOctal = SDL_SCANCODE_KP_OCTAL, + KpDecimal = SDL_SCANCODE_KP_DECIMAL, + KpHexadecimal = SDL_SCANCODE_KP_HEXADECIMAL, + LCtrl = SDL_SCANCODE_LCTRL, + LShift = SDL_SCANCODE_LSHIFT, + LAlt = SDL_SCANCODE_LALT, + LGui = SDL_SCANCODE_LGUI, + RCtrl = SDL_SCANCODE_RCTRL, + RShift = SDL_SCANCODE_RSHIFT, + RAlt = SDL_SCANCODE_RALT, + RGui = SDL_SCANCODE_RGUI, + Mode = SDL_SCANCODE_MODE, + AudioNext = SDL_SCANCODE_AUDIONEXT, + AudioPrev = SDL_SCANCODE_AUDIOPREV, + AudioStop = SDL_SCANCODE_AUDIOSTOP, + AudioPlay = SDL_SCANCODE_AUDIOPLAY, + AudioMute = SDL_SCANCODE_AUDIOMUTE, + MediaSelect = SDL_SCANCODE_MEDIASELECT, + Www = SDL_SCANCODE_WWW, + Mail = SDL_SCANCODE_MAIL, + Calculator = SDL_SCANCODE_CALCULATOR, + Computer = SDL_SCANCODE_COMPUTER, + AcSearch = SDL_SCANCODE_AC_SEARCH, + AcHome = SDL_SCANCODE_AC_HOME, + AcBack = SDL_SCANCODE_AC_BACK, + AcForward = SDL_SCANCODE_AC_FORWARD, + AcStop = SDL_SCANCODE_AC_STOP, + AcRefresh = SDL_SCANCODE_AC_REFRESH, + AcBookmarks = SDL_SCANCODE_AC_BOOKMARKS, + BrightnessDown = SDL_SCANCODE_BRIGHTNESSDOWN, + BrightnessUp = SDL_SCANCODE_BRIGHTNESSUP, + DisplaySwitch = SDL_SCANCODE_DISPLAYSWITCH, + KbdIllumToggle = SDL_SCANCODE_KBDILLUMTOGGLE, + KbdIllumDown = SDL_SCANCODE_KBDILLUMDOWN, + KbdIllumUp = SDL_SCANCODE_KBDILLUMUP, + Eject = SDL_SCANCODE_EJECT, + Sleep = SDL_SCANCODE_SLEEP, KEYBOARD_SIZE = SDL_NUM_SCANCODES // This defines the keyboard size }; enum class MouseButton { - MOUSE_LEFT = SDL_BUTTON_LEFT, - MOUSE_MIDDLE = SDL_BUTTON_MIDDLE, - MOUSE_RIGHT = SDL_BUTTON_RIGHT, - MOUSE_X1 = SDL_BUTTON_X1, - MOUSE_X2 = SDL_BUTTON_X2, + Left = SDL_BUTTON_LEFT, + Middle = SDL_BUTTON_MIDDLE, + Right = SDL_BUTTON_RIGHT, + X1 = SDL_BUTTON_X1, + X2 = SDL_BUTTON_X2, - MOUSE_MAX // No button, just to define max + MOUSE_SIZE // No button, just to define max // array size. }; } \ No newline at end of file diff --git a/BloomFramework/include/Input/InputEvents.h b/BloomFramework/include/Input/InputEvents.h index 1828eb7c..f0dd978a 100644 --- a/BloomFramework/include/Input/InputEvents.h +++ b/BloomFramework/include/Input/InputEvents.h @@ -33,7 +33,29 @@ namespace bloom::input { bool ctrl() const noexcept; bool alt() const noexcept; bool capsLock() const noexcept; - std::string getPrintable() const; + char toChar() const; + + operator char() const { + return toChar(); + } + + class SymRecorder { + friend class KeyboardEvent; + + public: + void start() noexcept; + std::string stop(); + void clear(); + void cancel(); + std::string transfer(); + const std::string& get() const; + + private: + void append(char sym); + + bool m_state{ false }; + std::string m_str{}; + } recorder{}; private: void reset(); @@ -41,9 +63,9 @@ namespace bloom::input { static bool isPrintable(SDL_Keycode key) noexcept; void updateModKeys(); - std::bitset(KeyboardKey::KEYBOARD_SIZE)> m_keyboard{0}; - std::bitset(KeyboardKey::KEYBOARD_SIZE)> m_stateChanged{ 0 }; - std::string m_printable{ "" }; + std::bitset(KeyboardKey::KEYBOARD_SIZE)> m_keyboard{}; + std::bitset(KeyboardKey::KEYBOARD_SIZE)> m_stateChanged{}; + char m_char{'\0'}; bool m_lockState = false; }; @@ -76,10 +98,9 @@ namespace bloom::input { void set(const SDL_MouseMotionEvent& mme) noexcept; void set(const SDL_MouseWheelEvent& mwe) noexcept; - std::array(MouseButton::MOUSE_MAX)> m_mouse{}; - std::bitset(MouseButton::MOUSE_MAX)> m_stateChanged{ 0 }; + std::array(MouseButton::MOUSE_SIZE)> m_mouse{}; + std::bitset(MouseButton::MOUSE_SIZE)> m_stateChanged{ 0 }; - // TODO: ise Point instead of pair here Coordinates m_pos{ 0, 0 }, m_offset{ 0, 0 }, m_scroll{ 0, 0 }; diff --git a/BloomFramework/src/Input/InputEvents.cpp b/BloomFramework/src/Input/InputEvents.cpp index 49cfa642..c52b19fb 100644 --- a/BloomFramework/src/Input/InputEvents.cpp +++ b/BloomFramework/src/Input/InputEvents.cpp @@ -1,13 +1,15 @@ #include "Input/InputEvents.h" -// checks keyboard key -#define checkKey(lockState, key) (!(lockState) && (key) != bloom::input::KeyboardKey::KEYBOARD_SIZE) -// checks mouse button -#define checkBtn(lockState, btn) (!(lockState) && (btn) != bloom::input::MouseButton::MOUSE_MAX) +namespace bloom::input { + constexpr bool checkKey(bool lockState, KeyboardKey key) { return !lockState && key != KeyboardKey::KEYBOARD_SIZE; } + + constexpr bool checkButton(bool lockState, MouseButton button) { return !lockState && button != MouseButton::MOUSE_SIZE; } + + constexpr bool isLockKey(SDL_Scancode key) { + return key == SDL_SCANCODE_CAPSLOCK || key == SDL_SCANCODE_NUMLOCKCLEAR || key == SDL_SCANCODE_SCROLLLOCK; + } -#define isLockKey(key) ((key) == SDL_SCANCODE_CAPSLOCK || (key) == SDL_SCANCODE_NUMLOCKCLEAR || (key) == SDL_SCANCODE_SCROLLLOCK) -namespace bloom::input { KeyboardEvent::KeyboardEvent() noexcept { int numKeys = 0; const auto kb = SDL_GetKeyboardState(&numKeys); @@ -45,19 +47,19 @@ namespace bloom::input { } bool KeyboardEvent::shift() const noexcept { - return (isPressed(KeyboardKey::KEY_LEFT_SHIFT) || isPressed(KeyboardKey::KEY_RIGHT_SHIFT)); + return (isPressed(KeyboardKey::LShift) || isPressed(KeyboardKey::RShift)); } bool KeyboardEvent::ctrl() const noexcept { - return (isPressed(KeyboardKey::KEY_LEFT_CTRL) || isPressed(KeyboardKey::KEY_RIGHT_CTRL)); + return (isPressed(KeyboardKey::LCtrl) || isPressed(KeyboardKey::RCtrl)); } bool KeyboardEvent::alt() const noexcept { - return (isPressed(KeyboardKey::KEY_LEFT_ALT) || isPressed(KeyboardKey::KEY_RIGHT_ALT)); + return (isPressed(KeyboardKey::LAlt) || isPressed(KeyboardKey::RAlt)); } bool KeyboardEvent::capsLock() const noexcept { - return isPressed(KeyboardKey::KEY_CAPSLOCK); + return isPressed(KeyboardKey::CapsLock); } bool KeyboardEvent::isPrintable(SDL_Keycode key) noexcept { @@ -70,12 +72,12 @@ namespace bloom::input { m_keyboard.set(SDL_SCANCODE_NUMLOCKCLEAR, mod & KMOD_NUM); } - std::string KeyboardEvent::getPrintable() const { - return m_printable; + char KeyboardEvent::toChar() const { + return m_char; } void KeyboardEvent::reset() { - m_printable.clear(); + m_char = '\0'; updateModKeys(); m_stateChanged.reset(); } @@ -93,14 +95,41 @@ namespace bloom::input { m_keyboard.set(kbe.keysym.scancode, kbe.state); } if (kbe.state && isPrintable(kbe.keysym.sym)) { - if (kbe.keysym.sym == SDLK_BACKSPACE) - m_printable += "\b "; - m_printable += static_cast(kbe.keysym.sym); + m_char = static_cast(kbe.keysym.sym); if ((shift() || capsLock()) && kbe.keysym.sym >= SDLK_a && kbe.keysym.sym <= SDLK_z) - m_printable.back() -= ('a' - 'A'); + m_char -= ('a' - 'A'); + recorder.append(m_char); } } + void KeyboardEvent::SymRecorder::start() noexcept { m_state = true; } + + void KeyboardEvent::SymRecorder::cancel() { m_state = false; m_str.clear(); } + + std::string KeyboardEvent::SymRecorder::transfer() { return std::move(m_str); } + + const std::string& KeyboardEvent::SymRecorder::get() const { return m_str; } + + std::string KeyboardEvent::SymRecorder::stop() { m_state = false; return std::move(m_str); } + + void KeyboardEvent::SymRecorder::clear() { m_str.clear(); } + + void KeyboardEvent::SymRecorder::append(char sym) { + if (m_state) { + switch (sym) { + case '\0': + break; + case '\r': case '\n': + m_str += "\r\n"; + case '\b': + if (!m_str.empty()) + m_str.pop_back(); + break; + default: + m_str.push_back(sym); + } + } + } MouseEvent::MouseEvent() noexcept { @@ -119,13 +148,13 @@ namespace bloom::input { } uint8_t MouseEvent::isPressed(MouseButton button) const noexcept { - if (checkBtn(m_lockState, button)) + if (checkButton(m_lockState, button)) return m_mouse[static_cast(button)]; return 0; } bool MouseEvent::stateChanged(MouseButton button) const noexcept { - return (checkBtn(m_lockState, button) + return (checkButton(m_lockState, button) && m_stateChanged[static_cast(button)]); } @@ -149,7 +178,7 @@ namespace bloom::input { return m_scroll; } - bool MouseEvent::isInside(const SDL_Rect & rectangle) const noexcept { + bool MouseEvent::isInside(const SDL_Rect& rectangle) const noexcept { return ((m_pos.x >= rectangle.x) && (m_pos.x <= rectangle.x + rectangle.w) && (m_pos.y >= rectangle.y) @@ -166,32 +195,22 @@ namespace bloom::input { void MouseEvent::reset() noexcept { m_stateChanged.reset(); - m_scroll.x = 0; m_scroll.y = 0; - m_offset.x = 0; m_offset.y = 0; - } - - void MouseEvent::set(const SDL_MouseButtonEvent & mbe) noexcept { - //switch (mbe.state) { - //case SDL_PRESSED: - // m_mouse[mbe.button] = mbe.clicks; - // break; - //case SDL_RELEASED: - // m_mouse[mbe.button] = 0; - // break; - //} + m_scroll.x = m_scroll.y = m_offset.x = m_offset.y = 0; + } + + void MouseEvent::set(const SDL_MouseButtonEvent& mbe) noexcept { if (static_cast(m_mouse[mbe.button]) != static_cast(mbe.state)) m_stateChanged.set(mbe.button); m_mouse[mbe.button] = mbe.state ? mbe.clicks : 0; m_pos.x = mbe.x; m_pos.y = mbe.y; } - void MouseEvent::set(const SDL_MouseMotionEvent & mme) noexcept { + void MouseEvent::set(const SDL_MouseMotionEvent& mme) noexcept { m_pos.x = mme.x; m_pos.y = mme.y; m_offset.x = mme.xrel; m_offset.y = mme.yrel; } - void MouseEvent::set(const SDL_MouseWheelEvent & mwe) noexcept { - //reset(); + void MouseEvent::set(const SDL_MouseWheelEvent& mwe) noexcept { m_scroll.x = mwe.x; m_scroll.y = mwe.y; } } \ No newline at end of file diff --git a/BloomFramework/src/Input/InputManager.cpp b/BloomFramework/src/Input/InputManager.cpp index d3d0b476..a9ce167f 100644 --- a/BloomFramework/src/Input/InputManager.cpp +++ b/BloomFramework/src/Input/InputManager.cpp @@ -8,7 +8,7 @@ namespace bloom::input { //mouse.m_mouseMoveX = 0; //mouse.m_mouseMoveY = 0; - //keyboard.m_printable.reset(); + //keyboard.m_char.reset(); //keyboard.m_keyboard = SDL_GetKeyboardState(nullptr); //mouse.m_mouse = SDL_GetMouseState(&mouse.m_mouseX, &mouse.m_mouseY); @@ -55,9 +55,9 @@ namespace bloom::input { //if (keyboard.isPrintable(pressedKey.sym)) { // if (pressedKey.sym == SDLK_BACKSPACE) - // keyboard.m_printable = "\b \b"; + // keyboard.m_char = "\b \b"; // else - // keyboard.m_printable = pressedKey.sym; + // keyboard.m_char = pressedKey.sym; //} break; diff --git a/Test Bench/main.cpp b/Test Bench/main.cpp index fb3f336c..9e434117 100644 --- a/Test Bench/main.cpp +++ b/Test Bench/main.cpp @@ -150,7 +150,7 @@ void test_drawer(const std::filesystem::path& dataDir) { // Test SpriteText2 std::string deltaTimeText{ "fps: " }; - + // If manual control of entities is required, this is the method to do so. auto& testGOpos = testRegistry.get(testGO.getEntityID()); @@ -165,32 +165,43 @@ void test_drawer(const std::filesystem::path& dataDir) { cursorPos.x = game->input.mouse.getX(); cursorPos.y = game->input.mouse.getY(); - std::cout << game->input.keyboard.getPrintable(); + switch (char sym = game->input.keyboard) { + case '\0': + break; + case '\b': + std::cout << "\b \b"; + break; + case '\n': case '\r': + std::cout << "\r\n"; + break; + default: + std::cout << sym; + } // vvv wasUp and wasDown testing vvv - if (game->input.keyboard.wasDown(input::KeyboardKey::KEY_0)) + if (game->input.keyboard.wasDown(input::KeyboardKey::Num0)) std::cout << std::endl << "zero was pressed" << std::endl; - if (game->input.keyboard.wasUp(input::KeyboardKey::KEY_0)) + if (game->input.keyboard.wasUp(input::KeyboardKey::Num0)) std::cout << std::endl << "zero was released" << std::endl; - if (game->input.keyboard.wasDown(input::KeyboardKey::KEY_CAPSLOCK)) + if (game->input.keyboard.wasDown(input::KeyboardKey::CapsLock)) std::cout << std::endl << "caps lock was pressed" << std::endl; - if (game->input.keyboard.wasUp(input::KeyboardKey::KEY_CAPSLOCK)) + if (game->input.keyboard.wasUp(input::KeyboardKey::CapsLock)) std::cout << std::endl << "caps lock was released" << std::endl; - if (game->input.mouse.wasDown(input::MouseButton::MOUSE_LEFT)) + if (game->input.mouse.wasDown(input::MouseButton::Left)) std::cout << std::endl << "left mouse button was pressed" << std::endl; - if (game->input.mouse.wasUp(input::MouseButton::MOUSE_LEFT)) + if (game->input.mouse.wasUp(input::MouseButton::Left)) std::cout << std::endl << "left mouse button was released" << std::endl; // ^^^ wasUp and wasDown testing ^^^ // vvv isPressed testing vvv int xOffset = 0, yOffset = 0; - if (game->input.keyboard.isPressed(input::KeyboardKey::KEY_W) || game->input.keyboard.isPressed(input::KeyboardKey::KEY_UP)) + if (game->input.keyboard.isPressed(input::KeyboardKey::W) || game->input.keyboard.isPressed(input::KeyboardKey::Up)) yOffset -= 5; - if (game->input.keyboard.isPressed(input::KeyboardKey::KEY_S) || game->input.keyboard.isPressed(input::KeyboardKey::KEY_DOWN)) + if (game->input.keyboard.isPressed(input::KeyboardKey::S) || game->input.keyboard.isPressed(input::KeyboardKey::Down)) yOffset += 5; - if (game->input.keyboard.isPressed(input::KeyboardKey::KEY_A) || game->input.keyboard.isPressed(input::KeyboardKey::KEY_LEFT)) + if (game->input.keyboard.isPressed(input::KeyboardKey::A) || game->input.keyboard.isPressed(input::KeyboardKey::Left)) xOffset -= 5; - if (game->input.keyboard.isPressed(input::KeyboardKey::KEY_D) || game->input.keyboard.isPressed(input::KeyboardKey::KEY_RIGHT)) + if (game->input.keyboard.isPressed(input::KeyboardKey::D) || game->input.keyboard.isPressed(input::KeyboardKey::Right)) xOffset += 5; // ^^^ isPressed testing ^^^