4 * Window management methods.
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
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:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
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.
28 #define FREEGLUT_BUILDING_LIB
29 #include <GL/freeglut.h>
30 #include "fg_internal.h"
33 * TODO BEFORE THE STABLE RELEASE:
35 * fgSetupPixelFormat -- ignores the display mode settings
36 * fgOpenWindow() -- check the Win32 version, -iconic handling!
37 * fgCloseWindow() -- check the Win32 version
38 * glutCreateWindow() -- Check when default position and size is {-1,-1}
39 * glutCreateSubWindow() -- Check when default position and size is {-1,-1}
40 * glutDestroyWindow() -- check the Win32 version
41 * glutSetWindow() -- check the Win32 version
42 * glutSetWindowTitle() -- check the Win32 version
43 * glutSetIconTitle() -- check the Win32 version
44 * glutShowWindow() -- check the Win32 version
45 * glutHideWindow() -- check the Win32 version
46 * glutIconifyWindow() -- check the Win32 version
47 * glutPushWindow() -- check the Win32 version
48 * glutPopWindow() -- check the Win32 version
52 extern void fgPlatformSetWindow ( SFG_Window *window );
53 extern void fgPlatformOpenWindow( SFG_Window* window, const char* title,
54 GLboolean positionUse, int x, int y,
55 GLboolean sizeUse, int w, int h,
56 GLboolean gameMode, GLboolean isSubWindow );
57 extern void fgPlatformCloseWindow( SFG_Window* window );
58 extern void fgPlatformGlutShowWindow( void );
59 extern void fgPlatformGlutHideWindow( void );
60 extern void fgPlatformGlutIconifyWindow( void );
61 extern void fgPlatformGlutSetWindowTitle( const char* title );
62 extern void fgPlatformGlutSetIconTitle( const char* title );
63 extern void fgPlatformGlutPositionWindow( int x, int y );
64 extern void fgPlatformGlutPushWindow( void );
65 extern void fgPlatformGlutPopWindow( void );
66 extern void fgPlatformGlutFullScreen( SFG_Window *win );
67 extern void fgPlatformGlutLeaveFullScreen( SFG_Window *win );
68 extern void fgPlatformGlutFullScreenToggle( SFG_Window *win );
71 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
73 int fghIsLegacyContextRequested( void )
75 return fgState.MajorVersion < 2 || (fgState.MajorVersion == 2 && fgState.MinorVersion <= 1);
78 int fghNumberOfAuxBuffersRequested( void )
80 if ( fgState.DisplayMode & GLUT_AUX4 ) {
83 if ( fgState.DisplayMode & GLUT_AUX3 ) {
86 if ( fgState.DisplayMode & GLUT_AUX2 ) {
89 if ( fgState.DisplayMode & GLUT_AUX1 ) { /* NOTE: Same as GLUT_AUX! */
90 return fgState.AuxiliaryBufferNumber;
95 int fghMapBit( int mask, int from, int to )
97 return ( mask & from ) ? to : 0;
101 void fghContextCreationError( void )
103 fgError( "Unable to create OpenGL %d.%d context (flags %x, profile %x)",
104 fgState.MajorVersion, fgState.MinorVersion, fgState.ContextFlags,
105 fgState.ContextProfile );
109 /* -- SYSTEM-DEPENDENT PRIVATE FUNCTIONS ------------------------------------ */
112 * Sets the OpenGL context and the fgStructure "Current Window" pointer to
113 * the window structure passed in.
115 void fgSetWindow ( SFG_Window *window )
117 fgPlatformSetWindow ( window );
119 fgStructure.CurrentWindow = window;
123 * Opens a window. Requires a SFG_Window object created and attached
124 * to the freeglut structure. OpenGL context is created here.
126 void fgOpenWindow( SFG_Window* window, const char* title,
127 GLboolean positionUse, int x, int y,
128 GLboolean sizeUse, int w, int h,
129 GLboolean gameMode, GLboolean isSubWindow )
131 fgPlatformOpenWindow( window, title,
134 gameMode, isSubWindow );
136 fgSetWindow( window );
138 window->Window.DoubleBuffered =
139 ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
141 #ifndef EGL_VERSION_1_0 /* No glDrawBuffer/glReadBuffer in GLES */
142 if ( ! window->Window.DoubleBuffered )
144 glDrawBuffer ( GL_FRONT );
145 glReadBuffer ( GL_FRONT );
148 window->Window.attribute_v_coord = -1;
149 window->Window.attribute_v_normal = -1;
153 * Closes a window, destroying the frame and OpenGL context
155 void fgCloseWindow( SFG_Window* window )
157 /* if we're in gamemode and we're closing the gamemode window,
158 * call glutLeaveGameMode first to make sure the gamemode is
159 * properly closed before closing the window
161 if (fgStructure.GameModeWindow != NULL && fgStructure.GameModeWindow->ID==window->ID)
164 fgPlatformCloseWindow ( window );
168 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
171 * Creates a new top-level freeglut window
173 int FGAPIENTRY glutCreateWindow( const char* title )
175 /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
176 * XXX application has not already done so. The "freeglut" community
177 * XXX decided not to go this route (freeglut-developer e-mail from
178 * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
179 * XXX Desired 'freeglut' behaviour when there is no current window"
181 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
183 return fgCreateWindow( NULL, title, fgState.Position.Use,
184 fgState.Position.X, fgState.Position.Y,
185 fgState.Size.Use, fgState.Size.X, fgState.Size.Y,
186 GL_FALSE, GL_FALSE )->ID;
190 * This function creates a sub window.
192 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
195 SFG_Window* window = NULL;
196 SFG_Window* parent = NULL;
198 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
199 parent = fgWindowByID( parentID );
200 freeglut_return_val_if_fail( parent != NULL, 0 );
203 x = parent->State.Width + x ;
204 if ( w >= 0 ) x -= w ;
207 if ( w < 0 ) w = parent->State.Width - x + w ;
216 y = parent->State.Height + y ;
217 if ( h >= 0 ) y -= h ;
220 if ( h < 0 ) h = parent->State.Height - y + h ;
227 window = fgCreateWindow( parent, "", GL_TRUE, x, y, GL_TRUE, w, h, GL_FALSE, GL_FALSE );
234 * Destroys a window and all of its subwindows
236 void FGAPIENTRY glutDestroyWindow( int windowID )
239 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
240 window = fgWindowByID( windowID );
241 freeglut_return_if_fail( window != NULL );
243 fgExecutionState ExecState = fgState.ExecState;
244 fgAddToWindowDestroyList( window );
245 fgState.ExecState = ExecState;
250 * This function selects the specified window as the current window
252 void FGAPIENTRY glutSetWindow( int ID )
254 SFG_Window* window = NULL;
256 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
257 if( fgStructure.CurrentWindow != NULL )
258 if( fgStructure.CurrentWindow->ID == ID )
261 window = fgWindowByID( ID );
264 fgWarning( "glutSetWindow(): window ID %d not found!", ID );
268 fgSetWindow( window );
272 * This function returns the ID number of the current window, 0 if none exists
274 int FGAPIENTRY glutGetWindow( void )
276 SFG_Window *win = fgStructure.CurrentWindow;
278 * Since GLUT did not throw an error if this function was called without a prior call to
279 * "glutInit", this function shouldn't do so here. Instead let us return a zero.
280 * See Feature Request "[ 1307049 ] glutInit check".
282 if ( ! fgState.Initialised )
285 while ( win && win->IsMenu )
287 return win ? win->ID : 0;
291 * This function makes the current window visible
293 void FGAPIENTRY glutShowWindow( void )
295 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
296 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
298 fgPlatformGlutShowWindow ();
300 fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
304 * This function hides the current window
306 void FGAPIENTRY glutHideWindow( void )
308 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
309 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
311 fgPlatformGlutHideWindow ();
313 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
317 * Iconify the current window (top-level windows only)
319 void FGAPIENTRY glutIconifyWindow( void )
321 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
322 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
324 fgStructure.CurrentWindow->State.Visible = GL_FALSE;
326 fgPlatformGlutIconifyWindow ();
328 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
332 * Set the current window's title
334 void FGAPIENTRY glutSetWindowTitle( const char* title )
336 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
337 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
338 if( ! fgStructure.CurrentWindow->Parent )
340 fgPlatformGlutSetWindowTitle ( title );
345 * Set the current window's iconified title
347 void FGAPIENTRY glutSetIconTitle( const char* title )
349 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
350 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
352 if( ! fgStructure.CurrentWindow->Parent )
354 fgPlatformGlutSetIconTitle ( title );
359 * Change the current window's size
361 void FGAPIENTRY glutReshapeWindow( int width, int height )
363 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
364 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
366 if (glutGet(GLUT_FULL_SCREEN))
368 /* Leave full screen state before resizing. */
369 glutLeaveFullScreen();
372 fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
373 fgStructure.CurrentWindow->State.Width = width ;
374 fgStructure.CurrentWindow->State.Height = height;
378 * Change the current window's position
380 void FGAPIENTRY glutPositionWindow( int x, int y )
382 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPositionWindow" );
383 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
385 if (glutGet(GLUT_FULL_SCREEN))
387 /* Leave full screen state before moving. */
388 glutLeaveFullScreen();
391 fgPlatformGlutPositionWindow ( x, y );
395 * Lowers the current window (by Z order change)
397 void FGAPIENTRY glutPushWindow( void )
399 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
400 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
402 fgPlatformGlutPushWindow ();
406 * Raises the current window (by Z order change)
408 void FGAPIENTRY glutPopWindow( void )
410 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
411 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
413 fgPlatformGlutPopWindow ();
417 * Resize the current window so that it fits the whole screen
419 void FGAPIENTRY glutFullScreen( void )
423 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
424 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
426 win = fgStructure.CurrentWindow;
430 /* Child windows cannot be made fullscreen, consistent with GLUT's behavior
431 * Also, what would it mean for a child window to be fullscreen, given that it
432 * is confined to its parent?
434 fgWarning("glutFullScreen called on a child window, ignoring...");
437 else if (fgStructure.GameModeWindow != NULL && fgStructure.GameModeWindow->ID==win->ID)
439 /* Ignore fullscreen call on GameMode window, those are always fullscreen already */
443 fgPlatformGlutFullScreen ( win );
447 * If we are fullscreen, resize the current window back to its original size
449 void FGAPIENTRY glutLeaveFullScreen( void )
453 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
454 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
456 win = fgStructure.CurrentWindow;
458 fgPlatformGlutLeaveFullScreen ( win );
462 * Toggle the window's full screen state.
464 void FGAPIENTRY glutFullScreenToggle( void )
468 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreenToggle" );
469 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreenToggle" );
471 win = fgStructure.CurrentWindow;
473 fgPlatformGlutFullScreenToggle ( win );
477 * A.Donev: Set and retrieve the window's user data
479 void* FGAPIENTRY glutGetWindowData( void )
481 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
482 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
483 return fgStructure.CurrentWindow->UserData;
486 void FGAPIENTRY glutSetWindowData(void* data)
488 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
489 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
490 fgStructure.CurrentWindow->UserData = data;
493 /*** END OF FILE ***/