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