2 * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
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.
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.
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/>.
18 #include "inputManager.h"
20 #include <bear/debug.h>
25 #include "config/keys.h"
28 /// ***** Private Variables *****
38 static const int keySize = 323;
39 static State keyState[keySize];
41 /// ***** Initializers/Cleaners *****
45 // default all keys to released
46 for(int i=0; i< keySize; i++)
56 /// ***** Public Methods *****
58 const Uint32 eventMask = -1; // ALL events
64 for(int i=0; i < keySize; i++)
70 case isR: newKeyState = isR; break;
71 case wasR: newKeyState = isR; break;
72 case isP: newKeyState = isP; break;
73 case wasP: newKeyState = isP; break;
74 default: DASSERT(false); return;
77 keyState[i] = newKeyState;
80 while(0 < SDL_PeepEvents(&event, 1, SDL_GETEVENT, eventMask))
85 keyState[event.key.keysym.sym] = wasR;
88 keyState[event.key.keysym.sym] = wasP;
91 // HACK: simulate an SDL_QUIT event as an ESC key press
92 keyState[key::end] = wasR;
95 break; // somthing I don't care about
100 Vector2 input::mousePosition()
103 SDL_GetMouseState(&x, &y);
108 bool input::mouseLeft()
110 Uint8 state = SDL_GetMouseState(NULL, NULL);
112 return state & SDL_BUTTON(1);
114 bool input::mouseRight()
116 Uint8 state = SDL_GetMouseState(NULL, NULL);
118 return state & SDL_BUTTON(3);
121 bool input::isPressed(const SDLKey& key)
123 return keyState[key] == isP || keyState[key] == wasP;
125 bool input::isReleased(const SDLKey& key)
127 return keyState[key] == isR || keyState[key] == wasR;
130 bool input::wasPressed(const SDLKey& key)
132 return keyState[key] == wasP;
134 bool input::wasReleased(const SDLKey& key)
136 return keyState[key] == wasR;