Parcourir la source

input: make keyboard state platform-independent.

legacy
Sam Hocevar sam il y a 11 ans
Parent
révision
6be31bba32
3 fichiers modifiés avec 270 ajouts et 51 suppressions
  1. +18
    -40
      src/input/input.cpp
  2. +3
    -3
      src/input/input.h
  3. +249
    -8
      src/platform/sdl/sdlinput.cpp

+ 18
- 40
src/input/input.cpp Voir le fichier

@@ -14,14 +14,6 @@

#include <cstdlib>

#if defined USE_SDL
# if defined HAVE_SDL_SDL_H
# include <SDL/SDL.h>
# else
# include <SDL.h>
# endif
#endif

#include "core.h"

namespace lol
@@ -43,12 +35,17 @@ public:
buttons(0),
nentities(0),
lastfocus(0)
{ }
{
m_keystate.Resize(Key::Last);
memset(&m_keystate[0], 0, m_keystate.Bytes());
}

private:
ivec2 mouse;
uint32_t buttons;

Array<uint8_t> m_keystate;

static int const MAX_ENTITIES = 100;
WorldEntity *entities[MAX_ENTITIES];
int nentities;
@@ -114,19 +111,14 @@ int InputTracker::GetPreviousButtonStatus(Key k)
//Internal : Updates the action status & timers
void InputTracker::UpdateActionStatus(float seconds)
{
#if defined USE_SDL
# if SDL_VERSION_ATLEAST(1,3,0)
Uint8 *keystate = SDL_GetKeyboardState(nullptr);
# else
Uint8 *keystate = SDL_GetKeyState(nullptr);
# endif
Array<uint8_t> &keystate = Input::GetKeyboardState();

//SOOOoooo ugly.
for (int i = 0; i < Key::Last; ++i)
{
m_input_status[i + Key::Last] = m_input_status[i];
m_input_status[i] = keystate[i];
}
#endif

for (int i = 0; i < m_input_assocation_list.Count(); i++)
{
@@ -267,22 +259,13 @@ vec2 Input::GetAxis(int axis)
{
vec2 ret;

#if defined USE_SDL
/* Simulate a joystick using the keyboard. This SDL call is free. */
# if SDL_VERSION_ATLEAST(1,3,0)
Uint8 *keystate = SDL_GetKeyboardState(nullptr);
# else
Uint8 *keystate = SDL_GetKeyState(nullptr);
# endif
int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]);
int up = (keystate[SDLK_w] | keystate[SDLK_z]) - keystate[SDLK_s] ;
/* Simulate a joystick using the keyboard. */
int left = GetKeyState(Key::D) - (GetKeyState(Key::A) | GetKeyState(Key::Q));
int up = (GetKeyState(Key::W) | GetKeyState(Key::Z)) - GetKeyState(Key::S);
ret.x += left;
ret.y += up;
if (left && up)
ret = ret * sqrtf(0.5f);
#else
ret = vec2(0, 0);
#endif

return ret;
}
@@ -298,19 +281,14 @@ uint32_t Input::GetMouseButtons()
return data->buttons;
}

//BH : Added this, is a v0.1 Alpha version.
int Input::GetButtonState(int button)
Array<uint8_t> &Input::GetKeyboardState()
{
#if defined USE_SDL
# if SDL_VERSION_ATLEAST(1,3,0)
Uint8 *keystate = SDL_GetKeyboardState(nullptr);
# else
Uint8 *keystate = SDL_GetKeyState(nullptr);
# endif
return keystate[button];
#else
return 0;
#endif
return data->m_keystate;
}

int Input::GetKeyState(int key)
{
return data->m_keystate[key];
}

//Helps link a software input Action-Id to an hardware input Button-Id.


+ 3
- 3
src/input/input.h Voir le fichier

@@ -413,9 +413,6 @@ public:
static ivec2 GetMousePos();
static uint32_t GetMouseButtons();

//BH : Shouldn't use this
static int GetButtonState(int button);

/* Action management */
static void LinkActionToKey(Action a, struct Key k);
static void UnlinkAction(Action a);
@@ -440,6 +437,9 @@ public:
static void UnsetMouseButton(int index);

/* Keyboard handling */
static Array<uint8_t> &GetKeyboardState();
static int GetKeyState(int key);

static Keyboard *CreateKeyboard();
static void DestroyKeyboard(Keyboard *keyboard);



+ 249
- 8
src/platform/sdl/sdlinput.cpp Voir le fichier

@@ -143,8 +143,8 @@ void SdlInputData::Tick(float seconds)
ivec2 mouse = SdlInputData::GetMousePos();;
Input::SetMousePos(mouse);

# if SDL_FORCE_POLL_JOYSTICK && EMSCRIPTEN
/* Pump all joystick events because no event is coming to us. */
# if SDL_FORCE_POLL_JOYSTICK && EMSCRIPTEN
SDL_JoystickUpdate();
for (int j = 0; j < m_joysticks.Count(); j++)
{
@@ -197,15 +197,256 @@ void SdlInputData::Tick(float seconds)
}

/* Send the whole keyboard state to the input system */
#if 0
Uint8 *keystate = SDL_GetKeyState(nullptr);
for (int i = 0; i < 256; i++)
if (keystate[i])
Input::KeyPressed(i, seconds);
#else
Array<uint8_t> &lolstate = Input::GetKeyboardState();

# if SDL_VERSION_ATLEAST(1,3,0)
Uint8 *sdlstate = SDL_GetKeyboardState(nullptr);
# else
Uint8 *sdlstate = SDL_GetKeyState(nullptr);
# endif

lolstate[Key::Unknown] = sdlstate[SDLK_UNKNOWN];
lolstate[Key::Backspace] = sdlstate[SDLK_BACKSPACE];
lolstate[Key::Tab] = sdlstate[SDLK_TAB];
lolstate[Key::Clear] = sdlstate[SDLK_CLEAR];
lolstate[Key::Return] = sdlstate[SDLK_RETURN];
lolstate[Key::Pause] = sdlstate[SDLK_PAUSE];
lolstate[Key::Escape] = sdlstate[SDLK_ESCAPE];
lolstate[Key::Space] = sdlstate[SDLK_SPACE];
lolstate[Key::Exclaim] = sdlstate[SDLK_EXCLAIM];
lolstate[Key::DoubleQuote] = sdlstate[SDLK_QUOTEDBL];
lolstate[Key::Hash] = sdlstate[SDLK_HASH];
lolstate[Key::Dollar] = sdlstate[SDLK_DOLLAR];
lolstate[Key::Ampersand] = sdlstate[SDLK_AMPERSAND];
lolstate[Key::Quote] = sdlstate[SDLK_QUOTE];
lolstate[Key::LeftParen] = sdlstate[SDLK_LEFTPAREN];
lolstate[Key::RightParen] = sdlstate[SDLK_RIGHTPAREN];
lolstate[Key::Asterisk] = sdlstate[SDLK_ASTERISK];
lolstate[Key::Plus] = sdlstate[SDLK_PLUS];
lolstate[Key::Comma] = sdlstate[SDLK_COMMA];
lolstate[Key::Minus] = sdlstate[SDLK_MINUS];
lolstate[Key::Period] = sdlstate[SDLK_PERIOD];
lolstate[Key::Slash] = sdlstate[SDLK_SLASH];
lolstate[Key::K0] = sdlstate[SDLK_0];
lolstate[Key::K1] = sdlstate[SDLK_1];
lolstate[Key::K2] = sdlstate[SDLK_2];
lolstate[Key::K3] = sdlstate[SDLK_3];
lolstate[Key::K4] = sdlstate[SDLK_4];
lolstate[Key::K5] = sdlstate[SDLK_5];
lolstate[Key::K6] = sdlstate[SDLK_6];
lolstate[Key::K7] = sdlstate[SDLK_7];
lolstate[Key::K8] = sdlstate[SDLK_8];
lolstate[Key::K9] = sdlstate[SDLK_9];
lolstate[Key::Colon] = sdlstate[SDLK_COLON];
lolstate[Key::Semicolon] = sdlstate[SDLK_SEMICOLON];
lolstate[Key::Less] = sdlstate[SDLK_LESS];
lolstate[Key::Equals] = sdlstate[SDLK_EQUALS];
lolstate[Key::Greater] = sdlstate[SDLK_GREATER];
lolstate[Key::Question] = sdlstate[SDLK_QUESTION];
lolstate[Key::At] = sdlstate[SDLK_AT];

lolstate[Key::LeftBracket] = sdlstate[SDLK_LEFTBRACKET];
lolstate[Key::BackSlash] = sdlstate[SDLK_BACKSLASH];
lolstate[Key::RightBracket] = sdlstate[SDLK_RIGHTBRACKET];
lolstate[Key::Caret] = sdlstate[SDLK_CARET];
lolstate[Key::Underscore] = sdlstate[SDLK_UNDERSCORE];
lolstate[Key::Backquote] = sdlstate[SDLK_BACKQUOTE];
lolstate[Key::A] = sdlstate[SDLK_a];
lolstate[Key::B] = sdlstate[SDLK_b];
lolstate[Key::C] = sdlstate[SDLK_c];
lolstate[Key::D] = sdlstate[SDLK_d];
lolstate[Key::E] = sdlstate[SDLK_e];
lolstate[Key::F] = sdlstate[SDLK_f];
lolstate[Key::G] = sdlstate[SDLK_g];
lolstate[Key::H] = sdlstate[SDLK_h];
lolstate[Key::I] = sdlstate[SDLK_i];
lolstate[Key::J] = sdlstate[SDLK_j];
lolstate[Key::K] = sdlstate[SDLK_k];
lolstate[Key::L] = sdlstate[SDLK_l];
lolstate[Key::M] = sdlstate[SDLK_m];
lolstate[Key::N] = sdlstate[SDLK_n];
lolstate[Key::O] = sdlstate[SDLK_o];
lolstate[Key::P] = sdlstate[SDLK_p];
lolstate[Key::Q] = sdlstate[SDLK_q];
lolstate[Key::R] = sdlstate[SDLK_r];
lolstate[Key::S] = sdlstate[SDLK_s];
lolstate[Key::T] = sdlstate[SDLK_t];
lolstate[Key::U] = sdlstate[SDLK_u];
lolstate[Key::V] = sdlstate[SDLK_v];
lolstate[Key::W] = sdlstate[SDLK_w];
lolstate[Key::X] = sdlstate[SDLK_x];
lolstate[Key::Y] = sdlstate[SDLK_y];
lolstate[Key::Z] = sdlstate[SDLK_z];
lolstate[Key::Delete] = sdlstate[SDLK_DELETE];

lolstate[Key::World0] = sdlstate[SDLK_WORLD_0];
lolstate[Key::World1] = sdlstate[SDLK_WORLD_1];
lolstate[Key::World2] = sdlstate[SDLK_WORLD_2];
lolstate[Key::World3] = sdlstate[SDLK_WORLD_3];
lolstate[Key::World4] = sdlstate[SDLK_WORLD_4];
lolstate[Key::World5] = sdlstate[SDLK_WORLD_5];
lolstate[Key::World6] = sdlstate[SDLK_WORLD_6];
lolstate[Key::World7] = sdlstate[SDLK_WORLD_7];
lolstate[Key::World8] = sdlstate[SDLK_WORLD_8];
lolstate[Key::World9] = sdlstate[SDLK_WORLD_9];
lolstate[Key::World10] = sdlstate[SDLK_WORLD_10];
lolstate[Key::World11] = sdlstate[SDLK_WORLD_11];
lolstate[Key::World12] = sdlstate[SDLK_WORLD_12];
lolstate[Key::World13] = sdlstate[SDLK_WORLD_13];
lolstate[Key::World14] = sdlstate[SDLK_WORLD_14];
lolstate[Key::World15] = sdlstate[SDLK_WORLD_15];
lolstate[Key::World16] = sdlstate[SDLK_WORLD_16];
lolstate[Key::World17] = sdlstate[SDLK_WORLD_17];
lolstate[Key::World18] = sdlstate[SDLK_WORLD_18];
lolstate[Key::World19] = sdlstate[SDLK_WORLD_19];
lolstate[Key::World20] = sdlstate[SDLK_WORLD_20];
lolstate[Key::World21] = sdlstate[SDLK_WORLD_21];
lolstate[Key::World22] = sdlstate[SDLK_WORLD_22];
lolstate[Key::World23] = sdlstate[SDLK_WORLD_23];
lolstate[Key::World24] = sdlstate[SDLK_WORLD_24];
lolstate[Key::World25] = sdlstate[SDLK_WORLD_25];
lolstate[Key::World26] = sdlstate[SDLK_WORLD_26];
lolstate[Key::World27] = sdlstate[SDLK_WORLD_27];
lolstate[Key::World28] = sdlstate[SDLK_WORLD_28];
lolstate[Key::World29] = sdlstate[SDLK_WORLD_29];
lolstate[Key::World30] = sdlstate[SDLK_WORLD_30];
lolstate[Key::World31] = sdlstate[SDLK_WORLD_31];
lolstate[Key::World32] = sdlstate[SDLK_WORLD_32];
lolstate[Key::World33] = sdlstate[SDLK_WORLD_33];
lolstate[Key::World34] = sdlstate[SDLK_WORLD_34];
lolstate[Key::World35] = sdlstate[SDLK_WORLD_35];
lolstate[Key::World36] = sdlstate[SDLK_WORLD_36];
lolstate[Key::World37] = sdlstate[SDLK_WORLD_37];
lolstate[Key::World38] = sdlstate[SDLK_WORLD_38];
lolstate[Key::World39] = sdlstate[SDLK_WORLD_39];
lolstate[Key::World40] = sdlstate[SDLK_WORLD_40];
lolstate[Key::World41] = sdlstate[SDLK_WORLD_41];
lolstate[Key::World42] = sdlstate[SDLK_WORLD_42];
lolstate[Key::World43] = sdlstate[SDLK_WORLD_43];
lolstate[Key::World44] = sdlstate[SDLK_WORLD_44];
lolstate[Key::World45] = sdlstate[SDLK_WORLD_45];
lolstate[Key::World46] = sdlstate[SDLK_WORLD_46];
lolstate[Key::World47] = sdlstate[SDLK_WORLD_47];
lolstate[Key::World48] = sdlstate[SDLK_WORLD_48];
lolstate[Key::World49] = sdlstate[SDLK_WORLD_49];
lolstate[Key::World50] = sdlstate[SDLK_WORLD_50];
lolstate[Key::World51] = sdlstate[SDLK_WORLD_51];
lolstate[Key::World52] = sdlstate[SDLK_WORLD_52];
lolstate[Key::World53] = sdlstate[SDLK_WORLD_53];
lolstate[Key::World54] = sdlstate[SDLK_WORLD_54];
lolstate[Key::World55] = sdlstate[SDLK_WORLD_55];
lolstate[Key::World56] = sdlstate[SDLK_WORLD_56];
lolstate[Key::World57] = sdlstate[SDLK_WORLD_57];
lolstate[Key::World58] = sdlstate[SDLK_WORLD_58];
lolstate[Key::World59] = sdlstate[SDLK_WORLD_59];
lolstate[Key::World60] = sdlstate[SDLK_WORLD_60];
lolstate[Key::World61] = sdlstate[SDLK_WORLD_61];
lolstate[Key::World62] = sdlstate[SDLK_WORLD_62];
lolstate[Key::World63] = sdlstate[SDLK_WORLD_63];
lolstate[Key::World64] = sdlstate[SDLK_WORLD_64];
lolstate[Key::World65] = sdlstate[SDLK_WORLD_65];
lolstate[Key::World66] = sdlstate[SDLK_WORLD_66];
lolstate[Key::World67] = sdlstate[SDLK_WORLD_67];
lolstate[Key::World68] = sdlstate[SDLK_WORLD_68];
lolstate[Key::World69] = sdlstate[SDLK_WORLD_69];
lolstate[Key::World70] = sdlstate[SDLK_WORLD_70];
lolstate[Key::World71] = sdlstate[SDLK_WORLD_71];
lolstate[Key::World72] = sdlstate[SDLK_WORLD_72];
lolstate[Key::World73] = sdlstate[SDLK_WORLD_73];
lolstate[Key::World74] = sdlstate[SDLK_WORLD_74];
lolstate[Key::World75] = sdlstate[SDLK_WORLD_75];
lolstate[Key::World76] = sdlstate[SDLK_WORLD_76];
lolstate[Key::World77] = sdlstate[SDLK_WORLD_77];
lolstate[Key::World78] = sdlstate[SDLK_WORLD_78];
lolstate[Key::World79] = sdlstate[SDLK_WORLD_79];
lolstate[Key::World80] = sdlstate[SDLK_WORLD_80];
lolstate[Key::World81] = sdlstate[SDLK_WORLD_81];
lolstate[Key::World82] = sdlstate[SDLK_WORLD_82];
lolstate[Key::World83] = sdlstate[SDLK_WORLD_83];
lolstate[Key::World84] = sdlstate[SDLK_WORLD_84];
lolstate[Key::World85] = sdlstate[SDLK_WORLD_85];
lolstate[Key::World86] = sdlstate[SDLK_WORLD_86];
lolstate[Key::World87] = sdlstate[SDLK_WORLD_87];
lolstate[Key::World88] = sdlstate[SDLK_WORLD_88];
lolstate[Key::World89] = sdlstate[SDLK_WORLD_89];
lolstate[Key::World90] = sdlstate[SDLK_WORLD_90];
lolstate[Key::World91] = sdlstate[SDLK_WORLD_91];
lolstate[Key::World92] = sdlstate[SDLK_WORLD_92];
lolstate[Key::World93] = sdlstate[SDLK_WORLD_93];
lolstate[Key::World94] = sdlstate[SDLK_WORLD_94];
lolstate[Key::World95] = sdlstate[SDLK_WORLD_95];

lolstate[Key::KP0] = sdlstate[SDLK_KP0];
lolstate[Key::KP1] = sdlstate[SDLK_KP1];
lolstate[Key::KP2] = sdlstate[SDLK_KP2];
lolstate[Key::KP3] = sdlstate[SDLK_KP3];
lolstate[Key::KP4] = sdlstate[SDLK_KP4];
lolstate[Key::KP5] = sdlstate[SDLK_KP5];
lolstate[Key::KP6] = sdlstate[SDLK_KP6];
lolstate[Key::KP7] = sdlstate[SDLK_KP7];
lolstate[Key::KP8] = sdlstate[SDLK_KP8];
lolstate[Key::KP9] = sdlstate[SDLK_KP9];
lolstate[Key::KPPeriod] = sdlstate[SDLK_KP_PERIOD];
lolstate[Key::KPDivide] = sdlstate[SDLK_KP_DIVIDE];
lolstate[Key::KPMultiply] = sdlstate[SDLK_KP_MULTIPLY];
lolstate[Key::KPMinus] = sdlstate[SDLK_KP_MINUS];
lolstate[Key::KPPlus] = sdlstate[SDLK_KP_PLUS];
lolstate[Key::KPEnter] = sdlstate[SDLK_KP_ENTER];
lolstate[Key::KPEquals] = sdlstate[SDLK_KP_EQUALS];

lolstate[Key::Up] = sdlstate[SDLK_UP];
lolstate[Key::Down] = sdlstate[SDLK_DOWN];
lolstate[Key::Right] = sdlstate[SDLK_RIGHT];
lolstate[Key::Left] = sdlstate[SDLK_LEFT];
lolstate[Key::Insert] = sdlstate[SDLK_INSERT];
lolstate[Key::Home] = sdlstate[SDLK_HOME];
lolstate[Key::End] = sdlstate[SDLK_END];
lolstate[Key::PageUp] = sdlstate[SDLK_PAGEUP];
lolstate[Key::PageDown] = sdlstate[SDLK_PAGEDOWN];

lolstate[Key::F1] = sdlstate[SDLK_F1];
lolstate[Key::F2] = sdlstate[SDLK_F2];
lolstate[Key::F3] = sdlstate[SDLK_F3];
lolstate[Key::F4] = sdlstate[SDLK_F4];
lolstate[Key::F5] = sdlstate[SDLK_F5];
lolstate[Key::F6] = sdlstate[SDLK_F6];
lolstate[Key::F7] = sdlstate[SDLK_F7];
lolstate[Key::F8] = sdlstate[SDLK_F8];
lolstate[Key::F9] = sdlstate[SDLK_F9];
lolstate[Key::F10] = sdlstate[SDLK_F10];
lolstate[Key::F11] = sdlstate[SDLK_F11];
lolstate[Key::F12] = sdlstate[SDLK_F12];
lolstate[Key::F13] = sdlstate[SDLK_F13];
lolstate[Key::F14] = sdlstate[SDLK_F14];
lolstate[Key::F15] = sdlstate[SDLK_F15];

lolstate[Key::NumLock] = sdlstate[SDLK_NUMLOCK];
lolstate[Key::CapsLock] = sdlstate[SDLK_CAPSLOCK];
lolstate[Key::ScrollLock] = sdlstate[SDLK_SCROLLOCK];
lolstate[Key::RightShift] = sdlstate[SDLK_RSHIFT];
lolstate[Key::LeftShift] = sdlstate[SDLK_LSHIFT];
lolstate[Key::RightCtrl] = sdlstate[SDLK_RCTRL];
lolstate[Key::LeftCtrl] = sdlstate[SDLK_LCTRL];
lolstate[Key::RightAlt] = sdlstate[SDLK_RALT];
lolstate[Key::LeftAlt] = sdlstate[SDLK_LALT];
lolstate[Key::RightMeta] = sdlstate[SDLK_RMETA];
lolstate[Key::LeftMeta] = sdlstate[SDLK_LMETA];
lolstate[Key::LeftSuper] = sdlstate[SDLK_LSUPER];
lolstate[Key::RightSuper] = sdlstate[SDLK_RSUPER];
lolstate[Key::Mode] = sdlstate[SDLK_MODE];
lolstate[Key::Compose] = sdlstate[SDLK_COMPOSE];

lolstate[Key::Help] = sdlstate[SDLK_HELP];
lolstate[Key::Print] = sdlstate[SDLK_PRINT];
lolstate[Key::SysReq] = sdlstate[SDLK_SYSREQ];
lolstate[Key::Break] = sdlstate[SDLK_BREAK];
lolstate[Key::Menu] = sdlstate[SDLK_MENU];
lolstate[Key::Power] = sdlstate[SDLK_POWER];
lolstate[Key::Euro] = sdlstate[SDLK_EURO];
lolstate[Key::Undo] = sdlstate[SDLK_UNDO];

UNUSED(seconds);
#endif
#endif
}

ivec2 SdlInputData::GetMousePos()


Chargement…
Annuler
Enregistrer