finished changes to deps directory building
[physics.git] / src / input / inputManager.cpp
... / ...
CommitLineData
1/*
2 * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "inputManager.h"
19
20#include <bear/debug.h>
21using namespace bear;
22
23#include <SDL/SDL.h>
24
25#include "config/keys.h"
26
27
28/// ***** Private Variables *****
29
30enum State
31{
32 isR,
33 wasP,
34 isP,
35 wasR
36};
37
38static const int keySize = 323;
39static State keyState[keySize];
40
41/// ***** Initializers/Cleaners *****
42
43void input::init()
44{
45 // default all keys to released
46 for(int i=0; i< keySize; i++)
47 {
48 keyState[i] = isR;
49 }
50}
51void input::clean()
52{
53
54}
55
56/// ***** Public Methods *****
57
58const Uint32 eventMask = -1; // ALL events
59
60void input::update()
61{
62 SDL_Event event;
63
64 for(int i=0; i < keySize; i++)
65 {
66 State newKeyState;
67
68 switch(keyState[i])
69 {
70 case isR: newKeyState = isR; break;
71 case wasR: newKeyState = isR; break;
72 case isP: newKeyState = isP; break;
73 case wasP: newKeyState = isP; break;
74 default: DASSERT(false); return;
75 }
76
77 keyState[i] = newKeyState;
78 }
79
80 while(0 < SDL_PeepEvents(&event, 1, SDL_GETEVENT, eventMask))
81 {
82 switch(event.type)
83 {
84 case SDL_KEYUP:
85 keyState[event.key.keysym.sym] = wasR;
86 break;
87 case SDL_KEYDOWN:
88 keyState[event.key.keysym.sym] = wasP;
89 break;
90 case SDL_QUIT:
91 // HACK: simulate an SDL_QUIT event as an ESC key press
92 keyState[key::end] = wasR;
93 break;
94 default:
95 break; // somthing I don't care about
96 }
97 }
98}
99
100Vector2 input::mousePosition()
101{
102 int x,y;
103 SDL_GetMouseState(&x, &y);
104
105 return Vector2(x,y);
106}
107
108bool input::mouseLeft()
109{
110 Uint8 state = SDL_GetMouseState(NULL, NULL);
111
112 return state & SDL_BUTTON(1);
113}
114bool input::mouseRight()
115{
116 Uint8 state = SDL_GetMouseState(NULL, NULL);
117
118 return state & SDL_BUTTON(3);
119}
120
121bool input::isPressed(const SDLKey& key)
122{
123 return keyState[key] == isP || keyState[key] == wasP;
124}
125bool input::isReleased(const SDLKey& key)
126{
127 return keyState[key] == isR || keyState[key] == wasR;
128}
129
130bool input::wasPressed(const SDLKey& key)
131{
132 return keyState[key] == wasP;
133}
134bool input::wasReleased(const SDLKey& key)
135{
136 return keyState[key] == wasR;
137}