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

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

core: fix the X360 build by re-adding macro barriers I removed.

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