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"
34 * TODO BEFORE THE STABLE RELEASE:
36 * fgSetupPixelFormat -- ignores the display mode settings
37 * fgOpenWindow() -- check the Win32 version, -iconic handling!
38 * fgCloseWindow() -- check the Win32 version
39 * glutCreateWindow() -- Check when default position and size is {-1,-1}
40 * glutCreateSubWindow() -- Check when default position and size is {-1,-1}
41 * glutDestroyWindow() -- check the Win32 version
42 * glutSetWindow() -- check the Win32 version
43 * glutSetWindowTitle() -- check the Win32 version
44 * glutSetIconTitle() -- check the Win32 version
45 * glutShowWindow() -- check the Win32 version
46 * glutHideWindow() -- check the Win32 version
47 * glutIconifyWindow() -- check the Win32 version
48 * glutPushWindow() -- check the Win32 version
49 * glutPopWindow() -- check the Win32 version
53 extern void fgPlatformSetWindow ( SFG_Window *window );
54 extern void fgPlatformOpenWindow( SFG_Window* window, const char* title,
55 GLboolean positionUse, int x, int y,
56 GLboolean sizeUse, int w, int h,
57 GLboolean gameMode, GLboolean isSubWindow );
58 extern void fgPlatformCloseWindow( SFG_Window* window );
59 extern void fgPlatformGlutShowWindow( void );
60 extern void fgPlatformGlutHideWindow( void );
61 extern void fgPlatformGlutIconifyWindow( void );
62 extern void fgPlatformGlutSetWindowTitle( const char* title );
63 extern void fgPlatformGlutSetIconTitle( const char* title );
64 extern void fgPlatformGlutPositionWindow( int x, int y );
65 extern void fgPlatformGlutPushWindow( void );
66 extern void fgPlatformGlutPopWindow( void );
67 extern void fgPlatformGlutFullScreen( SFG_Window *win );
68 extern void fgPlatformGlutLeaveFullScreen( SFG_Window *win );
69 extern void fgPlatformGlutFullScreenToggle( SFG_Window *win );
72 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
74 int fghIsLegacyContextRequested( void )
76 return fgState.MajorVersion < 2 || (fgState.MajorVersion == 2 && fgState.MinorVersion <= 1);
79 int fghNumberOfAuxBuffersRequested( void )
81 if ( fgState.DisplayMode & GLUT_AUX4 ) {
84 if ( fgState.DisplayMode & GLUT_AUX3 ) {
87 if ( fgState.DisplayMode & GLUT_AUX2 ) {
90 if ( fgState.DisplayMode & GLUT_AUX1 ) { /* NOTE: Same as GLUT_AUX! */
91 return fgState.AuxiliaryBufferNumber;
96 int fghMapBit( int mask, int from, int to )
98 return ( mask & from ) ? to : 0;
102 void fghContextCreationError( void )
104 fgError( "Unable to create OpenGL %d.%d context (flags %x, profile %x)",
105 fgState.MajorVersion, fgState.MinorVersion, fgState.ContextFlags,
106 fgState.ContextProfile );
110 /* -- SYSTEM-DEPENDENT PRIVATE FUNCTIONS ------------------------------------ */
113 * Sets the OpenGL context and the fgStructure "Current Window" pointer to
114 * the window structure passed in.
116 void fgSetWindow ( SFG_Window *window )
118 fgPlatformSetWindow ( window );
120 fgStructure.CurrentWindow = window;
124 * Opens a window. Requires a SFG_Window object created and attached
125 * to the freeglut structure. OpenGL context is created here.
127 void fgOpenWindow( SFG_Window* window, const char* title,
128 GLboolean positionUse, int x, int y,
129 GLboolean sizeUse, int w, int h,
130 GLboolean gameMode, GLboolean isSubWindow )
132 fgPlatformOpenWindow( window, title,
135 gameMode, isSubWindow );
137 fgSetWindow( window );
139 window->Window.DoubleBuffered =
140 ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
142 #ifndef EGL_VERSION_1_0 /* No glDrawBuffer/glReadBuffer in GLES */
143 if ( ! window->Window.DoubleBuffered )
145 glDrawBuffer ( GL_FRONT );
146 glReadBuffer ( GL_FRONT );
149 window->Window.attribute_v_coord = -1;
150 window->Window.attribute_v_normal = -1;
156 * Closes a window, destroying the frame and OpenGL context
158 void fgCloseWindow( SFG_Window* window )
160 /* if we're in gamemode and we're closing the gamemode window,
161 * call glutLeaveGameMode first to make sure the gamemode is
162 * properly closed before closing the window
164 if (fgStructure.GameModeWindow != NULL && fgStructure.GameModeWindow->ID==window->ID)
167 fgPlatformCloseWindow ( window );
171 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
174 * Creates a new top-level freeglut window
176 int FGAPIENTRY glutCreateWindow( const char* title )
178 /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
179 * XXX application has not already done so. The "freeglut" community
180 * XXX decided not to go this route (freeglut-developer e-mail from
181 * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
182 * XXX Desired 'freeglut' behaviour when there is no current window"
184 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
186 return fgCreateWindow( NULL, title, fgState.Position.Use,
187 fgState.Position.X, fgState.Position.Y,
188 fgState.Size.Use, fgState.Size.X, fgState.Size.Y,
189 GL_FALSE, GL_FALSE )->ID;
193 * This function creates a sub window.
195 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
198 SFG_Window* window = NULL;
199 SFG_Window* parent = NULL;
201 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
202 parent = fgWindowByID( parentID );
203 freeglut_return_val_if_fail( parent != NULL, 0 );
206 x = parent->State.Width + x ;
207 if ( w >= 0 ) x -= w ;
210 if ( w < 0 ) w = parent->State.Width - x + w ;
219 y = parent->State.Height + y ;
220 if ( h >= 0 ) y -= h ;
223 if ( h < 0 ) h = parent->State.Height - y + h ;
230 window = fgCreateWindow( parent, "", GL_TRUE, x, y, GL_TRUE, w, h, GL_FALSE, GL_FALSE );
237 * Destroys a window and all of its subwindows
239 void FGAPIENTRY glutDestroyWindow( int windowID )
242 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
243 window = fgWindowByID( windowID );
244 freeglut_return_if_fail( window != NULL );
246 fgExecutionState ExecState = fgState.ExecState;
247 fgAddToWindowDestroyList( window );
248 fgState.ExecState = ExecState;
253 * This function selects the specified window as the current window
255 void FGAPIENTRY glutSetWindow( int ID )
257 SFG_Window* window = NULL;
259 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
260 if( fgStructure.CurrentWindow != NULL )
261 if( fgStructure.CurrentWindow->ID == ID )
264 window = fgWindowByID( ID );
267 fgWarning( "glutSetWindow(): window ID %d not found!", ID );
271 fgSetWindow( window );
275 * This function returns the ID number of the current window, 0 if none exists
277 int FGAPIENTRY glutGetWindow( void )
279 SFG_Window *win = fgStructure.CurrentWindow;
281 * Since GLUT did not throw an error if this function was called without a prior call to
282 * "glutInit", this function shouldn't do so here. Instead let us return a zero.
283 * See Feature Request "[ 1307049 ] glutInit check".
285 if ( ! fgState.Initialised )
288 while ( win && win->IsMenu )
290 return win ? win->ID : 0;
294 * This function makes the current window visible
296 void FGAPIENTRY glutShowWindow( void )
298 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
299 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
301 fgPlatformGlutShowWindow ();
303 fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
307 * This function hides the current window
309 void FGAPIENTRY glutHideWindow( void )
311 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
312 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
314 fgPlatformGlutHideWindow ();
316 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
320 * Iconify the current window (top-level windows only)
322 void FGAPIENTRY glutIconifyWindow( void )
324 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
325 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
327 fgStructure.CurrentWindow->State.Visible = GL_FALSE;
329 fgPlatformGlutIconifyWindow ();
331 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
335 * Set the current window's title
337 void FGAPIENTRY glutSetWindowTitle( const char* title )
339 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
340 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
341 if( ! fgStructure.CurrentWindow->Parent )
343 fgPlatformGlutSetWindowTitle ( title );
348 * Set the current window's iconified title
350 void FGAPIENTRY glutSetIconTitle( const char* title )
352 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
353 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
355 if( ! fgStructure.CurrentWindow->Parent )
357 fgPlatformGlutSetIconTitle ( title );
362 * Change the current window's size
364 void FGAPIENTRY glutReshapeWindow( int width, int height )
366 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
367 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
369 if (glutGet(GLUT_FULL_SCREEN))
371 /* Leave full screen state before resizing. */
372 glutLeaveFullScreen();
375 fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
376 fgStructure.CurrentWindow->State.Width = width ;
377 fgStructure.CurrentWindow->State.Height = height;
381 * Change the current window's position
383 void FGAPIENTRY glutPositionWindow( int x, int y )
385 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPositionWindow" );
386 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
388 if (glutGet(GLUT_FULL_SCREEN))
390 /* Leave full screen state before moving. */
391 glutLeaveFullScreen();
394 fgPlatformGlutPositionWindow ( x, y );
398 * Lowers the current window (by Z order change)
400 void FGAPIENTRY glutPushWindow( void )
402 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
403 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
405 fgPlatformGlutPushWindow ();
409 * Raises the current window (by Z order change)
411 void FGAPIENTRY glutPopWindow( void )
413 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
414 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
416 fgPlatformGlutPopWindow ();
420 * Resize the current window so that it fits the whole screen
422 void FGAPIENTRY glutFullScreen( void )
426 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
427 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
429 win = fgStructure.CurrentWindow;
433 /* Child windows cannot be made fullscreen, consistent with GLUT's behavior
434 * Also, what would it mean for a child window to be fullscreen, given that it
435 * is confined to its parent?
437 fgWarning("glutFullScreen called on a child window, ignoring...");
440 else if (fgStructure.GameModeWindow != NULL && fgStructure.GameModeWindow->ID==win->ID)
442 /* Ignore fullscreen call on GameMode window, those are always fullscreen already */
446 fgPlatformGlutFullScreen ( win );
450 * If we are fullscreen, resize the current window back to its original size
452 void FGAPIENTRY glutLeaveFullScreen( void )
456 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
457 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
459 win = fgStructure.CurrentWindow;
461 fgPlatformGlutLeaveFullScreen ( win );
465 * Toggle the window's full screen state.
467 void FGAPIENTRY glutFullScreenToggle( void )
471 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreenToggle" );
472 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreenToggle" );
474 win = fgStructure.CurrentWindow;
476 fgPlatformGlutFullScreenToggle ( win );
480 * A.Donev: Set and retrieve the window's user data
482 void* FGAPIENTRY glutGetWindowData( void )
484 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
485 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
486 return fgStructure.CurrentWindow->UserData;
489 void FGAPIENTRY glutSetWindowData(void* data)
491 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
492 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
493 fgStructure.CurrentWindow->UserData = data;
496 /*** END OF FILE ***/