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