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