GameKit  0.0.1a
C++ gamedev tools
KeyboardHandler.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: KeyboardHandler.cpp
5  *
6  * Description:
7  *
8  * Created: 15/02/2019 15:59:56
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
15 #include "gk/core/Debug.hpp"
16 #include "gk/core/IntTypes.hpp"
17 #include "gk/core/XMLFile.hpp"
18 
19 namespace gk {
20 
21 void KeyboardHandler::loadKeysFromFile(const std::string &filename) {
22  XMLFile doc(filename);
23 
24  tinyxml2::XMLElement *keys = doc.FirstChildElement("keys").ToElement();
25  if (keys) {
26  GameKey key = 0;
27  tinyxml2::XMLElement *keyElement = keys->FirstChildElement("key");
28  while (keyElement) {
29  m_keys[key] = SDL_GetScancodeFromName(keyElement->Attribute("key"));
30 
31  if(m_keys[key] == SDL_SCANCODE_UNKNOWN) {
32  DEBUG("Key '", keyElement->Attribute("key"), "' not recognized");
33  }
34 
36 
37  ++key;
38  keyElement = keyElement->NextSiblingElement("key");
39  }
40  }
41 }
42 
44  const u8 *keyboardState = SDL_GetKeyboardState(nullptr);
45  SDL_Scancode keyScancode = m_keys[key];
46 
47  m_keysPressed[key] = keyboardState[keyScancode];
48 
49  return keyboardState[keyScancode];
50 }
51 
52 } // namespace gk
53 
std::map< GameKey, SDL_Scancode > m_keys
tinyxml2::XMLHandle FirstChildElement(const char *element)
Definition: XMLFile.hpp:30
#define DEBUG(args...)
Definition: Debug.hpp:31
unsigned char u8
Definition: IntTypes.hpp:21
std::map< GameKey, bool > m_keysPressed
Keys state.
void loadKeysFromFile(const std::string &filename)
u32 GameKey
Alias for game key type.
void addKey(GameKey key)
Add a new key to this input handler.
bool isKeyPressed(GameKey key)
Check if a key is pressed.