wip: more thread safety
[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
617dcc71
PG
21
22/// ***** Private Variables *****
23
f7b3b2eb
PG
24enum State
25{
26 isR,
27 wasP,
28 isP,
29 wasR
30};
31
32static const int keySize = 323;
33static State keyState[keySize];
34
617dcc71
PG
35/// ***** Initializers/Cleaners *****
36
68b2316d 37void input::init()
f7b3b2eb
PG
38{
39 for(int i=0; i< keySize; i++)
40 keyState[i] = isR;
41}
68b2316d 42void input::clean()
617dcc71
PG
43{
44
45}
46
47/// ***** Public Methods *****
f7b3b2eb 48
68b2316d 49void input::update()
f7b3b2eb
PG
50{
51 SDL_Event event;
52
53 for(int i=0; i< keySize; i++)
54 {
55 if(keyState[i] == wasR)
56 keyState[i] = isR;
57 else if(keyState[i] == wasP)
58 keyState[i] = isP;
59 }
60
61 while(SDL_PollEvent(&event))
62 {
63 switch(event.type)
64 {
65 case SDL_KEYUP:
66 keyState[event.key.keysym.sym] = wasR;
67 break;
68 case SDL_KEYDOWN:
69 keyState[event.key.keysym.sym] = wasP;
70 break;
71 }
72 }
73}
74
a823a800
PG
75Vector2 input::mousePosition()
76{
10baa49b 77 int x,y;
a823a800
PG
78 SDL_GetMouseState(&x, &y);
79
80 return Vector2(x,y);
81}
82
10baa49b
PG
83bool input::mouseLeft()
84{
85 Uint8 state = SDL_GetMouseState(NULL, NULL);
86
87 return state & SDL_BUTTON(1);
88}
89bool input::mouseRight()
90{
91 Uint8 state = SDL_GetMouseState(NULL, NULL);
92
93 return state & SDL_BUTTON(3);
94}
95
d6ce2baf 96bool input::isPressed(SDLKey key)
f7b3b2eb
PG
97{
98 return keyState[key] == isP || keyState[key] == wasP;
99}
d6ce2baf 100bool input::isReleased(SDLKey key)
f7b3b2eb
PG
101{
102 return keyState[key] == isR || keyState[key] == wasR;
103}
104
d6ce2baf 105bool input::wasPressed(SDLKey key)
f7b3b2eb
PG
106{
107 return keyState[key] == wasP;
108}
d6ce2baf 109bool input::wasReleased(SDLKey key)
f7b3b2eb
PG
110{
111 return keyState[key] == wasR;
112}