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