home *** CD-ROM | disk | FTP | other *** search
/ Game Audio Programming / GameAudioProgramming.iso / Extras / Sensaura / SDK1.0 / data1.cab / Example_Files / DS3DDemo / Source / cdinput.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-13  |  4.3 KB  |  142 lines

  1. /*
  2.     Company:            Sensaura
  3.     Copyright:          (C) 1998
  4.  
  5.     File Name:            cdinput.cpp
  6.     File Description:    Source file for implementation of DirectInputObject class.
  7.                         This class supports an IDirectInput object by managing input devices.
  8.     Author:                Adam Philp
  9.     File Version:        1.01.000
  10.     Last Update:        09-SEP-98
  11.  
  12.     Target Compiler:    Microsoft Visual C++ Version 5.0
  13. */
  14.  
  15. ///////////////////////    Included files ////////////////////////////////////////////////////////////
  16.  
  17. #include <windows.h>
  18.  
  19. #include "cdinput.h"                    // DirectInputObject class declarations
  20. #include "directx.h"                    // DirectX functions and macros
  21. #include "debug.h"                        // Debugging support macros and functions
  22.  
  23. enum
  24. {
  25.     KEY_SPACE        = 57,
  26.     KEY_UPARROW        = 200,
  27.     KEY_PAGEUP        = 201,
  28.     KEY_LEFTARROW    = 203,
  29.     KEY_RIGHTARROW    = 205,
  30.     KEY_DOWNARROW    = 208,
  31.     KEY_PAGEDOWN    = 209
  32. };
  33.  
  34. /////////////////////// DirectInputObject class implementation ////////////////////////////////////
  35.  
  36. DirectInputObject::DirectInputObject()
  37. {
  38.     m_pDI = NULL;
  39.     m_pKeyboard = NULL;
  40. }
  41.  
  42. DirectInputObject::~DirectInputObject()
  43. {
  44.     Release();
  45. }
  46.  
  47. /*
  48.     Function:        Create
  49.     Description:    Set up the input devices for the application here.  In this example only the
  50.                     keyboard is used for input.  If you want to use other devices e.g. mouse then
  51.                     initialize them here.
  52.     Parameters:        hwnd - handle of the window to which to attach the DirectInput object
  53.                     hinst - application instance handle
  54.     Return value:    true if a DirectInput object was successfully created, false otherwise
  55. */
  56.  
  57. bool DirectInputObject::Create(HWND hwnd, HINSTANCE hinst)
  58. {
  59.     ASSERT(!m_pDI);                        // Ensure that DirectInput object is not already created
  60.     ASSERT(!m_pKeyboard);
  61.                                         // Create the DirectInput object
  62.     TRY_DI(DirectInputCreate(hinst, DIRECTINPUT_VERSION, &m_pDI, NULL))
  63.                                         // Create a keyboard device object
  64.     TRY_DI(m_pDI->CreateDevice(GUID_SysKeyboard, &m_pKeyboard, NULL))
  65.     TRY_DI(m_pKeyboard->SetCooperativeLevel(hwnd, DISCL_BACKGROUND|DISCL_NONEXCLUSIVE))
  66.     TRY_DI(m_pKeyboard->SetDataFormat(&c_dfDIKeyboard))
  67.     TRY_DI(m_pKeyboard->Acquire())                                        
  68.     return true;
  69.  
  70. DI_ERROR:
  71.     Release();                            // If creation failed, make sure all objects are released
  72.     return false;
  73. }
  74.  
  75. /*
  76.     Function:        Release
  77.     Description:    Release all DirectInput devices and the IDirectInput interface and set member 
  78.                     pointers to NULL.  Call this function before calling Create() if you want to 
  79.                     create a DirectInput object    more than once in your application
  80. */
  81.  
  82. void DirectInputObject::Release()
  83. {
  84.     if(m_pKeyboard)                        // Release keyboard object
  85.     {                
  86.         m_pKeyboard->Unacquire();
  87.         m_pKeyboard->Release();
  88.         m_pKeyboard = NULL;
  89.     }
  90.     RELEASE(m_pDI)                        // Release DirectInput object
  91. }
  92.  
  93. /*
  94.     Function:        Update
  95.     Description:    Poll all the DirectInput devices attached to this DirectInput object.  In this
  96.                     example only the keyboard is used.  If you want to poll additional devices e.g.
  97.                     mouse, then do so here, adding extra pointers to the parameter list to return
  98.                     input data
  99.     Parameters:        pKeys - pointer to variable to contain flags indicating which keys are pressed
  100.     Return value:    true if all input devices were polled successfully, false otherwise
  101. */
  102.  
  103. bool DirectInputObject::Update(LPDWORD pKeys)
  104. {
  105.     static BYTE    abKeyboardState[256];    // Make array static so it isn't recreated all the time
  106.     int    i;
  107.                                         // Poll the keyboard
  108.     TRY_DI(m_pKeyboard->GetDeviceState(256, abKeyboardState))
  109.     
  110.     *pKeys = 0;
  111.     for(i = 0; i < 256; i++)            // Check every key to see if it's pressed
  112.     {
  113.         if(abKeyboardState[i])            // Has key been pressed ?
  114.             *pKeys |= TranslateKey(i);    // Translate it
  115.      }
  116.     return true;
  117.  
  118. DI_ERROR:
  119.     return false;
  120. }
  121.  
  122. /*
  123.     Function:        TranslateKey
  124.     Description:    Convert a DirectInput key code into a key flag for use by the application
  125.     Parameters:        nKey - the DirectInput code of the key to translate
  126.     Return value:    A KF_XXXX flag if the key is recognised, otherwise 0
  127. */
  128.  
  129. DWORD DirectInputObject::TranslateKey(int nKey)
  130. {
  131.     switch(nKey)
  132.     {
  133.     case KEY_SPACE:            return KF_SPACE;
  134.     case KEY_UPARROW:        return KF_UPARROW;
  135.     case KEY_PAGEUP:        return KF_PAGEUP;
  136.     case KEY_LEFTARROW:        return KF_LEFTARROW;
  137.     case KEY_RIGHTARROW:    return KF_RIGHTARROW;
  138.     case KEY_DOWNARROW:        return KF_DOWNARROW;
  139.     case KEY_PAGEDOWN:        return KF_PAGEDOWN;
  140.     default:                return 0;
  141.     }
  142. }