Jump to content
  • Advertisement

Archived

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

Jejor

DInput8 Acquiring Joystick, but Not Unaquiring

This topic is 5980 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here''s the code:
  
LPDIRECTINPUT8 lpdi = NULL;
LPDIRECTINPUTDEVICE8 lpdidKeyboard = NULL;
struct JOYSTICK
{
	LPDIRECTINPUTDEVICE8 lpdidJoystick;
	LPDIJOYSTATE2 lpdijs;
	JOYSTICK *Next;
};
JOYSTICK* Joystick = NULL;

bool InitDI()
{
	if(FAILED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**) &lpdi, NULL)))
		return false;
	if(FAILED(lpdi->CreateDevice(GUID_SysKeyboard, &lpdidKeyboard, NULL)))
	{
		lpdi->Release();
		return false;
	}
	if(FAILED(lpdidKeyboard->SetDataFormat(&c_dfDIKeyboard)))
	{
		lpdidKeyboard->Release();
		lpdi->Release();
		return false;
	}
	if(FAILED(lpdidKeyboard->SetCooperativeLevel(g_hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
	{
		lpdidKeyboard->Release();
		lpdi->Release();
		return false;
	}
	if(FAILED(lpdidKeyboard->Acquire()))
	{
		lpdidKeyboard->Release();
		lpdi->Release();
		return false;
	}

	if(FAILED(lpdi->EnumDevices(DI8DEVCLASS_GAMECTRL, EnumJoysticks, NULL, DIEDFL_ATTACHEDONLY)))
	{
		lpdidKeyboard->Unacquire();
		lpdidKeyboard->Release();
		lpdi->Release();
		return false;
	}
	return true;
}

BOOL CALLBACK EnumJoysticks(const DIDEVICEINSTANCE *pdidInstance, VOID* pNothing)
{
	JOYSTICK** Last = &Joystick;
	if((*Last) == NULL)
	{
		Joystick = new JOYSTICK;
		Last = &Joystick;
	} else {
		while((*Last)->Next != NULL)
		{
			Last = &(*Last)->Next;
		}
		(*Last)->Next = new JOYSTICK;
		Last = &(*Last)->Next;
		(*Last)->lpdidJoystick = NULL;
		(*Last)->lpdijs = NULL;
	}
	(*Last)->Next = NULL;
	if(FAILED(lpdi->CreateDevice(pdidInstance->guidInstance, &(*Last)->lpdidJoystick, NULL)))
	{
		delete (*Last);
		Last = NULL;
		return DIENUM_CONTINUE;
	}

	if(FAILED((*Last)->lpdidJoystick->SetDataFormat(&c_dfDIJoystick2)))
	{
		(*Last)->lpdidJoystick->Release();
		delete (*Last);
		(*Last) = NULL;
		return DIENUM_CONTINUE;
	}

	if(FAILED((*Last)->lpdidJoystick->SetCooperativeLevel(g_hwnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND)))
	{
		(*Last)->lpdidJoystick->Release();
		delete (*Last);
		(*Last) = NULL;
		return DIENUM_CONTINUE;
	}

	DIDEVCAPS diDevCaps;
	diDevCaps.dwSize = sizeof(diDevCaps);
	if(FAILED((*Last)->lpdidJoystick->GetCapabilities(&diDevCaps)))
	{
		(*Last)->lpdidJoystick->Release();
		delete Last;
		(*Last) = NULL;
		return DIENUM_CONTINUE;
	}

	if (FAILED((*Last)->lpdidJoystick->EnumObjects(EnumAxes, NULL, DIDFT_AXIS)))
	{
		(*Last)->lpdidJoystick->Release();
		delete Last;
		(*Last) = NULL;
		return DIENUM_CONTINUE;
	}

	return DIENUM_CONTINUE;	
}

BOOL CALLBACK EnumAxes(const DIDEVICEOBJECTINSTANCE* Instance, VOID* context)
{
    DIPROPRANGE propRange; 
    propRange.diph.dwSize       = sizeof(DIPROPRANGE); 
    propRange.diph.dwHeaderSize = sizeof(DIPROPHEADER); 
    propRange.diph.dwHow        = DIPH_BYID; 
    propRange.diph.dwObj        = Instance->dwType;
    propRange.lMin              = -1; 
    propRange.lMax              = +1;

	JOYSTICK* Last = Joystick;
	while(Last->Next != NULL)
	{
		Last = Last->Next;
	}
	
	if(FAILED(Last->lpdidJoystick->SetProperty(DIPROP_RANGE, &propRange.diph)))
	{
		return DIENUM_STOP;
	}
	return DIENUM_CONTINUE;
}

bool GetInput()
{
	nKeys = 256;
	char buffer[256];
	HRESULT hr;

	if (FAILED(lpdidKeyboard->GetDeviceState(256, buffer)))
	{
		hr = lpdidKeyboard->Acquire();
	    while(hr == DIERR_INPUTLOST)
			hr = lpdidKeyboard->Acquire();
		if(FAILED(hr) && hr != DIERR_OTHERAPPHASPRIO)
			return false;
		if(FAILED(lpdidKeyboard->GetDeviceState(256, buffer)))
			return false;
	}
	JOYSTICK* Last = Joystick;
//	while(Last != NULL)

//	{

		if (FAILED(Last->lpdidJoystick->Poll())) 
		{
			hr = Last->lpdidJoystick->Acquire(); <--Causes Error
//		    while(hr == DIERR_INPUTLOST)

//				hr = Last->lpdidJoystick->Acquire();

//			if(FAILED(hr) && hr != DIERR_OTHERAPPHASPRIO)

//				return false;

		}

//		if(FAILED(Last->lpdidJoystick->GetDeviceState(sizeof(DIJOYSTATE2), Last->lpdijs)))

//			return false;

//		nKeys += 42;

//		Last = Last->Next;

//	}


	if(bKeyState != NULL)
		delete bKeyState;
	bKeyState = new bool[nKeys];
	for(int x=0;x<256;x++)

		bKeyState[x] = (buffer[x] != 0);
	return true;
}

void CleanUpDI()
{
//	JOYSTICK* Temp;

//	while(Joystick != NULL)

//	{

//		Temp = Joystick->Next;

		Joystick->lpdidJoystick->Unacquire();
		Joystick->lpdidJoystick->Release();
		delete Joystick;
//		Joystick = Temp;

//	}

	lpdidKeyboard->Unacquire();
	lpdidKeyboard->Release();
	lpdi->Release();
}  
All I''m doing is acquiring the keyboard and all the joysticks, calling GetInput() to buffer the input, then calling CleanUpDI() I''m new to Direct X, so it could be simple. Anyways, when I run the program, call GetInput, and exit, I get the error message: "DINPUT8: unloaded before all objects released. (cRef:1)" I tried to comment some of the code so that only the first Joystick is used. And I tracked the problem down to acquiring the Joystick. Is there a problem with unacquiring the Joystick? Also, I''m getting the error: "DINPUT8: dwPmax == dwPc (0). Possible a bug." four times when I run it. Is that anything to worry about? Thanks

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!