Jump to content
  • Advertisement
Sign in to follow this  
frantic

Now what?

This topic is 4851 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 is my code to create myself a nice frame to start playing with DirectX in. Am I supposed to edit the message handler and say if there are no messages then enter the game loop? I'm not completely sure about this...

#include <windows.h>

/* globals */
char className[] = "GameFrame";

/* Windows message handling prototype */
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE pInstance, LPSTR lpCmdLine, int nCmdShow){
	
	/* fill out the windows class struct */
	WNDCLASS wc;
	
	wc.style			=	CS_VREDRAW | CS_HREDRAW;
	wc.lpfnWndProc		=	WindowProc;
	wc.cbClsExtra		=	0;
	wc.cbWndExtra		=	0;
	wc.hInstance		=	hInstance;
	wc.hIcon			=	LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor			=	LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground	=	(HBRUSH) GetStockObject(WHITE_BRUSH);
	wc.lpszClassName	=	className;
	wc.lpszMenuName		=	0;

	/* register the class */
	if(!RegisterClass(&wc)){
		::MessageBox(NULL, "Failure attempting to register the class!", "Fail", NULL);
		return 0;
	}

	/* Now we create the window */

	HWND hWndTest;

	hWndTest = CreateWindow(className,
							"Game Framework",
							WS_OVERLAPPEDWINDOW,
							0, 0, 800, 600,
							NULL, NULL, hInstance, NULL);

	if(hWndTest == NULL){
		::MessageBox(NULL, "Failure attempting to create the window!", "Fail", NULL);
		return 0;
	}

	/* Display the window */
	ShowWindow(hWndTest, nCmdShow);

	/* Enter the message loop */
	MSG msg;

	while(GetMessage(&msg, NULL, 0, 0)){
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
	/* simple message handler, lets the default deal with it */
	switch(uMsg){
		default:
			return DefWindowProc(hwnd, uMsg, wParam, lParam);
			break;
	}
};


Share this post


Link to post
Share on other sites
Advertisement
within the while loop or just after it?



while(GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
Game->EnterLoop();
}




or



while(GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Game->EnterLoop();


Share this post


Link to post
Share on other sites
Hi, you would not call your frame function in your message handler, but in the message loop, like so:


while(true) {
while(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
if ( !GetMessage(&msg, NULL, 0, 0) )
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
frame();
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Change GetMessage to PeekMessage and add in the loop similar to:

while( TRUE )
{
// Check for message
if (PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
// if message is to quit
if(msg.message == WM_QUIT)
// exit
break;

// Format msg
TranslateMessage(&msg);
// Pass it on
DispatchMessage(&msg);
}

else
{
Input_Loop();
if(FAILED(Main_Loop()))
{
PostQuitMessage(0);
}
//idle code
}
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by frantic
within the while loop or just after it?


No, within the while loop, because, if you don't process the messages regularly, your window will hang.

To clear things up: PeekMessage looks for a message in the queue and returns FALSE if there isn't one. GetMessage WAITS for a message, so your frame function would not run until until there is a message in the queue, and returns FALSE if the WM_QUIT message comes in.

Share this post


Link to post
Share on other sites
Thanks for the response. So heres my new message checking code:



while(true){

/* if there is a message to process */
if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)){

if(msg.message == WM_QUIT) { break; }

/* Process the message */
TranslateMessage(&msg);
DispatchMessage(&msg);

} else {

// Game code here
}
}





Am I right in believing that:
1) It will check for a message and return false if there is not one resulting
a) Running my main game loop
2) It will check for a message and return true if there is one resulting in
a) Checking to see if the user hasn't pressed quit
b) Dispatching the message to my message handler

Share this post


Link to post
Share on other sites
Yes, but you need to change either PeekMessage to PeekMessage( ... ... PM_REMOVE ) or use GetMessage() after it, to actually remove the message from the queue.

I always do a while ( PeekMessage() ) loop in my mainloop, before I call the frame function, because with a simple if ( PeekMessage() ) you can dispatch only 1 message per frame. Additionally, I use PeekMessage with PM_NOREMOVE and the GetMessage to check for the WM_QUIT message, because that seems cleaner to me and gives the system a little time (in the GetMessage call) to do other things, to keep the system responsive.

The WM_QUIT message is returned if the PostQuitMessage function was called. You can call it if you want to end the main loop, or it will be called from the DefWindowProc in response to a WM_DESTROY message. So you could check in your WindowProc e.g. for the key ESC pressed, roughly like so:


LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
/* simple message handler, lets the default deal with it */
switch(uMsg){
case WM_DESTROY: // note: this is the default behaviour of
PostQuitMessage( 0 );// DefWindowProc, you can omit that.
break;
case WM_KEYDOWN:
if (wParam == VK_ESCAPE) {
DestroyWindow( hWnd ); // sends destroy message
}
break;
default:

return DefWindowProc(hwnd, uMsg, wParam, lParam);
break; // unneccesary. You just returned.
}
};



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!