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 PG |
19 | |
20 | #include <pg/debug.h> | |
f7b3b2eb | 21 | |
4a0863f3 PG |
22 | #include <SDL/SDL.h> |
23 | ||
3d1f0813 | 24 | #include "config/keys.h" |
e616366d | 25 | |
617dcc71 PG |
26 | |
27 | /// ***** Private Variables ***** | |
28 | ||
f7b3b2eb PG |
29 | enum State |
30 | { | |
31 | isR, | |
32 | wasP, | |
33 | isP, | |
34 | wasR | |
35 | }; | |
36 | ||
37 | static const int keySize = 323; | |
38 | static State keyState[keySize]; | |
39 | ||
617dcc71 PG |
40 | /// ***** Initializers/Cleaners ***** |
41 | ||
68b2316d | 42 | void input::init() |
f7b3b2eb PG |
43 | { |
44 | for(int i=0; i< keySize; i++) | |
45 | keyState[i] = isR; | |
46 | } | |
68b2316d | 47 | void input::clean() |
617dcc71 PG |
48 | { |
49 | ||
50 | } | |
51 | ||
52 | /// ***** Public Methods ***** | |
f7b3b2eb | 53 | |
4a0863f3 PG |
54 | const Uint32 eventMask = -1; // ALL events |
55 | ||
68b2316d | 56 | void input::update() |
f7b3b2eb PG |
57 | { |
58 | SDL_Event event; | |
59 | ||
60 | for(int i=0; i< keySize; i++) | |
61 | { | |
62 | if(keyState[i] == wasR) | |
63 | keyState[i] = isR; | |
64 | else if(keyState[i] == wasP) | |
65 | keyState[i] = isP; | |
66 | } | |
67 | ||
4a0863f3 | 68 | while(0 < SDL_PeepEvents(&event, 1, SDL_GETEVENT, eventMask)) |
f7b3b2eb PG |
69 | { |
70 | switch(event.type) | |
71 | { | |
72 | case SDL_KEYUP: | |
73 | keyState[event.key.keysym.sym] = wasR; | |
74 | break; | |
75 | case SDL_KEYDOWN: | |
76 | keyState[event.key.keysym.sym] = wasP; | |
77 | break; | |
e616366d PG |
78 | case SDL_QUIT: |
79 | keyState[key::end] = wasR; | |
80 | break; | |
4a0863f3 PG |
81 | default: |
82 | break; | |
f7b3b2eb PG |
83 | } |
84 | } | |
85 | } | |
86 | ||
a823a800 PG |
87 | Vector2 input::mousePosition() |
88 | { | |
10baa49b | 89 | int x,y; |
a823a800 PG |
90 | SDL_GetMouseState(&x, &y); |
91 | ||
92 | return Vector2(x,y); | |
93 | } | |
94 | ||
10baa49b PG |
95 | bool input::mouseLeft() |
96 | { | |
97 | Uint8 state = SDL_GetMouseState(NULL, NULL); | |
98 | ||
99 | return state & SDL_BUTTON(1); | |
100 | } | |
101 | bool input::mouseRight() | |
102 | { | |
103 | Uint8 state = SDL_GetMouseState(NULL, NULL); | |
104 | ||
105 | return state & SDL_BUTTON(3); | |
106 | } | |
107 | ||
4a0863f3 | 108 | bool input::isPressed(const SDLKey& key) |
f7b3b2eb PG |
109 | { |
110 | return keyState[key] == isP || keyState[key] == wasP; | |
111 | } | |
4a0863f3 | 112 | bool input::isReleased(const SDLKey& key) |
f7b3b2eb PG |
113 | { |
114 | return keyState[key] == isR || keyState[key] == wasR; | |
115 | } | |
116 | ||
4a0863f3 | 117 | bool input::wasPressed(const SDLKey& key) |
f7b3b2eb PG |
118 | { |
119 | return keyState[key] == wasP; | |
120 | } | |
4a0863f3 | 121 | bool input::wasReleased(const SDLKey& key) |
f7b3b2eb PG |
122 | { |
123 | return keyState[key] == wasR; | |
124 | } |