Direct Input

Allows device independent access/control to the KB, mouse, joystick & all other forms of input devices in a uniform manner.

 

Ned’s Turkey Farm doesn’t use DirectInput, it uses the Win API.

 

Direct Input recognizes 3 types of devices:

·       keyboard,

·       mouse and

·       all others are considered joysticks.

 

Consists of:

·       run-time dlls

·       dinput.h

·       dinput.lib

 

To use:

·       create DI object with DirectInput8Create()

·       create one or more input devices with CreateDevice() passing it the GUID of the device to create. 

o      GUID_SysKeyboard: your primary KB

o      GUID_SysMouse: your mouse

·       set the cooperation level for each device with SetCooperativeLevel()

·       set the data format of each device with SetDataFormat() and set any special properties with SetProperty()

·       attach each device to your application with Acquire()

·       receive input with GetDeviceState() or call Poll() if device needs manual polling (most joysticks require polling)

 

DI works so well you can completely cut Windows out of the loop.

 


Creating a DI object

 

HRESULT DirectInputCreate(<hinst>,   // app handle

                  <version>,              // set to DIRECTINPUT_VERSION

                  <reference ID>       // set to IID_IDIRECTINPUT8

                  <ptr DI obj>,          // addr of ptr to DI interface

                    NULL);

 

Ex:

#include <DINPUT.H>

 

LPDIRECTINPUT8  lpdi = NULL;   // DI obj ptr

 

If (DirectInputCreate(hinstance, DIRECTINPUT_VERSION,

          _IDIRECTINPUT8, (void**)&lpdi, NULL) != DI_OK)

{/* error */}

 

and when done using the interface, free resources with:

     if (lpdi)  lpdi->Release();

 

     Note. Always release all the DI devices you created first!

 

The Keyboard

·       create KB device – this is an interface to the desired device

·       set cooperative level for KB

·       set data format

·        acquire the KB

 

creating KB device

HRESULT CreateDevice(GUID_<device type>,

                                              <device ptr>, NULL);

 

Ex:

#define INITGUID         // defines kb guid

#include <OBJBASE.H>

#include <dinput.h>

 

LPDIRECTINPUTDEVICE8 lpdiKey = NULL;  // KB ptr

 

If ( CreateDevice(GUID_SysKeyboard, &lpdiKey, NULL) !=    

          DI_OK)

{/* error */}

 

At this point lpdiKey points to you KB interface, so you can now call its methods

 

(use GUID_SysMouse for the mouse, call EnumDevices() to get a globally unique ID for all other devices.)

 

and when done using the device, free resources with:

     if (lpdiKey)  lpdiKey->Release();  // do before lpdi->Release()

 

 

Seting coop level

 

HRESULT SetCooperativeLevel(<hwnd>, <flags>);

 

Flag value

 DISCL_BACKGROUND

Can use DI device in foreground or background

 DISCL _FOREGROUND

Requires foreground access

 DISCL _EXCLUSIVE

After you acquire device no other app. can request exclusive access to it, but can request nonexclusive

 DISCL _NONEXCLUSIVE

Shareable access

 

Use DISCL_BACKGROUND | DISCL_NONEXCLUSIVE for most applications. FORGROUND will not send it data unless it is on top.

 

Ex:

If (SetCooperativeLevel(hwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE) != DD_OK) // error

 

 

Set Data Format

 

HRESULT SetDataFormat(<fmt ptr>);

 

typedef struct

{ DWORD dwSize;

   DWORD dwObjSize;  // bytes in OBJ below

   DWORD dwFlags;

   DWORD dwDataSize;    // data packet size

   DWORD dwNumObjs;   //# obj in next array

   LPDIOBJECTDATAFORMAT rgodf; // array of objs

} DIDATAFORMAT, * LPDIDATAFORMAT;

 

This is such a mess that MS created 3 predefined structs:

·       c_dfDIKeyboard

·       c_dfDIMouse

·       c_dfDIJoystick

·       c_dfDIJoystick2 – force feedback device

 

therefore you only need to

 

lpdikey->SetDataFormat(&c_dfDIKeyboard);

 

now:

lpdikey->Acquire();

Reading the Device

When you’re ready to read some data from the KB use:

 

HRESULT GetDeviceState(<data size>, < buff ptr>);

 

Ex:

UCHAR keyState[256]; // buff to hold state info

 

// read the KB

If (lpdikey->GetDeviceState(256, keyState) != DI_OK)

{/* error */}

else

{  // test for key press

    if (keyState[DIK_RIGHT] & 0X80)  // right arrow pressed

   

Values:

DIK_ESCAPE/0-9/A-Z/RETURN/LCONTROL/RCONTROL /SPACE/F1-F12/UP/DOWN/LEFT/RIGHT/PRIOR/NEXT

(see help for complete list)

 

When done with kb:

    lpdiKey->Unacquire();

    lpdiKey->Release();

 

KB demo:

 

// WINX GAME PROGRAMMING CONSOLE FUNCTIONS

int GameInit(void *parms)

{

  // this function is where you do all the initialization

  // for your game

 

  int index;         // looping var

  char filename[80]; // used to build up files names

 

  // initialize directdraw

  DD_Init(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP);

 

  // keyboard creation section

  // first create the direct input object

  if (DirectInputCreate(mainInstance,DIRECTINPUT_VERSION,&lpdi,NULL)!=DI_OK)

      return(0);

 

  // create a keyboard device 

  if (lpdi->CreateDevice(GUID_SysKeyboard, &lpdikey, NULL)!=DI_OK)

     return(0);

 

  // set cooperation level

  if (lpdikey->SetCooperativeLevel(mainWindowHandle,

                 DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)!=DI_OK)

      return(0);

 

  // set data format

  if (lpdikey->SetDataFormat(&c_dfDIKeyboard)!=DI_OK)

     return(0);

 

  // acquire the keyboard

  if (lpdikey->Acquire()!=DI_OK)

      return(0);

 

  // load the background

  Load_Bitmap_File(&bitmap8bit, "REACTOR.BMP");

 

  // set the palette to background image palette

  Set_Palette(bitmap8bit.palette);

 

  // create and load the reactor bitmap image

  Create_Bitmap(&reactor, 0,0, 640, 480);

  Load_Image_Bitmap(&reactor,&bitmap8bit,0,0,BITMAP_EXTRACT_MODE_ABS);

  Unload_Bitmap_File(&bitmap8bit);

 

  // now let's load in all the frames for the skelaton!!!

 

  // create skelaton bob

  if (!Create_BOB(&skelaton,0,0,56,72,32,

           BOB_ATTR_VISIBLE | BOB_ATTR_MULTI_ANIM,DDSCAPS_SYSTEMMEMORY))

      return(0);

 

  // load the frames in 8 directions, 4 frames each

  // each set of frames has a walk and a fire, frame sets

  // are loaded in counter clockwise order looking down

  // from a birds eys view or the x-z plane

  for (int direction = 0; direction < 8; direction++)

    {

      // build up file name

      sprintf(filename,"SKELSP%d.BMP",direction);

 

      // load in new bitmap file

      Load_Bitmap_File(&bitmap8bit,filename);

 

      Load_Frame_BOB(&skelaton,&bitmap8bit,0+direction*4,0,0,BITMAP_EXTRACT_MODE_CELL); 

      Load_Frame_BOB(&skelaton,&bitmap8bit,1+direction*4,1,0,BITMAP_EXTRACT_MODE_CELL); 

      Load_Frame_BOB(&skelaton,&bitmap8bit,2+direction*4,2,0,BITMAP_EXTRACT_MODE_CELL); 

      Load_Frame_BOB(&skelaton,&bitmap8bit,3+direction*4,0,1,BITMAP_EXTRACT_MODE_CELL); 

 

      // unload the bitmap file

      Unload_Bitmap_File(&bitmap8bit);

 

      // set the animation sequences for skelaton

      Load_Animation_BOB(&skelaton,direction,4,skelaton_anims[direction]);

    } // end for direction

 

  // set up stating state of skelaton

  Set_Animation_BOB(&skelaton, 0);

  Set_Anim_Speed_BOB(&skelaton, 4);

  Set_Vel_BOB(&skelaton, 0,0);

  Set_Pos_BOB(&skelaton, 0, 128);

 

  // set clipping rectangle to screen extents so mouse cursor

  // doens't mess up at edges

  RECT screen_rect = {0,0,screen_width,screen_height};

  lpddclipper = DD_Attach_Clipper(lpddsback,1,&screen_rect);

 

  // hide the mouse

  ShowCursor(FALSE);

 

  // return success

  return(1);

} // end GameInit

 

int GameShutdown(void *parms)

{

  // this function is where you shutdown your game and

  // release all resources that you allocated

 

  // kill the reactor

  Destroy_Bitmap(&reactor);

 

  // kill skelaton

  Destroy_BOB(&skelaton);

 

  // release keyboard

  lpdikey->Unacquire();

  lpdikey->Release();

  lpdi->Release();

 

  // shutdown directdraw

  DD_Shutdown();

 

  // return success

  return(1);

} // end GameShutdown

 

int GameMain(void *parms)

{

  // this is the workhorse of your game it will be called

  // continuously in real-time this is like main() in C

  // all the calls for you game go here!

 

  int          index;             // looping var

  int          dx,dy;             // general deltas used in collision detection

 

  static int   player_moving = 0; // tracks player motion

  static PALETTEENTRY glow = {0,0,0,PC_NOCOLLAPSE};  // used to animation red border

 

  // check of user is trying to exit

  if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE))

      PostMessage(mainWindowHandle, WM_DESTROY,0,0);

 

  // start the timing clock

  Start_Clock();

 

  // clear the drawing surface

  DD_Fill_Surface(lpddsback, 0);

 

  // lock the back buffer

  DD_Lock_Back_Surface();

 

  // draw the background reactor image

  Draw_Bitmap(&reactor, back_buffer, back_lpitch, 0);

 

  // unlock the back buffer

  DD_Unlock_Back_Surface();

 

  // get player input

 

  // get the keyboard data

  lpdikey->GetDeviceState(256, (LPVOID)keyboard_state);

 

  // reset motion flag

  player_moving = 0;

 

  // test direction of motion, this is a good example of testing the keyboard

  // although the code could be optimized this is more educational

 

  if (keyboard_state[DIK_RIGHT] && keyboard_state[DIK_UP])

   {

     // move skelaton

     skelaton.x+=2;

     skelaton.y-=2;

     dx=2; dy=-2;

 

     // set motion flag

     player_moving = 1;

 

     // check animation needs to change

     if (skelaton.curr_animation != SKELATON_NEAST)

        Set_Animation_BOB(&skelaton,SKELATON_NEAST);

   } // end if

  else if (keyboard_state[DIK_LEFT] && keyboard_state[DIK_UP])

   {

     // move skelaton

     skelaton.x-=2;

     skelaton.y-=2;

     dx=-2; dy=-2;

 

     // set motion flag

     player_moving = 1;

 

     // check animation needs to change

     if (skelaton.curr_animation != SKELATON_NWEST)

        Set_Animation_BOB(&skelaton,SKELATON_NWEST);

   } // end if

  else if (keyboard_state[DIK_LEFT] && keyboard_state[DIK_DOWN])

   {

     // move skelaton

     skelaton.x-=2;

     skelaton.y+=2;

     dx=-2; dy=2;

 

     // set motion flag

     player_moving = 1;

 

     // check animation needs to change

     if (skelaton.curr_animation != SKELATON_SWEST)

        Set_Animation_BOB(&skelaton,SKELATON_SWEST);

   } // end if

  else if (keyboard_state[DIK_RIGHT] && keyboard_state[DIK_DOWN])

   {

     // move skelaton

     skelaton.x+=2;

     skelaton.y+=2;

     dx=2; dy=2;

 

     // set motion flag

     player_moving = 1;

 

     // check animation needs to change

     if (skelaton.curr_animation != SKELATON_SEAST)

        Set_Animation_BOB(&skelaton,SKELATON_SEAST);

   } // end if

  else if (keyboard_state[DIK_RIGHT])

   {

     // move skelaton

     skelaton.x+=2;

     dx=2; dy=0;

 

     // set motion flag

     player_moving = 1;

 

     // check animation needs to change

     if (skelaton.curr_animation != SKELATON_EAST)

        Set_Animation_BOB(&skelaton,SKELATON_EAST);

   } // end if

  else if (keyboard_state[DIK_LEFT]) 

   {

     // move skelaton

     skelaton.x-=2;

     dx=-2; dy=0;

  

     // set motion flag

     player_moving = 1;

 

     // check animation needs to change

     if (skelaton.curr_animation != SKELATON_WEST)

        Set_Animation_BOB(&skelaton,SKELATON_WEST);

   } // end if

  else if (keyboard_state[DIK_UP])   

   {

     // move skelaton

     skelaton.y-=2;

     dx=0; dy=-2;

  

     // set motion flag

     player_moving = 1;

 

     // check animation needs to change

     if (skelaton.curr_animation != SKELATON_NORTH)

        Set_Animation_BOB(&skelaton,SKELATON_NORTH);

   } // end if

  else if (keyboard_state[DIK_DOWN]) 

   {

     // move skelaton

     skelaton.y+=2;

     dx=0; dy=+2;

 

     // set motion flag

     player_moving = 1;

 

     // check animation needs to change

     if (skelaton.curr_animation != SKELATON_SOUTH)

        Set_Animation_BOB(&skelaton,SKELATON_SOUTH);

   } // end if

 

  // only animate if player is moving

  if (player_moving)

   {

     // animate skelaton

     Animate_BOB(&skelaton);

 

 

     // see if skelaton hit a wall

  

     // lock surface, so we can scan it

     DD_Lock_Back_Surface();

  

     // call the color scanner with 29, the color of the glowing wall

     // try to center the scan in the center of the object to make it

     // more realistic

     if (Color_Scan(skelaton.x+16, skelaton.y+16,

                  skelaton.x+skelaton.width-16, skelaton.y+skelaton.height-16,                                   

                  29, back_buffer,back_lpitch))

      {

        // back the skelaton up along its last trajectory

        skelaton.x-=dx;

        skelaton.y-=dy;

      } // end if

  

     // done, so unlock

     DD_Unlock_Back_Surface();

   } // end if

 

  // draw the skelaton

  Draw_BOB(&skelaton, lpddsback);

 

  // animate green energy border

  glow.peGreen = rand()%256;

  Set_Palette_Entry(29, &glow);

 

 

  // draw some text

  Draw_Text_GDI("WHERE IS EVERYBODY? I HAVE A BONE TO PICK!",0,0,RGB(0,0,255),lpddsback);

 

  Draw_Text_GDI("USE ARROW KEYS TO MOVE ME.",0,SCREEN_HEIGHT-32,RGB(10,10,10),lpddsback);

 

  // flip the surfaces

  DD_Flip();

 

  // sync to 3o fps

  Wait_Clock(30);

 

  // return success

  return(1);

} // end GameMain