libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
input.cpp
Go to the documentation of this file.
1/**
2 * @file input.cpp
3 * @brief Implementation of mx::Input game-controller helper.
4 */
5#include"input.hpp"
6
7namespace mx {
8 static const int keys[] = {
9 SDL_SCANCODE_A, SDL_SCANCODE_S, SDL_SCANCODE_Z, SDL_SCANCODE_X,
10 SDL_SCANCODE_Q, SDL_SCANCODE_W, SDL_SCANCODE_UP, SDL_SCANCODE_DOWN,
11 SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RETURN, SDL_SCANCODE_ESCAPE
12 };
13
14 static const SDL_GameControllerButton ctrl[] = {
15 SDL_CONTROLLER_BUTTON_A, SDL_CONTROLLER_BUTTON_B, SDL_CONTROLLER_BUTTON_X, SDL_CONTROLLER_BUTTON_Y,
16 SDL_CONTROLLER_BUTTON_LEFTSHOULDER, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
17 SDL_CONTROLLER_BUTTON_DPAD_UP, SDL_CONTROLLER_BUTTON_DPAD_DOWN,
18 SDL_CONTROLLER_BUTTON_DPAD_LEFT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT,
19 SDL_CONTROLLER_BUTTON_START, SDL_CONTROLLER_BUTTON_BACK
20 };
21
23 const Uint8 *keystate = SDL_GetKeyboardState(0);
24 int btn = static_cast<int>(b);
25 if(keystate[keys[btn]]) {
26 return true;
27 }
28 if(active() && Controller::getButton(ctrl[btn])) {
29 return true;
30 }
31 return false;
32 }
33}
bool getButton(SDL_GameControllerButton button)
Test whether a mapped button is currently pressed.
Definition joystick.cpp:153
bool active() const
Check whether the controller is currently usable.
Definition joystick.hpp:221
bool getButton(Input_Button b)
Query whether a logical gamepad button is currently pressed.
Definition input.cpp:22
High-level game controller input abstraction built on Controller.
Definition cfg.cpp:11
static const SDL_GameControllerButton ctrl[]
Definition input.cpp:14
static const int keys[]
Definition input.cpp:8
Input_Button
Logical button identifiers for a standard gamepad.
Definition input.hpp:19