Commit | Line | Data |
---|---|---|
e68f847b PG |
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 | ||
f7b3b2eb | 18 | #include "inputManager.h" |
f32a9b7c | 19 | |
5e0713e5 PG |
20 | #include <bear/debug.h> |
21 | using namespace bear; | |
f7b3b2eb | 22 | |
4a0863f3 PG |
23 | #include <SDL/SDL.h> |
24 | ||
3d1f0813 | 25 | #include "config/keys.h" |
e616366d | 26 | |
617dcc71 PG |
27 | |
28 | /// ***** Private Variables ***** | |
29 | ||
f7b3b2eb PG |
30 | enum State |
31 | { | |
32 | isR, | |
33 | wasP, | |
34 | isP, | |
35 | wasR | |
36 | }; | |
37 | ||
38 | static const int keySize = 323; | |
39 | static State keyState[keySize]; | |
40 | ||
617dcc71 PG |
41 | /// ***** Initializers/Cleaners ***** |
42 | ||
68b2316d | 43 | void input::init() |
f7b3b2eb | 44 | { |
3ae4b047 | 45 | // default all keys to released |
f7b3b2eb | 46 | for(int i=0; i< keySize; i++) |
3ae4b047 | 47 | { |
f7b3b2eb | 48 | keyState[i] = isR; |
3ae4b047 | 49 | } |
f7b3b2eb | 50 | } |
68b2316d | 51 | void input::clean() |
617dcc71 PG |
52 | { |
53 | ||
54 | } | |
55 | ||
56 | /// ***** Public Methods ***** | |
f7b3b2eb | 57 | |
4a0863f3 PG |
58 | const Uint32 eventMask = -1; // ALL events |
59 | ||
68b2316d | 60 | void input::update() |
f7b3b2eb PG |
61 | { |
62 | SDL_Event event; | |
63 | ||
3ae4b047 | 64 | for(int i=0; i < keySize; i++) |
f7b3b2eb | 65 | { |
3ae4b047 PG |
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; | |
f7b3b2eb PG |
78 | } |
79 | ||
4a0863f3 | 80 | while(0 < SDL_PeepEvents(&event, 1, SDL_GETEVENT, eventMask)) |
f7b3b2eb PG |
81 | { |
82 | switch(event.type) | |
83 | { | |
3ae4b047 PG |
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 | |
f7b3b2eb PG |
96 | } |
97 | } | |
98 | } | |
99 | ||
a823a800 PG |
100 | Vector2 input::mousePosition() |
101 | { | |
10baa49b | 102 | int x,y; |
a823a800 PG |
103 | SDL_GetMouseState(&x, &y); |
104 | ||
105 | return Vector2(x,y); | |
106 | } | |
107 | ||
10baa49b PG |
108 | bool input::mouseLeft() |
109 | { | |
110 | Uint8 state = SDL_GetMouseState(NULL, NULL); | |
111 | ||
112 | return state & SDL_BUTTON(1); | |
113 | } | |
114 | bool input::mouseRight() | |
115 | { | |
116 | Uint8 state = SDL_GetMouseState(NULL, NULL); | |
117 | ||
118 | return state & SDL_BUTTON(3); | |
119 | } | |
120 | ||
4a0863f3 | 121 | bool input::isPressed(const SDLKey& key) |
f7b3b2eb PG |
122 | { |
123 | return keyState[key] == isP || keyState[key] == wasP; | |
124 | } | |
4a0863f3 | 125 | bool input::isReleased(const SDLKey& key) |
f7b3b2eb PG |
126 | { |
127 | return keyState[key] == isR || keyState[key] == wasR; | |
128 | } | |
129 | ||
4a0863f3 | 130 | bool input::wasPressed(const SDLKey& key) |
f7b3b2eb PG |
131 | { |
132 | return keyState[key] == wasP; | |
133 | } | |
4a0863f3 | 134 | bool input::wasReleased(const SDLKey& key) |
f7b3b2eb PG |
135 | { |
136 | return keyState[key] == wasR; | |
137 | } |