| 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 | bool input::isPressed(Uint8 key) |
| 76 | { |
| 77 | return keyState[key] == isP || keyState[key] == wasP; |
| 78 | } |
| 79 | bool input::isReleased(Uint8 key) |
| 80 | { |
| 81 | return keyState[key] == isR || keyState[key] == wasR; |
| 82 | } |
| 83 | |
| 84 | bool input::wasPressed(Uint8 key) |
| 85 | { |
| 86 | return keyState[key] == wasP; |
| 87 | } |
| 88 | bool input::wasReleased(Uint8 key) |
| 89 | { |
| 90 | return keyState[key] == wasR; |
| 91 | } |