Win32 input crash

Started by
2 comments, last by Vortez 10 years, 6 months ago

Hi

I've recently moved from directInput to using win32 for input and I've been experienced some hold ups in my program.
The window stops responding for a split second and works again, And the longer the program runs the longer the stops gets.
If the program loses focus and gets it back then the program stops responding completely and has to be terminated.
I'm suspecting it's the game loop and/or use of peekmessage()!

GameLoop


MSG msg;
bool gameRunning = true;
uint lastFrame = frameTimeStamp;

while(gameRunning)
{	
	lastFrameTimeStamp = frameTimeStamp;
	frameTimeStamp = timeGetTime();		

	if(frameTimeStamp - lastFrame > 17)
	{
		elapsedTime = (float)(frameTimeStamp - lastFrame) * 0.001f;

		input.Update();

		while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
			{
				gameRunning = false;
				break;
			}

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		game.Update();
		game.Render();

		lastFrame = frameTimeStamp;
	}
}

input class:


void HInput::Update()
{
	if (!wndHandle)
		return;

	keyEventCount = 0;
	mouseEventCount = 0;
	MSG msg;

	// shift current keystates to last keystates
	memcpy(lastKeyStates, currentKeyStates, sizeof(bool) * INPUT_KEY_COUNT);

	// intercept input messages from the window
	while (PeekMessage(&msg, wndHandle, WM_KEYFIRST , WM_KEYLAST, PM_REMOVE))
	{
		TranslateMessage(&msg);

		INPUT_KEY key = TranslateWin32Key(msg.wParam);

		INPUT_KEY button;
		int x = GET_X_LPARAM(msg.lParam);
		int y = GET_Y_LPARAM(msg.lParam);
		
		switch(msg.message)
		{
		case WM_KEYDOWN:
			if (key == KEY_UNSUPPORTED)
				break;

			SET_KEY(key);
			AddKeyEvent(key);
			break;
		case WM_KEYUP:
			if (key == KEY_UNSUPPORTED)
				break;

			CLEAR_KEY(key);
			break;
		case WM_CHAR:
			AddCharEvent(msg.wParam);
			break;
		}
	}

	POINT pos;
	GetCursorPos(&pos);
	ScreenToClient(wndHandle, &pos);
	
	deltaX = pos.x - mouseX;
	deltaY = pos.y - mouseY;

	mouseX = pos.x;
	mouseY = pos.y;

	while (PeekMessage(&msg, wndHandle, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE))
	{
		INPUT_KEY button;
		int x = GET_X_LPARAM(msg.lParam);
		int y = GET_Y_LPARAM(msg.lParam);		

		switch(msg.message)
		{
		case WM_LBUTTONDOWN:
			SET_KEY(MOUSE_LMB);
			AddMouseEvent(IMET_CLICK, MOUSE_LMB, x, y);
			break;
		case WM_LBUTTONUP:
			CLEAR_KEY(MOUSE_LMB);
			AddMouseEvent(IMET_RELEASE, MOUSE_LMB, x, y);
			break;
		case WM_LBUTTONDBLCLK:
			AddMouseEvent(IMET_DOUBLECLICK, MOUSE_LMB, x, y);
			break;
		case WM_RBUTTONDOWN:
			SET_KEY(MOUSE_RMB);
			AddMouseEvent(IMET_CLICK, MOUSE_RMB, x, y);
			break;
		case WM_RBUTTONUP:
			CLEAR_KEY(MOUSE_RMB);
			AddMouseEvent(IMET_RELEASE, MOUSE_RMB, x, y);
			break;
		case WM_RBUTTONDBLCLK:
			AddMouseEvent(IMET_DOUBLECLICK, MOUSE_RMB, x, y);
			break;
		case WM_MBUTTONDOWN:
			SET_KEY(MOUSE_MMB);
			AddMouseEvent(IMET_CLICK, MOUSE_MMB, x, y);
			break;
		case WM_MBUTTONUP:
			CLEAR_KEY(MOUSE_MMB);
			AddMouseEvent(IMET_RELEASE, MOUSE_MMB, x, y);
			break;
		case WM_MBUTTONDBLCLK:
			AddMouseEvent(IMET_DOUBLECLICK, MOUSE_MMB, x, y);
			break;
		case WM_XBUTTONDOWN:	// for both x buttons
			if (HIWORD(msg.wParam) & XBUTTON1)
				button = MOUSE_M4B;
			else if (HIWORD(msg.wParam) & XBUTTON2)
				button = MOUSE_M5B;

			SET_KEY(button);
			AddMouseEvent(IMET_CLICK, button, x, y);
			break;
		case WM_XBUTTONUP:
			if (HIWORD(msg.wParam) & XBUTTON1)
				button = MOUSE_M4B;
			else if (HIWORD(msg.wParam) & XBUTTON2)
				button = MOUSE_M5B;

			CLEAR_KEY(button);
			AddMouseEvent(IMET_RELEASE, button, x, y);
			break;
		case WM_XBUTTONDBLCLK:
			if (HIWORD(msg.wParam) & XBUTTON1)
				button = MOUSE_M4B;
			else if (HIWORD(msg.wParam) & XBUTTON2)
				button = MOUSE_M5B;

			AddMouseEvent(IMET_DOUBLECLICK, button, x, y);
			break;
		}
	}
}

EnJOJ Gaming
Advertisement

First of all, you're gameloop is a bit wrong, it should be


MSG msg;
bool gameRunning = true;
uint lastFrame = frameTimeStamp;

while(gameRunning)
{	
	lastFrameTimeStamp = frameTimeStamp;
	frameTimeStamp = timeGetTime();		

	if(frameTimeStamp - lastFrame > 17)
	{
		elapsedTime = (float)(frameTimeStamp - lastFrame) * 0.001f;

		input.Update();

		while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
			{
				gameRunning = false;
				break;
			}

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		} else {
		        game.Update();
		        game.Render();

    		        lastFrame = frameTimeStamp;
                }

	}
}

EDIT: this code wont work because you have 2 nested while loop, so the break wont work when you break on the quit message. I suggest implementing the timing stuff in it's own timer class.

Here's mine:


//-----------------------------------------------------------------------------
// The game loop...
//-----------------------------------------------------------------------------
void CVortez3DEngine::GameLoop()
{
	MSG msg;
	bool done = false;

	while(!done){
		if(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)){
			
			// If a quit message is received then stop the loop
			if(msg.message == WM_QUIT)
				done = true;
			
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		} else {
			// Update the scene and render a frame
			UpdateScene(EngineTimer.Tick());
    		        RenderScene();
		}
	}
}

Then, i dont understand your input class. You're calling peekmessage again, and you're not even using a windowproc function. You're making this stuff way more complicated than it should.

You should process message in your wndproc function, the one you passed to CreateWindowEx(). Ex:


//-----------------------------------------------------------------------------
// Process the windows messages
//-----------------------------------------------------------------------------
LRESULT CALLBACK MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	//Process quit message
	case WM_DESTROY:
	case WM_CLOSE:
		PostQuitMessage(0);
		break;	

	//Screensaver messages
	case WM_SYSCOMMAND:			// Intercept System Commands
		switch (wParam)			// Check System Calls
		{
		case SC_SCREENSAVE:		// Screensaver Trying To Start?
		case SC_MONITORPOWER:	// Monitor Trying To Enter Powersave?
			return 0;			// Prevent From Happening
		}
		break;		
	
	// Resize message
	case WM_SIZE:
		pVortez3DEngine->GameWindow.UpdateRects();
		pVortez3DEngine->OnGameWindowResized(LOWORD(lParam), HIWORD(lParam));
		break;

	// Keyboard messages, for text...
	case WM_CHAR:
		pVortez3DEngine->OnChar((UINT)wParam);
		break;	

	// Keyboard messages, , for keys...
	case WM_KEYDOWN:
		pKeyState[wParam] = true;
		pVortez3DEngine->OnKeyDown((UINT)wParam);
		break;	
	case WM_KEYUP:
		pKeyState[wParam] = false;
		pVortez3DEngine->OnKeyUp((UINT)wParam);
		break;

	// Mouse move messages
	case WM_MOUSEMOVE:
		pVortez3DEngine->OnMouseMove(LOWORD(lParam), HIWORD(lParam));
		break;
	
	// Mouse buttons messages
	case WM_LBUTTONDOWN:
		pMouseBtnState[LEFT_MOUSE_BUTTON] = true;
		pVortez3DEngine->OnMouseDown(LEFT_MOUSE_BUTTON, (int)LOWORD(lParam), (int)HIWORD(lParam));
		break;
	case WM_LBUTTONUP:
		pMouseBtnState[LEFT_MOUSE_BUTTON] = false;
		pVortez3DEngine->OnMouseUp(LEFT_MOUSE_BUTTON, (int)LOWORD(lParam), (int)HIWORD(lParam));
		break;
	case WM_MBUTTONDOWN:
		pMouseBtnState[MIDDLE_MOUSE_BUTTON] = true;
		pVortez3DEngine->OnMouseDown(MIDDLE_MOUSE_BUTTON, (int)LOWORD(lParam), (int)HIWORD(lParam));
		break;
	case WM_MBUTTONUP:
		pMouseBtnState[MIDDLE_MOUSE_BUTTON] = false;
		pVortez3DEngine->OnMouseUp(MIDDLE_MOUSE_BUTTON, (int)LOWORD(lParam), (int)HIWORD(lParam));
		break;
	case WM_RBUTTONDOWN:
		pMouseBtnState[RIGHT_MOUSE_BUTTON] = true;
		pVortez3DEngine->OnMouseDown(RIGHT_MOUSE_BUTTON, (int)LOWORD(lParam), (int)HIWORD(lParam));
		break;
	case WM_RBUTTONUP:
		pMouseBtnState[RIGHT_MOUSE_BUTTON] = false;
		pVortez3DEngine->OnMouseUp(RIGHT_MOUSE_BUTTON, (int)LOWORD(lParam), (int)HIWORD(lParam));
		break;

	//Mouse wheel message
	case WM_MOUSEWHEEL:
		{
			int RollCount = (signed short)HIWORD(wParam) / WHEEL_DELTA;
			pVortez3DEngine->OnMouseRoll(RollCount);
		}
		break;

	// Process other messages
	default: 
		OnCustomMessage(uMsg, wParam, lParam);
		break;
	}

	//Return result to windows
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}


This might seem complicated at first, but it's the code i use in my 3d engine so it has to be a little big, still, values are just read from wParam and lParam, and the values depend on the message.

Basically, im doing something like this:


LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_SIZE: 
        {
            int width = LOWORD(lParam);  // Macro to get the low-order word.
            int height = HIWORD(lParam); // Macro to get the high-order word.

            // Respond to the message:
            OnSize(hwnd, (UINT)wParam, width, height);
        }
        break;
        
    }
}

void OnSize(HWND hwnd, UINT flag, int width, int height);
{
    // Handle resizing
}

There's no need for a second loop with a second PeekMessage and no need either to use TranslateWin32Key... That function should be called automatically (by windows) every time DispatchMessage is called in your game loop.

For more information, i suggest reading that.

And if you want a good timer class, use this. It's very simple, everytime you call Tick(), it return the elapsed time, in second. For example, if 0.1 second has pass since the lass call to Tick(), you get 0.1 returned... And it use a high presicion timer, the one you're using is not very accurate. As a bonus, If you call Tick() once per frame, you can call GetFPS() whenever you want to draw the number of frame per second.

Ok, thanks for the tips.
But I want to make the input code separated from the callback function.

And I still have no clue why it causes my program to stall from time to time.

EnJOJ Gaming

If you want the input code encapsulated, just do as i did and use a global pointer to access your input class. I know globals are generally bad but in this case i think it's nice to make an exception, especially since the pointer never change. Maybe there's a way to pass the pointer to the wndproc function by CreateWindowEx(), i remember vagly reading something about that.

I think your program hang because of the double whiles loop. You dont need 2, one is enough. The problem is when you receive the WM_QUIT message, first of all, the break cause the message to not be processed at all in the wndproc, secondly, you just break the inner loop, then re-enter it immediatly, since the break only work for the peekmessage loop. EDIT: i didn't noticed the gamerunning bool, but still, i find that very strange for a game loop.

Just redesign your code similar to what i did and all will be fine. I've been using this code for years in my games and never had problem with it.

Sorry if im not explaining very well, but i haven't sleep all night. wacko.png

EDIT: Yeah, you can pass any pointer to the wndproc though the last argument of CreateWindowEx(), and access it later in the WM_CREATE message i think. Then, in the default switch of wndproc, you call input.Update(uMsg, wParam, lParam); and process any relevent message here.

Oh and if you want to see where the programs hang, why not just start it in debug and press break when it hang. You might have to press Step out a few times, or just consult the call stack, then you'll see where your code is stuck. The whole reason of using peekmessage is that it dosen't block, so that's not the problem.

This topic is closed to new replies.

Advertisement