RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
inputmanager.cpp
Go to the documentation of this file.
1 //--------------------------------------------------------------- @License begins
2 // RioEngine: The late night Coke -without whores- debugging sessions
3 // 2012-2015 Leopoldo Lomas Flores. Torreon, Coahuila. MEXICO
4 // leopoldolomas [at] gmail
5 // www.rioengine.com
6 // www.leopoldolomas.info
7 // "You have a problem, you face it like a man."
8 //
9 // This is free and unencumbered software released into the public domain.
10 //
11 // Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
12 // software, either in source code form or as a compiled binary, for any purpose,
13 // commercial or non-commercial, and by any means.
14 //
15 // In jurisdictions that recognize copyright laws, the author or authors of this
16 // software dedicate any and all copyright interest in the software to the public
17 // domain. We make this dedication for the benefit of the public at large and to
18 // the detriment of our heirs and successors. We intend this dedication to be
19 // an overt act of relinquishment in perpetuity of all present and future
20 // rights to this software under copyright law.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS
24 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
26 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //--------------------------------------------------------------- @License ends
29 
30 #include <climits>
31 #include <iostream>
32 #include <bitset>
33 #include "misc/mathhelper.hpp"
34 #include "cengine/cobject.h"
36 #include "inputmanager/joystick.h"
37 
38 //-----------------------------------------------------------------------------
39 
40 namespace USERINPUT {
41 
43  resetStates();
44 
45  m_leftJoystick = new Joystick();
46  m_rightJoystick = new Joystick();
47  m_mousePosition = new MousePosition();
48 
49  m_joystickEnabled = false;
50 }
51 
52 //-----------------------------------------------------------------------------
53 
55  resetStates();
56  updateJoysticksState();
57  updateKeyboardState();
58  updateMouseState();
59 }
60 
61 //-----------------------------------------------------------------------------
62 
64  return getAnyTrueValInArray(m_keysDown, k_KEYS_LENGTH);
65 }
66 
67 //-----------------------------------------------------------------------------
68 
70  return getAnyTrueValInArray(m_keysUp, k_KEYS_LENGTH);
71 }
72 
73 //-----------------------------------------------------------------------------
74 
75 bool InputManager::getKeyDown(int key_code) {
76  return m_keysDown[key_code];
77 }
78 
79 //-----------------------------------------------------------------------------
80 
81 bool InputManager::getKeyUp(int key_code) {
82  return m_keysUp[key_code];
83 }
84 
85 //-----------------------------------------------------------------------------
86 
87 void InputManager::setWheelVelocity(float wheel_velocity) {
88  m_mouseWheelVelocity = wheel_velocity;
89 }
90 
91 //-----------------------------------------------------------------------------
92 
94  return m_mouseWheelVelocity;
95 }
96 
97 //-----------------------------------------------------------------------------
98 
99 void InputManager::setMousePosition(const MousePosition& mouse_position) {
100  m_mousePosition->x = mouse_position.x;
101  m_mousePosition->y = mouse_position.y;
102 }
103 
104 //-----------------------------------------------------------------------------
105 
107  return m_mousePosition;
108 }
109 
110 //-----------------------------------------------------------------------------
111 
113  return getAnyTrueValInArray(m_mouseButtonsDown, k_MBUTTONS_LENGTH);
114 }
115 
116 //-----------------------------------------------------------------------------
117 
119  return getAnyTrueValInArray(m_mouseButtonsUp, k_MBUTTONS_LENGTH);
120 }
121 
122 //-----------------------------------------------------------------------------
123 
125  return m_mouseButtonsDown[button_number];
126 }
127 
128 //-----------------------------------------------------------------------------
129 
131  return m_mouseButtonsUp[button_number];
132 }
133 
134 //-----------------------------------------------------------------------------
135 
136 void InputManager::clearBoolArray(bool bool_array[], int array_length) {
137  for (int i = 0; i < array_length; i++) {
138  bool_array[i] = false;
139  }
140 }
141 
142 //-----------------------------------------------------------------------------
143 
144 void InputManager::resetStates() {
145  for(int i = 0; i < k_KEYS_LENGTH; i++) {
146  m_previousKeysDown[i] = m_keysDown[i];
147  }
148  clearBoolArray(m_keysDown, k_KEYS_LENGTH);
149  clearBoolArray(m_keysUp, k_KEYS_LENGTH);
150  clearBoolArray(m_mouseButtonsDown, k_MBUTTONS_LENGTH);
151  clearBoolArray(m_mouseButtonsUp, k_MBUTTONS_LENGTH);
152  clearBoolArray(m_joystickButtonsDown, k_JOYBUTTONS_LENGTH);
153 
154  setWheelVelocity(0.0f);
155 }
156 
157 //-----------------------------------------------------------------------------
158 
159 void InputManager::updateJoysticksState() {
160  if (!m_joystickEnabled) {
161  return;
162  }
163 
164  JOYINFOEX joyinfo;
165 
166  ZeroMemory(&joyinfo, sizeof(joyinfo));
167 
168  joyinfo.dwSize = sizeof(joyinfo);
169  joyinfo.dwFlags = JOY_RETURNALL|JOY_RETURNPOVCTS;
170  joyGetPosEx(0, &joyinfo); // retrieve joystick state
171 
172  // retrieve the joysticks' positions
173  m_leftJoystick->setX(rawPositionToVelocity((unsigned short)joyinfo.dwXpos));
174  m_leftJoystick->setY(rawPositionToVelocity((unsigned short)joyinfo.dwYpos));
175 
176  m_rightJoystick->setX(rawPositionToVelocity((unsigned short)joyinfo.dwUpos));
177  m_rightJoystick->setY(rawPositionToVelocity((unsigned short)joyinfo.dwRpos));
178 
179  if (joyinfo.dwButtonNumber > 0) { // at least one button was pressed?
180  setJoystickPressedButtonsCount(joyinfo.dwButtonNumber); // query the pressed buttons count
181 
182  std::bitset<32> ba((int)joyinfo.dwButtons); // convert the integer var to a bit array
183  std::string bit_array_as_string = ba.to_string(); // store the bit array as string
184 
185  unsigned short current_button = 0;
186 
187  for (unsigned int i = bit_array_as_string.length() - 1; i > bit_array_as_string.length() - k_JOYBUTTONS_LENGTH; i--) { // iterate the array backwards
188  if (bit_array_as_string[i] == '1') { // if the position contains '1' then we found a pressed button at index 'i'
189  m_joystickButtonsDown[current_button] = true;
190  }
191  current_button++;
192  }
193  }
194 }
195 
196 //-----------------------------------------------------------------------------
197 
198 void InputManager::updateKeyboardState() {
199  bool m_previousKeyState;
200  for (unsigned int i = 0; i < k_KEYS_LENGTH; i++) {
201  m_previousKeyState = m_previousKeysDown[i];
202  m_keysDown[i] = (GetAsyncKeyState(i) & 0x8000) != 0;
203 
204  if(m_previousKeyState == true && m_previousKeyState != m_keysDown[i]) {
205  m_keysUp[i] = true;
206  }
207  }
208  clearBoolArray(m_previousKeysDown, k_KEYS_LENGTH);
209 }
210 
211 //-----------------------------------------------------------------------------
212 
213 void InputManager::updateMouseState() {
214  POINT point;
215  RECT rect;
216 
217  if (GetCursorPos(&point)) {
218  if(GetWindowRect(GetActiveWindow(), &rect)) {
219  if(PtInRect(&rect, point)) {
220  m_mousePosition->x = point.x;
221  m_mousePosition->y = point.y;
222  }
223  }
224  }
225 
226  m_mouseButtonsDown[LeftButton] = GetAsyncKeyState(VK_LBUTTON) < 0;
227  m_mouseButtonsDown[MiddleButton] = GetAsyncKeyState(VK_MBUTTON) < 0;
228  m_mouseButtonsDown[RightButton] = GetAsyncKeyState(VK_RBUTTON) < 0;
229 }
230 
231 //-----------------------------------------------------------------------------
232 
233 bool InputManager::getAnyTrueValInArray(bool bool_array[], int array_length) {
234  for (int i = 0; i < array_length; i++) {
235  if (bool_array[i]) {
236  return true;
237  }
238  }
239  return false;
240 }
241 
242 //-----------------------------------------------------------------------------
243 
245  if (joySetCapture(handler, JOYSTICKID1, NULL, TRUE)) {
246  /*MessageBeep(MB_ICONEXCLAMATION);
247  MessageBox(handler, L"Joystick not found", NULL,
248  MB_OK | MB_ICONEXCLAMATION); */
249  } else {
250  m_joystickEnabled = true;
251  }
252 }
253 
254 //-----------------------------------------------------------------------------
255 
257  return getAnyTrueValInArray(m_joystickButtonsDown, k_JOYBUTTONS_LENGTH);
258 }
259 
260 //-----------------------------------------------------------------------------
261 
262 bool InputManager::getJoystickButtonDown(int button_number) {
263  return m_joystickButtonsDown[button_number - 1];
264 }
265 
266 //-----------------------------------------------------------------------------
267 
269  // TODO
270  return false;
271 }
272 
273 //-----------------------------------------------------------------------------
274 
276  return m_joystickPressedButtonsCount;
277 }
278 
279 //-----------------------------------------------------------------------------
280 
281 void InputManager::setJoystickPressedButtonsCount(int buttons_count) {
282  m_joystickPressedButtonsCount = buttons_count;
283 }
284 
285 //-----------------------------------------------------------------------------
286 
287 float InputManager::rawPositionToVelocity(unsigned short raw_value) {
288  unsigned short ushort_half_value = (unsigned short)MathHelper::roundCeil(USHRT_MAX / 2.0f, 0);
289 
290  float velocity = fabs((float)(ushort_half_value - raw_value));
291  velocity /= ushort_half_value; // sets velocity between 0 and 1
292 
293  if (raw_value < ushort_half_value) {
294  velocity *= -1; // then velocity must be negative
295  }
296 
297  return velocity;
298 }
299 
300 //-----------------------------------------------------------------------------
301 
303  return m_leftJoystick;
304 }
305 
306 //-----------------------------------------------------------------------------
307 
309  return m_rightJoystick;
310 }
311 
312 //-----------------------------------------------------------------------------
313 
315  SAFE_RELEASE(m_mousePosition);
316  SAFE_RELEASE(m_leftJoystick);
317  SAFE_RELEASE(m_rightJoystick);
318 }
319 
320 } // namespace USERINPUT
bool getJoystickButtonUp(int button_number)
float rawPositionToVelocity(unsigned short raw_value)
void initializeJoystick(HWND handler)
Joystick * getLeftJoystick()
bool getKeyUp(int key_code)
const MousePosition * getMousePosition()
void setMousePosition(const MousePosition &position)
static float roundCeil(float value, int decimal_places)
Definition: mathhelper.hpp:49
const int getJoystickButtonsPressedCount()
void setY(float y)
Definition: joystick.cpp:68
bool getKeyDown(int key_code)
#define SAFE_RELEASE(x)
Definition: cobject.h:36
bool getMouseButtonDown(MouseButtons button_number)
Joystick * getRightJoystick()
bool getJoystickButtonDown(int button_number)
const float getWheelVelocity()
void setWheelVelocity(float wheel_velocity)
void setX(float x)
Definition: joystick.cpp:53
bool getMouseButtonUp(MouseButtons button_number)