DirectInput Initialization
Covers the necessary steps in setting up DirectInput objects and devices.
We must create the DirectInput Object with the DirectInputCreateEx(); function. It takes five parameters. The first is a handle to the instance of the application, the second parameter should be filled in with the DIRECTINPUT_VERSION flag, so as to use the latest version of DirectInput. The third parameter is the IID_IDirectInput7 interface. The fourth parameter is the address of the LPDIRECTINPUT7 object and the last parameter is always NULL. So all my words jumbled together may be represented by the following code.
// Initialized in WinMain();
HINSTANCE main_instance;
LPDIRECTINPUT7 lpDI;
if(FAILED(DirectInputCreate(main_instance,DIRECTINPUT_VERSION,IID_IDirectInput7,&lpDI,NULL)))
{
// ERROR
}[size="5"]STEP 2 - Creating the Keyboard Device Object
Now we must create a device object for the keyboard using CreateDeviceEx(); which takes four parameters. The first parameter is a GUID for the keyboard which is GUID_SysKeyboard, the second parameter is the interface IID_DirectInputDevice7, the third parameter is the address of the LPDIRECTDRAWDEVICE7 object. The last parameter is NULL.
LPDIRECTINPUTDEVICE7 lpDIkeyboard;
if(FAILED(lpDI->CreateDeviceEx(GUID_SysKeyboard,IID_IDirectInputDevice7,&lpDIkeyboard,NULL)))
{
// ERROR
}[size="5"]STEP 3 - Setting the Data Format
The next thing is to set the data format for us to read from with the global variable "c_dfDIKeyboard." The member function of the LPDIRECTDRAWDEVICE7 object used for this task is SetDataFormat(); and it only takes one parameter, can you guess what that is?
if(FAILED(lpDIkeyboard->SetDataFormat(&c_dfDIkeyboard)))
{
//ERROR
}[size="5"]STEP 4 - Playing Nice With Windows
Now we must set the cooperative level with windows; if you are familiar with other Direct X interfaces then you know how this may look. We set the cooperation by each individual object not by the LPDIRECTINPUT7 object. So this function is a member function of the LPDIRECTINPUTDEVICE7 object. The function SetCooperativeLevel(); takes two parameters, the first is a handle to the window of the application and the second is a set of flags which I will explain later.
// Initialized in WinMain();
HWND main_window;
if(FAILED(lpDIkeyboard->SetCooperativeLevel(main_window,DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)))
{
// ERROR
}[size="5"]STEP 5 - Acquring the Keyboard
This is the easiest step because it is a call to the member function Acquire(); to get the keyboard control.
lpDIkeyboard->Acquire();[size="5"]STEP 6 Getting Input and Playing With It
This is necessary if you wish to get input from the keyboard, you must check what it is doing in your game loop and act accordingly. The member function GetDeviceState(similar to the Windows API) takes two parameters. The first is the size of a character array, which is used to keep the 256 BYTE data. So therefore our character array should be of size 256.
char buffer[256];
if(FAILED(lpDIkeyboard->GetDeviceState(sizeof(buffer),&buffer)))
{
// ERROR
}#define KEYDOWN(name,key) (name[key] & 0x80)
if(KEYDOWN(buffer,DIK_ESCAPE))
PostQuitMessage(0);[size="5"]STEP 7 - SHUTTING DOWN!(VERY IMPORTANT)
If you do not shut down correctly you will not have a pleasant experience afterwards. So this is what I use:
if(lpDI != NULL)
{
if(lpDIkeyboard != NULL)
{
lpDIkeyboard->Unacquire();
lpDIkeyboard->Release();
}
lpDI-Release();
}Related Tutorials
Procedural Generation of 2D Tile Maps in Games
The article explains how to build a procedural 2D cave map generator using cellular automata. It covers map representat…
Localizing A Game Into French — Which Variant Should You Choose?
So, you’re making an awesome indie game, and now you’re thinking about localizing it into French? Great idea! There ar…
How Long Does It Take To Localize An Indie Game?
So you’re planning on localizing your indie game, but you’re not sure how much time to schedule in. While the timing o…
Creating game videos: best practices and pitfalls to avoid
Game video production: practical tips on how to create a game trailer or teaser that you can be proud of. Give your aud…
Adopting CI/CD: How Midwinter Entertainment iterates at speed with the help of IMS
Game development was once like one long sprint: a huge effort until you reached the finish line — at which point you co…
GameDev.net
5 Things to Consider When Making a Video to Promote Your App or Game
How can you show an app or game in your video in a way that attracts new users? Let’s take a look at what to go by when…
Discussion
More from Anthony Wlodarski
Comparing Shadow Mapping Techniques with Shadow Explorer
New Incentives and a Whole New Platform From The Intel AppUp developer program
The final installment in this series on Intel's AppUp program details additional incentives and opportunities for devel…
The Game Maker
This book excerpt contains the first chapter of the book that introduces beginning game developers to the Game Maker pr…
Discussion