2
0

InputSystem.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #pragma once
  9. #include <SDL/SDL_scancode.h>
  10. #include <SDL/SDL_gamecontroller.h>
  11. #include <SDL/SDL_mouse.h>
  12. #include "Math.h"
  13. // The different button states
  14. enum ButtonState
  15. {
  16. ENone,
  17. EPressed,
  18. EReleased,
  19. EHeld
  20. };
  21. // Helper for keyboard input
  22. class KeyboardState
  23. {
  24. public:
  25. // Friend so InputSystem can easily update it
  26. friend class InputSystem;
  27. // Get just the boolean true/false value of key
  28. bool GetKeyValue(SDL_Scancode keyCode) const;
  29. // Get a state based on current and previous frame
  30. ButtonState GetKeyState(SDL_Scancode keyCode) const;
  31. private:
  32. const Uint8* mCurrState;
  33. Uint8 mPrevState[SDL_NUM_SCANCODES];
  34. };
  35. // Helper for mouse input
  36. class MouseState
  37. {
  38. public:
  39. friend class InputSystem;
  40. // For mouse position
  41. const Vector2& GetPosition() const { return mMousePos; }
  42. const Vector2& GetScrollWheel() const { return mScrollWheel; }
  43. bool IsRelative() const { return mIsRelative; }
  44. // For buttons
  45. bool GetButtonValue(int button) const;
  46. ButtonState GetButtonState(int button) const;
  47. private:
  48. // Store current mouse position
  49. Vector2 mMousePos;
  50. // Motion of scroll wheel
  51. Vector2 mScrollWheel;
  52. // Store button data
  53. Uint32 mCurrButtons;
  54. Uint32 mPrevButtons;
  55. // Are we in relative mouse mode
  56. bool mIsRelative;
  57. };
  58. // Helper for controller input
  59. class ControllerState
  60. {
  61. public:
  62. friend class InputSystem;
  63. // For buttons
  64. bool GetButtonValue(SDL_GameControllerButton button) const;
  65. ButtonState GetButtonState(SDL_GameControllerButton button) const;
  66. const Vector2& GetLeftStick() const { return mLeftStick; }
  67. const Vector2& GetRightStick() const { return mRightStick; }
  68. float GetLeftTrigger() const { return mLeftTrigger; }
  69. float GetRightTrigger() const { return mRightTrigger; }
  70. bool GetIsConnected() const { return mIsConnected; }
  71. private:
  72. // Current/previous buttons
  73. Uint8 mCurrButtons[SDL_CONTROLLER_BUTTON_MAX];
  74. Uint8 mPrevButtons[SDL_CONTROLLER_BUTTON_MAX];
  75. // Left/right sticks
  76. Vector2 mLeftStick;
  77. Vector2 mRightStick;
  78. // Left/right trigger
  79. float mLeftTrigger;
  80. float mRightTrigger;
  81. // Is this controller connected?
  82. bool mIsConnected;
  83. };
  84. // Wrapper that contains current state of input
  85. struct InputState
  86. {
  87. KeyboardState Keyboard;
  88. MouseState Mouse;
  89. ControllerState Controller;
  90. };
  91. class InputSystem
  92. {
  93. public:
  94. bool Initialize();
  95. void Shutdown();
  96. // Called right before SDL_PollEvents loop
  97. void PrepareForUpdate();
  98. // Called after SDL_PollEvents loop
  99. void Update();
  100. // Called to process an SDL event in input system
  101. void ProcessEvent(union SDL_Event& event);
  102. const InputState& GetState() const { return mState; }
  103. void SetRelativeMouseMode(bool value);
  104. private:
  105. float Filter1D(int input);
  106. Vector2 Filter2D(int inputX, int inputY);
  107. InputState mState;
  108. SDL_GameController* mController;
  109. };