source: trunk/src/input/input.cpp @ 1513

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

core: replace usage of sin() or std::sin() with lol::sin() where appropriate.

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