Platformer
Platformer
InputManager.hpp
1 #ifndef INPUT_MANAGER_HPP
2 #define INPUT_MANAGER_HPP
3 
4 #include <SDL2/SDL.h>
5 #include <map>
6 #include "Vector2.hpp"
7 
8 static const unsigned int N_KEY_INPUTS = 1024;
9 static const unsigned int N_BUTTON_INPUTS = 6;
10 
11 enum class Input {
12  LEFT_MOUSE, RIGHT_MOUSE, MIDDLE_MOUSE,
13 
14  LEFT, RIGHT, UP, DOWN,
15  ATTACK_LEFT, ATTACK_RIGHT, ATTACK_UP, ATTACK_DOWN,
16  JUMP,
17  ACTION_1, ACTION_2, ACTION_3, ACTION_4,
18  UTIL_1, UTIL_2, UTIL_3,
19  ENTER,
20  QUIT
21 };
22 
24 static const std::map<SDL_Keycode, Input> INPUT_MAP = {
25  {SDLK_LEFT, Input::LEFT},
26  {SDLK_a, Input::LEFT},
27  {SDLK_RIGHT, Input::RIGHT},
28  {SDLK_d, Input::RIGHT},
29  {SDLK_UP, Input::UP},
30  {SDLK_w, Input::UP},
31  {SDLK_DOWN, Input::DOWN},
32  {SDLK_s, Input::DOWN},
33 
34  {SDLK_j, Input::ATTACK_LEFT},
35  {SDLK_l, Input::ATTACK_RIGHT},
36  {SDLK_k, Input::ATTACK_DOWN},
37  {SDLK_i, Input::ATTACK_UP},
38 
39  {SDLK_SPACE, Input::JUMP},
40 
41  {SDLK_q, Input::ACTION_1},
42  {SDLK_e, Input::ACTION_2},
43  {SDLK_r, Input::ACTION_3},
44  {SDLK_f, Input::ACTION_4},
45 
46  {SDLK_z, Input::UTIL_1},
47  {SDLK_x, Input::UTIL_2},
48  {SDLK_c, Input::UTIL_3},
49 
50  {SDLK_RETURN, Input::ENTER},
51  {SDLK_ESCAPE, Input::QUIT}
52 };
53 
54 static const std::map<int, Input> MOUSE_BUTTON_MAP = {
55  {SDL_BUTTON_LEFT, Input::LEFT_MOUSE},
56  {SDL_BUTTON_MIDDLE, Input::MIDDLE_MOUSE},
57  {SDL_BUTTON_RIGHT, Input::RIGHT_MOUSE}
58 };
59 
64 class InputManager {
65  public:
67  static InputManager& instance();
68 
70  void PollInput();
71 
76  static bool GetActionPressed(
77  Input action);
78 
83  static bool GetActionHeld(
84  Input action);
85 
90  bool ShouldQuit() const;
91 
96  static Vector2 GetMousePos();
97 
99  InputManager(const InputManager&) = delete;
100 
102  InputManager& operator=(const InputManager&) = delete;
103 
104  private:
106  InputManager();
107 
109  ~InputManager();
110 
112  std::map<Input, bool> inputHeld;
113 
115  std::map<Input, bool> inputPressed;
116 
118  bool quitFlag;
119 
121  void ClearPresses();
122 
124  void UpEvent(Input input);
126  void DownEvent(Input input);
127 
130 };
131 
132 #endif
void DownEvent(Input input)
Called when a down event is registered for an input.
static bool GetActionHeld(Input action)
Gets whether or not the given input is held.
Definition: InputManager.cpp:51
void ClearPresses()
Resets the inputPressed map to falses each frame.
Definition: InputManager.cpp:57
This class manages the inputs of our game as a singleton.
Definition: InputManager.hpp:64
bool ShouldQuit() const
Gets whether or not the game should quit.
Definition: InputManager.cpp:55
Basic class representation of a vector 2 with an x and y coordinate.
Definition: Vector2.hpp:8
InputManager & operator=(const InputManager &)=delete
Stop the compiler from generating methods to copy the object.
static bool GetActionPressed(Input action)
Gets whether or not the given input was just pressed.
Definition: InputManager.cpp:48
InputManager()
Constructs an InputManager.
Definition: InputManager.cpp:3
bool quitFlag
Whether or not the game should quit.
Definition: InputManager.hpp:118
static InputManager & instance()
The singleton instance of the InputManager.
Definition: InputManager.cpp:6
~InputManager()
Destructs an InputManager.
Definition: InputManager.cpp:4
void UpEvent(Input input)
Called when an up event is registered for an input.
void PollInput()
Polls for input from the keyboard.
Definition: InputManager.cpp:12
static Vector2 GetMousePos()
Gets the position of the most recent mouse event.
Definition: InputManager.cpp:63
Vector2 mousePos
Location of the last mouse event.
Definition: InputManager.hpp:129
std::map< Input, bool > inputPressed
Array of inputs that were just pressed.
Definition: InputManager.hpp:115
std::map< Input, bool > inputHeld
Array of inputs that are currently held down.
Definition: InputManager.hpp:112