# Where to put game loop in vc2005 "new project"

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

## Recommended Posts

Hi, I've got a few problems with trying to use a new windows project generated by vc2005. The first problem is that the message loop is different to what I'm used to (usually I just base my projects on one of the dx tutorials), also I'm not sure about the hAccelTable etc. Can any oneone show me what to add/change, so that I can include a game loop? Thanks ps possibly using PeekMessage(...) as well rather than GetMessage(...).


//Part of vc2005 "Windows project" generated code

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR    lpCmdLine,
int       nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}



##### Share on other sites
I can't say that I have played around with 'vc2005' too much, but I would assume it fits somewhere in the GetMessage loop. I am going to guess that you will need to add an 'else' to the 'if', but that is just a guess...

I am afraid my only refence is "Windows 98 programming from the ground up", and I am at school (the book is at home). Does it mention anything in the vc2005 help files?

##### Share on other sites
Yeah, add an else to the if statement.

ace

##### Share on other sites
Hi Ace, Moe, no, thats not quite it, the normal way is to base it on seeing if a PostQuit() message has been sent, I'm sure it shouldn't be that hard to figure out, but really I've never actually learnt much about the message loop.

@Moe,
It would probably be the same for most compilers, its just the automatticaly generatesd code for when you want to do a new windows app, rather than using the dx Install Project App, or something.

 //From d3d "Textures" tutorial;            while( msg.message!=WM_QUIT )            {                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )                {                    TranslateMessage( &msg );                    DispatchMessage( &msg );                }                else                    Render();            }//compared too vc2005 code	// Main message loop:	while (GetMessage(&msg, NULL, 0, 0)) 	{		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 		{			TranslateMessage(&msg);			DispatchMessage(&msg);		}	}	return (int) msg.wParam;

It might have something to do with this,
from msdn, "TranslateAccelerator does not return until the window procedure has processed the message." It also says TranslateAccelerator translates messages for WM_KEYDOWN on menu's or something... I guess this is what you get for not just sticking with the normal tried & tested way of doing it, theres also a whole lot of other "junk" in there as well. I think I'll just stick to basing it on the tutorial code again.
Thanks anyway guys.

##### Share on other sites
Here is my home-grown code that I am using:

//the main message loop while(notDone) //notDone is a boolean variable...{	if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))	{		//test if it is a quit		if(msg.message ==WM_QUIT) 			notDone = false;		//translate any accelerator keys 		TranslateMessage(&msg);		//send the message to the window proc		DispatchMessage(&msg);	} else {		//main loop of the program goes here		//Be sure to check return type to see if it should exit!		if(engine.MainLoop() == ENGINE_FAIL)		{				//error code goes here			return(0);		}	}} //end while

Again, its a bit different because I am not using the automatically generated project (I create an empty project, then put in what I need).

##### Share on other sites
I was getting stuck because the generated code seems to be quite alot different to what I'm used too, your code seems alot better though for a message loop,- I'm guessing that the way you do it your actual render function is some where in your engine.MainLoop(), and doesn't have to be in the main project file, thats quite good for oo I think?

##### Share on other sites
Well, its not bad for OO. I just try to keep the basic code nice and clean so I can add a pile more files to a project without things becoming a rat's nest of code (ie: my first program ever was about 1,500 lines in two files with about 40 global variables. Evil!). If you want the source to the entire thing, which is basically a giant DirectX/Engine wrapper, I would be happy to send it to you.

##### Share on other sites
i am not sure which is more good.
//the main message loop while(notDone) //notDone is a boolean variable...{	if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))	{		//test if it is a quit		if(msg.message ==WM_QUIT) 			notDone = false;		//translate any accelerator keys 		TranslateMessage(&msg);		//send the message to the window proc		DispatchMessage(&msg);	}		GameMainLoop();  //render one frame} //end while

##### Share on other sites
I think you want an if/else for the game logic. Otherwise, you will be able to process a maximum of one windows message per frame update -- not a very good thing :)

##### Share on other sites
kosmon_x, what exactly do you mean, use an if/else statement?

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 18
• ### Forum Statistics

• Total Topics
632160
• Total Posts
3004500

×