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