source: trunk/src/input.cpp @ 1308

Last change on this file since 1308 was 1308, checked in by touky, 8 years ago

Added GetButtonState() in Input class.
Added Escape button in to quit Orbital.
Added Camera control with some little damping sweetness in Camera logic in main Tick.
First Commit \o/

  • Property svn:keywords set to Id
File size: 4.2 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
94//BH : Added this, is a v0.1 Alpha version.
95int Input::GetButtonState(int button)
96{
97#if defined USE_SDL
98#if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION >= 3
99    Uint8 *keystate = SDL_GetKeyboardState(NULL);
100#else
101    Uint8 *keystate = SDL_GetKeyState(NULL);
102#endif
103    return keystate[button];
104#else
105    return 0;
106#endif
107}
108
109void Input::TrackMouse(WorldEntity *e)
110{
111    if (data->nentities >= InputData::MAX_ENTITIES)
112        return;
113    data->entities[data->nentities] = e;
114    data->nentities++;
115}
116
117void Input::UntrackMouse(WorldEntity *e)
118{
119    for (int n = 0; n < data->nentities; n++)
120    {
121        if (data->entities[n] != e)
122            continue;
123
124        data->entities[n] = data->entities[data->nentities - 1];
125        data->nentities--;
126        n--;
127    }
128}
129
130void Input::SetMousePos(ivec2 coord)
131{
132    data->mouse = coord;
133
134    WorldEntity *top = NULL;
135
136    for (int n = 0; n < data->nentities; n++)
137    {
138        if (coord.x < data->entities[n]->m_bbox[0].x
139             || coord.x >= data->entities[n]->m_bbox[1].x
140             || coord.y < data->entities[n]->m_bbox[0].y
141             || coord.y >= data->entities[n]->m_bbox[1].y)
142            continue;
143
144        if (!top || top->m_bbox[1].z < data->entities[n]->m_bbox[1].z)
145            top = data->entities[n];
146    }
147
148    for (int n = 0; n < data->nentities; n++)
149    {
150        if (data->entities[n] == top)
151        {
152            data->entities[n]->m_mousepos = coord - (ivec2)top->m_bbox[0].xy;
153            if (top != data->lastfocus)
154                data->entities[n]->m_pressed = data->buttons;
155            else
156                data->entities[n]->m_clicked = ivec3(0);
157        }
158        else
159        {
160            data->entities[n]->m_mousepos = ivec2(-1);
161            /* FIXME */
162            data->entities[n]->m_released = ivec3(0);
163            data->entities[n]->m_pressed = ivec3(0);
164            data->entities[n]->m_clicked = ivec3(0);
165        }
166    }
167
168    data->lastfocus = top;
169}
170
171void Input::SetMouseButton(int index)
172{
173    data->buttons[index] = 1;
174
175    if (data->lastfocus)
176    {
177        if (!data->lastfocus->m_pressed[index])
178            data->lastfocus->m_clicked[index] = 1;
179        data->lastfocus->m_pressed[index] = 1;
180        data->lastfocus->m_released[index] = 0;
181    }
182}
183
184void Input::UnsetMouseButton(int index)
185{
186    data->buttons[index] = 0;
187
188    if (data->lastfocus)
189    {
190        if (data->lastfocus->m_pressed[index])
191            data->lastfocus->m_released[index] = 1;
192        data->lastfocus->m_pressed[index] = 0;
193        data->lastfocus->m_clicked[index] = 0;
194    }
195}
196
197} /* namespace lol */
198
Note: See TracBrowser for help on using the repository browser.