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> | |
b85b89ba | 21 | using namespace pg; |
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 PG |
44 | { |
45 | for(int i=0; i< keySize; i++) | |
46 | keyState[i] = isR; | |
47 | } | |
68b2316d | 48 | void input::clean() |
617dcc71 PG |
49 | { |
50 | ||
51 | } | |
52 | ||
53 | /// ***** Public Methods ***** | |
f7b3b2eb | 54 | |
4a0863f3 PG |
55 | const Uint32 eventMask = -1; // ALL events |
56 | ||
68b2316d | 57 | void input::update() |
f7b3b2eb PG |
58 | { |
59 | SDL_Event event; | |
60 | ||
61 | for(int i=0; i< keySize; i++) | |
62 | { | |
63 | if(keyState[i] == wasR) | |
64 | keyState[i] = isR; | |
65 | else if(keyState[i] == wasP) | |
66 | keyState[i] = isP; | |
67 | } | |
68 | ||
4a0863f3 | 69 | while(0 < SDL_PeepEvents(&event, 1, SDL_GETEVENT, eventMask)) |
f7b3b2eb PG |
70 | { |
71 | switch(event.type) | |
72 | { | |
73 | case SDL_KEYUP: | |
74 | keyState[event.key.keysym.sym] = wasR; | |
75 | break; | |
76 | case SDL_KEYDOWN: | |
77 | keyState[event.key.keysym.sym] = wasP; | |
78 | break; | |
e616366d PG |
79 | case SDL_QUIT: |
80 | keyState[key::end] = wasR; | |
81 | break; | |
4a0863f3 PG |
82 | default: |
83 | break; | |
f7b3b2eb PG |
84 | } |
85 | } | |
86 | } | |
87 | ||
a823a800 PG |
88 | Vector2 input::mousePosition() |
89 | { | |
10baa49b | 90 | int x,y; |
a823a800 PG |
91 | SDL_GetMouseState(&x, &y); |
92 | ||
93 | return Vector2(x,y); | |
94 | } | |
95 | ||
10baa49b PG |
96 | bool input::mouseLeft() |
97 | { | |
98 | Uint8 state = SDL_GetMouseState(NULL, NULL); | |
99 | ||
100 | return state & SDL_BUTTON(1); | |
101 | } | |
102 | bool input::mouseRight() | |
103 | { | |
104 | Uint8 state = SDL_GetMouseState(NULL, NULL); | |
105 | ||
106 | return state & SDL_BUTTON(3); | |
107 | } | |
108 | ||
4a0863f3 | 109 | bool input::isPressed(const SDLKey& key) |
f7b3b2eb PG |
110 | { |
111 | return keyState[key] == isP || keyState[key] == wasP; | |
112 | } | |
4a0863f3 | 113 | bool input::isReleased(const SDLKey& key) |
f7b3b2eb PG |
114 | { |
115 | return keyState[key] == isR || keyState[key] == wasR; | |
116 | } | |
117 | ||
4a0863f3 | 118 | bool input::wasPressed(const SDLKey& key) |
f7b3b2eb PG |
119 | { |
120 | return keyState[key] == wasP; | |
121 | } | |
4a0863f3 | 122 | bool input::wasReleased(const SDLKey& key) |
f7b3b2eb PG |
123 | { |
124 | return keyState[key] == wasR; | |
125 | } |