[ Pobierz całość w formacie PDF ]
.The remainder of this hour shows youhow to put DirectInput to work handling input from the keyboard, mouse, and traditionaljoysticks.Before you get into the specifics of each device, however, let s go over somegeneral DirectInput housekeeping code that will apply to all devices.Starting Up DirectInputFirst, let s take a look at some code that shows how to use the DirectInputCreate()function to create a DirectInput object:LPDIRECTINPUT lpDI;if (DirectInputCreate(hinst, DIRECTINPUT_VERSION, &lpDI, NULL) != DI_OK)return FALSE;This code is pretty straightforward; it calls the DirectInputCreate() function to create aDirectInput object.This object can then be used to enumerate and create input devices.The steps to creating and using different input devices are similar regardless of thedevice.Following are the general steps involved in creating and initializing anyDirectInput device:1.Enumerate the attached devices with a call to the EnumDevices() method on theDirectInput object.2.Create the DirectInputDevice object with a call to the CreateDevice() method onthe DirectInput object.3.Set the device s data format with a call to the SetDataFormat() method.24 1634xCH17 11/13/99 11:16 AM Page 340340 Hour 174.Set the device s behavior with a call to the SetCooperativeLevel() method.5.Acquire the device with a call to the Acquire() method.For standard devices such as the keyboard and mouse, you can skip the first step.This isbecause of the fact that there aren t likely to be any systems out there with more than onekeyboard or mouse.Besides, anyone that is enough of a power user to need more thanone keyboard or mouse is bound to figure out a way to make them work with your game!You ll work through the specific code for each of these steps on different devices in amoment.For now, just think in terms of the logical steps required to get your hands on adevice.Cleaning Up DirectInputJust as there is an established series of events that must take place in order to initializeDirectInput and access devices, there is also a complementary cleanup process.You ll beglad to know that cleaning up after DirectInput is actually far easier than initializing it.Following are the steps required to clean up a DirectInput session:1.Unacquire all previously acquired devices by calling the Unacquire() method oneach.2.Release all previously created devices by calling the Release() method on each.3.Release the DirectInput object by calling the Release() method on it.Following is code to perform these steps and clean up after DirectInput:if (lpDIDevice){lpDIDevice->Unacquire();lpDIDevice->Release();lpDIDevice = NULL;}SafeRelease(lpDI);This code assumes that the DirectInput and DirectInputDevice object pointers are namedlpDI and lpDIDevice, respectively.Of course, you might have multiple device pointersactive in a given game, in which case you would be responsible for unacquiring andreleasing all of them.If you recall, the SafeRelease() macro has been used throughoutthe book to safely release a DirectX object and null its pointer:#define SafeRelease(x) if (x) { x->Release(); x=NULL;}Many of the methods used to initialize and manipulate DirectInput devices are capable offailing; in which case they return a value other than DI_OK.It s important to clean up anyDirectInput objects that you ve created or acquired when a failure occurs.For example, if24 1634xCH17 11/13/99 11:16 AM Page 341Introducing DirectInput Getting User Input 341you successfully create a device but then fail to acquire it, you must release the device ifyou don t plan to immediately try to acquire it again.I don t always show extensiveerror-handling code throughout the hour in order to keep the code easier to follow, butkeep in mind that it is important to always clean up after DirectInput, even when some-thing doesn t go as planned.Handling Keyboard InputHandling keyboard input is relatively straightforward using DirectInput.Because thekeyboard is a standard device, you don t have to hassle with enumerating the devices;you can just assume that there is one system keyboard.Following is code to create akeyboard device using the CreateDevice() method on a DirectInput object:LPDIRECTINPUTDEVICE pKeyboard;17HRESULT hr;hr = lpDI->CreateDevice(GUID_SysKeyboard, &pKeyboard, NULL);The first parameter to this method is the global identifier of the device to be created.Inthe case of the system keyboard, you can use the predefined GUID_SysKeyboard globalidentifier.The second parameter is a pointer to a DirectInputDevice pointer, which isfilled in with the newly created device pointer.The last parameter is used to supportCOM aggregation, but is typically set to NULL.If the DirectInputDevice object is success-fully created, CreateDevice() will return DI_OK.Now that you ve created a keyboard device, you re ready to set the data format for thedevice.Fortunately, because the keyboard is a standard device, there is a predefinedglobal variable, c_dfDIKeyboard, that specifies the data format for the keyboard.Following is code to set the data format for the keyboard device:hr = pKeyboard->SetDataFormat(&c_dfDIKeyboard);With the data format established, you re ready to set the keyboard behavior with a call toSetCooperativeLevel().It s important to point out that DirectInput doesn t allowexclusive access to the keyboard, which probably wouldn t be a good idea even if it did.So, you should always set the keyboard s cooperative level to non-exclusive, as the fol-lowing code demonstrates:hr = pKeyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND |DISCL_NONEXCLUSIVE);Notice that a window handle is passed as the first parameter to this method.This is neces-sary because the cooperative level establishes how devices are shared among applications,and the application window is the basis for this sharing.So, be sure to pass a handle to themain application window as the first parameter to SetCooperativeLevel().24 1634xCH17 11/13/99 11:16 AM Page 342342 Hour 17If you recall from the earlier discussion of starting up a DirectInput device, the only re-maining step for preparing the keyboard is to acquire it.Following is code that acquiresthe keyboard:pKeyboard->Acquire();Nothing too complicated there! You re now ready to begin handling keyboard input,which I m sure you re more than ready to do.Before we get into the code of inputtingdata from the keyboard, let s establish the ground rules for keyboard input handling.It s important to understand that the keyboard provides absolute information
[ Pobierz całość w formacie PDF ]