OpenGLUT Documentation
Introduction | Documentation | Examples | Proposals | Authors | Copying | Todo | Bugs | Links

Main Loop
[OpenGLUT API Reference]

OpenGLUT Initialisation and Main Loop. More...


void glutInit (int *pargc, char **argv)
void glutLeaveMainLoop (void)
void glutMainLoop (void)
void glutMainLoopEvent (void)

Detailed Description

OpenGLUT Initialisation and Main Loop.

OpenGLUT's event model is based on callbacks (see the callbacks module). This is inherited from old GLUT in design, and from freeglut in implementation. The model works simply by letting you tell OpenGLUT which function of yours to call when certain events happen. Certain events, such as the redisplay event for updating your window graphics, can be postponed and coalesced so that one redisplay may occur in response to multiple hints to OpenGLUT that you require a a redisplay. Some events are manufactured by OpenGLUT, such as timers. Others may be caused by your own request, such as redisplays. Still others may only be caused by external stimulus.

You have two ways to enter OpenGLUT's event processing model. One is via glutMainLoop(), and the other is via glutMainLoopEvent().

When OpenGLUT has dispatched all pending events, it will do one of three things:

Old GLUT did not offer glutMainLoopEvent() nor glutLeaveMainLoop(), and would only exit glutMainLoop() if the user closed one of your windows or if you called exit(). In no case would old GLUT return control to your code by simply returning from glutMainLoop().

freeglut added some mechanisms to control this. One is an explicit glutLeaveMainLoop(). Another is a mode of execution that tells OpenGLUT what to do if a user closes one of your windows.

OpenGLUT inherited freeglut's variations.


Function Documentation

void glutInit int *  pargc,
char **  argv

Initialize OpenGLUT data structures.

pargc Pointer to something like main()'s argc.
argv Something like main()'s argv.
This function should be called once, near the start of any GLUT, freeglut, or OpenGLUT program. It serves two vital roles:

  • It allows OpenGLUT to initialize internal structures.
  • It allows OpenGLUT to process command-line arguments to control the initial window position, etc.

You should take note of the interaction between glutInit() and the related functions such as glutInitWindowPosition(). OpenGLUT always uses the most recent configuration information, so if you call glutInit(), then glutInitWindowPosition(), you prevent the user from controlling the initial window position via a command-line parameter.

glutInit() will remove from pargc, argv any parameters that it recognizes in the command line. The following command-line parameters are suported:

  • -display display-id This allows connection to an alternate X server.
  • -geometry geometry-spec This takes width, height, and window position. The position is given as a signed value (negative values being distance from the far boundary of the screen). For example, a window geometry of 5x7+11-17 is 5 pixels wide, 7 pixels tall, 11 pixels from the left, and 17 pixels from the bottom edge of the screen.
  • -direct Insist on only OpenGL direct rendering. Direct rendering is normally requested but indirect is normally accepted. -direct is not always available. If both direct and indirect are specified on the command line, the tail-most one determines OpenGLUT's behavior. See -indirect, -maybe-indirect.
  • -indirect Attempt only indirect OpenGL rendering. -indirect is always available. See -direct, -maybe-indirect.
  • -maybe-indirect Restore the default OpenGLUT behavior of trying direct rendering first, then falling back to indirect rendering. See -direct, -indirect.
  • -iconic Open the window in iconized form.
  • -gldebug Print any detected OpenGL errors via glutReportErrors(). Presently done at the bottom of glutMainLoopEvent().
  • -no-warnings Disable the showing of OpenGLUT warnings (e.g., on standard error consoles).
  • -warnings Show OpenGLUT warning messages (e.g., on standard error consoles). This is the default.
  • -no-informs Disable showing OpenGLUT informational messages (e.g., on standard error consoles). This is the default.
  • -informs Show OpenGLUT informational messages (e.g., on standard error consoles).
  • -sync Synchronize the window system communications heavily.

Additionally, this function checks whether the environment variable GLUT_FPS is defined (only on UNIX_X11); if so, OpenGLUT will periodically print the average number of times per second that your program calls glutSwapBuffers().

You really should always call this, even if you are a WIN32 user. It provides a way for the user to directly inform OpenGLUT about preferences without the application needing to explicitly deal with those issues. This is also where OpenGLUT retrieves your program's name to help disambiguate error and warning messages it may be forced to emit.

Option -sync sets a flag, but is not actually used at this time.

Lots of code does XFlush() on the X server, regardless of whether -sync is specified. Much of that appears to be required in order to support direct client invocation of glutMainLoopEvent(), regrettably. However, if one calls glutMainLoop(), instead, we might avoid gratuitous XFlush() calls. (That last sentence isn't particularly germain to this function, but there's no better place to make this remark at this time.) Even for glutMainLoopEvent(), we may be able to coalesce many XFlush() calls.

See also:
glutInitWindowPosition(), glutInitWindowSize(), glutInitDisplayMode(), glutInitDisplayString(), glutCreateWindow(), glutDisplayFunc(), glutMainLoop(), glutMainLoopEvent(), glutReportErrors(), glutSwapBuffers()

void glutLeaveMainLoop void   ) 

Breaks out of OpenGLUT's glutMainLoop().

This function allows you to unilaterally tell OpenGLUT that you are done and wish to exit. This is useful if you have also told OpenGLUT to return to you rather than to call exit() directly.

void glutMainLoop void   ) 

The standard GLUT event loop entry point.

This is the main driving force for an event-driven OpenGLUT program. It alternates between calling glutMainLoopEvent() to process pending events and then either sleeping or calling your idle function (see glutIdleFunc()).

This function can return, but GLUT's version of this function never returned. And you must do special things to OpenGLUT to cause OpenGLUT's version to return.

The cross-reference section for this function's documentation should ideally contain every callback, but the list would be tediously long and prone to omissions.

Talking to other message systems (e.g., network layers) can be a bit bothersome under the GLUT event model.

void glutMainLoopEvent void   ) 

Dispatches all pending events.

The general outline of this function is to first drain the queue of windowsystem events, in most cases dispatching each as it is found. After the queue is empty, we check for timer-based events, coalesced window events (e.g., redisplays), and windows that need to be closed.

The cross-reference section for this function's documentation should ideally contain every callback, but the list would be tediously long and prone to omissions.

Does not necessarily dispatch events that are received after this function starts processing.

At first glance, this function may not seem to afford any new capability that you couldn't get with an idle callback or glutLeaveMainLoop(). However there are other GLUT-like libraries that may have their own window event processing loops. Having glutMainLoopEvent() allows you to ask OpenGLUT to do its work in a batch, then return to whatever processing the other library (or libraries) require.

See also:
glutIdleFunc(), glutLeaveMainLoop(), glutMainLoop()

OpenGLUT Development @ Sourceforge
Homepage | Summary | Files | CVS | Forums | Lists | Bugs | RFE

Generated on Sat Feb 5 01:47:28 2005 for OpenGLUT by doxygen
The OpenGLUT project is hosted by