source: trunk/src/platform/sdl/sdlinput.cpp @ 1808

Last change on this file since 1808 was 1808, checked in by sam, 10 years ago

build: fix shitloads of warnings.

  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if defined USE_SDL
16#   if defined HAVE_SDL_SDL_H
17#      include <SDL/SDL.h>
18#   else
19#      include <SDL.h>
20#   endif
21#endif
22
23#include "core.h"
24#include "sdlinput.h"
25
26/* We force joystick polling because no events are received when
27 * there is no SDL display (eg. on the Raspberry Pi). */
28#define SDL_FORCE_POLL_JOYSTICK 1
29
30namespace lol
31{
32
33/*
34 * SDL Input implementation class
35 */
36
37class SdlInputData
38{
39    friend class SdlInput;
40
41private:
42    void Tick(float seconds);
43
44    static ivec2 GetMousePos();
45#if defined USE_SDL
46    Array<SDL_Joystick *, Stick *> m_joysticks;
47#endif
48};
49
50/*
51 * Public SdlInput class
52 */
53
54SdlInput::SdlInput()
55  : m_data(new SdlInputData())
56{
57#if defined USE_SDL
58    SDL_Init(SDL_INIT_TIMER | SDL_INIT_JOYSTICK);
59
60#   if SDL_FORCE_POLL_JOYSTICK
61    SDL_JoystickEventState(SDL_QUERY);
62#   else
63    SDL_JoystickEventState(SDL_ENABLE);
64#   endif
65
66    /* Register all the joysticks we can find, and let the input
67     * system decide what it wants to track. */
68    for (int i = 0; i < SDL_NumJoysticks(); i++)
69    {
70        SDL_Joystick *sdlstick = SDL_JoystickOpen(i);
71
72        /* Blacklist some devices:
73         *  - HDAPS, it's not a real joystick.
74         *  - X360 controllers, Xinput handles them better since
75         *    it won't think there is only one trigger axis. */
76        char const *name = SDL_JoystickName(i);
77        if (strstr(name, "HDAPS")
78#   if defined USE_XINPUT
79             || strstr(name, "XBOX 360 For Windows")
80#   endif
81             || false)
82        {
83            SDL_JoystickClose(sdlstick);
84            continue;
85        }
86
87        Stick *stick = Input::CreateStick();
88        stick->SetAxisCount(SDL_JoystickNumAxes(sdlstick));
89        stick->SetButtonCount(SDL_JoystickNumButtons(sdlstick));
90
91        /* It's possible to remap axes */
92        //stick->RemapAxis(4, 2);
93        //stick->RemapAxis(2, 4);
94
95        m_data->m_joysticks.Push(sdlstick, stick);
96    }
97#endif
98
99    m_gamegroup = GAMEGROUP_BEFORE;
100}
101
102SdlInput::~SdlInput()
103{
104#if defined USE_SDL
105    /* Unregister all the joysticks we added */
106    while (m_data->m_joysticks.Count())
107    {
108        SDL_JoystickClose(m_data->m_joysticks[0].m1);
109        Input::DestroyStick(m_data->m_joysticks[0].m2);
110        m_data->m_joysticks.Remove(0);
111    }
112#endif
113    delete m_data;
114}
115
116void SdlInput::TickGame(float seconds)
117{
118    Entity::TickGame(seconds);
119
120#if !defined _WIN32
121    m_data->Tick(seconds);
122#endif
123}
124
125void SdlInput::TickDraw(float seconds)
126{
127    Entity::TickDraw(seconds);
128
129#if defined _WIN32
130    m_data->Tick(seconds);
131#endif
132}
133
134void SdlInputData::Tick(float seconds)
135{
136#if defined USE_SDL
137    /* Handle mouse input */
138    ivec2 mouse = SdlInputData::GetMousePos();;
139    Input::SetMousePos(mouse);
140
141#   if SDL_FORCE_POLL_JOYSTICK
142    /* Pump all joystick events because no event is coming to us. */
143    SDL_JoystickUpdate();
144    for (int j = 0; j < m_joysticks.Count(); j++)
145    {
146        for (int i = 0; i < SDL_JoystickNumButtons(m_joysticks[j].m1); i++)
147            m_joysticks[j].m2->SetButton(i, SDL_JoystickGetButton(m_joysticks[j].m1, i));
148        for (int i = 0; i < SDL_JoystickNumAxes(m_joysticks[j].m1); i++)
149            m_joysticks[j].m2->SetAxis(i, (float)SDL_JoystickGetAxis(m_joysticks[j].m1, i) / 32768.f);
150    }
151#   endif
152
153    /* Handle keyboard and WM events */
154    SDL_Event event;
155    while (SDL_PollEvent(&event))
156    {
157        switch (event.type)
158        {
159        case SDL_QUIT:
160            Ticker::Shutdown();
161            break;
162#if 0
163        case SDL_KEYDOWN:
164            Input::KeyPressed(event.key.keysym.sym, seconds);
165            break;
166#endif
167        case SDL_MOUSEBUTTONDOWN:
168        case SDL_MOUSEBUTTONUP:
169        {
170            ivec2 newmouse = SdlInputData::GetMousePos();
171            if (newmouse != mouse)
172                Input::SetMousePos(mouse = newmouse);
173            if (event.type == SDL_MOUSEBUTTONDOWN)
174                Input::SetMouseButton(event.button.button - 1);
175            else
176                Input::UnsetMouseButton(event.button.button - 1);
177            break;
178        }
179
180#   if !SDL_FORCE_POLL_JOYSTICK
181        case SDL_JOYAXISMOTION:
182            m_joysticks[event.jaxis.which].m2->SetAxis(event.jaxis.axis, (float)event.jaxis.value / 32768.f);
183            break;
184
185        case SDL_JOYBUTTONUP:
186        case SDL_JOYBUTTONDOWN:
187            m_joysticks[event.jbutton.which].m2->SetButton(event.jbutton.button, event.jbutton.state);
188            break;
189#   endif
190        }
191    }
192
193    /* Send the whole keyboard state to the input system */
194#if 0
195    Uint8 *keystate = SDL_GetKeyState(NULL);
196    for (int i = 0; i < 256; i++)
197        if (keystate[i])
198            Input::KeyPressed(i, seconds);
199#else
200    (void)seconds;
201#endif
202#endif
203}
204
205ivec2 SdlInputData::GetMousePos()
206{
207    ivec2 ret(-1, -1);
208
209#if defined USE_SDL
210    if (SDL_GetAppState() & SDL_APPMOUSEFOCUS)
211    {
212        SDL_GetMouseState(&ret.x, &ret.y);
213        ret.y = Video::GetSize().y - 1 - ret.y;
214    }
215#endif
216    return ret;
217}
218
219} /* namespace lol */
220
Note: See TracBrowser for help on using the repository browser.