Added support for SDL_VIDEO_WINDOW_POS and SDL_VIDEO_CENTERED on Windows
authorSam Lantinga <slouken@libsdl.org>
Mon, 16 Feb 2004 21:09:24 +0000
changeset 83331fa08b36380
parent 832 f003714db2f4
child 834 d37179d10ccc
Added support for SDL_VIDEO_WINDOW_POS and SDL_VIDEO_CENTERED on Windows
src/video/wincommon/SDL_lowvideo.h
src/video/wincommon/SDL_sysevents.c
src/video/windib/SDL_dibevents.c
src/video/windib/SDL_dibvideo.c
src/video/windx5/SDL_dx5events.c
src/video/windx5/SDL_dx5video.c
     1.1 --- a/src/video/wincommon/SDL_lowvideo.h	Mon Feb 16 21:06:10 2004 +0000
     1.2 +++ b/src/video/wincommon/SDL_lowvideo.h	Mon Feb 16 21:09:24 2004 +0000
     1.3 @@ -83,6 +83,10 @@
     1.4  /* The bounds of the window in screen coordinates */
     1.5  extern RECT SDL_bounds;
     1.6  
     1.7 +/* The position of the window in windowed mode */
     1.8 +extern int SDL_windowX;
     1.9 +extern int SDL_windowY;
    1.10 +
    1.11  /* Flag -- SDL is performing a resize, rather than the user */
    1.12  extern int SDL_resizing;
    1.13  
     2.1 --- a/src/video/wincommon/SDL_sysevents.c	Mon Feb 16 21:06:10 2004 +0000
     2.2 +++ b/src/video/wincommon/SDL_sysevents.c	Mon Feb 16 21:09:24 2004 +0000
     2.3 @@ -59,6 +59,8 @@
     2.4  HINSTANCE SDL_Instance = NULL;
     2.5  HWND SDL_Window = NULL;
     2.6  RECT SDL_bounds = {0, 0, 0, 0};
     2.7 +int SDL_windowX = 0;
     2.8 +int SDL_windowY = 0;
     2.9  int SDL_resizing = 0;
    2.10  int mouse_relative = 0;
    2.11  int posted = 0;
    2.12 @@ -462,6 +464,10 @@
    2.13  			GetClientRect(SDL_Window, &SDL_bounds);
    2.14  			ClientToScreen(SDL_Window, (LPPOINT)&SDL_bounds);
    2.15  			ClientToScreen(SDL_Window, (LPPOINT)&SDL_bounds+1);
    2.16 +			if ( SDL_bounds.left || SDL_bounds.top ) {
    2.17 +				SDL_windowX = SDL_bounds.left;
    2.18 +				SDL_windowY = SDL_bounds.top;
    2.19 +			}
    2.20  			w = SDL_bounds.right-SDL_bounds.left;
    2.21  			h = SDL_bounds.bottom-SDL_bounds.top;
    2.22  			if ( this->input_grab != SDL_GRAB_OFF ) {
     3.1 --- a/src/video/windib/SDL_dibevents.c	Mon Feb 16 21:06:10 2004 +0000
     3.2 +++ b/src/video/windib/SDL_dibevents.c	Mon Feb 16 21:09:24 2004 +0000
     3.3 @@ -379,7 +379,7 @@
     3.4  	} else {
     3.5  		SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
     3.6                          (WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX),
     3.7 -                                 0, 0, 0, 0, NULL, NULL, SDL_Instance, NULL);
     3.8 +                        CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL, SDL_Instance, NULL);
     3.9  		if ( SDL_Window == NULL ) {
    3.10  			SDL_SetError("Couldn't create window");
    3.11  			return(-1);
     4.1 --- a/src/video/windib/SDL_dibvideo.c	Mon Feb 16 21:06:10 2004 +0000
     4.2 +++ b/src/video/windib/SDL_dibvideo.c	Mon Feb 16 21:09:24 2004 +0000
     4.3 @@ -463,12 +463,8 @@
     4.4  	HDC hdc;
     4.5  	RECT bounds;
     4.6  	int x, y;
     4.7 -	BOOL was_visible;
     4.8  	Uint32 Rmask, Gmask, Bmask;
     4.9  
    4.10 -	/* See whether or not we should center the window */
    4.11 -	was_visible = IsWindowVisible(SDL_Window);
    4.12 -
    4.13  	/* Clean up any GL context that may be hanging around */
    4.14  	if ( current->flags & SDL_OPENGL ) {
    4.15  		WIN_GL_ShutDown(this);
    4.16 @@ -599,7 +595,7 @@
    4.17  	}
    4.18  
    4.19  	/* DJM: Don't piss of anyone who has setup his own window */
    4.20 -	if (!SDL_windowid)
    4.21 +	if ( SDL_windowid == NULL )
    4.22  		SetWindowLong(SDL_Window, GWL_STYLE, style);
    4.23  
    4.24  	/* Delete the old bitmap if necessary */
    4.25 @@ -678,24 +674,47 @@
    4.26  	if ( SDL_windowid == NULL ) {
    4.27  		HWND top;
    4.28  		UINT swp_flags;
    4.29 +		const char *window = getenv("SDL_VIDEO_WINDOW_POS");
    4.30 +		const char *center = getenv("SDL_VIDEO_CENTERED");
    4.31 +
    4.32 +		if ( !SDL_windowX && !SDL_windowY ) {
    4.33 +			if ( window ) {
    4.34 +				if ( sscanf(window, "%d,%d", &x, &y) == 2 ) {
    4.35 +					SDL_windowX = x;
    4.36 +					SDL_windowY = y;
    4.37 +				}
    4.38 +				if ( strcmp(window, "center") == 0 ) {
    4.39 +					center = window;
    4.40 +					window = NULL;
    4.41 +				}
    4.42 +			}
    4.43 +		}
    4.44 +		swp_flags = (SWP_NOCOPYBITS | SWP_SHOWWINDOW);
    4.45  
    4.46  		SDL_resizing = 1;
    4.47 -		bounds.left = 0;
    4.48 -		bounds.top = 0;
    4.49 -		bounds.right = video->w;
    4.50 -		bounds.bottom = video->h;
    4.51 +		bounds.left = SDL_windowX;
    4.52 +		bounds.top = SDL_windowY;
    4.53 +		bounds.right = SDL_windowX+video->w;
    4.54 +		bounds.bottom = SDL_windowY+video->h;
    4.55  		AdjustWindowRectEx(&bounds, GetWindowLong(SDL_Window, GWL_STYLE), FALSE, 0);
    4.56  		width = bounds.right-bounds.left;
    4.57  		height = bounds.bottom-bounds.top;
    4.58 -		x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
    4.59 -		y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
    4.60 +		if ( (flags & SDL_FULLSCREEN) ) {
    4.61 +			x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
    4.62 +			y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
    4.63 +		} else if ( SDL_windowX || SDL_windowY || window ) {
    4.64 +			x = bounds.left;
    4.65 +			y = bounds.top;
    4.66 +		} else if ( center ) {
    4.67 +			x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
    4.68 +			y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
    4.69 +		} else {
    4.70 +			x = y = -1;
    4.71 +			swp_flags |= SWP_NOMOVE;
    4.72 +		}
    4.73  		if ( y < 0 ) { /* Cover up title bar for more client area */
    4.74  			y -= GetSystemMetrics(SM_CYCAPTION)/2;
    4.75  		}
    4.76 -		swp_flags = (SWP_NOCOPYBITS | SWP_FRAMECHANGED | SWP_SHOWWINDOW);
    4.77 -		if ( was_visible && !(flags & SDL_FULLSCREEN) ) {
    4.78 -			swp_flags |= SWP_NOMOVE;
    4.79 -		}
    4.80  		if ( flags & SDL_FULLSCREEN ) {
    4.81  			top = HWND_TOPMOST;
    4.82  		} else {
     5.1 --- a/src/video/windx5/SDL_dx5events.c	Mon Feb 16 21:06:10 2004 +0000
     5.2 +++ b/src/video/windx5/SDL_dx5events.c	Mon Feb 16 21:09:24 2004 +0000
     5.3 @@ -856,7 +856,7 @@
     5.4  	} else {
     5.5  		SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
     5.6                          (WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX),
     5.7 -                                 0, 0, 0, 0, NULL, NULL, SDL_Instance, NULL);
     5.8 +                        CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL, SDL_Instance, NULL);
     5.9  		if ( SDL_Window == NULL ) {
    5.10  			SDL_SetError("Couldn't create window");
    5.11  			return(-1);
     6.1 --- a/src/video/windx5/SDL_dx5video.c	Mon Feb 16 21:06:10 2004 +0000
     6.2 +++ b/src/video/windx5/SDL_dx5video.c	Mon Feb 16 21:09:24 2004 +0000
     6.3 @@ -1003,17 +1003,14 @@
     6.4  			(WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX);
     6.5  	const DWORD resizestyle =
     6.6  			(WS_THICKFRAME|WS_MAXIMIZEBOX);
     6.7 +	int windowX, windowY;
     6.8  	DDSURFACEDESC ddsd;
     6.9  	LPDIRECTDRAWSURFACE  dd_surface1;
    6.10  	LPDIRECTDRAWSURFACE3 dd_surface3;
    6.11 -	BOOL was_visible;
    6.12  
    6.13  #ifdef DDRAW_DEBUG
    6.14   fprintf(stderr, "Setting %dx%dx%d video mode\n", width, height, bpp);
    6.15  #endif
    6.16 -	/* See whether or not we should center the window */
    6.17 -	was_visible = IsWindowVisible(SDL_Window);
    6.18 -
    6.19  	/* Clean up any previous DirectDraw surfaces */
    6.20  	if ( current->hwdata ) {
    6.21  		this->FreeHWSurface(this, current);
    6.22 @@ -1134,31 +1131,57 @@
    6.23  			if (IsZoomed(SDL_Window)) style |= WS_MAXIMIZE;
    6.24  #endif
    6.25  		}
    6.26 -		SetWindowLong(SDL_Window, GWL_STYLE, style);
    6.27 +
    6.28 +		/* DJM: Don't piss of anyone who has setup his own window */
    6.29 +		if ( SDL_windowid == NULL )
    6.30 +			SetWindowLong(SDL_Window, GWL_STYLE, style);
    6.31  
    6.32  		/* Resize the window (copied from SDL WinDIB driver) */
    6.33  		if ( SDL_windowid == NULL ) {
    6.34  			HWND top;
    6.35  			UINT swp_flags;
    6.36 +			const char *window = getenv("SDL_VIDEO_WINDOW_POS");
    6.37 +			const char *center = getenv("SDL_VIDEO_CENTERED");
    6.38 +
    6.39 +			if ( !SDL_windowX && !SDL_windowY ) {
    6.40 +				if ( window ) {
    6.41 +					if ( sscanf(window, "%d,%d", &x, &y) == 2 ) {
    6.42 +						SDL_windowX = x;
    6.43 +						SDL_windowY = y;
    6.44 +					}
    6.45 +					if ( strcmp(window, "center") == 0 ) {
    6.46 +						center = window;
    6.47 +						window = NULL;
    6.48 +					}
    6.49 +				}
    6.50 +			}
    6.51 +			swp_flags = (SWP_NOCOPYBITS | SWP_SHOWWINDOW);
    6.52  
    6.53  			SDL_resizing = 1;
    6.54 -			bounds.top    = 0;
    6.55 -			bounds.bottom = video->h;
    6.56 -			bounds.left   = 0;
    6.57 -			bounds.right  = video->w;
    6.58 +			bounds.left = SDL_windowX;
    6.59 +			bounds.top = SDL_windowY;
    6.60 +			bounds.right = SDL_windowX+video->w;
    6.61 +			bounds.bottom = SDL_windowY+video->h;
    6.62  			AdjustWindowRectEx(&bounds, GetWindowLong(SDL_Window, GWL_STYLE), FALSE, 0);
    6.63  			width = bounds.right-bounds.left;
    6.64  			height = bounds.bottom-bounds.top;
    6.65 -			x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
    6.66 -			y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
    6.67 +			if ( (flags & SDL_FULLSCREEN) ) {
    6.68 +				x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
    6.69 +				y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
    6.70 +			} else if ( SDL_windowX || SDL_windowY || window ) {
    6.71 +				x = bounds.left;
    6.72 +				y = bounds.top;
    6.73 +			} else if ( center ) {
    6.74 +				x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
    6.75 +				y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
    6.76 +			} else {
    6.77 +				x = y = -1;
    6.78 +				swp_flags |= SWP_NOMOVE;
    6.79 +			}
    6.80  			if ( y < 0 ) { /* Cover up title bar for more client area */
    6.81  				y -= GetSystemMetrics(SM_CYCAPTION)/2;
    6.82  			}
    6.83 -			swp_flags = (SWP_NOCOPYBITS | SWP_FRAMECHANGED | SWP_SHOWWINDOW);
    6.84 -			if ( was_visible && !(video->flags & SDL_FULLSCREEN) ) {
    6.85 -				swp_flags |= SWP_NOMOVE;
    6.86 -			}
    6.87 -			if ( video->flags & SDL_FULLSCREEN ) {
    6.88 +			if ( flags & SDL_FULLSCREEN ) {
    6.89  				top = HWND_TOPMOST;
    6.90  			} else {
    6.91  				top = HWND_NOTOPMOST;
    6.92 @@ -1177,6 +1200,8 @@
    6.93  	}
    6.94  
    6.95  	/* Set the appropriate window style */
    6.96 +	windowX = SDL_windowX;
    6.97 +	windowY = SDL_windowY;
    6.98  	style = GetWindowLong(SDL_Window, GWL_STYLE);
    6.99  	style &= ~(resizestyle|WS_MAXIMIZE);
   6.100  	if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   6.101 @@ -1197,7 +1222,9 @@
   6.102  		if (IsZoomed(SDL_Window)) style |= WS_MAXIMIZE;
   6.103  #endif
   6.104  	}
   6.105 -	SetWindowLong(SDL_Window, GWL_STYLE, style);
   6.106 +	/* DJM: Don't piss of anyone who has setup his own window */
   6.107 +	if ( SDL_windowid == NULL )
   6.108 +		SetWindowLong(SDL_Window, GWL_STYLE, style);
   6.109  
   6.110  	/* Set DirectDraw sharing mode.. exclusive when fullscreen */
   6.111  	if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   6.112 @@ -1210,18 +1237,26 @@
   6.113  		SetDDerror("DirectDraw2::SetCooperativeLevel", result);
   6.114  		return(NULL);
   6.115  	}
   6.116 +	SDL_windowX = windowX;
   6.117 +	SDL_windowY = windowY;
   6.118  
   6.119  	/* Set the display mode, if we are in fullscreen mode */
   6.120  	if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   6.121 +		RECT bounds;
   6.122  		struct DX5EnumRect *rect;
   6.123  		int maxRefreshRate;
   6.124  
   6.125  		/* Cover up desktop during mode change */
   6.126  		SDL_resizing = 1;
   6.127 -		SetWindowPos(SDL_Window, NULL, 0, 0, 
   6.128 -			GetSystemMetrics(SM_CXSCREEN),
   6.129 -			GetSystemMetrics(SM_CYSCREEN),
   6.130 -			(SWP_NOCOPYBITS | SWP_NOZORDER));
   6.131 +		bounds.left = 0;
   6.132 +		bounds.top = 0;
   6.133 +		bounds.right = GetSystemMetrics(SM_CXSCREEN);
   6.134 +		bounds.bottom = GetSystemMetrics(SM_CYSCREEN);
   6.135 +		AdjustWindowRectEx(&bounds, GetWindowLong(SDL_Window, GWL_STYLE), FALSE, 0);
   6.136 +		SetWindowPos(SDL_Window, HWND_TOPMOST,
   6.137 +			bounds.left, bounds.top, 
   6.138 +			bounds.right - bounds.left,
   6.139 +			bounds.bottom - bounds.top, SWP_NOCOPYBITS);
   6.140  		SDL_resizing = 0;
   6.141  		ShowWindow(SDL_Window, SW_SHOW);
   6.142  		while ( GetForegroundWindow() != SDL_Window ) {
   6.143 @@ -1485,6 +1520,8 @@
   6.144  		RECT bounds;
   6.145  		int  x, y;
   6.146  		UINT swp_flags;
   6.147 +		const char *window = getenv("SDL_VIDEO_WINDOW_POS");
   6.148 +		const char *center = getenv("SDL_VIDEO_CENTERED");
   6.149  
   6.150  		/* Create and set a clipper on our primary surface */
   6.151  		if ( SDL_clipper == NULL ) {
   6.152 @@ -1516,26 +1553,47 @@
   6.153  			return(NULL);
   6.154  		}
   6.155  
   6.156 -		/* Set the size of the window, centering and adjusting */
   6.157 +		if ( !SDL_windowX && !SDL_windowY ) {
   6.158 +			if ( window ) {
   6.159 +				if ( sscanf(window, "%d,%d", &x, &y) == 2 ) {
   6.160 +					SDL_windowX = x;
   6.161 +					SDL_windowY = y;
   6.162 +				}
   6.163 +				if ( strcmp(window, "center") == 0 ) {
   6.164 +					center = window;
   6.165 +					window = NULL;
   6.166 +				}
   6.167 +			}
   6.168 +		}
   6.169 +		swp_flags = SWP_NOCOPYBITS;
   6.170 +
   6.171  		SDL_resizing = 1;
   6.172 -		bounds.top    = 0;
   6.173 -		bounds.bottom = video->h;
   6.174 -		bounds.left   = 0;
   6.175 -		bounds.right  = video->w;
   6.176 +		bounds.left = SDL_windowX;
   6.177 +		bounds.top = SDL_windowY;
   6.178 +		bounds.right = SDL_windowX+video->w;
   6.179 +		bounds.bottom = SDL_windowY+video->h;
   6.180  		AdjustWindowRectEx(&bounds, GetWindowLong(SDL_Window, GWL_STYLE), FALSE, 0);
   6.181  		width = bounds.right-bounds.left;
   6.182  		height = bounds.bottom-bounds.top;
   6.183 -		x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
   6.184 -		y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
   6.185 +		if ( (flags & SDL_FULLSCREEN) ) {
   6.186 +			x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
   6.187 +			y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
   6.188 +		} else if ( SDL_windowX || SDL_windowY || window ) {
   6.189 +			x = bounds.left;
   6.190 +			y = bounds.top;
   6.191 +		} else if ( center ) {
   6.192 +			x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
   6.193 +			y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
   6.194 +		} else {
   6.195 +			x = y = -1;
   6.196 +			swp_flags |= SWP_NOMOVE;
   6.197 +		}
   6.198  		if ( y < 0 ) { /* Cover up title bar for more client area */
   6.199  			y -= GetSystemMetrics(SM_CYCAPTION)/2;
   6.200  		}
   6.201 -		swp_flags = (SWP_NOCOPYBITS | SWP_NOZORDER);
   6.202 -		if ( was_visible ) {
   6.203 -			swp_flags |= SWP_NOMOVE;
   6.204 -		}
   6.205 -		SetWindowPos(SDL_Window, NULL, x, y, width, height, swp_flags);
   6.206 +		SetWindowPos(SDL_Window, HWND_NOTOPMOST, x, y, width, height, swp_flags);
   6.207  		SDL_resizing = 0;
   6.208 +
   6.209  	}
   6.210  	ShowWindow(SDL_Window, SW_SHOW);
   6.211  	SetForegroundWindow(SDL_Window);