8faa4a5a3a164c7a51f050cbfe0f94f8dc5669ec
[freeglut] / src / freeglut_display.c
1 /*
2  * freeglut_display.c
3  *
4  * Display message posting, context buffer swapping.
5  *
6  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7  * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8  * Creation date: Fri Dec 3 1999
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
30
31 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
32
33 /*
34  * Marks the current window to have the redisplay performed when possible...
35  */
36 void FGAPIENTRY glutPostRedisplay( void )
37 {
38     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPostRedisplay" );
39     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPostRedisplay" );
40     fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
41 }
42
43 /*
44  * Swaps the buffers for the current window (if any)
45  */
46 void FGAPIENTRY glutSwapBuffers( void )
47 {
48     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSwapBuffers" );
49     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSwapBuffers" );
50
51     glFlush( );
52     if( ! fgStructure.CurrentWindow->Window.DoubleBuffered )
53         return;
54
55 #if TARGET_HOST_UNIX_X11
56     glXSwapBuffers( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
57 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
58     SwapBuffers( fgStructure.CurrentWindow->Window.Device );
59 #endif
60
61     /* GLUT_FPS env var support */
62     if( fgState.FPSInterval )
63     {
64         GLint t = glutGet( GLUT_ELAPSED_TIME );
65         fgState.SwapCount++;
66         if( fgState.SwapTime == 0 )
67             fgState.SwapTime = t;
68         else if( t - fgState.SwapTime > fgState.FPSInterval )
69         {
70             float time = 0.001f * ( t - fgState.SwapTime );
71             float fps = ( float )fgState.SwapCount / time;
72             fprintf( stderr,
73                      "freeglut: %d frames in %.2f seconds = %.2f FPS\n",
74                      fgState.SwapCount, time, fps );
75             fgState.SwapTime = t;
76             fgState.SwapCount = 0;
77         }
78     }
79 }
80
81 /*
82  * Mark appropriate window to be displayed
83  */
84 void FGAPIENTRY glutPostWindowRedisplay( int windowID )
85 {
86     SFG_Window* window;
87
88     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPostWindowRedisplay" );
89     window = fgWindowByID( windowID );
90     freeglut_return_if_fail( window );
91     window->State.Redisplay = GL_TRUE;
92 }
93
94 /*** END OF FILE ***/