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 );
151 * Closes a window, destroying the frame and OpenGL context
153 void fgCloseWindow( SFG_Window* window )
155 /* if we're in gamemode and we're closing the gamemode window,
156 * call glutLeaveGameMode first to make sure the gamemode is
157 * properly closed before closing the window
159 if (fgStructure.GameModeWindow != NULL && fgStructure.GameModeWindow->ID==window->ID)
162 fgPlatformCloseWindow ( window );
166 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
169 * Creates a new top-level freeglut window
171 int FGAPIENTRY glutCreateWindow( const char* title )
173 /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
174 * XXX application has not already done so. The "freeglut" community
175 * XXX decided not to go this route (freeglut-developer e-mail from
176 * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
177 * XXX Desired 'freeglut' behaviour when there is no current window"
179 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
181 return fgCreateWindow( NULL, title, fgState.Position.Use,
182 fgState.Position.X, fgState.Position.Y,
183 fgState.Size.Use, fgState.Size.X, fgState.Size.Y,
184 GL_FALSE, GL_FALSE )->ID;
188 * This function creates a sub window.
190 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
193 SFG_Window* window = NULL;
194 SFG_Window* parent = NULL;
196 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
197 parent = fgWindowByID( parentID );
198 freeglut_return_val_if_fail( parent != NULL, 0 );
201 x = parent->State.Width + x ;
202 if ( w >= 0 ) x -= w ;
205 if ( w < 0 ) w = parent->State.Width - x + w ;
214 y = parent->State.Height + y ;
215 if ( h >= 0 ) y -= h ;
218 if ( h < 0 ) h = parent->State.Height - y + h ;
225 window = fgCreateWindow( parent, "", GL_TRUE, x, y, GL_TRUE, w, h, GL_FALSE, GL_FALSE );
232 * Destroys a window and all of its subwindows
234 void FGAPIENTRY glutDestroyWindow( int windowID )
237 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
238 window = fgWindowByID( windowID );
239 freeglut_return_if_fail( window != NULL );
241 fgExecutionState ExecState = fgState.ExecState;
242 fgAddToWindowDestroyList( window );
243 fgState.ExecState = ExecState;
248 * This function selects the specified window as the current window
250 void FGAPIENTRY glutSetWindow( int ID )
252 SFG_Window* window = NULL;
254 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
255 if( fgStructure.CurrentWindow != NULL )
256 if( fgStructure.CurrentWindow->ID == ID )
259 window = fgWindowByID( ID );
262 fgWarning( "glutSetWindow(): window ID %d not found!", ID );
266 fgSetWindow( window );
270 * This function returns the ID number of the current window, 0 if none exists
272 int FGAPIENTRY glutGetWindow( void )
274 SFG_Window *win = fgStructure.CurrentWindow;
276 * Since GLUT did not throw an error if this function was called without a prior call to
277 * "glutInit", this function shouldn't do so here. Instead let us return a zero.
278 * See Feature Request "[ 1307049 ] glutInit check".
280 if ( ! fgState.Initialised )
283 while ( win && win->IsMenu )
285 return win ? win->ID : 0;
289 * This function makes the current window visible
291 void FGAPIENTRY glutShowWindow( void )
293 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
294 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
296 fgPlatformGlutShowWindow ();
298 fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
302 * This function hides the current window
304 void FGAPIENTRY glutHideWindow( void )
306 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
307 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
309 fgPlatformGlutHideWindow ();
311 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
315 * Iconify the current window (top-level windows only)
317 void FGAPIENTRY glutIconifyWindow( void )
319 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
320 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
322 fgStructure.CurrentWindow->State.Visible = GL_FALSE;
324 fgPlatformGlutIconifyWindow ();
326 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
330 * Set the current window's title
332 void FGAPIENTRY glutSetWindowTitle( const char* title )
334 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
335 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
336 if( ! fgStructure.CurrentWindow->Parent )
338 fgPlatformGlutSetWindowTitle ( title );
343 * Set the current window's iconified title
345 void FGAPIENTRY glutSetIconTitle( const char* title )
347 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
348 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
350 if( ! fgStructure.CurrentWindow->Parent )
352 fgPlatformGlutSetIconTitle ( title );
357 * Change the current window's size
359 void FGAPIENTRY glutReshapeWindow( int width, int height )
361 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
362 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
364 if (glutGet(GLUT_FULL_SCREEN))
366 /* Leave full screen state before resizing. */
367 glutLeaveFullScreen();
370 fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
371 fgStructure.CurrentWindow->State.Width = width ;
372 fgStructure.CurrentWindow->State.Height = height;
376 * Change the current window's position
378 void FGAPIENTRY glutPositionWindow( int x, int y )
380 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPositionWindow" );
381 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
383 if (glutGet(GLUT_FULL_SCREEN))
385 /* Leave full screen state before moving. */
386 glutLeaveFullScreen();
389 fgPlatformGlutPositionWindow ( x, y );
393 * Lowers the current window (by Z order change)
395 void FGAPIENTRY glutPushWindow( void )
397 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
398 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
400 fgPlatformGlutPushWindow ();
404 * Raises the current window (by Z order change)
406 void FGAPIENTRY glutPopWindow( void )
408 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
409 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
411 fgPlatformGlutPopWindow ();
415 * Resize the current window so that it fits the whole screen
417 void FGAPIENTRY glutFullScreen( void )
421 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
422 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
424 win = fgStructure.CurrentWindow;
428 /* Child windows cannot be made fullscreen, consistent with GLUT's behavior
429 * Also, what would it mean for a child window to be fullscreen, given that it
430 * is confined to its parent?
432 fgWarning("glutFullScreen called on a child window, ignoring...");
435 else if (fgStructure.GameModeWindow != NULL && fgStructure.GameModeWindow->ID==win->ID)
437 /* Ignore fullscreen call on GameMode window, those are always fullscreen already */
441 fgPlatformGlutFullScreen ( win );
445 * If we are fullscreen, resize the current window back to its original size
447 void FGAPIENTRY glutLeaveFullScreen( void )
451 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
452 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
454 win = fgStructure.CurrentWindow;
456 fgPlatformGlutLeaveFullScreen ( win );
460 * Toggle the window's full screen state.
462 void FGAPIENTRY glutFullScreenToggle( void )
466 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreenToggle" );
467 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreenToggle" );
469 win = fgStructure.CurrentWindow;
471 fgPlatformGlutFullScreenToggle ( win );
475 * A.Donev: Set and retrieve the window's user data
477 void* FGAPIENTRY glutGetWindowData( void )
479 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
480 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
481 return fgStructure.CurrentWindow->UserData;
484 void FGAPIENTRY glutSetWindowData(void* data)
486 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
487 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
488 fgStructure.CurrentWindow->UserData = data;
491 /*** END OF FILE ***/