moved event pumping onto the draw thread, should fix windows event problems
[physics.git] / src / input / inputManager.cpp
CommitLineData
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
PG
18#include "inputManager.h"
19#include "../debug.h"
20
4a0863f3
PG
21#include <SDL/SDL.h>
22
e616366d
PG
23#include "../config/keys.h"
24
617dcc71
PG
25
26/// ***** Private Variables *****
27
f7b3b2eb
PG
28enum State
29{
30 isR,
31 wasP,
32 isP,
33 wasR
34};
35
36static const int keySize = 323;
37static State keyState[keySize];
38
617dcc71
PG
39/// ***** Initializers/Cleaners *****
40
68b2316d 41void input::init()
f7b3b2eb
PG
42{
43 for(int i=0; i< keySize; i++)
44 keyState[i] = isR;
45}
68b2316d 46void input::clean()
617dcc71
PG
47{
48
49}
50
51/// ***** Public Methods *****
f7b3b2eb 52
4a0863f3
PG
53const Uint32 eventMask = -1; // ALL events
54
68b2316d 55void input::update()
f7b3b2eb
PG
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
4a0863f3 67 while(0 < SDL_PeepEvents(&event, 1, SDL_GETEVENT, eventMask))
f7b3b2eb
PG
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;
e616366d
PG
77 case SDL_QUIT:
78 keyState[key::end] = wasR;
79 break;
4a0863f3
PG
80 default:
81 break;
f7b3b2eb
PG
82 }
83 }
84}
85
a823a800
PG
86Vector2 input::mousePosition()
87{
10baa49b 88 int x,y;
a823a800
PG
89 SDL_GetMouseState(&x, &y);
90
91 return Vector2(x,y);
92}
93
10baa49b
PG
94bool input::mouseLeft()
95{
96 Uint8 state = SDL_GetMouseState(NULL, NULL);
97
98 return state & SDL_BUTTON(1);
99}
100bool input::mouseRight()
101{
102 Uint8 state = SDL_GetMouseState(NULL, NULL);
103
104 return state & SDL_BUTTON(3);
105}
106
4a0863f3 107bool input::isPressed(const SDLKey& key)
f7b3b2eb
PG
108{
109 return keyState[key] == isP || keyState[key] == wasP;
110}
4a0863f3 111bool input::isReleased(const SDLKey& key)
f7b3b2eb
PG
112{
113 return keyState[key] == isR || keyState[key] == wasR;
114}
115
4a0863f3 116bool input::wasPressed(const SDLKey& key)
f7b3b2eb
PG
117{
118 return keyState[key] == wasP;
119}
4a0863f3 120bool input::wasReleased(const SDLKey& key)
f7b3b2eb
PG
121{
122 return keyState[key] == wasR;
123}