From cfd85fb3a73d183b07f5783c6dc918bb8a8616e3 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 19 Feb 2019 13:59:19 +0100 Subject: [PATCH] input: remove some dead code and clarify some function names. --- src/input/input_internal.h | 11 ++--- src/input/sdl-input.cpp | 67 +++++++++-------------------- src/platform/android/androidapp.cpp | 10 ++--- src/platform/d3d9/d3d9input.cpp | 16 +++---- 4 files changed, 39 insertions(+), 65 deletions(-) diff --git a/src/input/input_internal.h b/src/input/input_internal.h index 207e5b59..06f2db46 100644 --- a/src/input/input_internal.h +++ b/src/input/input_internal.h @@ -1,8 +1,8 @@ // // Lol Engine // -// Copyright © 2010—2015 Benjamin Litzelmann -// © 2017—2018 Sam Hocevar +// Copyright © 2017—2019 Sam Hocevar +// © 2010—2015 Benjamin Litzelmann // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -47,17 +47,18 @@ public: AddCursor(-1, name); } - void SetKey(int id, bool state) + /* Internal functions for the platform-specific drivers. */ + void internal_set_key(int id, bool state) { m_keys[id] = state; } - void AddText(std::string const &text) + void internal_add_text(std::string const &text) { m_text += text; } - void SetAxis(int id, float value) + void internal_set_axis(int id, float value) { m_axis[id].m1 = value; } diff --git a/src/input/sdl-input.cpp b/src/input/sdl-input.cpp index d0d78f06..b27c33d6 100644 --- a/src/input/sdl-input.cpp +++ b/src/input/sdl-input.cpp @@ -39,21 +39,8 @@ namespace lol { -//------------------------------------------------------------------------- #define _SC(id, str, name) static const uint16_t SDLOL_##name = id; #include "input/keys.h" -//------------------------------------------------------------------------- -static bool ScanCodeIsValid(int sc) -{ - switch (sc) - { -#define _SC(id, str, name) \ - case id: return true; -#include "input/keys.h" - default: return false; - } - return false; -} //------------------------------------------------------------------------- /* DEBUG STUFF @@ -232,13 +219,13 @@ void SdlInputData::Tick(float seconds) for (int j = 0; j < m_joysticks.count(); j++) { for (int i = 0; i < SDL_JoystickNumButtons(m_joysticks[j].m1); i++) - m_joysticks[j].m2->SetKey(i, SDL_JoystickGetButton(m_joysticks[j].m1, i) != 0); + m_joysticks[j].m2->internal_set_key(i, SDL_JoystickGetButton(m_joysticks[j].m1, i) != 0); for (int i = 0; i < SDL_JoystickNumAxes(m_joysticks[j].m1); i++) - m_joysticks[j].m2->SetAxis(i, (float)SDL_JoystickGetAxis(m_joysticks[j].m1, i) / 32768.f); + m_joysticks[j].m2->internal_set_axis(i, (float)SDL_JoystickGetAxis(m_joysticks[j].m1, i) / 32768.f); } # endif - m_mouse->SetAxis(4, 0); + m_mouse->internal_set_axis(4, 0); if (m_keyboard->IsTextInputActive()) SDL_StartTextInput(); @@ -259,7 +246,7 @@ void SdlInputData::Tick(float seconds) case SDL_KEYUP: switch (int sc = event.key.keysym.scancode) { - //Lock management + //Lock management case SDLOL_CapsLock: case SDLOL_ScrollLock: case SDLOL_NumLockClear: @@ -280,7 +267,7 @@ void SdlInputData::Tick(float seconds) sc2 = SDLOL_NumLockClearStatus; break; } - m_keyboard->SetKey(sc2, !m_keyboard->GetKey(sc2)); + m_keyboard->internal_set_key(sc2, !m_keyboard->GetKey(sc2)); /* DEBUG STUFF msg::debug("Repeat: 0x%02x : %s/%s/%s/%i\n", (int)m_keyboard, ScanCodeToText(sc2).C(), ScanCodeToName(sc2).C(), @@ -289,38 +276,24 @@ void SdlInputData::Tick(float seconds) } # endif default: - if (ScanCodeIsValid(sc)) - { - // Set key updates the corresponding key - m_keyboard->SetKey(sc, event.type == SDL_KEYDOWN); - - /* DEBUG STUFF - msg::debug("Repeat: 0x%02x : %s/%s/%s/%i\n", - (int)m_keyboard, ScanCodeToText(sc).C(), ScanCodeToName(sc).C(), - event.type == SDL_KEYDOWN ? "up" : "down", event.key.repeat); - */ - } - /* DEBUG STUFF - else - msg::error("unknown keypress (sym 0x%02x, scancode %0d)\n", - event.key.keysym.sym, event.key.keysym.scancode); - */ + // Set key updates the corresponding key + m_keyboard->internal_set_key(sc, event.type == SDL_KEYDOWN); + break; } break; - //case SDL_TEXTEDITING: //TODO: handle that ? + //case SDL_TEXTEDITING: //TODO: handle that? case SDL_TEXTINPUT: - m_keyboard->AddText(event.text.text); + m_keyboard->internal_add_text(event.text.text); break; - case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: //event.button.which - m_mouse->SetKey(event.button.button - 1, event.type == SDL_MOUSEBUTTONDOWN); + m_mouse->internal_set_key(event.button.button - 1, event.type == SDL_MOUSEBUTTONDOWN); break; case SDL_MOUSEWHEEL: - m_mouse->SetAxis(4, (float)event.button.y); + m_mouse->internal_set_axis(4, (float)event.button.y); break; case SDL_WINDOWEVENT: { @@ -328,11 +301,11 @@ void SdlInputData::Tick(float seconds) { case SDL_WINDOWEVENT_ENTER: case SDL_WINDOWEVENT_FOCUS_GAINED: - m_mouse->SetKey(3, true); + m_mouse->internal_set_key(3, true); break; case SDL_WINDOWEVENT_LEAVE: case SDL_WINDOWEVENT_FOCUS_LOST: - m_mouse->SetKey(3, false); + m_mouse->internal_set_key(3, false); break; case SDL_WINDOWEVENT_RESIZED: Video::Resize(ivec2(event.window.data1, event.window.data2)); @@ -343,12 +316,12 @@ void SdlInputData::Tick(float seconds) # if !SDL_FORCE_POLL_JOYSTICK case SDL_JOYAXISMOTION: - m_joysticks[event.jaxis.which].m2->SetAxis(event.jaxis.axis, (float)event.jaxis.value / 32768.f); + m_joysticks[event.jaxis.which].m2->internal_set_axis(event.jaxis.axis, (float)event.jaxis.value / 32768.f); break; case SDL_JOYBUTTONUP: case SDL_JOYBUTTONDOWN: - m_joysticks[event.jbutton.which].m2->SetKey(event.jbutton.button, event.jbutton.state); + m_joysticks[event.jbutton.which].m2->internal_set_key(event.jbutton.button, event.jbutton.state); break; # endif } @@ -375,12 +348,12 @@ void SdlInputData::Tick(float seconds) vec2 vprevmouse = vec2(m_prevmouse); m_mouse->SetCursor(0, vmouse / m_app, mouse); // Note: 100.0f is an arbitrary value that makes it feel about the same than an xbox controller joystick - m_mouse->SetAxis(0, (mouse.x - vprevmouse.x) * MOUSE_SPEED_MOD / max_screen_size); + m_mouse->internal_set_axis(0, (mouse.x - vprevmouse.x) * MOUSE_SPEED_MOD / max_screen_size); // Y Axis is also negated to match the usual joystick Y axis (negatives values are for the upper direction) - m_mouse->SetAxis(1,-(mouse.y - vprevmouse.y) * MOUSE_SPEED_MOD / max_screen_size); + m_mouse->internal_set_axis(1,-(mouse.y - vprevmouse.y) * MOUSE_SPEED_MOD / max_screen_size); //Pixel movement - m_mouse->SetAxis(2, (mouse.x - vprevmouse.x)); - m_mouse->SetAxis(3,-(mouse.y - vprevmouse.y)); + m_mouse->internal_set_axis(2, (mouse.x - vprevmouse.x)); + m_mouse->internal_set_axis(3,-(mouse.y - vprevmouse.y)); } if (m_mousecapture) diff --git a/src/platform/android/androidapp.cpp b/src/platform/android/androidapp.cpp index 2eaf15f3..4ab80922 100644 --- a/src/platform/android/androidapp.cpp +++ b/src/platform/android/androidapp.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -209,17 +209,17 @@ int32_t lol::AndroidAppData::HandleInput(AInputEvent* event) pos.y = m_wanted_resolution.y - 1 - pos.y; m_mouse->SetCursor(0, vec2(pos) / vec2(m_wanted_resolution), pos); // Note: 100.0f is an arbitrary value that makes it feel about the same than an xbox controller joystick - m_mouse->SetAxis(0, (pos.x - m_prev_pos.x) / max_screen_size * 100.f); + m_mouse->internal_set_axis(0, (pos.x - m_prev_pos.x) / max_screen_size * 100.f); // Unlike SDL, no need to negate Y axis - m_mouse->SetAxis(1, (pos.y - m_prev_pos.y) / max_screen_size * -100.f); + m_mouse->internal_set_axis(1, (pos.y - m_prev_pos.y) / max_screen_size * -100.f); m_prev_pos = pos; switch (AKeyEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK) { case AMOTION_EVENT_ACTION_DOWN: - m_mouse->SetKey(0, true); + m_mouse->internal_set_key(0, true); break; case AMOTION_EVENT_ACTION_UP: - m_mouse->SetKey(0, false); + m_mouse->internal_set_key(0, false); break; } return 1; diff --git a/src/platform/d3d9/d3d9input.cpp b/src/platform/d3d9/d3d9input.cpp index d0e605e8..d6652d60 100644 --- a/src/platform/d3d9/d3d9input.cpp +++ b/src/platform/d3d9/d3d9input.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2016 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -110,12 +110,12 @@ void D3d9Input::tick_game(float seconds) if (XInputGetState(m_data->m_joysticks[i].m1, &state) != ERROR_SUCCESS) continue; - m_data->m_joysticks[i].m2->SetAxis(0, (float)state.Gamepad.sThumbLX / 32768.f); - m_data->m_joysticks[i].m2->SetAxis(1, -(float)state.Gamepad.sThumbLY / 32768.f); - m_data->m_joysticks[i].m2->SetAxis(2, (float)state.Gamepad.sThumbRX / 32768.f); - m_data->m_joysticks[i].m2->SetAxis(3, -(float)state.Gamepad.sThumbRY / 32768.f); - m_data->m_joysticks[i].m2->SetAxis(4, (float)state.Gamepad.bLeftTrigger / 32768.f); - m_data->m_joysticks[i].m2->SetAxis(5, (float)state.Gamepad.bRightTrigger / 32768.f); + m_data->m_joysticks[i].m2->internal_set_axis(0, (float)state.Gamepad.sThumbLX / 32768.f); + m_data->m_joysticks[i].m2->internal_set_axis(1, -(float)state.Gamepad.sThumbLY / 32768.f); + m_data->m_joysticks[i].m2->internal_set_axis(2, (float)state.Gamepad.sThumbRX / 32768.f); + m_data->m_joysticks[i].m2->internal_set_axis(3, -(float)state.Gamepad.sThumbRY / 32768.f); + m_data->m_joysticks[i].m2->internal_set_axis(4, (float)state.Gamepad.bLeftTrigger / 32768.f); + m_data->m_joysticks[i].m2->internal_set_axis(5, (float)state.Gamepad.bRightTrigger / 32768.f); for (int b = 0; b < 16; b++) { @@ -125,7 +125,7 @@ void D3d9Input::tick_game(float seconds) int key_index = (1 << b) > XINPUT_GAMEPAD_RIGHT_SHOULDER ? b - 2 : b; - m_data->m_joysticks[i].m2->SetKey(key_index, ((uint16_t)(state.Gamepad.wButtons) >> b) & 1); + m_data->m_joysticks[i].m2->internal_set_key(key_index, ((uint16_t)(state.Gamepad.wButtons) >> b) & 1); } } #endif