From ca42fec4477c1c6f69c3113a686578cbf615b302 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 19 Feb 2019 14:02:27 +0100 Subject: [PATCH] input: clean up a lot of SDL input code. --- src/input/input_internal.h | 22 ++++---- src/input/sdl-input.cpp | 112 +++++++++---------------------------- src/input/sdl-input.h | 37 +++++++++--- 3 files changed, 64 insertions(+), 107 deletions(-) diff --git a/src/input/input_internal.h b/src/input/input_internal.h index 06f2db46..d641ed7d 100644 --- a/src/input/input_internal.h +++ b/src/input/input_internal.h @@ -47,6 +47,17 @@ public: AddCursor(-1, name); } + void SetCursor(int id, vec2 const & position, ivec2 const & pixel) + { + m_cursors[id].m1 = position; + m_cursors[id].m2 = pixel; + } + + ivec2 GetCursorPixelPos(int id) + { + return m_cursors[id].m2; + } + /* Internal functions for the platform-specific drivers. */ void internal_set_key(int id, bool state) { @@ -63,17 +74,6 @@ public: m_axis[id].m1 = value; } - void SetCursor(int id, vec2 const & position, ivec2 const & pixel) - { - m_cursors[id].m1 = position; - m_cursors[id].m2 = pixel; - } - - ivec2 GetCursorPixelPos(int id) - { - return m_cursors[id].m2; - } - static bool GetMouseCapture() { return m_capturemouse; diff --git a/src/input/sdl-input.cpp b/src/input/sdl-input.cpp index b27c33d6..096f0fed 100644 --- a/src/input/sdl-input.cpp +++ b/src/input/sdl-input.cpp @@ -73,53 +73,16 @@ static String ScanCodeToName(int sc) } */ -/* - * SDL Input implementation class - */ - -class SdlInputData -{ - friend class SdlInput; - -private: - void Tick(float seconds); - - static ivec2 GetMousePos(); - static void SetMousePos(ivec2 position); - - SdlInputData(int app_w, int app_h, int screen_w, int screen_h) - : m_mouse(nullptr), - m_keyboard(nullptr), - m_prevmouse(ivec2::zero), - m_app(vec2((float)app_w, (float)app_h)), - m_screen(vec2((float)screen_w, (float)screen_h)), - m_mousecapture(false), - m_tick_in_draw_thread(false) - { } - -#if LOL_USE_SDL - array m_joysticks; - InputDeviceInternal *m_mouse; - InputDeviceInternal *m_keyboard; -#endif // LOL_USE_SDL - - ivec2 m_prevmouse; - vec2 m_app; - vec2 m_screen; - - bool m_mousecapture; - bool m_tick_in_draw_thread; -}; - /* * Public SdlInput class */ SdlInput::SdlInput(int app_w, int app_h, int screen_w, int screen_h) - : m_data(new SdlInputData(app_w, app_h, screen_w, screen_h)) + : m_app(vec2((float)app_w, (float)app_h)), + m_screen(vec2((float)screen_w, (float)screen_h)) { #if _WIN32 - m_data->m_tick_in_draw_thread = true; + m_tick_in_draw_thread = true; #endif #if __EMSCRIPTEN__ @@ -129,17 +92,13 @@ SdlInput::SdlInput(int app_w, int app_h, int screen_w, int screen_h) SDL_Init(SDL_INIT_TIMER | SDL_INIT_JOYSTICK); #endif - m_data->m_keyboard = InputDeviceInternal::CreateStandardKeyboard(); - m_data->m_mouse = InputDeviceInternal::CreateStandardMouse(); + m_keyboard = InputDeviceInternal::CreateStandardKeyboard(); + m_mouse = InputDeviceInternal::CreateStandardMouse(); #if LOL_USE_SDL // XXX: another option is to properly handle gamepad support # if !__EMSCRIPTEN__ -# if SDL_FORCE_POLL_JOYSTICK - SDL_JoystickEventState(SDL_QUERY); -# else - SDL_JoystickEventState(SDL_ENABLE); -# endif //SDL_FORCE_POLL_JOYSTICK + SDL_JoystickEventState(SDL_FORCE_POLL_JOYSTICK ? SDL_QUERY : SDL_ENABLE); /* Register all the joysticks we can find, and let the input * system decide what it wants to track. */ @@ -169,7 +128,7 @@ SdlInput::SdlInput(int app_w, int app_h, int screen_w, int screen_h) for (int j = 0; j < SDL_JoystickNumButtons(sdlstick); ++j) stick->AddKey(format("Button%d", j + 1).c_str()); - m_data->m_joysticks.push(sdlstick, stick); + m_joysticks.push(sdlstick, stick); } # endif // __EMSCRIPTEN__ #endif @@ -181,33 +140,32 @@ SdlInput::~SdlInput() { #if LOL_USE_SDL && !__EMSCRIPTEN__ /* Unregister all the joysticks we added */ - while (m_data->m_joysticks.count()) + while (m_joysticks.count()) { - SDL_JoystickClose(m_data->m_joysticks[0].m1); - delete m_data->m_joysticks[0].m2; - m_data->m_joysticks.remove(0); + SDL_JoystickClose(m_joysticks[0].m1); + delete m_joysticks[0].m2; + m_joysticks.remove(0); } #endif - delete m_data; } void SdlInput::tick_game(float seconds) { Entity::tick_game(seconds); - if (!m_data->m_tick_in_draw_thread) - m_data->Tick(seconds); + if (!m_tick_in_draw_thread) + tick(seconds); } void SdlInput::tick_draw(float seconds, Scene &scene) { Entity::tick_draw(seconds, scene); - if (m_data->m_tick_in_draw_thread) - m_data->Tick(seconds); + if (m_tick_in_draw_thread) + tick(seconds); } -void SdlInputData::Tick(float seconds) +void SdlInput::tick(float seconds) { #if LOL_USE_SDL /* FIXME: maybe we should make use of this? */ @@ -328,15 +286,18 @@ void SdlInputData::Tick(float seconds) } /* Handle mouse input */ - ivec2 mouse = SdlInputData::GetMousePos(); + ivec2 mouse(-1, -1); + SDL_GetMouseState(&mouse.x, &mouse.y); + mouse.y = Video::GetSize().y - 1 - mouse.y; + if (InputDeviceInternal::GetMouseCapture() != m_mousecapture) { m_mousecapture = InputDeviceInternal::GetMouseCapture(); -# if LOL_USE_SDL SDL_SetRelativeMouseMode(m_mousecapture ? SDL_TRUE : SDL_FALSE); -# endif - mouse = (ivec2)m_app / 2; - SdlInputData::SetMousePos(mouse); + mouse = ivec2(m_app * .5f); + + // FIXME: how do I warped mouse? + //SDL_WarpMouse((uint16_t)mouse.x, (uint16_t)mouse.y); //SDL_ShowCursor(m_mousecapture ? SDL_DISABLE : SDL_ENABLE); } @@ -359,7 +320,7 @@ void SdlInputData::Tick(float seconds) if (m_mousecapture) { mouse = ivec2(m_app * .5f); - SdlInputData::SetMousePos(mouse); + //SDL_WarpMouse((uint16_t)mouse.x, (uint16_t)mouse.y); } m_prevmouse = mouse; @@ -369,28 +330,5 @@ void SdlInputData::Tick(float seconds) #endif //LOL_USE_SDL } -// NOTE: these two functions are pointless now and could be inlined directly -ivec2 SdlInputData::GetMousePos() -{ - ivec2 ret(-1, -1); - -#if LOL_USE_SDL - { - SDL_GetMouseState(&ret.x, &ret.y); - ret.y = Video::GetSize().y - 1 - ret.y; - } -#endif - return ret; -} - -void SdlInputData::SetMousePos(ivec2 position) -{ - UNUSED(position); -#if LOL_USE_SDL - // FIXME: how do I warped mouse? - //SDL_WarpMouse((uint16_t)position.x, (uint16_t)position.y); -#endif -} - } /* namespace lol */ diff --git a/src/input/sdl-input.h b/src/input/sdl-input.h index 1e4e0093..f532bf87 100644 --- a/src/input/sdl-input.h +++ b/src/input/sdl-input.h @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2013 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// 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 +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #pragma once @@ -17,11 +19,17 @@ #include "engine/entity.h" +#if LOL_USE_SDL +# if HAVE_SDL2_SDL_H +# include +# elif HAVE_SDL_H +# include +# endif +#endif + namespace lol { -class SdlInputData; - class SdlInput : public Entity { public: @@ -36,7 +44,18 @@ protected: virtual void tick_draw(float seconds, Scene &scene); private: - SdlInputData *m_data; + void tick(float seconds); + + array m_joysticks; + InputDeviceInternal *m_mouse = nullptr; + InputDeviceInternal *m_keyboard = nullptr; + + ivec2 m_prevmouse = ivec2::zero; + vec2 m_app; + vec2 m_screen; + + bool m_mousecapture = false; + bool m_tick_in_draw_thread = false; }; } /* namespace lol */