Skip to main content
gamedev.net
🔒 Locked

Ah Direct Input!

Started by Galileo430 7 replies 400+ views
Original Post
Galileo430
Galileo430
    
// INCLUDES========================================================================

#include <windows.h>
#include <dinput.h>

// EXTERNS=========================================================================

extern HINSTANCE	hinstance;
extern HWND			hwnd;
extern UCHAR		buffer[256];

// GLOBALS=========================================================================

LPDIRECTINPUT7 g_DI;
LPDIRECTINPUTDEVICE7 g_KDIDev;
HRESULT	error;

// FUNCTIONS=======================================================================


///////////////////////////////////////////////////////////////////////////////////

// InitDI - Turns on Direct Input												 //

///////////////////////////////////////////////////////////////////////////////////

BOOL InitDI()
{
	// Create Direct Input Object

	if ( DirectInputCreateEx( hinstance, DIRECTINPUT_VERSION, IID_IDirectInput7, (void**)&g_DI, NULL ) )
    {
		return(FALSE);									
    }

    // Create The Keyboard Device

    if ( g_DI->CreateDeviceEx( GUID_SysKeyboard, IID_IDirectInputDevice7, (void**)&g_KDIDev, NULL ) )
    {
	        return(FALSE);		
    }	
	// Set Keyboard Data Format

	error = g_KDIDev->SetDataFormat(&c_dfDIKeyboard);
	if (error)
	{
		return FALSE;
	}

	// Set CoOp Level (with windows not players)
	error = g_KDIDev->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE );
	if (error)
	{
		return FALSE;
	}

	if (g_KDIDev)
	{
		g_KDIDev->Acquire();
	}
	else
	{
		return FALSE;
	}

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////////////

// ShutdownDI - Shuts Off Direct Input (duh!)									   //

/////////////////////////////////////////////////////////////////////////////////////

void ShutdownDI()
{
	if (g_DI)
	{
		if (g_KDIDev)
		{
			g_KDIDev->Unacquire();
			g_KDIDev->Release();
			g_KDIDev = NULL;
		}
		
		g_DI->Release();
		g_DI = NULL;
	}
}

/////////////////////////////////////////////////////////////////////////////////////

// RefreshKeyBoard - Gets Keyboard State			        					   //

/////////////////////////////////////////////////////////////////////////////////////

void RefreshKeyBoard()
{
	g_KDIDev->GetDeviceState(256, buffer);
} 
    
Here is my entire direct input code. Everytime I call RefreshKeyBoard, it crashs. Access Violation 0xC00005. This code fits into a OpenGL engine similar to the NeHe Tutorials but almost completely recoded.
------------------------------------------------------------I wrote the best video game ever, then I woke up...
Jankey
Jankey
if ( g_DI->CreateDeviceEx( GUID_SysKeyboard, IID_IDirectInputDevice7, (void**)&g_KDIDev, NULL ) )


Have you tried to put the "(void **)" out??

If you have some Questions about Dinput OR OpenGL
ask me:

jankey_webhacker@yahoo.de



J.A.N.K.E.Y.: Journeying Artificial Nocturnal Killing and Exploration Youth
Galileo430
Galileo430
Still no luck.
------------------------------------------------------------I wrote the best video game ever, then I woke up...
ColdFlame
ColdFlame
Have you tried DirectX 6? Generally it''s easier to use older, less bloated versions of Microsofts API''s - that actually WORK!

Of course I could be wrong. It''s happened before.
Oh well, Whatever, Nevermind
Galileo430
Galileo430
I''m useing DX 7a.

Anyone heard of some errors in Direct Input 7a?
------------------------------------------------------------I wrote the best video game ever, then I woke up...
furby100
furby100
In those if statements of yours you should be putting != DI_OK or != S_OK after each one, which is in fact zero, so your current function returns immediately whenever a call is sucessful.



Please state the nature of the debugging emergency.


sharewaregames.20m.com

Quantum
Quantum
Nope.. because he''s testing if they are non-zero... and if they are non-zero theres an error..because they''re only zero if it''s successful. The code looks fine to me..its pretty much exactly what I use. Have you tried reinstalling DirectX?
Goz-o
Goz-o
Firstly ALWAYS use HRESULTs ... they make life so much easier ...

Then You can go

HRESULT hr = S_OK;
hr = g_DI->CreateDeviceEx( GUID_SysKeyboard, IID_IDirectInputDevice7, (void**)&g_KDIDev, NULL );
if ( FAILED( hr ) )
return FALSE;

But the main problem is that you have written gibberish. You are testing the exit code to see if it non-zero ... unfortunately S_OK IS 0! So your code will return false if the function succeeds. I suspect this is the root of all your problems.

Microsoft provide 2 standard macros for dealing with HRESULTs

SUCCEEDED( hr ) and FAILED( hr )

surprisingly the dirst returns true if the function "succeeded" and the second returns true if the function "failed".

Using these macros is probbaly the safest way of working it because they will ALWAYS work even if MS change the HRESULTs about.

Goz

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.