Decoupling input bindings from game systems with C++/SDL
This is the 4th devlog of Warmonger Dynasty, a 4X turn-based strategy game made in C++/SDL. If you want to read the previous entries, take a look at my reading list.
When developing a game, you almost certainly want to read inputs from the player. Be it via the mouse, the keyboard, a gamepad, or a touchscreen.
My game is no exception, and the controls are via the mouse/keyboard. However, I don’t want to hard-code the bindings in my game systems. That would make it hard to make them configurable later on, and even harder to keep track of.
Therefore, I need to design an Input Manager which will allow my game systems to query for inputs without worrying about the specific bindings tied to them.
3 types of input actions
For my needs, I identified 3 player actions I want to be able to react to:
- Trigger: like a mouse click, I want to know if the action is active (the mouse button is still pressed), if the action has been performed (the mouse button has been pressed this frame), and if the action has been cancelled (the mouse button has been released this frame)
- Axis: like the keyboard’s arrows (or WASD), or a joystick, I want to know the value of the horizontal and vertical axis…