visual C 8 game loop

Started by
8 comments, last by simon10k 17 years, 11 months ago
anyone know how to implement a game loop with the code generated by the win32 app project?
Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)
Advertisement
something like:

while(!done) {  ... Code here ... }


? :)
"Game Maker For Life, probably never professional thou." =)
// Main message loop:while (GetMessage(&msg, NULL, 0, 0)) {	if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 	{		TranslateMessage(&msg);		DispatchMessage(&msg);	}}


change that to:
// Main message loop:while(!done) {while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {	if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 	{		TranslateMessage(&msg);		DispatchMessage(&msg);	}}// Update game here


But i suggest you reed a tutorial on how to make a real game loop and dont use the app created by windows you have more control if you make your own app..
Crush your enemies, see them driven before you, and hear the lamentations of their women!
Will this work?
do		{		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))		{			if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))			{				if (msg.message == WM_QUIT) break;				TranslateMessage(&msg);				DispatchMessage(&msg);			}		}		else		{                   //game stuff		}	}	while(TRUE);
Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)
Quote:Original post by etsuja
Will this work?
*** Source Snippet Removed ***

Yup.
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]
Quote:Original post by etsuja
Will this work?
*** Source Snippet Removed ***


Well, it will work, but is that really how you want it? (And out of curiousity, why use a do{}while() loop in place of a while() loop or maybe even for()?)

But what if you did this:
while(true) { // makes my life easier :D    get_message(&message); // don't know win32, help me out here!    switch (message.code) { // again, win32        case QUIT: // need I say it?            break; // oh-oh!        break;        case KEYDOWN:            // super-cool keydown!        break;    }}

The break would break the switch, not the while loop.... so I would do instead something like:
for (bool done = false; !done; /*none*/) { // makes my life easier :D    get_message(&message); // don't know win32, help me out here!    switch (message.code) { // again, win32        case QUIT: // need I say it?            done = true; // walla!        break;        case KEYDOWN:            // super-cool keydown!        break;    }}


Just a heads-up.
Moving to For Beginners.
Just follow SmurfKiller's advice as this is the way I do it and it does the job perfectly.

However, You probably will not need the TranslateAccelerator function in your game as this will only be wasted cycles. If you don't intend on processing the WM_CHAR message then you don't need the TranslateMessage function either.


The best way is simply something like this: -

// main game loopwhile(g_pGame->IsRunning()){    // clear the screen    g_pGraphicsManager->ClearScreen();    // get the frame time    float fFrameTime = g_pTimeManager->GetFrameTime();    // get the user input    g_pInputManager->Update(fFrameTime);    // update AI logic    g_pAIManager->Update(fFrameTime);    // update game logic    g_pGame->Update(fFrameTime);    // update audio    g_pAudioManager->Update(fFrameTime);    // render the scene    g_pGraphicsManager->RenderScene(fFrameTime);}


This is the normal game loop for a muti-platform, client only game. All of the main computation is taken off and handled by the respective objects.

NOTE: The input here will be handled by some for of input API, DirectInput maybe for Win32 systems.
My game loop looks like this

MSG msg = {};while(msg.message != WM_QUIT){    if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))	DispatchMessage(&msg);    else        RunFrame();}
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003

This topic is closed to new replies.

Advertisement