📄 dijoystick.cpp
字号:
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
// DIJoystick.cpp: implementation of the CDIJoystick class.
//
// Written and Developed by Jason Brooks
// (C) 2000 Jason Brooks
//
// You may use this code freely, however a mention in your credits
// would be nice.
//
// For more information, Bug Reports and so on I can be contacted :-
//
// E-Mail: DirectInput@muckypaws.com
//
// Web: www.muckypaws.com
// ICQ: 9609400
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "DIJoystick.h"
#include <dinput.h>
#define BUFFERSIZE 16
// Set the maxmimum range to which we'll gauge the swing
#define JOYMAX 10000
#define JOYMIN -10000
/* Y
^
|
|
X -10,000 <---*---> +10,000
|
|
\/
*/
// Dead zone is the amount of sway the joystick can have before we start registering movement
// In this case 20%
#define JOYDEAD 2000
// The Saturation Point Is Where the Joystick is deemed to be at Full Swing, in this case 95%
#define JOYSAT 9500
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDIJoystick::CDIJoystick()
{
// Initialise Member Variables
m_EnumerationStarted=false;
m_Initialised=false;
m_hInstance=GetModuleHandle(NULL);
// Initialise Direct Input
Initialise();
// Start Enumeration of Attached Joysticks.
Enumerate_Joysticks();
}
//////////////////////////////////////////////////////////////////////
//
// Destroy The Direct Input Joystick Control and tidy up.
//
//////////////////////////////////////////////////////////////////////
CDIJoystick::~CDIJoystick()
{
Shutdown();
}
//////////////////////////////////////////////////////////////////////
//
// Initialise Direct Input
//
//////////////////////////////////////////////////////////////////////
bool CDIJoystick::Initialise()
{
HRESULT hr;
hr = DirectInput8Create(m_hInstance, DIRECTINPUT_VERSION,
IID_IDirectInput8, (void**)&m_lpDI, NULL);
if FAILED(hr)
{
// DirectInput not available;
// take appropriate action
OutputDebugString("Failed To Initialise Direct Input 9 in CDIJoystick::Initialise\n");
OutputDebugString(GetDIError(hr));
return false;
}
m_hwnd=NULL;
if FAILED(hr)
{
OutputDebugString(GetDIError(hr));
Shutdown();
return false;
}
m_Initialised=true;
return true; // Successfully Created Direct Input 9 Object
}
//////////////////////////////////////////////////////////////////////
//
// Shutdown the Direct input object and release it.
//
// Basically clean up any memory allocated to this object
//
//////////////////////////////////////////////////////////////////////
void CDIJoystick::Shutdown()
{
ClearFriendlyButtonNames();
// Remove Joystick Information
if(!m_DIJoystickList.IsEmpty())
{
POSITION pos=m_DIJoystickList.GetHeadPosition();
LPVOID del=NULL;
while(pos)
{
del=static_cast<LPVOID>(m_DIJoystickList.GetNext(pos));
if(del)
{
delete del;
}
}
m_DIJoystickList.RemoveAll();
}
// Shutdown Direct Input!
if (m_lpDI)
{
if (m_lpDIDevice)
{
// Always unacquire device before calling Release().
try
{
Acquire(false);
m_lpDIDevice->Release();
m_lpDIDevice = NULL;
}
catch(...)
{
OutputDebugString("Failed to Release Pointer in CDIJoystick::Shutdown\n");
}
}
try
{
m_lpDI->Release();
m_lpDI = NULL;
}
catch(...)
{
OutputDebugString("Failed to Release DI7 Pointer in CDIJoystick::Shutdown\n");
}
}
m_Initialised=false;
}
//////////////////////////////////////////////////////////////////////
//
// Start the Enumeration Of Attached Joystick Devices.
//
//////////////////////////////////////////////////////////////////////
bool CDIJoystick::Enumerate_Joysticks()
{
HRESULT hr=NULL;
if(!m_Initialised) Initialise();
if(!m_lpDI) // Has a Direct Input Interface Already Been Initialised?
{
hr = DirectInput8Create(m_hInstance, DIRECTINPUT_VERSION,
IID_IDirectInput8, (void**)&m_lpDI, NULL);
if FAILED(hr)
{
OutputDebugString("Error in CDIJoystick::Enumerate_Joysticks()\n");
OutputDebugString(GetDIError(hr));
return false;
}
}
if(m_lpDI) hr = EnumJoystick();
if FAILED(hr)
{
OutputDebugString("Error in CDIJoystick::Enumerate_Joysticks()\n");
OutputDebugString(GetDIError(hr));
return false;
}
OutputDebugString("Enumerating Joystick Devices\n");
return true;
}
//////////////////////////////////////////////////////////////////////
//
// Add Enumerated Devices To A Link List Object
// Continue Enumeration Of The Device
//
//////////////////////////////////////////////////////////////////////
BOOL CDIJoystick::EnumDevicesProc(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef)
{
CDIJoystick *obj=(CDIJoystick*)(pvRef);
obj->AddDeviceInfo(lpddi);
return DIENUM_CONTINUE;
}
//////////////////////////////////////////////////////////////////////
//
// Add Device Info To The List Of Pointers Held in m_DIJoystickList
//
// return true = Successfully Added
// false = Failed To Add
//
//////////////////////////////////////////////////////////////////////
bool CDIJoystick::AddDeviceInfo(LPCDIDEVICEINSTANCE lpddi)
{
m_EnumerationStarted=true;
LPCDIDEVICEINSTANCE lpdi2=new DIDEVICEINSTANCE;
if(lpdi2)
{
memcpy((void*)lpdi2,lpddi,sizeof(DIDEVICEINSTANCE));
if(m_DIJoystickList.AddHead((void*)lpdi2))return true;
OutputDebugString("Failed To Add Device Info in CDIJoystick::AddDeviceInfo(LPCDIDEVICEINSTANCE lpddi)\n");
}
return false;
}
//////////////////////////////////////////////////////////////////////
//
// Return First Joystick Device Data If Possible
//
// return NULL if Failed or No Joysticks Available
//
//////////////////////////////////////////////////////////////////////
LPCDIDEVICEINSTANCE CDIJoystick::GetFirstJoystickID()
{
if(!m_EnumerationStarted)
{
OutputDebugString("Joystick Have Not Yet Been Enumerated Returning NULL from CDIJoystick::GetFirstJoystickID()\n");
return NULL;
}
if(m_DIJoystickList.IsEmpty())
{
OutputDebugString("Joysticks Have Been Enumerated However None Were Found Attached To This System\n"
"Therefore I am Returning NULL from CDIJoystick::GetFirstJoystickID()\n");
return NULL;
}
m_DevicePOS=m_DIJoystickList.GetHeadPosition();
return GetNextJoystickID();
LPCDIDEVICEINSTANCE info=static_cast<LPCDIDEVICEINSTANCE>(m_DIJoystickList.GetNext(m_DevicePOS));
return info;
}
//////////////////////////////////////////////////////////////////////
//
// Return Next Joystick Device Data If Possible
//
// return NULL if Failed or No Joysticks Available
//
//////////////////////////////////////////////////////////////////////
LPCDIDEVICEINSTANCE CDIJoystick::GetNextJoystickID()
{
if(!m_DevicePOS) return NULL;
return static_cast<LPCDIDEVICEINSTANCE>(m_DIJoystickList.GetNext(m_DevicePOS));
}
//////////////////////////////////////////////////////////////////////
//
// Return First Joystick Button Name Data If Possible
//
// return NULL if Failed or No Joysticks Available
//
//////////////////////////////////////////////////////////////////////
TCHAR* CDIJoystick::GetFirstButtonName()
{
if(!m_EnumerationStarted)
{
OutputDebugString("Joystick Have Not Yet Been Enumerated\nor None attached to this systemReturning NULL from CDIJoystick::GetFirstJoystickID()\n");
return NULL;
}
if(m_DIButtonNames.IsEmpty())
{
OutputDebugString("Joysticks Have Been Enumerated However None Were Found Attached To This System\n"
"Therefore I am Returning NULL from CDIJoystick::GetFirstButtonName()\n");
return NULL;
}
m_ButtonPOS=m_DIButtonNames.GetHeadPosition();
TCHAR* info=static_cast<TCHAR*>(m_DIButtonNames.GetNext(m_ButtonPOS));
return info;
}
//////////////////////////////////////////////////////////////////////
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -