source: trunk/src/input.cpp @ 686

Last change on this file since 686 was 686, checked in by sam, 12 years ago

Put everything in the "lol" namespace. Better late than never.

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