📄 dijoystick.cpp
字号:
// Return First Joystick Button Name Data If Possible
//
// return NULL if Failed or No Joysticks Available
//
//////////////////////////////////////////////////////////////////////
TCHAR* CDIJoystick::GetNextButtonName()
{
if(!m_ButtonPOS) return NULL;
return static_cast<TCHAR*>(m_DIButtonNames.GetNext(m_ButtonPOS));
}
//////////////////////////////////////////////////////////////////////
//
// Create the Joystick Device Using GUID Passed
//
//////////////////////////////////////////////////////////////////////
bool CDIJoystick::CreateDevice(GUID *guid)
{
HRESULT hr=NULL;
// If a device already exists and is created, Release it.
if (m_lpDIDevice)
{
// Always unacquire device before calling Release().
try
{
hr=m_lpDIDevice->Unacquire();
}
catch(...)
{
}
try
{
hr=m_lpDIDevice->Release();
if(FAILED(hr))
{
OutputDebugString(GetDIError(hr));
}
else m_lpDIDevice = NULL;
}
catch(...)
{
OutputDebugString("Failed to Release Pointer in CDIJoystick::CreateDevice(GUID *guid)\n");
}
}
// Check to see that Direct Input has been created and initialised.
if(!m_lpDI)
{
OutputDebugString("Failed to Create DI 9 interface to device in CDIJoystick::CreateDevice(GUID *guid) m_lpDI not initialised.\n");
return false;
}
// Attempt to create the device based on the GUID passed to this routine
hr=m_lpDI->CreateDevice(*guid, &m_lpDIDevice, NULL);
if(FAILED(hr))
{
OutputDebugString("Failed to Create DI 9 interface to device in CDIJoystick::CreateDevice(GUID *guid)\n");
return false;
}
// We must have been successful at this point.
// Therefore copy the GUID to this members instance for future reference.
memcpy(&m_JoystickGUID,guid,sizeof(GUID));
return true;
}
//////////////////////////////////////////////////////////////////////
//
// Return How Many Buttons The Attached Device Has Installed
// When giving the player an option of which joystick to use
// You may wish to evaluate the buttons available per attached device.
// Returns the number of buttons for the currently selected device.
//
//////////////////////////////////////////////////////////////////////
int CDIJoystick::HowManyButtons()
{
DIDEVICEOBJECTINSTANCE didoi;
DWORD x;
DWORD dwOfs;
int count=0;
HRESULT hr=NULL;
ClearFriendlyButtonNames();
//if(m_lpDIDevice)
if(InitDevice())
{
ZeroMemory(&didoi,sizeof(DIDEVICEOBJECTINSTANCE));
didoi.dwSize = sizeof( didoi );
for ( x = 0; x < 32; x++ )
{
dwOfs = DIJOFS_BUTTON( x );
hr=m_lpDIDevice->GetObjectInfo( &didoi, dwOfs, DIPH_BYOFFSET );
if ( SUCCEEDED( hr ) )
{
count++;
TCHAR* name=new char[sizeof(didoi.tszName)];
// Should include UNICODE support here.
strcpy(name,didoi.tszName);
// Add the button name to the Pointer List for future reference.
m_DIButtonNames.AddTail(name);
}
else
{
OutputDebugString(GetDIError(hr));
}
}
}
return count; // How many buttons did we find?
}
////////////////////////////////////////////////////////////////////////
//
// Shutdown the the link list of Friendly Button Names
//
////////////////////////////////////////////////////////////////////////
void CDIJoystick::ClearFriendlyButtonNames()
{
try
{
if(!m_DIButtonNames.IsEmpty())
{
POSITION pos=m_DIButtonNames.GetHeadPosition();
LPVOID del=NULL;
while(pos)
{
del=static_cast<LPVOID>(m_DIButtonNames.GetNext(pos));
if(del)
{
delete del;
}
}
}
m_DIButtonNames.RemoveAll();
}
catch(...)
{
OutputDebugString("Some unforseen error occurred in CDIJoystick::ClearFriendlyButtonNames()\n");
}
}
//////////////////////////////////////////////////////////////////////
//
// Initialise The Joystick!
//
//////////////////////////////////////////////////////////////////////
bool CDIJoystick::InitJoystick()
{
// Set range.
// Note: range, deadzone, and saturation are being set for the
// entire device. This could have unwanted effects on
// sliders, dials, etc.
DIPROPRANGE diprg;
diprg.diph.dwSize = sizeof( diprg );
diprg.diph.dwHeaderSize = sizeof( diprg.diph );
diprg.diph.dwObj = 0;
diprg.diph.dwHow = DIPH_DEVICE;
diprg.lMin = JOYMIN;
diprg.lMax = JOYMAX;
if ( FAILED( m_lpDIDevice->SetProperty( DIPROP_RANGE, &diprg.diph ) ) )
return FALSE;
// Set deadzone.
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof( dipdw );
dipdw.diph.dwHeaderSize = sizeof( dipdw.diph );
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = JOYDEAD;
if ( FAILED( m_lpDIDevice->SetProperty( DIPROP_DEADZONE, &dipdw.diph ) ) )
return FALSE;
// Set saturation.
dipdw.dwData = JOYSAT;
if ( FAILED( m_lpDIDevice->SetProperty( DIPROP_SATURATION, &dipdw.diph ) ) )
return FALSE;
// Find out if force feedback available.
DIDEVCAPS didc;
didc.dwSize = sizeof( DIDEVCAPS );
if ( FAILED( m_lpDIDevice->GetCapabilities( &didc ) ) )
return FALSE;
m_FFAvailable = ( didc.dwFlags & DIDC_FORCEFEEDBACK );
// If it's a force feedback stick, turn off autocenter so it doesn't
// get in the way of our effects.
if ( m_FFAvailable )
{
DIPROPDWORD DIPropAutoCenter;
DIPropAutoCenter.diph.dwSize = sizeof( DIPropAutoCenter );
DIPropAutoCenter.diph.dwHeaderSize = sizeof( DIPROPHEADER );
DIPropAutoCenter.diph.dwObj = 0;
DIPropAutoCenter.diph.dwHow = DIPH_DEVICE;
DIPropAutoCenter.dwData = 0;
m_lpDIDevice->SetProperty( DIPROP_AUTOCENTER,
&DIPropAutoCenter.diph );
}
return TRUE;
}
//////////////////////////////////////////////////////////////////////
//
// Initialise The Joystick Device
//
//////////////////////////////////////////////////////////////////////
bool CDIJoystick::InitDevice()
{
HRESULT hr;
DIDEVICEINSTANCE diDeviceInstance;
// Just a precaution when Enumerating Devices and button Names if you create
// An options Dialog before creating your main gaming window.
// Then simply use the desktop window, temporarily!
if(!m_hwnd)m_hwnd=::GetDesktopWindow(); // 防止失去焦点,以桌面为主窗口
// Release device if it already exists.
if ( m_lpDIDevice )
{
//if ( m_FFAvailable ) ReleaseEffects();
Acquire( false);
hr=m_lpDIDevice->Release();
if(FAILED(hr))
{
OutputDebugString("Error Releasing Interface in CDIJoystick::InitDevice()\n");
OutputDebugString(GetDIError(hr));
}
else m_lpDIDevice=NULL;
}
// Create game device and set IDirectInputDevice7 interface in m_lpDIDevice.
if(!CreateDevice( &m_JoystickGUID ))
{
OutputDebugString("Failed to create device in CDIJoystick::InitDevice()\n");
return false;
}
// Find out what type it is and set data format accordingly.
diDeviceInstance.dwSize = sizeof( DIDEVICEINSTANCE );
hr=m_lpDIDevice->GetDeviceInfo( &diDeviceInstance );
if ( FAILED( hr ) )
{
OutputDebugString("Failed to obtain device info in CDIJoystick::InitDevice()\n");
OutputDebugString(GetDIError(hr));
return false;
}
// Set the data format to be a Joystick
hr = m_lpDIDevice->SetDataFormat( &c_dfDIJoystick2 );
if ( FAILED( hr ) )
{
OutputDebugString("Failed to create device in CDIJoystick::InitDevice()\n");
OutputDebugString(GetDIError(hr));
return false;
}
// Set cooperative level.
DWORD cl, cl1;
cl = DISCL_EXCLUSIVE;
// Set foreground level for release version, but use background level
// for debugging so we don't keep losing the device when switching to
// a debug window.
//cl1 = DISCL_FOREGROUND;
cl1 = DISCL_BACKGROUND;
#ifdef _DEBUG
cl1 = DISCL_BACKGROUND;
#endif
// now set the co-operation level.
hr=m_lpDIDevice->SetCooperativeLevel( m_hwnd, cl | cl1 );
if ( FAILED( hr ))
{
OutputDebugString( "Failed to set game device cooperative level.\n" );
OutputDebugString(GetDIError(hr));
return false;
}
// Set up the data buffer.
DIPROPDWORD dipdw =
{
// The header.
{
sizeof( DIPROPDWORD ), // diph.dwSize
sizeof( DIPROPHEADER ), // diph.dwHeaderSize
0, // diph.dwObj
DIPH_DEVICE, // diph.dwHow
},
// Number of elements in data buffer.
BUFFERSIZE // dwData
};
hr=m_lpDIDevice->SetProperty( DIPROP_BUFFERSIZE, &dipdw.diph );
if(FAILED(hr))
{
OutputDebugString( "Failed to set up Data Buffer property.\n" );
OutputDebugString(GetDIError(hr));
return false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -