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 fgPlatformGlutSetWindowTitle( const char* title );
60 extern void fgPlatformGlutSetIconTitle( const char* title );
63 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
65 int fghIsLegacyContextRequested( SFG_Window *win )
67 int vmajor = fgState.MajorVersion;
68 int vminor = fgState.MinorVersion;
69 /* XXX: menu windows are drawn with the fixed function pipeline, therefore
70 * the context created for them can't be a modern core-profile context.
71 * Force the traditional context creation for menu windows.
73 return vmajor < 2 || (vmajor == 2 && vminor <= 1) || win->IsMenu;
76 int fghNumberOfAuxBuffersRequested( void )
78 if ( fgState.DisplayMode & GLUT_AUX4 ) {
81 if ( fgState.DisplayMode & GLUT_AUX3 ) {
84 if ( fgState.DisplayMode & GLUT_AUX2 ) {
87 if ( fgState.DisplayMode & GLUT_AUX1 ) { /* NOTE: Same as GLUT_AUX! */
88 return fgState.AuxiliaryBufferNumber;
93 int fghMapBit( int mask, int from, int to )
95 return ( mask & from ) ? to : 0;
99 void fghContextCreationError( void )
101 fgError( "Unable to create OpenGL %d.%d context (flags %x, profile %x)",
102 fgState.MajorVersion, fgState.MinorVersion, fgState.ContextFlags,
103 fgState.ContextProfile );
107 /* -- SYSTEM-DEPENDENT PRIVATE FUNCTIONS ------------------------------------ */
110 * Sets the OpenGL context and the fgStructure "Current Window" pointer to
111 * the window structure passed in.
113 void fgSetWindow ( SFG_Window *window )
115 fgPlatformSetWindow ( window );
117 fgStructure.CurrentWindow = window;
121 * Opens a window. Requires a SFG_Window object created and attached
122 * to the freeglut structure. OpenGL context is created here.
124 void fgOpenWindow( SFG_Window* window, const char* title,
125 GLboolean positionUse, int x, int y,
126 GLboolean sizeUse, int w, int h,
127 GLboolean gameMode, GLboolean isSubWindow )
129 fgPlatformOpenWindow( window, title,
132 gameMode, isSubWindow );
134 fgSetWindow( window );
136 #ifndef EGL_VERSION_1_0
137 window->Window.DoubleBuffered =
138 ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
140 if ( ! window->Window.DoubleBuffered )
142 glDrawBuffer ( GL_FRONT );
143 glReadBuffer ( GL_FRONT );
146 /* - EGL is always double-buffered */
147 /* - No glDrawBuffer/glReadBuffer in GLES */
148 window->Window.DoubleBuffered = 1;
150 window->Window.attribute_v_coord = -1;
151 window->Window.attribute_v_normal = -1;
152 window->Window.attribute_v_texture = -1;
156 window->State.WorkMask |= GLUT_INIT_WORK;
160 * Closes a window, destroying the frame and OpenGL context
162 void fgCloseWindow( SFG_Window* window )
164 /* if we're in gamemode and we're closing the gamemode window,
165 * call glutLeaveGameMode first to make sure the gamemode is
166 * properly closed before closing the window
168 if (fgStructure.GameModeWindow != NULL && fgStructure.GameModeWindow->ID==window->ID)
171 fgPlatformCloseWindow ( window );
175 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
178 * Creates a new top-level freeglut window
180 int FGAPIENTRY glutCreateWindow( const char* title )
182 /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
183 * XXX application has not already done so. The "freeglut" community
184 * XXX decided not to go this route (freeglut-developer e-mail from
185 * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
186 * XXX Desired 'freeglut' behaviour when there is no current window")
188 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
190 return fgCreateWindow( NULL, title,
191 fgState.Position.Use, fgState.Position.X, fgState.Position.Y,
192 fgState.Size.Use, fgState.Size.X, fgState.Size.Y,
193 GL_FALSE, GL_FALSE )->ID;
197 * This function creates a sub window.
199 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
202 SFG_Window* window = NULL;
203 SFG_Window* parent = NULL;
205 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
206 parent = fgWindowByID( parentID );
207 freeglut_return_val_if_fail( parent != NULL, 0 );
209 if ( fgState.AllowNegativeWindowPosition )
211 /* XXX This results in different widths/heights than if AllowNegativeWindowPosition
212 * XXX was false. The "freeglut" community defined this logic.
213 * XXX (freeglut-developer e-mail from Diederick C. Niehorster, 11/15/2015, 4:06 PM EST.
214 * XXX "Re: [Freeglut-developer] glutInitWindowPosition with negative coordinate(s)")
217 if ( w < 0 ) w = parent->State.Width + w ;
218 if ( h < 0 ) h = parent->State.Height + h ;
224 x = parent->State.Width + x ;
225 if ( w > 0 ) x -= w ;
228 if ( w < 0 ) w = parent->State.Width - x + w ;
237 y = parent->State.Height + y ;
238 if ( h > 0 ) y -= h ;
241 if ( h < 0 ) h = parent->State.Height - y + h ;
249 window = fgCreateWindow( parent, "",
252 GL_FALSE, GL_FALSE );
259 * Destroys a window and all of its subwindows
261 void FGAPIENTRY glutDestroyWindow( int windowID )
264 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
265 window = fgWindowByID( windowID );
266 freeglut_return_if_fail( window != NULL );
268 fgExecutionState ExecState = fgState.ExecState;
269 fgAddToWindowDestroyList( window );
270 fgState.ExecState = ExecState;
275 * This function selects the specified window as the current window
277 void FGAPIENTRY glutSetWindow( int ID )
279 SFG_Window* window = NULL;
281 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
282 if( fgStructure.CurrentWindow != NULL )
283 if( fgStructure.CurrentWindow->ID == ID )
286 window = fgWindowByID( ID );
289 fgWarning( "glutSetWindow(): window ID %d not found!", ID );
293 fgSetWindow( window );
297 * This function returns the ID number of the current window, 0 if none exists
299 int FGAPIENTRY glutGetWindow( void )
301 SFG_Window *win = fgStructure.CurrentWindow;
303 * Since GLUT did not throw an error if this function was called without a prior call to
304 * "glutInit", this function shouldn't do so here. Instead let us return a zero.
305 * See Feature Request "[ 1307049 ] glutInit check".
307 if ( ! fgState.Initialised )
310 while ( win && win->IsMenu )
312 return win ? win->ID : 0;
316 * This function makes the current window visible
318 void FGAPIENTRY glutShowWindow( void )
320 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
321 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
323 fgStructure.CurrentWindow->State.WorkMask |= GLUT_VISIBILITY_WORK;
324 fgStructure.CurrentWindow->State.DesiredVisibility = DesireNormalState;
326 fgStructure.CurrentWindow->State.WorkMask |= GLUT_DISPLAY_WORK;
330 * This function hides the current window
332 void FGAPIENTRY glutHideWindow( void )
334 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
335 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
337 fgStructure.CurrentWindow->State.WorkMask |= GLUT_VISIBILITY_WORK;
338 fgStructure.CurrentWindow->State.DesiredVisibility = DesireHiddenState;
340 fgStructure.CurrentWindow->State.WorkMask &= ~GLUT_DISPLAY_WORK;
344 * Iconify the current window (top-level windows only)
346 void FGAPIENTRY glutIconifyWindow( void )
348 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
349 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
351 fgStructure.CurrentWindow->State.WorkMask |= GLUT_VISIBILITY_WORK;
352 fgStructure.CurrentWindow->State.DesiredVisibility = DesireIconicState;
354 fgStructure.CurrentWindow->State.WorkMask &= ~GLUT_DISPLAY_WORK;
358 * Set the current window's title
360 void FGAPIENTRY glutSetWindowTitle( const char* title )
362 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
363 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
364 if( ! fgStructure.CurrentWindow->Parent )
366 fgPlatformGlutSetWindowTitle ( title );
371 * Set the current window's iconified title
373 void FGAPIENTRY glutSetIconTitle( const char* title )
375 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
376 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
378 if( ! fgStructure.CurrentWindow->Parent )
380 fgPlatformGlutSetIconTitle ( title );
385 * Change the current window's size
387 void FGAPIENTRY glutReshapeWindow( int width, int height )
389 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
390 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
392 if (glutGet(GLUT_FULL_SCREEN))
394 /* Leave full screen state before resizing. */
395 glutLeaveFullScreen();
398 fgStructure.CurrentWindow->State.WorkMask |= GLUT_SIZE_WORK;
399 fgStructure.CurrentWindow->State.DesiredWidth = width ;
400 fgStructure.CurrentWindow->State.DesiredHeight = height;
404 * Change the current window's position
406 void FGAPIENTRY glutPositionWindow( int x, int y )
408 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPositionWindow" );
409 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
411 if (glutGet(GLUT_FULL_SCREEN))
413 /* Leave full screen state before moving. */
414 glutLeaveFullScreen();
417 fgStructure.CurrentWindow->State.WorkMask |= GLUT_POSITION_WORK;
418 fgStructure.CurrentWindow->State.DesiredXpos = x;
419 fgStructure.CurrentWindow->State.DesiredYpos = y;
423 * Lowers the current window (by Z order change)
425 void FGAPIENTRY glutPushWindow( void )
427 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
428 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
430 fgStructure.CurrentWindow->State.WorkMask |= GLUT_ZORDER_WORK;
431 fgStructure.CurrentWindow->State.DesiredZOrder = -1;
435 * Raises the current window (by Z order change)
437 void FGAPIENTRY glutPopWindow( void )
439 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
440 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
442 fgStructure.CurrentWindow->State.WorkMask |= GLUT_ZORDER_WORK;
443 fgStructure.CurrentWindow->State.DesiredZOrder = 1;
447 * Resize the current window so that it fits the whole screen
449 void FGAPIENTRY glutFullScreen( void )
453 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
454 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
456 win = fgStructure.CurrentWindow;
460 /* Child windows cannot be made fullscreen, consistent with GLUT's behavior
461 * Also, what would it mean for a child window to be fullscreen, given that it
462 * is confined to its parent?
464 fgWarning("glutFullScreen called on a child window, ignoring...");
467 else if (fgStructure.GameModeWindow != NULL && fgStructure.GameModeWindow->ID==win->ID && win->State.IsFullscreen)
469 /* Ignore fullscreen call on GameMode window, those are always fullscreen already
470 * only exception is when first entering GameMode
475 if (!win->State.IsFullscreen)
476 win->State.WorkMask |= GLUT_FULL_SCREEN_WORK;
480 * If we are fullscreen, resize the current window back to its original size
482 void FGAPIENTRY glutLeaveFullScreen( void )
484 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
485 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
487 if (fgStructure.CurrentWindow->State.IsFullscreen)
488 fgStructure.CurrentWindow->State.WorkMask |= GLUT_FULL_SCREEN_WORK;
492 * Toggle the window's full screen state.
494 void FGAPIENTRY glutFullScreenToggle( void )
496 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreenToggle" );
497 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreenToggle" );
499 fgStructure.CurrentWindow->State.WorkMask |= GLUT_FULL_SCREEN_WORK;
503 * A.Donev: Set and retrieve the window's user data
505 void* FGAPIENTRY glutGetWindowData( void )
507 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
508 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
509 return fgStructure.CurrentWindow->UserData;
512 void FGAPIENTRY glutSetWindowData(void* data)
514 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
515 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
516 fgStructure.CurrentWindow->UserData = data;
519 /*** END OF FILE ***/