Jump to content
  • Advertisement
Sign in to follow this  
stenny

Mouse is not shown with DirectInput App!

This topic is 4359 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

Hello there, I'm trying to learn DirectInput from my book and I already know how to handle the keyboard. Now I'm trying out the mouse, but whenever I run the app my mouse disappears! Also, the function only important atm are ReadDevice, InitMouse and WinMain. The rest of the functions are for windows classes and more and have nothing to do with DirectInput.
#include <windows.h>
#include <stdio.h>
#include <dinput.h>

HWND		g_hWnd;
HINSTANCE	g_hInst;

#define		WNDWIDTH 400
#define		WNDHEIGHT 400
#define		WNDTYPE WS_OVERLAPPEDWINDOW

const char	g_szClass[] = "EnumClass";
const char	g_szCaption[] = "Enumeration Demo";

int WINAPI	WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow);
void		AppError(BOOL Fatal, char *Text, ...);
LRESULT		CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL		RegisterWindowClasses(HINSTANCE hInst);
BOOL		UnregisterWindowClasses(HINSTANCE hInst);
HWND		CreateMainWindow(HINSTANCE hInst);
BOOL		CALLBACK EnumDevices(LPCDIDEVICEINSTANCE pdInst, LPVOID pvRef);

BOOL		DoInit();
BOOL		DoShutdown();
BOOL		DoPreFrame();
BOOL		DoFrame();
BOOL		DoPostFrame();

IDirectInputDevice8 *InitMouse(HWND hWnd, IDirectInput8 *pDI);
BOOL ReadDevice(IDirectInputDevice8 *pDIDevice, void *DataBuffer, long BufferSize);

IDirectInput8		*g_pDI;
IDirectInputDevice8	*g_pDIDevice;

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow)
{
	MSG Msg;
	DIMOUSESTATE MouseState;
	char KeyStateBuffer[256];
	long XPos = 0, YPos = 0;

	g_hInst = hInst;

	if(RegisterWindowClasses(hInst) == FALSE) { return FALSE; }

	if((g_hWnd = CreateMainWindow(hInst)) == NULL) { return FALSE; }

	DirectInput8Create(g_hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&g_pDI, NULL);
	g_pDIDevice = InitMouse(g_hWnd, g_pDI);

	if(DoInit() == TRUE)
	{
		ZeroMemory(&Msg, sizeof(MSG));
		while(Msg.message != WM_QUIT)
		{
			if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
			{
				TranslateMessage(&Msg);
				DispatchMessage(&Msg);
			}
			ReadDevice(g_pDIDevice, (void*)&MouseState, sizeof(DIMOUSESTATE));
			XPos += MouseState.lX;
			YPos += MouseState.lY;
		}
	}

	g_pDIDevice->Unacquire();
	g_pDIDevice->Release();
	g_pDI->Release();

	DoShutdown();
	UnregisterWindowClasses(hInst);

	return TRUE;
}

BOOL RegisterWindowClasses(HINSTANCE hInst)
{
	WNDCLASSEX wcex;
	wcex.cbSize        = sizeof(wcex);
	wcex.style         = CS_CLASSDC;
	wcex.lpfnWndProc   = WindowProc;
	wcex.cbClsExtra    = 0;
	wcex.cbWndExtra    = 0;
	wcex.hInstance     = hInst;
	wcex.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	wcex.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = NULL;
	wcex.lpszMenuName  = NULL;
	wcex.lpszClassName = g_szClass;
	wcex.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wcex)) { return FALSE; }

	return TRUE;
}

BOOL UnregisterWindowClasses(HINSTANCE hInst)
{
	UnregisterClass(g_szClass, hInst);
	return TRUE;
}

HWND CreateMainWindow(HINSTANCE hInst)
{
	HWND hWnd;

	hWnd = CreateWindow(g_szClass, g_szCaption, WNDTYPE, 0, 0, WNDWIDTH, WNDHEIGHT, NULL, NULL, hInst, NULL);

	if(!hWnd) { return NULL; }

	ShowWindow(hWnd, SW_SHOWNORMAL);
	UpdateWindow(hWnd);

	return hWnd;
}

void AppError(BOOL Fatal, char *Text, ...)
{
	char CaptionText[12];
	char ErrorText[2048];
	va_list valist;

	if(Fatal == FALSE)
	{
		strcpy(CaptionText, "Error");
	} else {
		strcpy(CaptionText, "Fatal Error");
	}

	va_start(valist, Text);
	vsprintf(ErrorText, Text, valist);
	va_end(valist);

	MessageBox(NULL, ErrorText, CaptionText, MB_OK | MB_ICONEXCLAMATION);

	if(Fatal == TRUE)
	{
		PostQuitMessage(0);
	}
}

HRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}

	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

BOOL DoInit()
{
	return TRUE;
}

BOOL DoShutdown()
{
	return TRUE;
}

BOOL DoPreFrame()
{
	return TRUE;
}

BOOL DoFrame()
{
	return TRUE;
}

BOOL DoPostFrame()
{
	return TRUE;
}

IDirectInputDevice8 *InitMouse(HWND hWnd, IDirectInput8 *pDI)
{
	IDirectInputDevice8		*pDIDevice;

	if(FAILED(pDI->CreateDevice(GUID_SysMouse, &pDIDevice, NULL))) { return NULL; }
	if(FAILED(pDIDevice->SetDataFormat(&c_dfDIMouse)))
	{
		pDIDevice->Release();
		return NULL;
	}
	if(FAILED(pDIDevice->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE)))
	{
		pDIDevice->Release();
		return NULL;
	}
	if(FAILED(pDIDevice->Acquire()))
	{
		pDIDevice->Release();
		return NULL;
	}

	return pDIDevice;
}
BOOL ReadDevice(IDirectInputDevice8 *pDIDevice, void *DataBuffer, long BufferSize)
{
	while(1)
	{
		HRESULT hr;

		g_pDIDevice->Poll();

		if(SUCCEEDED(hr = pDIDevice->GetDeviceState(BufferSize, (LPVOID)DataBuffer))) { break; }
		if(hr != DIERR_INPUTLOST && hr != DIERR_NOTACQUIRED) { return FALSE; }
		if(FAILED(g_pDIDevice->Acquire())) { return FALSE; }
	}

	return TRUE;
}





I know there are some useless functions like DoFrame, but it's just created from a FrameWork. Does anyone know how to solve this? -Stenny

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by stenny
Also, the function only important atm are ReadDevice, InitMouse and WinMain. The rest of the functions are for windows classes and more and have nothing to do with DirectInput.

You could post just those functions, no need to post all your code.

As for your question, you are creating the Input Device with the Exclusive flag. This means that only your application has access to the input device from now on, and even Windows can't access it. Since Windows can't get mouse data, it acts as though no mouse is available, and hides it.
Try using the Non-Exclusive flag, and that should fix it.

Hope this helps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!