Designing a Controller Manager
It's very easy to read the state of various controllers with the SDL events. Whenever the state changes an event is fired and you just have to filter through it to figure out what type of event it is, what it's updating and the new value.
My first crack at using it was very straightforward, I read the new state and then updated the Player Object. This worked fine and dandy 100% but something doesn't sit well when my event loop needs to know about my Player Object.
I decided to build a ControllerManager and Controller class.
1. The ControllerManager stores a list of Controllers connected to the system and is responsible for dishing them out.
2. The Controller Class defines the interface between the Controller and the Object being controlled. In this case I defined a controller to have 4 axis (e.g. 2 analog sticks) and 4 buttons.
3. I use inheritance to define specific controllers e.g. "XBox-360 Gamepad" or "Keyboard and Mouse" which implement the interface of the Controller.
The idea being that when the game starts I can initialise the ControllerManager which will detect all connected devices, create a Controller of the right sub-class and then pass on the events to the correct controller.
Interfacing with the Controller
From the standard interface defined in the Controller Class I can read the state of the pad. I've added an Updated member so that I only have to read it if the state changes to avoid excessive polling too. My issue lies in how to build the relationship between Player Object and Controller Object.
1. A Player Object has a Controller Object. I include a pointer to the Controller (retrieve from the ControllerManager) then on each Update I check to see if there's been an Update and if there has I update the state.
2. A Controller Object has a Player Object. I include a pointer to the Player Object and define a standard interface on all Playable Objects. When the event is triggered I can update to the Player Object.
3. Another option?
Your comments would be appreciated.