game loop in mfc

Started by
4 comments, last by jharkey 22 years, 5 months ago
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
Advertisement
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
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.
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
thanks a lot, i appreciate the help a lot
If you want it to work well with MFC, you kinda need to use a seperate thread. OnIdle won''t get called if/when the user clicks on the title bar or a menu item.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement