-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.cpp
More file actions
175 lines (114 loc) · 3.15 KB
/
Input.cpp
File metadata and controls
175 lines (114 loc) · 3.15 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "stdafx.h"
#include "Input.h"
#include "Game.h"
#include "LogManager.h"
#pragma region glfw_callbacks
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
Event e((Event::Type)-1);
switch (action) {
case GLFW_RELEASE:
e.type = Event::KEY_UP;
break;
case GLFW_PRESS:
e.type = Event::KEY_DOWN;
break;
case GLFW_REPEAT:
return;
}
e.keyboard.charCode = key;
e.keyboard.keyCode = scancode;
Input::instance()->dispatchEvent(e);
}
#pragma endregion glfw_callbacks
Input Input::s_xInstance;
Input::Input() {
m_bJoyButtons = nullptr;
m_fJoyAxes = nullptr;
m_bJoyPresent = false;
}
Input::~Input() {
if (m_fJoyAxes != nullptr)
delete [] m_fJoyAxes;
if (m_bJoyButtons != nullptr)
delete [] m_bJoyButtons;
}
Input* Input::instance() {
return &s_xInstance;
}
void Input::init(GLFWwindow* window) {
glfwSetKeyCallback(window, keyCallback);
if (m_bJoyPresent = glfwJoystickPresent(0))
joyPluggedIn();
}
void Input::update() {
if (m_bJoyPresent && !glfwJoystickPresent(0)) {
joyPluggedOut();
return;
} else if (!m_bJoyPresent && glfwJoystickPresent(0)) {
joyPluggedIn();
return;
} else if (!m_bJoyPresent) {
return;
}
const float* axes = glfwGetJoystickAxes(0, &m_iNumJoyAxes);
const unsigned char* buttons = glfwGetJoystickButtons(0, &m_iNumJoyButtons);
for (int i = 0; i < m_iNumJoyAxes; ++i) {
if (m_fJoyAxes[i] != axes[i]) {
Event e(Event::JOY_AXIS_CHANGE);
e.joypad.joypad = 0;
e.joypad.button = -1;
e.joypad.axis = i;
e.joypad.axisValue = axes[i];
dispatchEvent(e);
}
m_fJoyAxes[i] = axes[i];
}
for (int i = 0; i < m_iNumJoyButtons; ++i) {
if (m_bJoyButtons[i] != (bool)buttons[i]) {
Event e(buttons[i] ? Event::JOY_BUTTON_DOWN : Event::JOY_BUTTON_UP);
e.joypad.joypad = 0;
e.joypad.button = i;
e.joypad.axis = -1;
e.joypad.axisValue = 0.0f;
dispatchEvent(e);
}
m_bJoyButtons[i] = buttons[i];
}
}
bool Input::isJoyPluggedIn() {
return m_bJoyPresent;
}
void Input::joyPluggedIn() {
m_bJoyPresent = true;
const float* axes = glfwGetJoystickAxes(0, &m_iNumJoyAxes);
const unsigned char* buttons = glfwGetJoystickButtons(0, &m_iNumJoyButtons);
m_fJoyAxes = new float[m_iNumJoyAxes];
memcpy(m_fJoyAxes, axes, sizeof(float) * m_iNumJoyAxes);
m_bJoyButtons = new bool[m_iNumJoyButtons];
for (int i = 0; i < m_iNumJoyButtons; ++i) {
m_bJoyButtons[i] = buttons[i];
}
Event e(Event::JOY_PLUGGED_IN);
e.joypad.joypad = 0;
e.joypad.button = -1;
e.joypad.axis = -1;
e.joypad.axisValue = 0.0f;
dispatchEvent(e);
Log::Writeln(std::string("Joypad plugged in: ") + glfwGetJoystickName(0), Log::COLOR_LIGHT_AQUA);
std::cout << "Number of axes: " << m_iNumJoyAxes << std::endl;
std::cout << "Number of buttons: " << m_iNumJoyButtons << std::endl;
}
void Input::joyPluggedOut() {
m_bJoyPresent = false;
delete [] m_fJoyAxes;
m_fJoyAxes = nullptr;
delete [] m_bJoyButtons;
m_bJoyButtons = nullptr;
Event e(Event::JOY_PLUGGED_OUT);
e.joypad.joypad = 0;
e.joypad.button = -1;
e.joypad.axis = -1;
e.joypad.axisValue = 0.0f;
dispatchEvent(e);
Log::Writeln("Joypad disconnected", Log::COLOR_LIGHT_YELLOW);
}