changed src so the libpg headers are now used
[physics.git] / src / input / inputManager.cpp
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 <pg/debug.h>
21
22 #include <SDL/SDL.h>
23
24 #include "config/keys.h"
25
26
27 /// ***** Private Variables *****
28
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
40 /// ***** Initializers/Cleaners *****
41
42 void input::init()
43 {
44     for(int i=0; i< keySize; i++)
45         keyState[i] = isR;
46 }
47 void input::clean()
48 {
49
50 }
51
52 /// ***** Public Methods *****
53
54 const Uint32 eventMask = -1; // ALL events
55
56 void input::update()
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
68     while(0 < SDL_PeepEvents(&event, 1, SDL_GETEVENT, eventMask))
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;
78         case SDL_QUIT:
79             keyState[key::end] = wasR;
80             break;
81         default:
82             break;
83         }
84     }
85 }
86
87 Vector2 input::mousePosition()
88 {
89     int x,y;
90     SDL_GetMouseState(&x, &y);
91
92     return Vector2(x,y);
93 }
94
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
108 bool input::isPressed(const SDLKey& key)
109 {
110     return keyState[key] == isP || keyState[key] == wasP;
111 }
112 bool input::isReleased(const SDLKey& key)
113 {
114     return keyState[key] == isR || keyState[key] == wasR;
115 }
116
117 bool input::wasPressed(const SDLKey& key)
118 {
119     return keyState[key] == wasP;
120 }
121 bool input::wasReleased(const SDLKey& key)
122 {
123     return keyState[key] == wasR;
124 }