-
Notifications
You must be signed in to change notification settings - Fork 1
/
Input.cpp
130 lines (107 loc) · 4.03 KB
/
Input.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// ========================================================================= //
// Fighting game framework (2D) with online multiplayer.
// Copyright(C) 2014 Jordan Sparks <[email protected]>
//
// This program is free software; you can redistribute it and / or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or(at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// ========================================================================= //
// File: Input.cpp
// Author: Jordan Sparks <[email protected]>
// ================================================ //
// Implements Input class.
// ================================================ //
#include "Input.hpp"
#include "Config.hpp"
// ================================================ //
Input::Input(const std::string& bmap) :
m_pad(nullptr),
m_padDeadzone(1000),
m_movementMode(Input::MovementMode::JOYSTICK)
{
// Set all button states to false
std::fill_n(m_buttons, static_cast<int>(Input::NUM_BUTTONS), false);
std::fill_n(m_reactivated, static_cast<int>(Input::NUM_BUTTONS), true);
std::fill_n(m_keyboardMap, static_cast<int>(Input::NUM_BUTTONS), 0);
std::fill_n(m_gamepadMap, static_cast<int>(Input::NUM_BUTTONS), 0);
// Load button map
this->loadButtonMap(bmap);
}
// ================================================ //
Input::~Input(void)
{
}
// ================================================ //
void Input::loadButtonMap(const std::string& file)
{
Log::getSingletonPtr()->logMessage("Loading button map from \"" + file + "\"");
Config c(file);
if (c.isLoaded()){
// Load keyboard keys.
m_keyboardMap[BUTTON_UP] = c.parseIntValue("keyboard", "up");
m_keyboardMap[BUTTON_DOWN] = c.parseIntValue("keyboard", "down");
m_keyboardMap[BUTTON_LEFT] = c.parseIntValue("keyboard", "left");
m_keyboardMap[BUTTON_RIGHT] = c.parseIntValue("keyboard", "right");
m_keyboardMap[BUTTON_LP] = c.parseIntValue("keyboard", "LP");
// Load gamepad buttons.
m_gamepadMap[BUTTON_UP] = c.parseIntValue("gamepad", "up");
m_gamepadMap[BUTTON_DOWN] = c.parseIntValue("gamepad", "down");
m_gamepadMap[BUTTON_LEFT] = c.parseIntValue("gamepad", "left");
m_gamepadMap[BUTTON_RIGHT] = c.parseIntValue("gamepad", "right");
m_gamepadMap[BUTTON_START] = c.parseIntValue("gamepad", "start");
m_gamepadMap[BUTTON_SELECT] = c.parseIntValue("gamepad", "select");
m_gamepadMap[BUTTON_BACK] = c.parseIntValue("gamepad", "back");
m_padDeadzone = c.parseIntValue("gamepad", "deadzone");
Log::getSingletonPtr()->logMessage("Button map loaded!");
}
else{
Log::getSingletonPtr()->logMessage("ERROR: Failed to open button map file!");
}
}
// ================================================ //
void Input::resetAllButtons(void)
{
std::fill_n(m_buttons, static_cast<int>(Input::NUM_BUTTONS), false);
}
// ================================================ //
const int Input::SDLButtonToMappedButton(const int button, const bool gamepad)
{
// Test each mapped button to see if it matches the button parameter.
if (gamepad){
for (int i = 0; i < NUM_BUTTONS; ++i){
if (button == m_gamepadMap[i]){
return i;
}
}
}
else{
for (int i = 0; i < NUM_BUTTONS; ++i){
if (button == m_keyboardMap[i]){
return i;
}
}
}
// No mapped button found.
return -1;
}
// ================================================ //
const int Input::getPadID(void) const
{
// Get the instance ID of the controller using the underlying joystick.
SDL_Joystick* joystick = nullptr;
joystick = SDL_GameControllerGetJoystick(m_pad);
if (joystick){
return SDL_JoystickInstanceID(joystick);
}
return -1;
}
// ================================================ //