Written by: Tuesday, May 10, 2011
XNA makes input handling very easy. It provides one static class for each input device: Keyboard, Mouse, and GamePad. In each frame, you can query the device states and examine the current key and button states. But sooner or later you discover that there are a few tasks that are not supported:
This becomes especially apparent when you create a game with a complex user interface (windows, text boxes, etc.).
And to be clear: It is ok that these tasks are not supported because the XNA Framework is not a game engine!
While creating our XNA GUI library (see DigitalRune Game UI), we have identified several commonly needed tasks and packed them into an input service. This and the next blog posts describe our input service design.
First step: We create an IInputService interface:
public interface IInputService { … }
All game components that process input should use this central interface. This interface will be implemented by the InputManager class. (Follow this link to learn more about services, managers and the service provider pattern: Service Provider articles.)
The InputManager implements IInputService and has one additional method:
public class InputManager : IInputService { … /// <summary> /// Updates the input states. This method must be called once per frame. /// </summary> /// <param name="deltaTime">The elapsed time since the last update.</param> public void Update(float deltaTime) { … } }
The Update() method must be called once per frame, and it does all the “hard” work of our input manager.
It is worth to discuss why we do not simply use a static class with global variables. We have used the service provider pattern for several years now, and it is one of the most useful patterns for game programming. A few of its advantages in regard to input handling:
In the next blog posts, we will describe the functionality of the IInputService, including:
If you want to look at the final image in advance, here is the class diagram of the input classes in our DigitalRune Game UI library.
And here is the current documentation of our input classes: DigitalRune Game UI – Input Handling. (The library is currently an alpha version. We are still looking for possible improvements. Any feedback is welcome!)
0 comment(s) so far...
A collection of the most useful blog articles can be found here:
Article Collection (on Documentation page)
DigitalRune is a trademark of Garstenauer Information Technology OG.
Garstenauer Information Technology OG Weingartenstrasse 35, 4452 Ternberg Austria (EUROPE) office@digitalrune.com