source: trunk/src/input/input.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.9 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#include <cstdlib>
16
17#if defined HAVE_SDL_SDL_H
18#   include <SDL/SDL.h>
19#else
20#   include <SDL.h>
21#endif
22
23#include "core.h"
24
25namespace lol
26{
27
28/*
29 * Input implementation class
30 */
31
32static class InputData
33{
34    friend class Input;
35
36public:
37    InputData()
38      : mouse(-1),
39        buttons(0),
40        nentities(0),
41        lastfocus(0)
42    { }
43
44private:
45    ivec2 mouse;
46    ivec3 buttons;
47
48    static int const MAX_ENTITIES = 100;
49    WorldEntity *entities[MAX_ENTITIES];
50    int nentities;
51    WorldEntity *lastfocus;
52
53    Array<Stick *> m_sticks;
54}
55inputdata;
56
57static InputData * const data = &inputdata;
58
59/*
60 * Public Input class
61 */
62
63#if 0
64vec2 Input::GetAxis(int axis)
65{
66    vec2 ret;
67
68#if defined USE_SDL
69    /* Simulate a joystick using the keyboard. This SDL call is free. */
70#if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION >= 3
71    Uint8 *keystate = SDL_GetKeyboardState(NULL);
72#else
73    Uint8 *keystate = SDL_GetKeyState(NULL);
74#endif
75    int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]);
76    int up = (keystate[SDLK_w] | keystate[SDLK_z]) - keystate[SDLK_s] ;
77    ret.x += left;
78    ret.y += up;
79    if (left && up)
80        ret = ret * sqrtf(0.5f);
81#else
82    ret = vec2(0, 0);
83#endif
84
85    return ret;
86}
87#endif
88
89ivec2 Input::GetMousePos()
90{
91    return data->mouse;
92}
93
94ivec3 Input::GetMouseButtons()
95{
96    return data->buttons;
97}
98
99//BH : Added this, is a v0.1 Alpha version.
100int Input::GetButtonState(int button)
101{
102#if defined USE_SDL
103#if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION >= 3
104    Uint8 *keystate = SDL_GetKeyboardState(NULL);
105#else
106    Uint8 *keystate = SDL_GetKeyState(NULL);
107#endif
108    return keystate[button];
109#else
110    return 0;
111#endif
112}
113
114void Input::TrackMouse(WorldEntity *e)
115{
116    if (data->nentities >= InputData::MAX_ENTITIES)
117        return;
118    data->entities[data->nentities] = e;
119    data->nentities++;
120}
121
122void Input::UntrackMouse(WorldEntity *e)
123{
124    for (int n = 0; n < data->nentities; n++)
125    {
126        if (data->entities[n] != e)
127            continue;
128
129        data->entities[n] = data->entities[data->nentities - 1];
130        data->nentities--;
131        n--;
132    }
133}
134
135void Input::SetMousePos(ivec2 coord)
136{
137    data->mouse = coord;
138
139    WorldEntity *top = NULL;
140
141    for (int n = 0; n < data->nentities; n++)
142    {
143        if (coord.x < data->entities[n]->m_bbox[0].x
144             || coord.x >= data->entities[n]->m_bbox[1].x
145             || coord.y < data->entities[n]->m_bbox[0].y
146             || coord.y >= data->entities[n]->m_bbox[1].y)
147            continue;
148
149        if (!top || top->m_bbox[1].z < data->entities[n]->m_bbox[1].z)
150            top = data->entities[n];
151    }
152
153    for (int n = 0; n < data->nentities; n++)
154    {
155        if (data->entities[n] == top)
156        {
157            data->entities[n]->m_mousepos = coord - (ivec2)top->m_bbox[0].xy;
158            if (top != data->lastfocus)
159                data->entities[n]->m_pressed = data->buttons;
160            else
161                data->entities[n]->m_clicked = ivec3(0);
162        }
163        else
164        {
165            data->entities[n]->m_mousepos = ivec2(-1);
166            /* FIXME */
167            data->entities[n]->m_released = ivec3(0);
168            data->entities[n]->m_pressed = ivec3(0);
169            data->entities[n]->m_clicked = ivec3(0);
170        }
171    }
172
173    data->lastfocus = top;
174}
175
176void Input::SetMouseButton(int index)
177{
178    data->buttons[index] = 1;
179
180    if (data->lastfocus)
181    {
182        if (!data->lastfocus->m_pressed[index])
183            data->lastfocus->m_clicked[index] = 1;
184        data->lastfocus->m_pressed[index] = 1;
185        data->lastfocus->m_released[index] = 0;
186    }
187}
188
189void Input::UnsetMouseButton(int index)
190{
191    data->buttons[index] = 0;
192
193    if (data->lastfocus)
194    {
195        if (data->lastfocus->m_pressed[index])
196            data->lastfocus->m_released[index] = 1;
197        data->lastfocus->m_pressed[index] = 0;
198        data->lastfocus->m_clicked[index] = 0;
199    }
200}
201
202Stick *Input::CreateStick()
203{
204    Stick *stick = new Stick();
205    Ticker::Ref(stick);
206    data->m_sticks.Push(stick);
207    return stick;
208}
209
210void Input::DestroyStick(Stick *stick)
211{
212    for (int i = 0; i < data->m_sticks.Count(); i++)
213        if (data->m_sticks[i] == stick)
214            data->m_sticks.Remove(i);
215    Ticker::Unref(stick);
216}
217
218Stick *Input::TrackStick()
219{
220    /* FIXME: add the possibility to choose amongst sticks */
221    if (!data->m_sticks.Count())
222        return NULL;
223    Ticker::Ref(data->m_sticks[0]);
224    return data->m_sticks[0];
225}
226
227void Input::UntrackStick(Stick *stick)
228{
229    Ticker::Unref(stick);
230}
231
232} /* namespace lol */
233
Note: See TracBrowser for help on using the repository browser.