Archived

This topic is now archived and is closed to further replies.

KingPin

Reseting DirectInput8???

Recommended Posts

I wanted a reset button, just like on my Playstation. But I think you have to flush the input buffer? But I can't find a flush function ... heres my code. [NOTE: DXTRACE() is just a macro for debugging]
                  
//

#define DX_HELPER_RELEASE(x)	if((x) != NULL) { (x)->Release(); (x) = NULL; }
//

#define DI_HELPER_RELEASE(x)	if((x) != NULL) { (x)->Unacquire(); (x)->Release(); (x) = NULL; }
//

#define KEYDOWN(x,key)		(   (x[key]) & 0x80  )
//

#define KEYUP(x,key)		( !(KEYDOWN(x,key)) )
//

#define NUM_KEYS		256
//

static IDirectInput8		*g_DI		= NULL;
static IDirectInputDevice8	*g_DIDevice	= NULL;
static byte			g_kbBuffer[NUM_KEYS];
//

void INDX_Init()
{
	DXTRACE( DirectInput8Create( sysw_hinst, DIRECTINPUT_VERSION, 
			IID_IDirectInput8, (void**)&g_DI, NULL) );

	DXTRACE( g_DI->CreateDevice( GUID_SysKeyboard, &g_DIDevice, NULL) );

	DXTRACE( g_DIDevice->SetDataFormat( &c_dfDIKeyboard) );

	DXTRACE( g_DIDevice->SetCooperativeLevel( sysw_hwnd, 
				DISCL_FOREGROUND | DISCL_NONEXCLUSIVE) );

	while( g_DIDevice->Acquire() == DIERR_INPUTLOST );
}
//

void INDX_Shutdown()
{
	DI_HELPER_RELEASE(g_DIDevice);
	DX_HELPER_RELEASE(g_DI);
}
//

void INDX_Reset()
{
	INDX_Shutdown();
	INDX_Init();
}
//

void INDX_Frame()
{
	ASSERT(g_DIDevice);

	ZeroMemory((void*)g_kbBuffer, NUM_KEYS);
	if(FAILED( g_DIDevice->GetDeviceState(NUM_KEYS, g_kbBuffer) ))
	{
        	while( g_DIDevice->Acquire() == DIERR_INPUTLOST );
	}

	if(KEYDOWN(g_kbBuffer, DIK_UP))
	{
		GAME_MoveSnake(e_up);
	} 
	else if(KEYDOWN(g_kbBuffer, DIK_DOWN))
	{
		GAME_MoveSnake(e_down);
	}
	
	if(KEYDOWN(g_kbBuffer, DIK_LEFT))
	{
		GAME_MoveSnake(e_left);
	} 
	else if(KEYDOWN(g_kbBuffer, DIK_RIGHT))
	{
		GAME_MoveSnake(e_right);
	}
}

                
After INDX_Reset() is called, and then INDX_Frame() is called for the first time, DirectInput8 thinks that a key was pressed, and calls GAME_MoveSnake(). Even though I haven't pressed any keys for about 5 seconds or 400 frames. Thanks for any help! "1-2GB of virtual memory, that's way more than i'll ever need!" - Bill Gates The Adventure: Quite possibly the game of the century!" - The Gaming Community Edited by - kingpin on December 10, 2001 11:42:23 PM

Share this post


Link to post
Share on other sites