From 163e0eeafd973489bd1db41d85365c0cbb7a7a87 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 17 Sep 2021 17:22:54 +0100 Subject: [PATCH] KEYBOARDEVENTS DONE????? --- Aeon/Input/Input.cpp | 26 ++++++++++++-------------- Aeon/Input/Input.hpp | 3 ++- Aeon/Input/InputMap.hpp | 10 +++++----- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/Aeon/Input/Input.cpp b/Aeon/Input/Input.cpp index c55b03b..b520099 100644 --- a/Aeon/Input/Input.cpp +++ b/Aeon/Input/Input.cpp @@ -20,8 +20,6 @@ Input::Input() mKeyboardEventDispatcher.RegisterAsSource( "ENGINE_INPUT_KEYBOARD" ); mKbdState = static_cast(SDL_GetKeyboardState( &mNumScancodes )); - mOldKbdState = static_cast(malloc( mNumScancodes / sizeof( uint8_t ) )); - memcpy( mOldKbdState, mKbdState, 242 / sizeof( uint8_t ) ); } Input::~Input() @@ -30,7 +28,7 @@ Input::~Input() mMouseEventDispatcher.DeRegisterAsSource( "ENGINE_INPUT_MOUSE" ); mKeyboardEventDispatcher.DeRegisterAsSource( "ENGINE_INPUT_KEYBOARD" ); - free( mOldKbdState ); + // Do not free mKbdState as that is done by SDL } void Input::PollInput() @@ -74,8 +72,6 @@ void Input::PollInput() // keyboard processing mPollScanKeyboard(); - - memcpy( mOldKbdState, mKbdState, mNumScancodes / sizeof( uint8_t ) ); } void Input::mPollDisplay() @@ -231,17 +227,19 @@ void Input::mPollKeyboard() void Input::mPollScanKeyboard() { - // first we want to check if any key has changed - // if a key has changed, then we want to check if any keys are pressed - // if a key is pressed we want to check the keys and dispatch events - // according to the scancode - // keydown and keyup will be done seperately - if ( !std::equal( mKbdState, mKbdState + mNumScancodes, mOldKbdState ) ) + // this is naive, can be optimised with double buffering + for ( int i = 0; i < mNumScancodes; i++ ) { - for ( int i = 0; i < mNumScancodes; i++ ) + bool isKeyPressed = (bool)mKbdState[i]; + if ( isKeyPressed ) { - if ( mKbdState[i] != 0 ) - std::cout << KeyCodeFromSDL( mKbdState[i] ) << " "; + EKeyCode whatKeyPressed = KeyCodeFromScanCode( (SDL_Scancode)i ); + + Aeon::Core::GenericEvent e; + e.keyCode = whatKeyPressed; + e.Type = "KEYBOARD_KEYPRESS"; + + mKeyboardEventDispatcher.Dispatch( e ); } } } diff --git a/Aeon/Input/Input.hpp b/Aeon/Input/Input.hpp index 261b2ee..8ffd96b 100644 --- a/Aeon/Input/Input.hpp +++ b/Aeon/Input/Input.hpp @@ -1,6 +1,8 @@ #ifndef AEON_INPUT_INPUT_H_ #define AEON_INPUT_INPUT_H_ +#include + #include #include "Aeon/Singleton.hpp" @@ -29,7 +31,6 @@ private: int mNumScancodes = 242; const uint8_t* mKbdState; - uint8_t* mOldKbdState; Aeon::Core::EventDispatcher mDisplayEventDispatcher; Aeon::Core::EventDispatcher mKeyboardEventDispatcher; diff --git a/Aeon/Input/InputMap.hpp b/Aeon/Input/InputMap.hpp index 57de1be..77f7813 100644 --- a/Aeon/Input/InputMap.hpp +++ b/Aeon/Input/InputMap.hpp @@ -173,6 +173,11 @@ inline EKeyCode KeyCodeFromSDL( SDL_Keycode key ) return (EKeyCode)key; } +inline EKeyCode KeyCodeFromScanCode( SDL_Scancode key ) +{ + return KeyCodeFromSDL( SDL_GetKeyFromScancode( key ) ); +} + inline EKeyCode KeyCodeFromKeymod( SDL_Keymod key ) { switch ( key ) @@ -221,11 +226,6 @@ inline EKeyCode KeyCodeFromKeymod( SDL_Keymod key ) } } -inline EKeyCode KeyCodeFromSDL( SDL_Scancode key ) -{ - return KeyCodeFromSDL( SDL_SCANCODE_TO_KEYCODE( key ) ); -} - } #endif