首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络游戏 >

游戏杆有关问题,可以编译过,但是。

2012-02-08 
游戏杆问题,可以编译过,但是。。。。。。不插游戏杆运行的时候就出错,插了就没事。BOOLInitJoystick(intiMin,inti

游戏杆问题,可以编译过,但是。。。。。。
不插游戏杆运行的时候就出错,插了就没事。
BOOL   InitJoystick(int   iMin,   int   iMax,   int   iDeadZone)
{
hr   =   DirectInput8Create(g_hInst,DIRECTINPUT_VERSION,IID_IDirectInput8,(void**)&pIDirectInput,NULL);
if   (FAILED(hr))  
{
MessageBox(NULL, "建立接口对象失败 ", "   ",MB_OK|MB_ICONINFORMATION);
return   false;
}
hr   =   pIDirectInput-> EnumDevices(DI8DEVCLASS_GAMECTRL,EnumJSCallback,&pIDirectInputDevice,DIEDFL_ATTACHEDONLY);
if   (FAILED(hr))
{
MessageBox(NULL, "枚举游戏杆失败 ", "   ",MB_OK|MB_ICONINFORMATION);
return   false;
}
if   (FAILED(pIDirectInputDevice   ==   NULL))
{
MessageBox(NULL, "没有安装游戏杆 ", "   ",MB_OK|MB_ICONINFORMATION);
return   false;
}
hr   =   pIDirectInputDevice-> SetDataFormat(&c_dfDIJoystick2);
if   (FAILED(hr))
{
MessageBox(NULL, "设置游戏杆数据读取数据格式失败 ", "   ",MB_OK|MB_ICONINFORMATION);
SafeRelease(pIDirectInputDevice);
SafeRelease(pIDirectInput);
return   false;
}
hr   =   pIDirectInputDevice-> SetCooperativeLevel(g_hWnd,DISCL_FOREGROUND|DISCL_NONEXCLUSIVE);
if   (FAILED(hr))
{
MessageBox(NULL, "设置游戏杆协调及别失败 ", "   ",MB_OK|MB_ICONINFORMATION);
SafeRelease(pIDirectInputDevice);
SafeRelease(pIDirectInput);
return   false;
}
dipROPRANGE.diph.dwSize   =   sizeof(DIPROPRANGE);
dipROPRANGE.diph.dwHeaderSize   =   sizeof(DIPROPHEADER);
dipROPRANGE.diph.dwHow   =   DIPH_DEVICE;
dipROPRANGE.diph.dwObj   =   0;
dipROPRANGE.lMax   =   iMax;
dipROPRANGE.lMin   =   iMin;
hr   =   pIDirectInputDevice-> SetProperty(DIPROP_RANGE,&dipROPRANGE.diph);
if   (FAILED(hr))
{
MessageBox(NULL, "设置游戏杆轴范围属性失败 ", "   ",MB_OK|MB_ICONINFORMATION);
SafeRelease(pIDirectInputDevice);
SafeRelease(pIDirectInput);
return   false;
}
dipROPDWORD.diph.dwSize   =   sizeof(DIPROPDWORD);
dipROPDWORD.diph.dwHeaderSize   =   sizeof(DIPROPHEADER);
dipROPDWORD.diph.dwHow   =   DIPH_DEVICE;
dipROPDWORD.diph.dwObj   =   0;
dipROPDWORD.dwData   =   iDeadZone;
hr   =   pIDirectInputDevice-> SetProperty(DIPROP_DEADZONE,&dipROPDWORD.diph);
if   (FAILED(hr))
{
MessageBox(NULL, "设置无效范围属性失败 ", "   ",MB_OK|MB_ICONINFORMATION);
SafeRelease(pIDirectInputDevice);
SafeRelease(pIDirectInput);
return   false;
}
ZeroMemory(&diJOYSTATE2,sizeof(DIJOYSTATE2));
return   true;
}

void   CaptureJoystick()
{
hr   =   pIDirectInputDevice-> Acquire();
if   (FAILED(hr))
{
MessageBox(NULL, "取得游戏杆的访问权失败 ", "   ",MB_OK|MB_ICONINFORMATION);
SafeRelease(pIDirectInputDevice);
SafeRelease(pIDirectInput);
return;
}
hr   =   pIDirectInputDevice-> Poll();
if   (FAILED(hr))
{
MessageBox(NULL, "检测游戏杆失败 ", "   ",MB_OK|MB_ICONINFORMATION);
SafeRelease(pIDirectInputDevice);
SafeRelease(pIDirectInput);
return;
}
hr   =   pIDirectInputDevice-> GetDeviceState(sizeof(DIJOYSTATE2),&diJOYSTATE2);
if   (hr   ==   DIERR_INPUTLOST)
{
pIDirectInputDevice-> Acquire();


hr   =   pIDirectInputDevice-> GetDeviceState(sizeof(DIJOYSTATE2),&diJOYSTATE2);
if   (FAILED(hr))
{
MessageBox(NULL, "获取游戏杆数据失败 ", "   ",MB_OK|MB_ICONINFORMATION);
SafeRelease(pIDirectInputDevice);
SafeRelease(pIDirectInput);
return;
}

}
}

[解决办法]
因为在程序中要枚举你的系统所安装的游戏杆……所以在装有游戏杆时编译没有问题……
hr = pIDirectInput-> EnumDevices(DI8DEVCLASS_GAMECTRL,EnumJSCallback,&pIDirectInputDevice,DIEDFL_ATTACHEDONLY);
if (FAILED(hr))
{
MessageBox(NULL, "枚举游戏杆失败 ", " ",MB_OK|MB_ICONINFORMATION);
return false;
}
DIEDFL_ATTACHEDONLY就是只枚举时枚举你所安装的游戏设备。当你没有安装游戏设备时上面的代码是不会成功的。

热点排行