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