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 fghIsLegacyContextVersionRequested( void )
75 return fgState.MajorVersion < 2 || (fgState.MajorVersion == 2 && fgState.MinorVersion <= 1);
78 int fghIsLegacyContextRequested( void )
80 return fghIsLegacyContextVersionRequested() &&
81 fgState.ContextFlags == 0 &&
82 fgState.ContextProfile == 0;
85 int fghNumberOfAuxBuffersRequested( void )
87 if ( fgState.DisplayMode & GLUT_AUX4 ) {
90 if ( fgState.DisplayMode & GLUT_AUX3 ) {
93 if ( fgState.DisplayMode & GLUT_AUX2 ) {
96 if ( fgState.DisplayMode & GLUT_AUX1 ) { /* NOTE: Same as GLUT_AUX! */
97 return fgState.AuxiliaryBufferNumber;
102 int fghMapBit( int mask, int from, int to )
104 return ( mask & from ) ? to : 0;
108 void fghContextCreationError( void )
110 fgError( "Unable to create OpenGL %d.%d context (flags %x, profile %x)",
111 fgState.MajorVersion, fgState.MinorVersion, fgState.ContextFlags,
112 fgState.ContextProfile );
116 /* -- SYSTEM-DEPENDENT PRIVATE FUNCTIONS ------------------------------------ */
119 * Sets the OpenGL context and the fgStructure "Current Window" pointer to
120 * the window structure passed in.
122 void fgSetWindow ( SFG_Window *window )
124 fgPlatformSetWindow ( window );
126 fgStructure.CurrentWindow = window;
130 * Opens a window. Requires a SFG_Window object created and attached
131 * to the freeglut structure. OpenGL context is created here.
133 void fgOpenWindow( SFG_Window* window, const char* title,
134 GLboolean positionUse, int x, int y,
135 GLboolean sizeUse, int w, int h,
136 GLboolean gameMode, GLboolean isSubWindow )
138 fgPlatformOpenWindow( window, title,
141 gameMode, isSubWindow );
143 fgSetWindow( window );
145 window->Window.DoubleBuffered =
146 ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
148 #ifndef EGL_VERSION_1_0 /* No glDrawBuffer/glReadBuffer in GLES */
149 if ( ! window->Window.DoubleBuffered )
151 glDrawBuffer ( GL_FRONT );
152 glReadBuffer ( GL_FRONT );
158 * Closes a window, destroying the frame and OpenGL context
160 void fgCloseWindow( SFG_Window* window )
162 /* if we're in gamemode and we're closing the gamemode window,
163 * call glutLeaveGameMode first to make sure the gamemode is
164 * properly closed before closing the window
166 if (fgStructure.GameModeWindow != NULL && fgStructure.GameModeWindow->ID==window->ID)
169 fgPlatformCloseWindow ( window );
173 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
176 * Creates a new top-level freeglut window
178 int FGAPIENTRY glutCreateWindow( const char* title )
180 /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
181 * XXX application has not already done so. The "freeglut" community
182 * XXX decided not to go this route (freeglut-developer e-mail from
183 * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
184 * XXX Desired 'freeglut' behaviour when there is no current window"
186 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
188 return fgCreateWindow( NULL, title, fgState.Position.Use,
189 fgState.Position.X, fgState.Position.Y,
190 fgState.Size.Use, fgState.Size.X, fgState.Size.Y,
191 GL_FALSE, GL_FALSE )->ID;
195 * This function creates a sub window.
197 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
200 SFG_Window* window = NULL;
201 SFG_Window* parent = NULL;
203 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
204 parent = fgWindowByID( parentID );
205 freeglut_return_val_if_fail( parent != NULL, 0 );
208 x = parent->State.Width + x ;
209 if ( w >= 0 ) x -= w ;
212 if ( w < 0 ) w = parent->State.Width - x + w ;
221 y = parent->State.Height + y ;
222 if ( h >= 0 ) y -= h ;
225 if ( h < 0 ) h = parent->State.Height - y + h ;
232 window = fgCreateWindow( parent, "", GL_TRUE, x, y, GL_TRUE, w, h, GL_FALSE, GL_FALSE );
239 * Destroys a window and all of its subwindows
241 void FGAPIENTRY glutDestroyWindow( int windowID )
244 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
245 window = fgWindowByID( windowID );
246 freeglut_return_if_fail( window != NULL );
248 fgExecutionState ExecState = fgState.ExecState;
249 fgAddToWindowDestroyList( window );
250 fgState.ExecState = ExecState;
255 * This function selects the specified window as the current window
257 void FGAPIENTRY glutSetWindow( int ID )
259 SFG_Window* window = NULL;
261 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
262 if( fgStructure.CurrentWindow != NULL )
263 if( fgStructure.CurrentWindow->ID == ID )
266 window = fgWindowByID( ID );
269 fgWarning( "glutSetWindow(): window ID %d not found!", ID );
273 fgSetWindow( window );
277 * This function returns the ID number of the current window, 0 if none exists
279 int FGAPIENTRY glutGetWindow( void )
281 SFG_Window *win = fgStructure.CurrentWindow;
283 * Since GLUT did not throw an error if this function was called without a prior call to
284 * "glutInit", this function shouldn't do so here. Instead let us return a zero.
285 * See Feature Request "[ 1307049 ] glutInit check".
287 if ( ! fgState.Initialised )
290 while ( win && win->IsMenu )
292 return win ? win->ID : 0;
296 * This function makes the current window visible
298 void FGAPIENTRY glutShowWindow( void )
300 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
301 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
303 fgPlatformGlutShowWindow ();
305 fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
309 * This function hides the current window
311 void FGAPIENTRY glutHideWindow( void )
313 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
314 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
316 fgPlatformGlutHideWindow ();
318 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
322 * Iconify the current window (top-level windows only)
324 void FGAPIENTRY glutIconifyWindow( void )
326 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
327 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
329 fgStructure.CurrentWindow->State.Visible = GL_FALSE;
331 fgPlatformGlutIconifyWindow ();
333 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
337 * Set the current window's title
339 void FGAPIENTRY glutSetWindowTitle( const char* title )
341 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
342 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
343 if( ! fgStructure.CurrentWindow->Parent )
345 fgPlatformGlutSetWindowTitle ( title );
350 * Set the current window's iconified title
352 void FGAPIENTRY glutSetIconTitle( const char* title )
354 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
355 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
357 if( ! fgStructure.CurrentWindow->Parent )
359 fgPlatformGlutSetIconTitle ( title );
364 * Change the current window's size
366 void FGAPIENTRY glutReshapeWindow( int width, int height )
368 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
369 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
371 if (glutGet(GLUT_FULL_SCREEN))
373 /* Leave full screen state before resizing. */
374 glutLeaveFullScreen();
377 fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
378 fgStructure.CurrentWindow->State.Width = width ;
379 fgStructure.CurrentWindow->State.Height = height;
383 * Change the current window's position
385 void FGAPIENTRY glutPositionWindow( int x, int y )
387 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPositionWindow" );
388 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
390 if (glutGet(GLUT_FULL_SCREEN))
392 /* Leave full screen state before moving. */
393 glutLeaveFullScreen();
396 fgPlatformGlutPositionWindow ( x, y );
400 * Lowers the current window (by Z order change)
402 void FGAPIENTRY glutPushWindow( void )
404 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
405 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
407 fgPlatformGlutPushWindow ();
411 * Raises the current window (by Z order change)
413 void FGAPIENTRY glutPopWindow( void )
415 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
416 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
418 fgPlatformGlutPopWindow ();
422 * Resize the current window so that it fits the whole screen
424 void FGAPIENTRY glutFullScreen( void )
428 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
429 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
431 win = fgStructure.CurrentWindow;
435 /* Child windows cannot be made fullscreen, consistent with GLUT's behavior
436 * Also, what would it mean for a child window to be fullscreen, given that it
437 * is confined to its parent?
439 fgWarning("glutFullScreen called on a child window, ignoring...");
442 else if (fgStructure.GameModeWindow != NULL && fgStructure.GameModeWindow->ID==win->ID)
444 /* Ignore fullscreen call on GameMode window, those are always fullscreen already */
448 fgPlatformGlutFullScreen ( win );
452 * If we are fullscreen, resize the current window back to its original size
454 void FGAPIENTRY glutLeaveFullScreen( void )
458 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
459 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
461 win = fgStructure.CurrentWindow;
463 fgPlatformGlutLeaveFullScreen ( win );
467 * Toggle the window's full screen state.
469 void FGAPIENTRY glutFullScreenToggle( void )
473 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreenToggle" );
474 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreenToggle" );
476 win = fgStructure.CurrentWindow;
478 fgPlatformGlutFullScreenToggle ( win );
482 * A.Donev: Set and retrieve the window's user data
484 void* FGAPIENTRY glutGetWindowData( void )
486 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
487 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
488 return fgStructure.CurrentWindow->UserData;
491 void FGAPIENTRY glutSetWindowData(void* data)
493 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
494 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
495 fgStructure.CurrentWindow->UserData = data;
498 /*** END OF FILE ***/