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

Last change on this file since 1399 was 1399, checked in by sam, 8 years ago

input: fix brain fart in our SDL joystick input.

  • Property svn:keywords set to Id
File size: 4.2 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#   include <SDL.h>
17#endif
18
19#include "core.h"
20#include "sdlinput.h"
21
22namespace lol
23{
24
25/*
26 * SDL Input implementation class
27 */
28
29class SdlInputData
30{
31    friend class SdlInput;
32
33private:
34    void Tick(float seconds);
35
36    static ivec2 GetMousePos();
37#if defined USE_SDL
38    Array<SDL_Joystick *, Stick *> m_joysticks;
39#endif
40};
41
42/*
43 * Public SdlInput class
44 */
45
46SdlInput::SdlInput()
47  : m_data(new SdlInputData())
48{
49#if defined USE_SDL
50    SDL_Init(SDL_INIT_TIMER | SDL_INIT_JOYSTICK);
51
52    /* Register all the joysticks we can find, and let the input
53     * system decide what it wants to track. */
54    SDL_JoystickEventState(SDL_ENABLE);
55    for (int i = 0; i < SDL_NumJoysticks(); i++)
56    {
57        SDL_Joystick *sdlstick = SDL_JoystickOpen(i);
58
59        /* Blacklist HDAPS, it's not a real joystick */
60        char const *name = SDL_JoystickName(i);
61        if (strstr(name, "HDAPS")
62#   if !defined USE_D3D9
63             || strstr(name, "XBOX 360 For Windows")
64#   endif
65             || false)
66        {
67            SDL_JoystickClose(sdlstick);
68            continue;
69        }
70
71        Stick *stick = Input::CreateStick();
72        stick->SetAxisCount(SDL_JoystickNumAxes(sdlstick));
73        stick->SetButtonCount(SDL_JoystickNumButtons(sdlstick));
74
75        /* It's possible to remap axes */
76        //stick->RemapAxis(4, 2);
77        //stick->RemapAxis(2, 4);
78
79        m_data->m_joysticks.Push(sdlstick, stick);
80    }
81#endif
82
83    m_gamegroup = GAMEGROUP_BEFORE;
84}
85
86SdlInput::~SdlInput()
87{
88#if defined USE_SDL
89    /* Unregister all the joysticks we added */
90    while (m_data->m_joysticks.Count())
91    {
92        SDL_JoystickClose(m_data->m_joysticks[0].m1);
93        Input::DestroyStick(m_data->m_joysticks[0].m2);
94        m_data->m_joysticks.Remove(0);
95    }
96#endif
97    delete m_data;
98}
99
100void SdlInput::TickGame(float seconds)
101{
102    Entity::TickGame(seconds);
103
104#if !defined _WIN32
105    m_data->Tick(seconds);
106#endif
107}
108
109void SdlInput::TickDraw(float seconds)
110{
111    Entity::TickDraw(seconds);
112
113#if defined _WIN32
114    m_data->Tick(seconds);
115#endif
116}
117
118void SdlInputData::Tick(float seconds)
119{
120#if defined USE_SDL
121    /* Handle mouse input */
122    ivec2 mouse = SdlInputData::GetMousePos();;
123    Input::SetMousePos(mouse);
124
125    /* Handle keyboard and WM events */
126    SDL_Event event;
127    while (SDL_PollEvent(&event))
128    {
129        switch (event.type)
130        {
131        case SDL_QUIT:
132            Ticker::Shutdown();
133            break;
134#if 0
135        case SDL_KEYDOWN:
136            Input::KeyPressed(event.key.keysym.sym, seconds);
137            break;
138#endif
139        case SDL_MOUSEBUTTONDOWN:
140        case SDL_MOUSEBUTTONUP:
141        {
142            ivec2 newmouse = SdlInputData::GetMousePos();
143            if (newmouse != mouse)
144                Input::SetMousePos(mouse = newmouse);
145            if (event.type == SDL_MOUSEBUTTONDOWN)
146                Input::SetMouseButton(event.button.button - 1);
147            else
148                Input::UnsetMouseButton(event.button.button - 1);
149            break;
150        }
151
152        case SDL_JOYAXISMOTION:
153            m_joysticks[event.jaxis.which].m2->SetAxis(event.jaxis.axis, (float)event.jaxis.value / 32768.f);
154            break;
155
156        case SDL_JOYBUTTONUP:
157        case SDL_JOYBUTTONDOWN:
158            m_joysticks[event.jbutton.which].m2->SetButton(event.jbutton.button, event.jbutton.state);
159            break;
160        }
161    }
162
163    /* Send the whole keyboard state to the input system */
164#if 0
165    Uint8 *keystate = SDL_GetKeyState(NULL);
166    for (int i = 0; i < 256; i++)
167        if (keystate[i])
168            Input::KeyPressed(i, seconds);
169#endif
170#endif
171}
172
173ivec2 SdlInputData::GetMousePos()
174{
175    ivec2 ret(-1, -1);
176
177#if defined USE_SDL
178    if (SDL_GetAppState() & SDL_APPMOUSEFOCUS)
179    {
180        SDL_GetMouseState(&ret.x, &ret.y);
181        ret.y = Video::GetSize().y - 1 - ret.y;
182    }
183#endif
184    return ret;
185}
186
187} /* namespace lol */
188
Note: See TracBrowser for help on using the repository browser.