InputSystem.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 class InputSystem;
  23. bool GetKeyValue(SDL_Scancode code) const;
  24. ButtonState GetKeyState(SDL_Scancode code) const;
  25. private:
  26. const Uint8* mCurrState;
  27. const Uint8 mPrevState[SDL_NUM_SCANCODES];
  28. };
  29. // Wrapper that contains current state of input
  30. struct InputState
  31. {
  32. KeyboardState Keyboard;
  33. };
  34. class InputSystem
  35. {
  36. public:
  37. InputSystem();
  38. bool Initialize();
  39. void Shutdown();
  40. // Called right before SDL_PollEvents loop
  41. void PrepareForUpdate();
  42. // Called after SDL_PollEvents loop
  43. void Update();
  44. const InputState& GetState() const { return mState; }
  45. private:
  46. InputState mState;
  47. };