DirectInput Help Please!

Started by
7 comments, last by BrandonisMaster 15 years, 5 months ago
I'm sorry for like 2-3 threads but I need this badly(sort of). Ok here's the code:

#include "dxinput.h"

#define BUTTON_DOWN(obj, button) (obj.rgbButtons[button] & 0x80)

LPDIRECTINPUT8 dinput;
LPDIRECTINPUTDEVICE8 dimouse;
LPDIRECTINPUTDEVICE8 dikeyboard;
LPDIRECTINPUTDEVICE8 dijoystick;
DIMOUSESTATE mouse_state;

char keys[256];

int Init_DirectInput(HWND hWnd)
{
	HRESULT result = DirectInput8Create(
		GetModuleHandle(NULL),
		DIRECTINPUT_VERSION,
		IID_IDirectInput8,
		(void**)&dinput,
		NULL);

	if(result != DI_OK)
		return 0;

	result = dinput->CreateDevice(GUID_SysMouse, &dimouse, NULL);
	if(result != DI_OK)
		return 0;

	result = dinput->CreateDevice(GUID_SysKeyboard, &dikeyboard, NULL);
	if(result != DI_OK)
		return 0;


	return 1;
}

int Init_Mouse(HWND hWnd)
{
	HRESULT result = dimouse->SetDataFormat(&c_dfDIMouse);
	if(result != DI_OK)
		return 0;

	result = dimouse->SetCooperativeLevel(hWnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
	if(result != DI_OK)
		return 0;

	result = dimouse->Acquire();
	if (result != DI_OK)
		return 0;

	return 1;
}
This part is where the debug says its wrong:

HRESULT result = dimouse->SetDataFormat(&c_dfDIMouse);
Someone please help me Thanks, Brandon
Advertisement
Try:

dinput->CreateDevice(GUID_SysMouseEm2, &dimouse, NULL);
Quote:Original post by streamer
Try:

dinput->CreateDevice(GUID_SysMouseEm2, &dimouse, NULL);


That didn't work either. But when I debugged it, I found out that dimouse was a NULL pointer. Any more things you think could be wrong with it?

Brandon
1. Using DirectInput for keyboard or mouse input is a terrible, terrible idea and will just cause your app to break on a large number of systems

2. DirectX functions can return any one of 2 billion possible codes for success, and 2 billion for failure. You're only checking one success code - use SUCCEEDED, not testing for DI_OK.
Quote:Original post by Evil Steve
1. Using DirectInput for keyboard or mouse input is a terrible, terrible idea and will just cause your app to break on a large number of systems

2. DirectX functions can return any one of 2 billion possible codes for success, and 2 billion for failure. You're only checking one success code - use SUCCEEDED, not testing for DI_OK.


Ok, what do I use then???
Btw, my result variable is -858993460. So is there something wrong with this function: HRESULT result = DirectInput8Create(
GetModuleHandle(NULL),
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(void**)&dinput,
NULL);
?
Quote:Original post by BrandonisMaster
Quote:Original post by Evil Steve
1. Using DirectInput for keyboard or mouse input is a terrible, terrible idea and will just cause your app to break on a large number of systems

2. DirectX functions can return any one of 2 billion possible codes for success, and 2 billion for failure. You're only checking one success code - use SUCCEEDED, not testing for DI_OK.


Ok, what do I use then???


Well, WM_MOUSEMOVE, WM_LBUTTONDOWN / UP, WM_RBUTTONDOWN / UP and WM_CHAR , WM_KEYDOWN messages [smile]

mine looks like

LPDIRECTINPUT8       g_pDI; // The DirectInput objectHRESULT hr;g_pDI = NULL;if( FAILED( hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, 		IID_IDirectInput8, (VOID**)&g_pDI, NULL ) ) ){		TraceT("Create DirectInput8 Error."));		return false;}


Try to include this define before dinput include:

#define DIRECTINPUT_VERSION 0x0800
#include<dinput.h>


Handle possible errors, log them, you will much faster find where the bugs are.
For DirectInput8Create these are:
DIERR_BETADIRECTINPUTVERSION
DIERR_INVALIDPARAM
DIERR_OLDDIRECTINPUTVERSION
DIERR_OUTOFMEMORY

what are you getting?
Is it possible the mouse device is NULL because my mouse is USB? I don't think so, but it might have something to do with it.

Thanks,

Brandon

This topic is closed to new replies.

Advertisement