gravity well now follows the mouse
[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{
77 int x;
78 int y;
79
80 SDL_GetMouseState(&x, &y);
81
82 return Vector2(x,y);
83}
84
68b2316d 85bool input::isPressed(Uint8 key)
f7b3b2eb
PG
86{
87 return keyState[key] == isP || keyState[key] == wasP;
88}
68b2316d 89bool input::isReleased(Uint8 key)
f7b3b2eb
PG
90{
91 return keyState[key] == isR || keyState[key] == wasR;
92}
93
68b2316d 94bool input::wasPressed(Uint8 key)
f7b3b2eb
PG
95{
96 return keyState[key] == wasP;
97}
68b2316d 98bool input::wasReleased(Uint8 key)
f7b3b2eb
PG
99{
100 return keyState[key] == wasR;
101}