InputSystem.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "InputSystem.h"
  9. #include <SDL/SDL.h>
  10. #include <cstring>
  11. bool KeyboardState::GetKeyValue(SDL_Scancode keyCode) const
  12. {
  13. return mCurrState[keyCode] == 1;
  14. }
  15. ButtonState KeyboardState::GetKeyState(SDL_Scancode keyCode) const
  16. {
  17. if (mPrevState[keyCode] == 0)
  18. {
  19. if (mCurrState[keyCode] == 0)
  20. {
  21. return ENone;
  22. }
  23. else
  24. {
  25. return EPressed;
  26. }
  27. }
  28. else // Prev state must be 1
  29. {
  30. if (mCurrState[keyCode] == 0)
  31. {
  32. return EReleased;
  33. }
  34. else
  35. {
  36. return EHeld;
  37. }
  38. }
  39. }
  40. bool MouseState::GetButtonValue(int button) const
  41. {
  42. return (SDL_BUTTON(button) & mCurrButtons);
  43. }
  44. ButtonState MouseState::GetButtonState(int button) const
  45. {
  46. int mask = SDL_BUTTON(button);
  47. if ((mask & mPrevButtons) == 0)
  48. {
  49. if ((mask & mCurrButtons) == 0)
  50. {
  51. return ENone;
  52. }
  53. else
  54. {
  55. return EPressed;
  56. }
  57. }
  58. else
  59. {
  60. if ((mask & mCurrButtons) == 0)
  61. {
  62. return EReleased;
  63. }
  64. else
  65. {
  66. return EHeld;
  67. }
  68. }
  69. }
  70. bool ControllerState::GetButtonValue(SDL_GameControllerButton button) const
  71. {
  72. return mCurrButtons[button] == 1;
  73. }
  74. ButtonState ControllerState::GetButtonState(SDL_GameControllerButton button) const
  75. {
  76. if (mPrevButtons[button] == 0)
  77. {
  78. if (mCurrButtons[button] == 0)
  79. {
  80. return ENone;
  81. }
  82. else
  83. {
  84. return EPressed;
  85. }
  86. }
  87. else // Prev state must be 1
  88. {
  89. if (mCurrButtons[button] == 0)
  90. {
  91. return EReleased;
  92. }
  93. else
  94. {
  95. return EHeld;
  96. }
  97. }
  98. }
  99. bool InputSystem::Initialize()
  100. {
  101. // Keyboard
  102. // Assign current state pointer
  103. mState.Keyboard.mCurrState = SDL_GetKeyboardState(NULL);
  104. // Clear previous state memory
  105. memset(mState.Keyboard.mPrevState, 0,
  106. SDL_NUM_SCANCODES);
  107. // Mouse (just set everything to 0)
  108. mState.Mouse.mCurrButtons = 0;
  109. mState.Mouse.mPrevButtons = 0;
  110. // Get the connected controller, if it exists
  111. mController = SDL_GameControllerOpen(0);
  112. // Initialize controller state
  113. mState.Controller.mIsConnected = (mController != nullptr);
  114. memset(mState.Controller.mCurrButtons, 0,
  115. SDL_CONTROLLER_BUTTON_MAX);
  116. memset(mState.Controller.mPrevButtons, 0,
  117. SDL_CONTROLLER_BUTTON_MAX);
  118. return true;
  119. }
  120. void InputSystem::Shutdown()
  121. {
  122. }
  123. void InputSystem::PrepareForUpdate()
  124. {
  125. // Copy current state to previous
  126. // Keyboard
  127. memcpy(mState.Keyboard.mPrevState,
  128. mState.Keyboard.mCurrState,
  129. SDL_NUM_SCANCODES);
  130. // Mouse
  131. mState.Mouse.mPrevButtons = mState.Mouse.mCurrButtons;
  132. mState.Mouse.mIsRelative = false;
  133. mState.Mouse.mScrollWheel = Vector2::Zero;
  134. // Controller
  135. memcpy(mState.Controller.mPrevButtons,
  136. mState.Controller.mCurrButtons,
  137. SDL_CONTROLLER_BUTTON_MAX);
  138. }
  139. void InputSystem::Update()
  140. {
  141. // Mouse
  142. int x = 0, y = 0;
  143. if (mState.Mouse.mIsRelative)
  144. {
  145. mState.Mouse.mCurrButtons =
  146. SDL_GetRelativeMouseState(&x, &y);
  147. }
  148. else
  149. {
  150. mState.Mouse.mCurrButtons =
  151. SDL_GetMouseState(&x, &y);
  152. }
  153. mState.Mouse.mMousePos.x = static_cast<float>(x);
  154. mState.Mouse.mMousePos.y = static_cast<float>(y);
  155. // Controller
  156. // Buttons
  157. for (int i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++)
  158. {
  159. mState.Controller.mCurrButtons[i] =
  160. SDL_GameControllerGetButton(mController,
  161. SDL_GameControllerButton(i));
  162. }
  163. // Triggers
  164. mState.Controller.mLeftTrigger =
  165. Filter1D(SDL_GameControllerGetAxis(mController,
  166. SDL_CONTROLLER_AXIS_TRIGGERLEFT));
  167. mState.Controller.mRightTrigger =
  168. Filter1D(SDL_GameControllerGetAxis(mController,
  169. SDL_CONTROLLER_AXIS_TRIGGERRIGHT));
  170. // Sticks
  171. x = SDL_GameControllerGetAxis(mController,
  172. SDL_CONTROLLER_AXIS_LEFTX);
  173. y = -SDL_GameControllerGetAxis(mController,
  174. SDL_CONTROLLER_AXIS_LEFTY);
  175. mState.Controller.mLeftStick = Filter2D(x, y);
  176. x = SDL_GameControllerGetAxis(mController,
  177. SDL_CONTROLLER_AXIS_RIGHTX);
  178. y = -SDL_GameControllerGetAxis(mController,
  179. SDL_CONTROLLER_AXIS_RIGHTY);
  180. mState.Controller.mRightStick = Filter2D(x, y);
  181. }
  182. void InputSystem::ProcessEvent(SDL_Event& event)
  183. {
  184. switch (event.type)
  185. {
  186. case SDL_MOUSEWHEEL:
  187. mState.Mouse.mScrollWheel = Vector2(
  188. static_cast<float>(event.wheel.x),
  189. static_cast<float>(event.wheel.y));
  190. break;
  191. default:
  192. break;
  193. }
  194. }
  195. void InputSystem::SetRelativeMouseMode(bool value)
  196. {
  197. SDL_bool set = value ? SDL_TRUE : SDL_FALSE;
  198. SDL_SetRelativeMouseMode(set);
  199. mState.Mouse.mIsRelative = value;
  200. }
  201. float InputSystem::Filter1D(int input)
  202. {
  203. // A value < dead zone is interpreted as 0%
  204. const int deadZone = 250;
  205. // A value > max value is interpreted as 100%
  206. const int maxValue = 30000;
  207. float retVal = 0.0f;
  208. // Take absolute value of input
  209. int absValue = input > 0 ? input : -input;
  210. // Ignore input within dead zone
  211. if (absValue > deadZone)
  212. {
  213. // Compute fractional value between dead zone and max value
  214. retVal = static_cast<float>(absValue - deadZone) /
  215. (maxValue - deadZone);
  216. // Make sure sign matches original value
  217. retVal = input > 0 ? retVal : -1.0f * retVal;
  218. // Clamp between -1.0f and 1.0f
  219. retVal = Math::Clamp(retVal, -1.0f, 1.0f);
  220. }
  221. return retVal;
  222. }
  223. Vector2 InputSystem::Filter2D(int inputX, int inputY)
  224. {
  225. const float deadZone = 8000.0f;
  226. const float maxValue = 30000.0f;
  227. // Make into 2D vector
  228. Vector2 dir;
  229. dir.x = static_cast<float>(inputX);
  230. dir.y = static_cast<float>(inputY);
  231. float length = dir.Length();
  232. // If length < deadZone, should be no input
  233. if (length < deadZone)
  234. {
  235. dir = Vector2::Zero;
  236. }
  237. else
  238. {
  239. // Calculate fractional value between
  240. // dead zone and max value circles
  241. float f = (length - deadZone) / (maxValue - deadZone);
  242. // Clamp f between 0.0f and 1.0f
  243. f = Math::Clamp(f, 0.0f, 1.0f);
  244. // Normalize the vector, and then scale it to the
  245. // fractional value
  246. dir *= f / length;
  247. }
  248. return dir;
  249. }