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
| Caution: While the GetProperty method of IDirectInputDevice can be used to retrieve the granularity, some game controller devices report this information incorrectly.
Buttons Game controller button state is reported exactly as keyboard or mouse buttons: If the high bit of the low word is set, the button is down; otherwise it is up. Windows Functions Windows provides several functions to developers for determining game controller capabilities and retrieving input data. For the most part, these functions are incredibly powerful and can report almost as much data as their DirectInput counterparts. For most applications, these functions may supply all the required power.
| Note: MMSystem must be added to the Uses clause of any unit employing these functions.
Game controller capabilities can be retrieved by using the joyGetDevCaps function. joyGetDevCaps is defined as:
    joyGetDevCaps(

    uJoyID: UINT;              // the game controller identifier

    lpCaps: PJoyCaps;          // a TJoyCaps structure

    uSize: UINT                // the size of the TJoyCaps structure

    ): MMRESULT;               // returns an error condition
The first parameter is a constant indicating the game controller whose properties are to be retrieved. The second parameter points to a TJoyCaps structure, a rather large structure containing information such as the minimum and maximum range values of axes, the number of buttons on the controller, etc. The final parameter must be set to the size of the TJoyCaps structure. Developers should use this function first to determine what objects are available on the device before retrieving input data. The actual state of all objects on the game controller is retrieved by using the joyGetPosEx structure. joyGetPosEx is defined as:
    joyGetPosEx(

    uJoyID: UINT;                // the game controller identifier

    lpInfo: PJoyInfoEx           // a TJoyInfoEx structure

    ): MMRESULT;                 // returns an error condition
The first parameter is a constant indicating the game controller whose state is to be retrieved (presumably the same value passed to the joyGetDevCaps function). The second value is a pointer to a TJoyInfoEx structure. This structure defines the state of up to six axes (three axes and their rotations), 32 buttons, and one POV. The following example demonstrates how this function can report the state of a game controller. Figure 7-2 shows the output. Listing 7-11: Retrieving game controller input through joyGetPosEx
    procedure TfrmJoyTestBed.GetJoyInfoPoll;

    var

       CrossHairX,

       CrossHairY: Integer;

       iCount: Integer;

       JoyPos: TJoyInfoEx;

    begin

      {poll the joystick for its status, retrieving information for up to 32 buttons

       and 6 axis positions}

      JoyPos.dwSize := SizeOf(TJoyInfoEx);

      JoyPos.dwFlags := JOY_RETURNALL;

      joyGetPosEx(JOYSTICKID1, @JoyPos);

      {retrieve the coordinates of the X, Y axis relative to the panel}

      CrossHairX := JoyToRelative(0, pbCrossHairSurface.Width, JoyCaps.wXmin,

                                  JoyCaps.wXmax, JoyPos.wXpos);

      CrossHairY := JoyToRelative(0, pbCrossHairSurface.Width, JoyCaps.wYmin,

                                  JoyCaps.wYmax, JoyPos.wYpos);

      {draw the X, Y axis crosshair}

      DrawCrossHair(CrossHairX, CrossHairY);

      {draw the Point Of View direction}

      if (JoyCaps.wCaps and JOYCAPS_HASPOV) = JOYCAPS_HASPOV then

        DrawPOV(JoyPos.dwPOV);

      {draw the position of additional axes}

      if (JoyCaps.wCaps and JOYCAPS_HASZ) = JOYCAPS_HASZ then

        shpZBarPos.Width := JoyToRelative(0, pnlZBar.Width, JoyCaps.wZmin,

                                          JoyCaps.wZmax, JoyPos.wZpos);

      if (JoyCaps.wCaps and JOYCAPS_HASR) = JOYCAPS_HASR then

        shpRBarPos.Width := JoyToRelative(0, pnlRBar.Width, JoyCaps.wRmin,

                                          JoyCaps.wRmax, JoyPos.dwRpos);

      if (JoyCaps.wCaps and JOYCAPS_HASU) = JOYCAPS_HASU then

        shpUBarPos.Width := JoyToRelative(0, pnlUBar.Width, JoyCaps.wUmin,

                                          JoyCaps.wUmax, JoyPos.dwUpos);

      if (JoyCaps.wCaps and JOYCAPS_HASV) = JOYCAPS_HASV then

        shpVBarPos.Width := JoyToRelative(0, pnlVBar.Width, JoyCaps.wVmin,

                                          JoyCaps.wVmax, JoyPos.dwVpos);

      {test the joystick buttons}

      for iCount := 0 to 31 do

        if (ButtonLights[iCount]<>nil) then

          if Longbool(JoyPos.wButtons and Trunc(Power(2, iCount))) then

            ButtonLights[iCount].Brush.Color := clRed

          else

            ButtonLights[iCount].Brush.Color := clMaroon;

    end;
Unfortunately, some game controllers have objects that provide input that joyGetPosEx simply cannot detect. Additionally, there is no Win32 API support for force feedback capabilities. These functions can allow a developer to rapidly implement game controller support, and may be useful for prototyping. However, for the best response times, support for any game controller, and force feedback functionality, developers must use DirectInput. Figure 7-2: Win32 joystick input DirectInput Initialization We must follow most of the steps outlined above to initialize DirectInput to receive game controller data. However, since there is no system game controller and we are required to set up our own data format, there are several additional steps that must be taken. As always, we start by creating the DirectInput object. We must know the exact instance GUID of the game controller device before a DirectInputDevice object can be created, so we must enumerate the attached game controllers. Therefore, we call the EnumDevices method, passing it a type of DIDEVTYPE_JOYSTICK to indicate that we wish to enumerate game controllers. Usually there is only one game controller connected to the system. However, you may wish to create a list of all attached devices and present it to the user, as we explained earlier in the chapter. Once the instance GUID of the game controller is known, we can create the input device by calling the CreateDevice method. We need to use the Poll method, which is only available in the IDirectInputDevice2 interface, so we’ll need to use QueryInterface to retrieve the appropriate object. Now that we’ve created the device, we can retrieve its capabilities. We need to know how many buttons, axes, etc., this controller has; this and other information can be retrieved by calling the GetCapabilities method of the IDirectInputDevice object. GetCapabilities is defined as:
    function GetCapabilities(

    var lpDIDevCaps: TDIDevCaps                 // a TDIDevCaps structure

    ): HResult;                                 // returns a DirectX error code
The only parameter to this method is a pointer to a TDIDevCaps structure. This structure describes the capabilities of the queried device, and is defined as:
    TDIDevCaps = packed record

      dwSize: DWORD;                            // the size of the structure

      dwFlags: DWORD;                           // device flags

      dwDevType: DWORD;                         // device type identifier

      dwAxes: DWORD;                            // number of axes

      dwButtons: DWORD;                         // number of buttons

      dwPOVs: DWORD;                            // number of POVs

      dwFFSamplePeriod: DWORD;                  // force feedback sample period

      dwFFMinTimeResolution: DWORD;             // force feedback time resolution

      dwFirmwareRevision: DWORD;                // firmware version

      dwHardwareRevision: DWORD;                // hardware version

      dwFFDriverVersion: DWORD;                 // device driver version

    end;
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.