home *** CD-ROM | disk | FTP | other *** search
- /*
- Company: Sensaura
- Copyright: (C) 1998
-
- File Name: cdinput.cpp
- File Description: Source file for implementation of DirectInputObject class.
- This class supports an IDirectInput object by managing input devices.
- Author: Adam Philp
- File Version: 1.01.000
- Last Update: 09-SEP-98
-
- Target Compiler: Microsoft Visual C++ Version 5.0
- */
-
- /////////////////////// Included files ////////////////////////////////////////////////////////////
-
- #include <windows.h>
-
- #include "cdinput.h" // DirectInputObject class declarations
- #include "directx.h" // DirectX functions and macros
- #include "debug.h" // Debugging support macros and functions
-
- enum
- {
- KEY_SPACE = 57,
- KEY_UPARROW = 200,
- KEY_PAGEUP = 201,
- KEY_LEFTARROW = 203,
- KEY_RIGHTARROW = 205,
- KEY_DOWNARROW = 208,
- KEY_PAGEDOWN = 209
- };
-
- /////////////////////// DirectInputObject class implementation ////////////////////////////////////
-
- DirectInputObject::DirectInputObject()
- {
- m_pDI = NULL;
- m_pKeyboard = NULL;
- }
-
- DirectInputObject::~DirectInputObject()
- {
- Release();
- }
-
- /*
- Function: Create
- Description: Set up the input devices for the application here. In this example only the
- keyboard is used for input. If you want to use other devices e.g. mouse then
- initialize them here.
- Parameters: hwnd - handle of the window to which to attach the DirectInput object
- hinst - application instance handle
- Return value: true if a DirectInput object was successfully created, false otherwise
- */
-
- bool DirectInputObject::Create(HWND hwnd, HINSTANCE hinst)
- {
- ASSERT(!m_pDI); // Ensure that DirectInput object is not already created
- ASSERT(!m_pKeyboard);
- // Create the DirectInput object
- TRY_DI(DirectInputCreate(hinst, DIRECTINPUT_VERSION, &m_pDI, NULL))
- // Create a keyboard device object
- TRY_DI(m_pDI->CreateDevice(GUID_SysKeyboard, &m_pKeyboard, NULL))
- TRY_DI(m_pKeyboard->SetCooperativeLevel(hwnd, DISCL_BACKGROUND|DISCL_NONEXCLUSIVE))
- TRY_DI(m_pKeyboard->SetDataFormat(&c_dfDIKeyboard))
- TRY_DI(m_pKeyboard->Acquire())
- return true;
-
- DI_ERROR:
- Release(); // If creation failed, make sure all objects are released
- return false;
- }
-
- /*
- Function: Release
- Description: Release all DirectInput devices and the IDirectInput interface and set member
- pointers to NULL. Call this function before calling Create() if you want to
- create a DirectInput object more than once in your application
- */
-
- void DirectInputObject::Release()
- {
- if(m_pKeyboard) // Release keyboard object
- {
- m_pKeyboard->Unacquire();
- m_pKeyboard->Release();
- m_pKeyboard = NULL;
- }
- RELEASE(m_pDI) // Release DirectInput object
- }
-
- /*
- Function: Update
- Description: Poll all the DirectInput devices attached to this DirectInput object. In this
- example only the keyboard is used. If you want to poll additional devices e.g.
- mouse, then do so here, adding extra pointers to the parameter list to return
- input data
- Parameters: pKeys - pointer to variable to contain flags indicating which keys are pressed
- Return value: true if all input devices were polled successfully, false otherwise
- */
-
- bool DirectInputObject::Update(LPDWORD pKeys)
- {
- static BYTE abKeyboardState[256]; // Make array static so it isn't recreated all the time
- int i;
- // Poll the keyboard
- TRY_DI(m_pKeyboard->GetDeviceState(256, abKeyboardState))
-
- *pKeys = 0;
- for(i = 0; i < 256; i++) // Check every key to see if it's pressed
- {
- if(abKeyboardState[i]) // Has key been pressed ?
- *pKeys |= TranslateKey(i); // Translate it
- }
- return true;
-
- DI_ERROR:
- return false;
- }
-
- /*
- Function: TranslateKey
- Description: Convert a DirectInput key code into a key flag for use by the application
- Parameters: nKey - the DirectInput code of the key to translate
- Return value: A KF_XXXX flag if the key is recognised, otherwise 0
- */
-
- DWORD DirectInputObject::TranslateKey(int nKey)
- {
- switch(nKey)
- {
- case KEY_SPACE: return KF_SPACE;
- case KEY_UPARROW: return KF_UPARROW;
- case KEY_PAGEUP: return KF_PAGEUP;
- case KEY_LEFTARROW: return KF_LEFTARROW;
- case KEY_RIGHTARROW: return KF_RIGHTARROW;
- case KEY_DOWNARROW: return KF_DOWNARROW;
- case KEY_PAGEDOWN: return KF_PAGEDOWN;
- default: return 0;
- }
- }