• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Johannes1991

Win32 input crash

3 posts in this topic

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;
		}
	}
}

 

0

Share this post


Link to post
Share on other sites

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. 

Edited by Vortez
-2

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

Edited by Vortez
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0