3ecb477866776500da716639413053c3b178a70e
[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
22 /// ***** Private Variables *****
23
24 enum State
25 {
26     isR,
27     wasP,
28     isP,
29     wasR
30 };
31
32 static const int keySize = 323;
33 static State keyState[keySize];
34
35 /// ***** Initializers/Cleaners *****
36
37 void input::init()
38 {
39     for(int i=0; i< keySize; i++)
40         keyState[i] = isR;
41 }
42 void input::clean()
43 {
44
45 }
46
47 /// ***** Public Methods *****
48
49 void input::update()
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
75 Vector2 input::mousePosition()
76 {
77     int x,y;
78     SDL_GetMouseState(&x, &y);
79
80     return Vector2(x,y);
81 }
82
83 bool input::mouseLeft()
84 {
85     Uint8 state = SDL_GetMouseState(NULL, NULL);
86
87     return state & SDL_BUTTON(1);
88 }
89 bool input::mouseRight()
90 {
91     Uint8 state = SDL_GetMouseState(NULL, NULL);
92
93     return state & SDL_BUTTON(3);
94 }
95
96 bool input::isPressed(SDLKey key)
97 {
98     return keyState[key] == isP || keyState[key] == wasP;
99 }
100 bool input::isReleased(SDLKey key)
101 {
102     return keyState[key] == isR || keyState[key] == wasR;
103 }
104
105 bool input::wasPressed(SDLKey key)
106 {
107     return keyState[key] == wasP;
108 }
109 bool input::wasReleased(SDLKey key)
110 {
111     return keyState[key] == wasR;
112 }