|
|
我的程序出了问题
以下是简化版的程序代码
但也说不出是那里出了毛病
有人可以告诉我到底哪里出了错?
#include "dinput.h"
#include "windows.h"
#define KEYDOWN( name, key ) ( name[ key ] & 0x80)
HWND g_hWnd = 0;
HINSTANCE g_hInstance = 0;
IDirectInput8 *g_pDInputInterface; //dinput interface
IDirectInputDevice8 *g_pKeyboardDevice; //keyboard device
unsigned char *g_auchKeyboardState = new unsigned char[ 256 ];
LRESULT CALLBACK Wnd_Proc( HWND hWnd_, unsigned int uiMsg_, WPARAM wParam_, LPARAM lParam_ )
{
switch ( uiMsg_ )
{
case WM_DESTROY:
{
PostQuitMessage( 0 );
break;
}
case WM_ACTIVATE:
{
if ( 0 != g_pKeyboardDevice )
{
g_pKeyboardDevice->Acquire();
}
break;
}
case WM_PAINT:
{
ValidateRect( hWnd_, NULL );
break;
}
}
return DefWindowProc( hWnd_, uiMsg_, wParam_, lParam_ );
}
void CheckKeyboard( void )
{
ZeroMemory( g_auchKeyboardState, sizeof( unsigned char [ 256 ] ) );
long lResult = g_pKeyboardDevice->GetDeviceState( sizeof( g_auchKeyboardState ),
( void * ) &g_auchKeyboardState );
if ( FAILED( lResult ) )
{
lResult = g_pKeyboardDevice->Acquire( );
while ( DIERR_INPUTLOST == lResult )
{
lResult = g_pKeyboardDevice->Acquire( );
}
if ( DIERR_OTHERAPPHASPRIO == lResult || DIERR_NOTACQUIRED == lResult )
{
return;
}
}
for( int i = 0; i < 256; i++ )
{
if( g_auchKeyboardState & 0x80 )
{
PostQuitMessage( 0 );
}
}
}
int WINAPI WinMain( HINSTANCE hInstance_, HINSTANCE hPrevInstance_,
char* szCmdLine_, int iShowCmd_ )
{
//register the class with default value
WNDCLASS WndClass = { 0, Wnd_Proc, 0, 0, hInstance_,
0, 0, ( HBRUSH )GetStockObject( BLACK_BRUSH ), NULL, "Netmez! DInput" };
RegisterClass( &WndClass );
//adjust window size
RECT WinRectStruct;
SetRect( &WinRectStruct, 0, 0, 400, 300 );
AdjustWindowRect( &WinRectStruct, WS_OVERLAPPEDWINDOW, false );
//create window
g_hWnd = CreateWindow( "Netmez! DInput", "DInput", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, 400, 300, 0, 0, g_hInstance, 0 );
ShowWindow( g_hWnd, true );
UpdateWindow( g_hWnd );
if ( 0 != g_pDInputInterface )
{
return 0;
}
DirectInput8Create( hInstance_, DIRECTINPUT_VERSION, IID_IDirectInput8,
( void** ) &g_pDInputInterface, 0 );
//if dinput interface is still 0, that means dinput create failed,
//so post error message and quit
if ( 0 == g_pDInputInterface )
{
PostQuitMessage( 0 );
}
g_pDInputInterface->CreateDevice( GUID_SysKeyboard, &g_pKeyboardDevice, 0 );
//if keyboard device is 0, that means device create failed,
//so post error message and quit
if ( 0 == g_pKeyboardDevice )
{
PostQuitMessage( 0 );
}
//set up keyboard device
g_pKeyboardDevice->SetDataFormat( &c_dfDIKeyboard );
g_pKeyboardDevice->SetCooperativeLevel( g_hWnd, DISCL_BACKGROUND| DISCL_NONEXCLUSIVE );
g_pKeyboardDevice->Acquire( );
for ( MSG MsgStruct ; ; )
{
//if there is a message
if ( 0 != PeekMessage( &MsgStruct, 0, 0, 0, PM_REMOVE ) )
{
//if user request quit
if ( WM_QUIT == MsgStruct.message )
{
PostQuitMessage( 0 );
break;
}
TranslateMessage( &MsgStruct );
DispatchMessage( &MsgStruct );
}
else
{
CheckKeyboard( );
}
}
UnregisterClass( "Netmez! DInput", hInstance_ );
delete [ ] g_auchKeyboardState;
//if the keyboard device not 0, that means device has been initialize,
//so unacquire and release device
if ( 0 != g_pKeyboardDevice )
{
g_pKeyboardDevice->Unacquire( );
g_pKeyboardDevice->Release( );
}
//if the dinput interface not 0, that means interface has initialize,
//so release the interface
if ( 0 != g_pDInputInterface )
{
g_pDInputInterface->Release( );
}
return 0;
}
[em7] |
|