InputSystem.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. // The different button states
  11. enum ButtonState
  12. {
  13. ENone,
  14. EPressed,
  15. EReleased,
  16. EHeld
  17. };
  18. // Helper for Keyboard input
  19. class KeyboardState
  20. {
  21. public:
  22. // Friend so InputSystem can easily update it
  23. friend class InputSystem;
  24. // Get just the boolean true/false value of key
  25. bool GetKeyValue(int keyCode) const;
  26. // Get a state based on current and previous frame
  27. ButtonState GetKeyState(int keyCode) const;
  28. private:
  29. const Uint8* mCurrState;
  30. Uint8 mPrevState[SDL_NUM_SCANCODES];
  31. };
  32. // Wrapper that contains current state of input
  33. struct InputState
  34. {
  35. KeyboardState Keyboard;
  36. };
  37. class InputSystem
  38. {
  39. public:
  40. bool Initialize();
  41. void Shutdown();
  42. // Called right before SDL_PollEvents loop
  43. void PrepareForUpdate();
  44. // Called after SDL_PollEvents loop
  45. void Update();
  46. const InputState& GetState() const { return mState; }
  47. private:
  48. InputState mState;
  49. };