Sign in to follow this  
etsuja

visual C 8 game loop

Recommended Posts


// 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..

Share this post


Link to post
Share on other sites
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);


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites
The best way is simply something like this: -



// main game loop
while(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.

Share this post


Link to post
Share on other sites
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();
}

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