InputSystem.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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(int keyCode) const
  12. {
  13. return mCurrState[keyCode] == 1;
  14. }
  15. ButtonState KeyboardState::GetKeyState(int 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) == 1;
  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 InputSystem::Initialize()
  71. {
  72. // Keyboard
  73. // Assign current state pointer
  74. mState.Keyboard.mCurrState = SDL_GetKeyboardState(NULL);
  75. // Clear previous state memory
  76. memset(mState.Keyboard.mPrevState, 0,
  77. SDL_NUM_SCANCODES);
  78. // Mouse (just set everything to 0)
  79. mState.Mouse.mCurrButtons = 0;
  80. mState.Mouse.mPrevButtons = 0;
  81. return true;
  82. }
  83. void InputSystem::Shutdown()
  84. {
  85. }
  86. void InputSystem::PrepareForUpdate()
  87. {
  88. // Copy current state to previous
  89. // Keyboard
  90. memcpy(mState.Keyboard.mPrevState,
  91. mState.Keyboard.mCurrState,
  92. SDL_NUM_SCANCODES);
  93. // Mouse
  94. mState.Mouse.mPrevButtons = mState.Mouse.mCurrButtons;
  95. mState.Mouse.mIsRelative = false;
  96. mState.Mouse.mScrollWheel = Vector2::Zero;
  97. }
  98. void InputSystem::Update()
  99. {
  100. // Mouse
  101. int x = 0, y = 0;
  102. if (mState.Mouse.mIsRelative)
  103. {
  104. mState.Mouse.mCurrButtons =
  105. SDL_GetRelativeMouseState(&x, &y);
  106. }
  107. else
  108. {
  109. mState.Mouse.mCurrButtons =
  110. SDL_GetMouseState(&x, &y);
  111. }
  112. mState.Mouse.mMousePos.x = static_cast<float>(x);
  113. mState.Mouse.mMousePos.y = static_cast<float>(y);
  114. }
  115. void InputSystem::ProcessEvent(SDL_Event& event)
  116. {
  117. switch (event.type)
  118. {
  119. case SDL_MOUSEWHEEL:
  120. mState.Mouse.mScrollWheel = Vector2(
  121. static_cast<float>(event.wheel.x),
  122. static_cast<float>(event.wheel.y));
  123. break;
  124. default:
  125. break;
  126. }
  127. }
  128. void InputSystem::SetRelativeMouseMode(bool value)
  129. {
  130. SDL_bool set = value ? SDL_TRUE : SDL_FALSE;
  131. SDL_SetRelativeMouseMode(set);
  132. mState.Mouse.mIsRelative = value;
  133. }