Since much of the code to handle screens and gamepad will be the same for Binary Blaster, I decided to write my first game library. For now, I’m calling it ResistorKit since it borrows a lot of code from the Resistor game.
For now, it just includes two classes: Screen and GamePadHandler. The Screen class defines a standard format for a game screen, along with handling standard user input events. The Gamepad class handles the button presses from the active GamePad.
How to Use it
Right click your project in the Solution Explorer (right pane), select Add Reference, Browse, and then select ResistorKit.lib. Under References you should now see ResistorKit.
In your main game class, add the following line to use the ResistorKit API:
Note: If the ResistorKit.dll library is updated, then you will need to delete the DLL out of the XBox360/bin directory to import a new copy of the library. Otherwise, it will keep using the old library.
Create GamePadHandler and Screen objects as instance variables in your main game class. In the update method, call handleGamePad on the GamePadHandler object and pass in the gameTime and Screen object. Subclass the Screen class, implementing all of the abstract methods. These methods will be called when a button is pressed/released, on update, and on draw.
In the main game class Draw method, add a call to drawScreen on the Screen object and pass in the spriteBatch, a texture array, a font array, and title safe Rectangle. Make sure to call spriteBatch Begin and End before and after this method.
That’s all there is to it. Put the logic to handle button presses in the appropriate methods in the implemented Screen class. Put the drawing code in the draw method of the screen class. Put any update logic in the update method. Multiple screens can be defined, and screens can be switched by assigning the active screen to the current screen instance variable in the main game class.
Using this framework, I was able to get a simple sprite moving, jumping, and shooting around the screen.