created input, graphics and game namespaces
[physics.git] / src / input / inputManager.cpp
1 #include "inputManager.h"
2 #include "../debug.h"
3
4
5 /// ***** Private Variables *****
6
7 enum State
8 {
9     isR,
10     wasP,
11     isP,
12     wasR
13 };
14
15 static const int keySize = 323;
16 static State keyState[keySize];
17
18 /// ***** Initializers/Cleaners *****
19
20 void input::init()
21 {
22     for(int i=0; i< keySize; i++)
23         keyState[i] = isR;
24 }
25 void input::clean()
26 {
27
28 }
29
30 /// ***** Public Methods *****
31
32 void input::update()
33 {
34     SDL_Event event;
35
36     for(int i=0; i< keySize; i++)
37     {
38         if(keyState[i] == wasR)
39             keyState[i] = isR;
40         else if(keyState[i] == wasP)
41             keyState[i] = isP;
42     }
43
44     while(SDL_PollEvent(&event))
45     {
46         switch(event.type)
47         {
48         case SDL_KEYUP:
49             keyState[event.key.keysym.sym] = wasR;
50             break;
51         case SDL_KEYDOWN:
52             keyState[event.key.keysym.sym] = wasP;
53             break;
54         }
55     }
56 }
57
58 bool input::isPressed(Uint8 key)
59 {
60     return keyState[key] == isP || keyState[key] == wasP;
61 }
62 bool input::isReleased(Uint8 key)
63 {
64     return keyState[key] == isR || keyState[key] == wasR;
65 }
66
67 bool input::wasPressed(Uint8 key)
68 {
69     return keyState[key] == wasP;
70 }
71 bool input::wasReleased(Uint8 key)
72 {
73     return keyState[key] == wasR;
74 }