]> git.jsancho.org Git - lugaru.git/blob - Source/Input.cpp
Migrated to SDL2
[lugaru.git] / Source / Input.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010 - Côme <MCMic> BERNIGAUD
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
16 See the GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 */
22
23 /**> HEADER FILES <**/
24 #include "Input.h"
25
26 extern bool keyboardfrozen;
27
28 bool keyDown[SDL_NUM_SCANCODES + 6];
29 bool keyPressed[SDL_NUM_SCANCODES + 6];
30
31 void Input::Tick()
32 {
33     SDL_PumpEvents();
34     int numkeys;
35     const Uint8 *keyState = SDL_GetKeyboardState(&numkeys);
36     for (int i = 0; i < numkeys; i++) {
37         keyPressed[i] = !keyDown[i] && keyState[i];
38         keyDown[i] = keyState[i];
39     }
40     Uint8 mb = SDL_GetMouseState(NULL, NULL);
41     for (int i = 1; i < 6; i++) {
42         keyPressed[SDL_NUM_SCANCODES + i] = !keyDown[SDL_NUM_SCANCODES + i] && (mb & SDL_BUTTON(i));
43         keyDown[SDL_NUM_SCANCODES + i] = (mb & SDL_BUTTON(i));
44     }
45 }
46
47 bool Input::isKeyDown(int k)
48 {
49     if (keyboardfrozen || k >= SDL_NUM_SCANCODES + 6) // really useful? check that.
50         return false;
51     return keyDown[k];
52 }
53
54 bool Input::isKeyPressed(int k)
55 {
56     if (keyboardfrozen || k >= SDL_NUM_SCANCODES + 6)
57         return false;
58     return keyPressed[k];
59 }
60
61 const char* Input::keyToChar(unsigned short i)
62 {
63     if (i < SDL_NUM_SCANCODES)
64         return SDL_GetScancodeName(SDL_Scancode(i));
65     else if (i == MOUSEBUTTON1)
66         return "mouse1";
67     else if (i == MOUSEBUTTON2)
68         return "mouse2";
69     else if (i == MOUSEBUTTON3)
70         return "mouse3";
71     else
72         return "unknown";
73 }
74
75 unsigned short Input::CharToKey(const char* which)
76 {
77     for (unsigned short i = 0; i < SDL_NUM_SCANCODES; i++) {
78         if (!strcasecmp(which, SDL_GetScancodeName(SDL_Scancode(i))))
79             return i;
80     }
81     if (!strcasecmp(which, "mouse1")) {
82         return MOUSEBUTTON1;
83     }
84     if (!strcasecmp(which, "mouse2")) {
85         return MOUSEBUTTON2;
86     }
87     if (!strcasecmp(which, "mouse3")) {
88         return MOUSEBUTTON3;
89     }
90     return SDL_NUM_SCANCODES;
91 }
92
93 Boolean Input::MouseClicked()
94 {
95     return isKeyPressed(SDL_NUM_SCANCODES + SDL_BUTTON_LEFT);
96 }