Search Tips Advanced Search |
| Delphi Graphics and Game Programming Exposed with DirectX 7.0 by John Ayres Wordware Publishing, Inc. ISBN: 1556226373 Pub Date: 12/01/99
Search this book:
Previous | Table of Contents | Next
| Tip: It is standard practice to inform users to close all other applications before starting your game. It is doubtful that users would try to play two or more games at once, so you should generally set the cooperative level to exclusive/foreground.
Device Acquisition Once a device is set up, the application cannot receive input data until it instructs DirectInput that it is ready to do so. This process is known as acquiring the device. Once a device is acquired, input data can be retrieved in whatever manner desired as discussed above. Successfully acquiring the device is dependent on the cooperative level specified by the application and by those of other applications using DirectInput, as we just illustrated. However, bear in mind that a device can be forcibly unacquired by the application. This may happen when another application receives focus or acquires the device in exclusive mode (again dependent on the cooperative level of both applications and who acquired the device first). In this instance, any method that returns input data will return a value of DIERR_INPUTLOST or DIERR_NOTACQUIRED. You will need to check for this error value in your application, possibly attempting to reacquire the device until successful. You may also deliberately unacquire the device, such as when Windows needs the mouse in order to give the user access to menus. #### Initializing DirectInput As with other DirectX components, several steps are required in order to set up DirectInput and begin interfacing with user input devices. In general, most of these steps are required to read data from keyboards, mice, and game controllers. We will cover these steps in a broad sense here in order to familiarize you with them. They will be covered again more precisely when we look at retrieving data from specific input devices. DirectInput, and DirectInput devices, are instantiated by performing the following: • Creating the DirectInput object • Enumerating input devices • Creating the input device • Setting the data format • Setting the cooperative level • Setting device properties • Acquiring the device Creating the DirectInput Object The first step in retrieving data from an input device is to create the DirectInput object itself. Creating the DirectInput object involves calling the DirectInputCreate function, which is defined as:
    function DirectInputCreate(

    hinst: THandle;                    // the application instance handle

    dwVersion: DWORD;                  // DirectInput version number

    out ppDI: IDirectInput;            // the IDirectInput object to be instantiated

    punkOuter: IUnknown                // unused, set to nil

    ): HResult; stdcall;               // returns a DirectX error code
The first parameter is a handle to the calling application’s instance. The second parameter indicates the DirectInput version number. Most applications should simply set this parameter to DIRECTINPUT_VERSION (a constant defined in the DInput.pas file). However, if an earlier version number is indicated, DirectInput will emulate that version. This is useful if you had no need for newer DirectInput functionality, such as force feedback. The third parameter takes a pointer to the IDirectInput object. When this function returns, this object will be instantiated. The last parameter is used for COM aggregation, and must be set to nil as it is currently unsupported.
| Caution: If indicating an earlier version in the call to DirectInputCreate, the application must use the data structures compatible with this earlier version. Note that this will also affect the value to which the dwSize member of these structures must be initialized.
Enumerating Input Devices Technically speaking, this step is not required if your application will only support keyboard and mouse input devices. It may be useful if the target machine has more than one keyboard or mouse attached, but you can still tell DirectInput to create objects that represent the primary keyboard and mouse. However, game controllers are another story. Since there are no primary or default game controllers, applications that wish to support joysticks, steering wheels, etc., must obtain individual GUIDs for these objects through enumeration. The EnumDevices method of the IDirectInput object is used to list all available input devices. It is a callback function that will call the specified function once for each type of device attached to the system as indicated by the scope flags. The EnumDevices method is defined as:
    function EnumDevices(

    dwDevType: DWORD;                        // indicates the type to enumerate

    lpCallback: TDIEnumDevicesCallbackA;     // the callback function

    pvRef: Pointer;                          // an application-defined value

    dwFlags: DWORD                           // flags controlling the scope

    ): HResult;                              // returns a DirectX error code
The first parameter is a flag indicating the type of devices to enumerate. This can be set to zero to enumerate all devices, DIDEVTYPE_MOUSE to enumerate mice, DIDEVTYPE_KEYBOARD to enumerate keyboards, or DIDEVTYPE_ JOYSTICK to enumerate game controllers. The second parameter is a pointer to the callback function itself. The third parameter is a pointer to an application-defined value. The final parameter is a series of flags that further restrict the enumeration. This can be set to DIEDFL_ATTACHEDONLY to restrict enumeration to attached devices and/or DIEDFL_FORCEFEEDBACK to enumerate only force feedback devices. The callback function is defined as:
    function TDIEnumDevicesCallbackA(

    var lpddi: TDIDeviceInstance;            // a device description record

    pvRef: Pointer                           // the application-defined value

    ): BOOL; stdcall;                        // returns TRUE or FALSE
The first parameter contains a TDIDeviceInstance structure initialized with information about the enumerated input device. The second parameter contains a pointer to the application-defined value. This function must return TRUE to continue the enumeration; FALSE will stop the enumeration.
Previous | Table of Contents | Next

Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.