2
0

InputSystem.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 (mCurrState[keyCode] == 0)
  18. {
  19. if (mPrevState[keyCode] == 0)
  20. {
  21. return ENone;
  22. }
  23. else
  24. {
  25. return EReleased;
  26. }
  27. }
  28. else // must be 1
  29. {
  30. if (mPrevState[keyCode] == 0)
  31. {
  32. return EPressed;
  33. }
  34. else
  35. {
  36. return EHeld;
  37. }
  38. }
  39. }
  40. bool InputSystem::Initialize()
  41. {
  42. // Assign current state pointer
  43. mState.Keyboard.mCurrState = SDL_GetKeyboardState(NULL);
  44. // Clear previous state memory
  45. memset(mState.Keyboard.mPrevState, 0,
  46. SDL_NUM_SCANCODES);
  47. return true;
  48. }
  49. void InputSystem::Shutdown()
  50. {
  51. }
  52. void InputSystem::PrepareForUpdate()
  53. {
  54. // Copy current state to previous
  55. // Keyboard
  56. memcpy(mState.Keyboard.mPrevState,
  57. mState.Keyboard.mCurrState,
  58. SDL_NUM_SCANCODES);
  59. }
  60. void InputSystem::Update()
  61. {
  62. }