为什么我社始化DirectInput时会出错呢

一个傻冒 2003-07-19 09:59:48
大家好,我是刚开始学习DX。用初始化DirectInput时。一直再出错。为什么?
...全文
72 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaolizi 2003-07-20
  • 打赏
  • 举报
回复
怎么个出错信息啊?我也是刚开始学dx。:)
给出一个简单的dinput封装,是从dxsdk sample中借鉴来的

有一点让我很郁闷的是dinput对于键盘码不是和windows的一致的,而是另外搞了一套,
都是以div_开头的。


class lwDirectInput
{
enum {
IMMEDIATE_BUFFER_SIZE = 8,
DEFAULT_BUFFER_SIZE = 8 ,
KEYBOARD_BUFFER_SIZE = 256
};
public:
LPDIRECTINPUT8 _di;
LPDIRECTINPUTDEVICE8 _did_keyboard;
LPDIRECTINPUTDEVICE8 _did_mouse;

BYTE _keyboard_buf[KEYBOARD_BUFFER_SIZE];
public:
lwDirectInput();
~lwDirectInput();

LPDIRECTINPUTDEVICE8 AttachKeyBoard(LPDIRECTINPUTDEVICE8 did);
LPDIRECTINPUTDEVICE8 DetachKeyBoard();

int Create(HINSTANCE hinst);
int CreateKeyBoard(HWND hwnd,DWORD coop=DISCL_NONEXCLUSIVE|DISCL_FOREGROUND,int be_immediate=1);
int CreateMouse(DWORD coop);

void Destroy();

int ReadBufferData();
inline void AcquireKeyBoard() { if( _did_keyboard ) _did_keyboard->Acquire(); }
inline int GetKeyState(BYTE key) { return _keyboard_buf[key] & 0x80; }

//inline operator LPDIRECTINPUTDEVICE8() { return GetObject(); }
//inline LPDIRECTINPUTDEVICE8 GetObject() { return _did; }

};


lwDirectInput::lwDirectInput()
: _di(0),_did_keyboard(0),_did_mouse(0)
{
memset( _keyboard_buf, 0, sizeof( BYTE ) * KEYBOARD_BUFFER_SIZE );
}

lwDirectInput::~lwDirectInput()
{
Destroy();
}

LPDIRECTINPUTDEVICE8 lwDirectInput::AttachKeyBoard(LPDIRECTINPUTDEVICE8 did)
{
LPDIRECTINPUTDEVICE8 old = _did_keyboard;
_did_keyboard = did;
return old;
}
LPDIRECTINPUTDEVICE8 lwDirectInput::DetachKeyBoard()
{
return AttachKeyBoard( 0 );
}

int lwDirectInput::Create(HINSTANCE hinst)
{
return SUCCEEDED( DirectInput8Create(hinst,DIRECTINPUT_VERSION,IID_IDirectInput8,(VOID**)&_di, NULL ) );
}
int lwDirectInput::CreateKeyBoard(HWND hwnd,DWORD coop,int be_immediate)
{
int ret = 0;
HRESULT hr;
if( FAILED( hr=_di->CreateDevice( GUID_SysKeyboard, &_did_keyboard, NULL ) ) ) {
goto __ret;
}

if( FAILED( hr=_did_keyboard->SetDataFormat( &c_dfDIKeyboard ) ) )
goto __ret;


if( FAILED( hr=_did_keyboard->SetCooperativeLevel( hwnd, coop ) ) ) {
if( hr == DIERR_UNSUPPORTED ) {
MessageBox( 0, "DirectInput Keyboard Device SetCooperativeLevel failure",0,MB_OK );
}
goto __ret;
}

if( !be_immediate ) {
// IMPORTANT STEP TO USE BUFFERED DEVICE DATA!
//
// DirectInput uses unbuffered I/O (buffer size = 0) by default.
// If you want to read buffered data, you need to set a nonzero
// buffer size.
//
// Set the buffer size to DINPUT_BUFFERSIZE (defined above) elements.
//
// The buffer size is a DWORD property associated with the device.
DIPROPDWORD dipdw;

dipdw.diph.dwSize = sizeof(DIPROPDWORD);
dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = IMMEDIATE_BUFFER_SIZE; // Arbitary buffer size

if( FAILED( _did_keyboard->SetProperty( DIPROP_BUFFERSIZE, &dipdw.diph ) ) )
goto __ret;
}

// Acquire the newly created device
_did_keyboard->Acquire();

ret = 1;
__ret:
return ret;
}


void lwDirectInput::Destroy()
{
LW_SAFE_RELEASE( _did_keyboard );
LW_SAFE_RELEASE( _did_mouse );
LW_SAFE_RELEASE( _di );
}

int lwDirectInput::ReadBufferData()
{
if( _did_keyboard == 0 )
return 0;

BYTE buf[KEYBOARD_BUFFER_SIZE];
//hr = _did_keyboard->GetDeviceData( sizeof(DIDEVICEOBJECTDATA),didod, &dwElements, 0 );
HRESULT hr = _did_keyboard->GetDeviceState( KEYBOARD_BUFFER_SIZE, &buf );
if( hr == DI_OK ) {
memcpy( _keyboard_buf, buf, sizeof(BYTE)*KEYBOARD_BUFFER_SIZE );
}
else {
// We got an error or we got DI_BUFFEROVERFLOW.
//
// Either way, it means that continuous contact with the
// device has been lost, either due to an external
// interruption, or because the buffer overflowed
// and some events were lost.
//
// Consequently, if a button was pressed at the time
// the buffer overflowed or the connection was broken,
// the corresponding "up" message might have been lost.
//
// But since our simple sample doesn't actually have
// any state associated with button up or down events,
// there is no state to reset. (In a real game, ignoring
// the buffer overflow would result in the game thinking
// a key was held down when in fact it isn't; it's just
// that the "up" event got lost because the buffer
// overflowed.)
//
// If we want to be cleverer, we could do a
// GetDeviceState() and compare the current state
// against the state we think the device is in,
// and process all the states that are currently
// different from our private state.
hr = _did_keyboard->Acquire();
while( hr == DIERR_INPUTLOST )
hr = _did_keyboard->Acquire();
}

return 1;
}


skull 2003-07-19
  • 打赏
  • 举报
回复
因为程序有错误:)。

8,303

社区成员

发帖
与我相关
我的任务
社区描述
游戏开发相关内容讨论专区
社区管理员
  • 游戏开发
  • 呆呆敲代码的小Y
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧