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
Setting the Cooperative Level We are almost ready to begin using the device, so now it is time to set the cooperative level. The cooperative levels and their ramifications were explained above. Setting the cooperative level requires a call to the IDirectInputDevice’s SetCooperativeLevel method. The SetCooperativeLevel method is defined as:
    function SetCooperativeLevel(

    hwnd: HWND;                       // a handle to the application window

    dwFlags: DWORD                    // cooperative level flags

    ): HResult;                       // returns a DirectX error code
The first parameter is the handle to the application’s main window. This lets Windows and DirectInput know the application to which the input data should be sent. The second parameter is a combination of flags indicating the desired cooperative level. This must be set to one of each from DISCL_BACKGROUND/ DISCL_FOREGROUND and DISCL_EXCLUSIVE/DISCL_NONEXCLUSIVE. Setting Device Properties This step may be optional depending on the type of input device and the method by which input data is retrieved (i.e., immediate or buffered). However, some devices have certain properties that must be initialized before data can be retrieved from them. For example, joysticks must have their axes ranges set before they can return meaningful input data. Also, if you intend on using buffered data retrieval, you must set the size of the buffer. Setting device properties is accomplished by calling the IDirectInputDevice’s SetProperty method. The SetProperty method is defined as:
    function SetProperty(

    rguidProp: TRefGUID;                 // property identifier

    var pdiph: TDIPropHeader             // the property data structure

    ): HResult;                          // returns a DirectX error code
This is another method where DirectInput’s open architecture results in complexity. The first parameter identifies which property of the device to set. The value of this parameter is very dependent on the type of the device. For instance, it would be meaningless to set the range of an axis for a keyboard device. This parameter could be a GUID provided by the manufacturer of the device that identifies some specific property. Fortunately, however, there are several predefined values that we can use for setting specific properties. Perhaps the most common value passed to this parameter is DIPROP_BUFFERSIZE, which indicates we wish to set the size of the buffer for buffered data retrieval. The second parameter is a pointer to a TDIPropHeader data structure. Here’s where the complexity comes in. This parameter is a little misleading, because the method actually takes a pointer to whatever data structure is defined for the particular property being set. This could be some huge, custom data structure as defined by the product manufacturer. The only requirement is that the data structure must begin with a TDIPropHeader structure, which lets DirectInput know just how big the main structure is and the object on the device (such as an axis) whose properties are being set. Fortunately, most properties require only a single DWORD of information (such as buffer size) or perhaps two LongWords (in the case of ranges). The TDIPropHeader structure itself is defined as:
    TDIPropHeader = packed record

      dwSize: DWORD;                  // the size of the main structure

      dwHeaderSize: DWORD;            // the size of the TDIPropHeader structure

      dwObj: DWORD;                   // the device object identifier

      dwHow: DWORD;                   // indicates how to identify the device object

    end;
The first member of this structure identifies the size of the overall structure. The second member should be set to the size of the TDIPropHeader structure. The third member identifies the device object (such as an axis) whose property is being set. There are a number of predefined identifiers that we will use to set various properties, and we’ll see examples of this later in the sections on mouse and joystick programming. This member could be set to zero if a property that affects the entire device is being set, such as the buffer size. The last member tells DirectInput how the dwObj member is interpreted. This should be set to DIPH_DEVICE in the case of properties that affect the entire device. For individual device objects, there are two methods by which these objects can be identified. In this book, we will be concerned only with the offset method, so we should set this last member to DIPH_BYOFFSET. DirectInput defines two standard structures that are most commonly used with SetProperty. The TDIPropDWord structure is used for setting properties that take only a single DWORD of information (such as buffer size), and the TDIPropRange structure is used for setting properties requiring two LongWords (such as ranges). The TDIPropDWord structure is defined as:
    TDIPropDWord = packed record

     diph: TDIPropHeader;            // the TDIPropHeader structure

      dwData: DWORD;                 // the value for the DWORD property

    end;
The TDIPropRange structure is defined as:
    TDIPropRange = packed record

      diph: TDIPropHeader;           // the TDIPropHeader structure

      lMin: Longint;                 // the minimum range value

      lMax: Longint;                 // the maximum range value

    end;
We’ll see examples using the SetProperty method with these two data structures later in the chapter.
| Note: Device properties can be retrieved by using the GetProperty method, which takes the exact same parameters.
Acquiring the Device The input device has been set up, and we are now ready to begin retrieving data. As explained above, we must acquire the device, which is accomplished by calling IDirectInputDevice’s Acquire method. This simple method is defined as:
    function Acquire: HResult;          // returns a DirectX error code
Subsequent calls to Acquire, once the device has been acquired, have no effect. A device can similarly be unacquired by calling the Unacquire method, which also takes no parameters. ### Retrieving Input Data While the steps required for initializing and setting up DirectInput and DirectInputDevices are similar for every device, exactly how the input data is retrieved and extracted varies from device to device. This will also depend on whether you will be using immediate data retrieval or buffered data retrieval. This leads to subtle nuances in which initialization steps are required, and how the data returned from certain methods is interpreted. In this section, we will take a look at examples for reading input data from three standard devices: the keyboard, the mouse, and the joystick. Each is similar in implementation, but there are differences that warrant a closer inspection. At the same time, we will also demonstrate both immediate data retrieval and buffered data retrieval. While we will only implement one or the other method of data retrieval in each example to simplify the code, immediate or buffered data retrieval can be used for any input device. One thing to consider when developing your game application is if the complexity of DirectInput is even warranted. The Windows API contains numerous powerful functions that allow the user to read input from the keyboard, the mouse, or even game controllers. There is no support for force feedback, but if your application only requires a very simple interface to input devices, you may want to stick with standard Windows messages, or perhaps the Win32 API. In the following examples, we will demonstrate simplistic methods for reading input from devices using Win32 API functions before delving into the more complex DirectInput methods. Which you choose will be totally dependent on the type of game and the performance it will require, but either or both techniques are perfectly acceptable.
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.