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

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

build: tweak everything to allow building with SDL on the Raspberry Pi.

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