Archived

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

doctorsixstring

MFC "Main Loop"

Recommended Posts

Is it possible to implement a "main loop" in MFC? As far as I can tell, every function I execute can only be launched via windows messages. Is it possible however, to have a function that is run every frame, so I can update DirectInput and DirectDraw constantly, and not just when a message is dispatched? -Mike

Share this post


Link to post
Share on other sites
Yup, you need to overide OnIdle in the Main App class that then calls the display function of your window. I can''t remember the exact syntax, so you''ll have to look it up, though I can remember that you have to return true if you want the idle to keep running ..... (or something like that)

Share this post


Link to post
Share on other sites
Yes [edit]OnIdle is only called when there are no messages [/edit]. So, you could (if you really wanted) overide Run and impliment your own loop. You'd then end up doing the message processing manually, and to be fair if you wanted to go down that route, you might as well use Win32 and leave MFC by the side of the M5 somewhere near bromsgrove.

Generally you want all messages to be processed as quick as possible, ie, WM_CLOSE hints that the user wants to leave your app Now, not when your game loop has finished... Which will probably never happen if you aren't processing the messages.

The best method is to allow all messages to be processed and then, process your game/animation code (whatever), followed by a re-draw (if required). I reackon it's the OnIdle func that you need, if not I'll eat my mousemat.....


Rob


[edited by - RobTheBloke on September 2, 2002 4:36:08 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by RobTheBloke
Yes [edit]OnIdle is only called when there are no messages [/edit]. So, you could (if you really wanted) overide Run and impliment your own loop.

you''re confused. read the docs on OnIdle(), specifically the return value.

to answer the original question: put all your game loop code in OnIdle handler and you should be ok.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Look up the SetTimer() KillTimer and OnTimer methods in your documentation.

You should be able to set the timer in your initialization which will cause a timer message to be sent to your application every time interval you specify.

The ClassWizard will allow you to add a Timer processing method.

Killtimer stops the timer.

Share this post


Link to post
Share on other sites
launch a thread from InitInstance()
then the thread will run in the background.

Its my duty, to please that booty ! - John Shaft

Share this post


Link to post
Share on other sites