source: trunk/src/input.cpp @ 1082

Last change on this file since 1082 was 1057, checked in by gary, 8 years ago

ps3: compilation fixes for the PS3.

  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 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#include <cmath>
17
18#if defined USE_SDL
19#   include <SDL.h>
20#endif
21
22#include "core.h"
23
24namespace lol
25{
26
27/*
28 * Input implementation class
29 */
30
31static class InputData
32{
33    friend class Input;
34
35public:
36    InputData()
37      : mouse(-1),
38        buttons(0),
39        nentities(0),
40        lastfocus(0)
41    { }
42
43private:
44    ivec2 mouse;
45    ivec3 buttons;
46
47    static int const MAX_ENTITIES = 100;
48    WorldEntity *entities[MAX_ENTITIES];
49    int nentities;
50    WorldEntity *lastfocus;
51}
52inputdata;
53
54static InputData * const data = &inputdata;
55
56/*
57 * Public Input class
58 */
59
60vec2 Input::GetAxis(int axis)
61{
62    vec2 ret;
63
64#if defined USE_SDL
65    /* Simulate a joystick using the keyboard. This SDL call is free. */
66#if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION >= 3
67    Uint8 *keystate = SDL_GetKeyboardState(NULL);
68#else
69    Uint8 *keystate = SDL_GetKeyState(NULL);
70#endif
71    int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]);
72    int up = (keystate[SDLK_w] | keystate[SDLK_z]) - keystate[SDLK_s] ;
73    ret.x += left;
74    ret.y += up;
75    if (left && up)
76        ret = ret * sqrtf(0.5f);
77#else
78    ret = vec2(0, 0);
79#endif
80
81    return ret;
82}
83
84ivec2 Input::GetMousePos()
85{
86    return data->mouse;
87}
88
89ivec3 Input::GetMouseButtons()
90{
91    return data->buttons;
92}
93
94void Input::TrackMouse(WorldEntity *e)
95{
96    if (data->nentities >= InputData::MAX_ENTITIES)
97        return;
98    data->entities[data->nentities] = e;
99    data->nentities++;
100}
101
102void Input::UntrackMouse(WorldEntity *e)
103{
104    for (int n = 0; n < data->nentities; n++)
105    {
106        if (data->entities[n] != e)
107            continue;
108
109        data->entities[n] = data->entities[data->nentities - 1];
110        data->nentities--;
111        n--;
112    }
113}
114
115void Input::SetMousePos(ivec2 coord)
116{
117    data->mouse = coord;
118
119    WorldEntity *top = NULL;
120
121    for (int n = 0; n < data->nentities; n++)
122    {
123        if (coord.x < data->entities[n]->bbox[0].x
124             || coord.x >= data->entities[n]->bbox[1].x
125             || coord.y < data->entities[n]->bbox[0].y
126             || coord.y >= data->entities[n]->bbox[1].y)
127            continue;
128
129        if (!top || top->bbox[1].z < data->entities[n]->bbox[1].z)
130            top = data->entities[n];
131    }
132
133    for (int n = 0; n < data->nentities; n++)
134    {
135        if (data->entities[n] == top)
136        {
137            data->entities[n]->mousepos = coord - top->bbox[0].xy();
138            if (top != data->lastfocus)
139                data->entities[n]->pressed = data->buttons;
140            else
141                data->entities[n]->clicked = ivec3(0);
142        }
143        else
144        {
145            data->entities[n]->mousepos = ivec2(-1);
146            /* FIXME */
147            data->entities[n]->released = ivec3(0);
148            data->entities[n]->pressed = ivec3(0);
149            data->entities[n]->clicked = ivec3(0);
150        }
151    }
152
153    data->lastfocus = top;
154}
155
156void Input::SetMouseButton(int index)
157{
158    data->buttons[index] = 1;
159
160    if (data->lastfocus)
161    {
162        if (!data->lastfocus->pressed[index])
163            data->lastfocus->clicked[index] = 1;
164        data->lastfocus->pressed[index] = 1;
165        data->lastfocus->released[index] = 0;
166    }
167}
168
169void Input::UnsetMouseButton(int index)
170{
171    data->buttons[index] = 0;
172
173    if (data->lastfocus)
174    {
175        if (data->lastfocus->pressed[index])
176            data->lastfocus->released[index] = 1;
177        data->lastfocus->pressed[index] = 0;
178        data->lastfocus->clicked[index] = 0;
179    }
180}
181
182} /* namespace lol */
183
Note: See TracBrowser for help on using the repository browser.