Splitting the X11-specific "freeglut_gamemode.c" code into its own file
[freeglut] / src / Common / freeglut_gamemode.c
1 /*\r
2  * freeglut_gamemode.c\r
3  *\r
4  * The game mode handling code.\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: Thu Dec 16 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  * TODO BEFORE THE STABLE RELEASE:\r
33  *\r
34  *  glutGameModeString()    -- missing\r
35  *  glutEnterGameMode()     -- X11 version\r
36  *  glutLeaveGameMode()     -- is that correct?\r
37  *  glutGameModeGet()       -- is that correct?\r
38  */\r
39 \r
40 \r
41 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */\r
42 extern void fgPlatformRememberState( void );\r
43 extern void fgPlatformRestoreState( void );\r
44 extern GLboolean fgPlatformChangeDisplayMode( GLboolean haveToTest );\r
45 extern void fgPlatformEnterGameMode( void );\r
46 extern void fgPlatformLeaveGameMode( void );\r
47 \r
48 \r
49 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */\r
50 \r
51 /*\r
52  * Sets the game mode display string\r
53  */\r
54 void FGAPIENTRY glutGameModeString( const char* string )\r
55 {\r
56     int width = -1, height = -1, depth = -1, refresh = -1;\r
57 \r
58     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeString" );\r
59 \r
60     /*\r
61      * This one seems a bit easier than glutInitDisplayString. The bad thing\r
62      * about it that I was unable to find the game mode string definition, so\r
63      * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which\r
64      * appears in all GLUT game mode programs I have seen to date.\r
65      */\r
66     if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) !=\r
67         4 )\r
68         if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )\r
69             if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )\r
70                 if( sscanf( string, "%ix%i", &width, &height ) != 2 )\r
71                     if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )\r
72                         if( sscanf( string, ":%i", &depth ) != 1 )\r
73                             if( sscanf( string, "@%i", &refresh ) != 1 )\r
74                                 fgWarning(\r
75                                     "unable to parse game mode string `%s'",\r
76                                     string\r
77                                 );\r
78 \r
79     /* All values not specified are now set to -1, which means those\r
80      * aspects of the current display mode are not changed in\r
81      * fgPlatformChangeDisplayMode() above.\r
82      */\r
83     fgState.GameModeSize.X  = width;\r
84     fgState.GameModeSize.Y  = height;\r
85     fgState.GameModeDepth   = depth;\r
86     fgState.GameModeRefresh = refresh;\r
87 }\r
88 \r
89 \r
90 \r
91 /*\r
92  * Enters the game mode\r
93  */\r
94 int FGAPIENTRY glutEnterGameMode( void )\r
95 {\r
96     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutEnterGameMode" );\r
97 \r
98     if( fgStructure.GameModeWindow )\r
99         fgAddToWindowDestroyList( fgStructure.GameModeWindow );\r
100     else\r
101         fgPlatformRememberState( );\r
102 \r
103     if( ! fgPlatformChangeDisplayMode( GL_FALSE ) )\r
104     {\r
105         fgWarning( "failed to change screen settings" );\r
106         return 0;\r
107     }\r
108 \r
109     fgStructure.GameModeWindow = fgCreateWindow(\r
110         NULL, "FREEGLUT", GL_TRUE, 0, 0,\r
111         GL_TRUE, fgState.GameModeSize.X, fgState.GameModeSize.Y,\r
112         GL_TRUE, GL_FALSE\r
113     );\r
114 \r
115     fgStructure.GameModeWindow->State.Width  = fgState.GameModeSize.X;\r
116     fgStructure.GameModeWindow->State.Height = fgState.GameModeSize.Y;\r
117     fgStructure.GameModeWindow->State.NeedToResize = GL_TRUE;\r
118 \r
119     fgPlatformEnterGameMode();\r
120 \r
121     return fgStructure.GameModeWindow->ID;\r
122 }\r
123 \r
124 /*\r
125  * Leaves the game mode\r
126  */\r
127 void FGAPIENTRY glutLeaveGameMode( void )\r
128 {\r
129     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveGameMode" );\r
130 \r
131     freeglut_return_if_fail( fgStructure.GameModeWindow );\r
132 \r
133     fgAddToWindowDestroyList( fgStructure.GameModeWindow );\r
134     fgStructure.GameModeWindow = NULL;\r
135 \r
136     fgPlatformLeaveGameMode();\r
137 \r
138     fgPlatformRestoreState();\r
139 }\r
140 \r
141 /*\r
142  * Returns information concerning the freeglut game mode\r
143  */\r
144 int FGAPIENTRY glutGameModeGet( GLenum eWhat )\r
145 {\r
146     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeGet" );\r
147 \r
148     switch( eWhat )\r
149     {\r
150     case GLUT_GAME_MODE_ACTIVE:\r
151         return !!fgStructure.GameModeWindow;\r
152 \r
153     case GLUT_GAME_MODE_POSSIBLE:\r
154         return fgPlatformChangeDisplayMode( GL_TRUE );\r
155 \r
156     case GLUT_GAME_MODE_WIDTH:\r
157         return fgState.GameModeSize.X;\r
158 \r
159     case GLUT_GAME_MODE_HEIGHT:\r
160         return fgState.GameModeSize.Y;\r
161 \r
162     case GLUT_GAME_MODE_PIXEL_DEPTH:\r
163         return fgState.GameModeDepth;\r
164 \r
165     case GLUT_GAME_MODE_REFRESH_RATE:\r
166         return fgState.GameModeRefresh;\r
167 \r
168     case GLUT_GAME_MODE_DISPLAY_CHANGED:\r
169         /*\r
170          * This is true if the game mode has been activated successfully..\r
171          */\r
172         return !!fgStructure.GameModeWindow;\r
173     }\r
174 \r
175     fgWarning( "Unknown gamemode get: %d", eWhat );\r
176     return -1;\r
177 }\r
178 \r
179 /*** END OF FILE ***/\r