| Home
| Previous
| Next
| Up

GM_mainLoop

Runs the main loop for the Game Framework

Declaration

void MGLAPI GM_mainLoop(void)

Prototype In

gm/gm.h

Description

This function is the main event loop for the Game Framework, and controls execution of the game until the game exists. This function is responsible for farming out events to the event handling callbacks registered by the game along with calling the game's gameLogic and draw callbacks. Note that if we are suspended on the task bar we continue to process events and call the gameLogic function (so networking can continue) however we skip the call the to drawing function to avoid attempting to write to video memory we no longer own.

The main loop is a simple loop constructed of the following steps (note that MyGameLogic and MyDrawFrame are assumed to be your game logic and draw functions that you would normally register with the Game Framework before calling the GM_mainLoop function):

GM_exitMainLoop = false;
while (!GM_exitMainLoop) {
GM_processEvents();
MyGameLogic();
if (GM_doDraw)
MyDrawFrame();
}
GM_cleanup();

If you wish to replace the main loop with your own, you can take the existing main loop code and replace it with your own variations. The GM_processEvents functions processes events via the MGL event handling functions and dispatches them to the registered event callbacks. If you wish you can call GM_processEventsWin instead, which will simply flush the windows message queue and you will be expected to handle all keyboard and mouse events with the window procedure registered with GM_registerEventProc. Hence an alternate main loop with all event handling done in regular window procedure would be coded as follows:

GM_exitMainLoop = false;
GM_registerEventProc(MyWindowProc);
while (!GM_exitMainLoop) {
GM_processEventsWin();
MyGameLogic();
if (GM_doDraw)
MyDrawFrame();
}
GM_cleanup();

Note: If you do replace the main loop with your own, make absolutely sure that you don't call your draw function if the global variable GM_doDraw is set to false, otherwise your game could lock the system when the user Alt-Tabs away and back to the desktop. Also make sure that you call GM_cleanup on the way out.

Note: To exit the main loop, call the GM_exit function which lets the main loop know that it should exit on the next iteration.

Note: You must call GM_processEvents or GM_processEventsWin in your main loop to ensure that the Game Framework has a chance to process some internal functions every loop.

See Also

GM_init, GM_setDrawFunc, GM_setGameLogicFunc, GM_exit, GM_processEvents, GM_processEventsWin, GM_cleanup

Copyright © 2002 SciTech Software, Inc. Visit our web site at http://www.scitechsoft.com