In our game created with c++ and sdl we are having difficulty delegating how the main character should be controlled. We have a keyboard class and a sprite class for the main character, but are unsure if the main character should have an instance of the keyboard class or if keyboard input should be done through the game loop. If you need anymore information I would be happy to provide it, thanks in advance for any help.
Your main character class should not have a reference to the keyboard or any input-related class. Input should be handled in the game loop, or preferably a dedicated input class, decoupled from other entities so they'll only need to respond to abstract actions, not raw input.
Not all input is related to your characters and and not all types of input devices are the same, so your best bet is to separate the input handling into several stages so you can handle it contextually e.g.
- Gather input data
- Examine current game state or context (MainMenu, InGame, InGameMenu, GameOver etc.)
- Mapping raw input to actions ... etc.
This will make it easier for you to implement new actions, control schemes, input devices or multiplayer controls.