Browse Source

input: clean up a lot of SDL input code.

legacy
Sam Hocevar 6 years ago
parent
commit
ca42fec447
3 changed files with 64 additions and 107 deletions
  1. +11
    -11
      src/input/input_internal.h
  2. +25
    -87
      src/input/sdl-input.cpp
  3. +28
    -9
      src/input/sdl-input.h

+ 11
- 11
src/input/input_internal.h View File

@@ -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;


+ 25
- 87
src/input/sdl-input.cpp View File

@@ -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<SDL_Joystick *, InputDeviceInternal *> 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 */


+ 28
- 9
src/input/sdl-input.h View File

@@ -1,11 +1,13 @@
//
// Lol Engine
// Lol Engine
//
// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
// 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 <sam@hocevar.net>
//
// 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 <SDL2/SDL.h>
# elif HAVE_SDL_H
# include <SDL.h>
# 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<SDL_Joystick *, class InputDeviceInternal *> 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 */


Loading…
Cancel
Save