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