#### Archived

This topic is now archived and is closed to further replies.

# game loop in mfc

## Recommended Posts

jharkey    110
hello, im going to write a little game in mfc, and i am having trouble setting up the rendering loop. i dont really know where to put it. the things i have tried are, overriding OnIdle() , but that like takes over the system, and gets called like 20000 times a second, no joke. overriding Run() and tapping into the message loop, but i cant quite figure out were to do it in the mfc loop. i tried it, but if there arent any messages in the message queue, windows doesnt give that thread any processing time. this works good if i set up a timer to go off every thousanth of a second, so there is always something there, and then i get about 200 frames per second. but if there is nothing, it doesnt render at all. and on the OnPaint(), but i want it to run in real time, not only when the window needs repainting. i tried just calling Invalidate at the end of the OnPaint function, so it would just call it again, but that slows everthing down way to much. what i was wondering, is if anyone has a good setup for an mfc game loop, and has gotten one to work good. i dont want to use direct x or anything. or does anyone have any good ideas on how to set one up. any help would be appreciated

##### Share on other sites
I think you should use OnIdle(), like this:

  BOOL CApp::OnIdle( DWORD ){ // Do not render if the app is minimized if( m_pMainWnd->IsIconic() ) return FALSE; // Update and render a frame if( g_AppFormView->IsReady() ) { g_pYour3DApp->RenderScene(); } // Keep requesting more idle time return TRUE;}

Zeblar Nagrim, Lord of Chaos

##### Share on other sites
jharkey    110
this may seem like a stupid question, but idle time is requested? i mean, if there are lots of processes going, windows will take care of them, and not give your program more idle time? the reason i ask is that when i run my program, the task manager says it gets 99% of the cpu usage. so if other things run this isnt a problem? it will not hog all the time, just the idle time windows allows it? i dont know if i am making sense or not.

##### Share on other sites
Just be sure that your game dosn´t work when it´s unnecessery ie. only update graphic when a animation is played or when the user are moving his character etc. And never do anything while your app is minimized.

Zeblar Nagrim, Lord of Chaos

##### Share on other sites
jharkey    110
thanks a lot, i appreciate the help a lot