Game Development Reference
In-Depth Information
The first line calls the
InitDirectInput()
method to initialize DirectInput for us.
We will create this method in a second, but first we need to finish looking at the
UserInput()
method.Thenexttwolinesinitializeourkeyboardstatevariableswith
the empty
KeyboardState
objects. This is necessary to prevent a crash that would
occur if the program tries to access these variables on the first frame (when they
would be uninitialized, and therefore null, which would result in a
Null Reference
ex-
ception). This type of exception occurs when the program tries to access a variable
that is null. You can't use an object before you initialize it, after all!
The last two lines do exactly the same thing, but this time for our mouse state vari-
ables.
Initializing DirectInput
Now that our constructor is done, we need to create our
InitDirectInput()
method. It is a pretty short method, and here is the code:
private void InitDirectInput()
{
m_DirectInput = new DirectInput();
// Create our keyboard and mouse devices.
m_Keyboard = new Keyboard(m_DirectInput);
m_Mouse = new Mouse(m_DirectInput);
}
This method only has three lines of code at the moment. The first one creates
and initializes our DirectInput object and stores it in our
m_DirectInput
member
variable. The second line creates and initializes our keyboard object, storing it in
our
m_Keyboard
member variable. The third line does the same thing, but for our
mouse object, storing it in our
m_Mouse
member variable.
The fact that this method is short as it is, owes itself to SlimDX helping us out. If you
were to write this same code in C++ and without SlimDX, it would be much longer
and also a bit more cryptic. This is one of the things that makes SlimDX a great