]> git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
adjust window position and size to the available desktop area (= screen size adjusted...
authordivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Mon, 3 Aug 2015 16:22:14 +0000 (16:22 +0000)
committerdivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Mon, 3 Aug 2015 16:22:14 +0000 (16:22 +0000)
From: hbeham <horst@beham.biz>

git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@12215 d7cf8633-e32d-0410-b094-e92efae38249

vid_sdl.c
vid_wgl.c

index 942cbdd24bea465bcf19374ce0f29681b1a7cfc6..e3c934a7adac167d2e208bc99650f645e43f38d5 100644 (file)
--- a/vid_sdl.c
+++ b/vid_sdl.c
@@ -2414,6 +2414,50 @@ static void VID_OutputVersion(void)
                                        version.major, version.minor, version.patch );
 }
 
+#ifdef WIN32
+static void AdjustWindowBounds(viddef_mode_t *mode, RECT *rect)
+{
+       LONG width = mode->width; // vid_width
+       LONG height = mode->height; // vid_height
+
+       // adjust width and height for the space occupied by window decorators (title bar, borders)
+       rect->top = 0;
+       rect->left = 0;
+       rect->right = width;
+       rect->bottom = height;
+       AdjustWindowRectEx(rect, WS_CAPTION|WS_THICKFRAME, false, 0);
+
+       RECT workArea;
+       SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);
+       int workWidth = workArea.right - workArea.left;
+       int workHeight = workArea.bottom - workArea.top;
+
+       // SDL forces the window height to be <= screen height - 27px (on Win8.1 - probably intended for the title bar) 
+       // If the task bar is docked to the the left screen border and we move the window to negative y,
+       // there would be some part of the regular desktop visible on the bottom of the screen.
+       int titleBarPixels = 2;
+       int screenHeight = GetSystemMetrics(SM_CYSCREEN);
+       if (screenHeight == workHeight)
+               titleBarPixels = -rect->top;
+
+       //Con_Printf("window mode: %dx%d, workArea: %d/%d-%d/%d (%dx%d), title: %d\n", width, height, workArea.left, workArea.top, workArea.right, workArea.bottom, workArea.right - workArea.left, workArea.bottom - workArea.top, titleBarPixels);
+
+       // if height and width matches the physical or previously adjusted screen height and width, adjust it to available desktop area
+       if ((width == GetSystemMetrics(SM_CXSCREEN) || width == workWidth) && (height == screenHeight || height == workHeight - titleBarPixels))
+       {
+               rect->left = workArea.left;
+               mode->width = workWidth;
+               rect->top = workArea.top + titleBarPixels;
+               mode->height = workHeight - titleBarPixels;
+       }
+       else 
+       {
+               rect->left = workArea.left + max(0, (workWidth - width) / 2);
+               rect->top = workArea.top + (0, (workHeight - height) / 2);
+       }
+}
+#endif
+
 static qboolean VID_InitModeGL(viddef_mode_t *mode)
 {
 #if SDL_MAJOR_VERSION == 1
@@ -2483,6 +2527,8 @@ static qboolean VID_InitModeGL(viddef_mode_t *mode)
        // Knghtbrd: should do platform-specific extension string function here
 
        vid_isfullscreen = false;
+       int xPos = SDL_WINDOWPOS_UNDEFINED;
+       int yPos = SDL_WINDOWPOS_UNDEFINED;
 #if SDL_MAJOR_VERSION == 1
        {
                const SDL_VideoInfo *vi = SDL_GetVideoInfo();
@@ -2516,6 +2562,15 @@ static qboolean VID_InitModeGL(viddef_mode_t *mode)
                                windowflags |= SDL_WINDOW_FULLSCREEN;
                        vid_isfullscreen = true;
                }
+               else {
+#ifdef WIN32
+                       DWORD windowStyle = 0;
+                       RECT rect;
+                       AdjustWindowBounds(mode, &rect);
+                       xPos = rect.left;
+                       yPos = rect.top;
+#endif
+               }
        }
 #endif
        //flags |= SDL_HWSURFACE;
@@ -2572,7 +2627,7 @@ static qboolean VID_InitModeGL(viddef_mode_t *mode)
        mode->height = screen->h;
 #else
        window_flags = windowflags;
-       window = SDL_CreateWindow(gamename, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, mode->width, mode->height, windowflags);
+       window = SDL_CreateWindow(gamename, xPos, yPos, mode->width, mode->height, windowflags);
        if (window == NULL)
        {
                Con_Printf("Failed to set video mode to %ix%i: %s\n", mode->width, mode->height, SDL_GetError());
index 24379fbfdfc1a2da78da0af792be1cea94887f6f..7989055390be5cb11f175519dbf85ed800e30487 100644 (file)
--- a/vid_wgl.c
+++ b/vid_wgl.c
@@ -216,7 +216,7 @@ static HINSTANCE hInstDI;
 
 // forward-referenced functions
 static void IN_StartupMouse (void);
-
+static void AdjustWindowBounds(int fullscreen, int &width, int &height, viddef_mode_t *mode, DWORD WindowStyle, RECT &rect);
 
 //====================================
 
@@ -913,7 +913,6 @@ qboolean VID_InitModeGL(viddef_mode_t *mode)
        int pixelformat, newpixelformat;
        UINT numpixelformats;
        DWORD WindowStyle, ExWindowStyle;
-       int CenterX, CenterY;
        const char *gldrivername;
        int depth;
        DEVMODE thismode;
@@ -1167,32 +1166,7 @@ qboolean VID_InitModeGL(viddef_mode_t *mode)
                ExWindowStyle = 0;
        }
 
-       rect.top = 0;
-       rect.left = 0;
-       rect.right = width;
-       rect.bottom = height;
-       AdjustWindowRectEx(&rect, WindowStyle, false, 0);
-
-       if (fullscreen)
-       {
-               CenterX = 0;
-               CenterY = 0;
-       }
-       else
-       {
-               CenterX = (GetSystemMetrics(SM_CXSCREEN) - (rect.right - rect.left)) / 2;
-               CenterY = (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom - rect.top)) / 2;
-       }
-       CenterX = max(0, CenterX);
-       CenterY = max(0, CenterY);
-
-       // x and y may be changed by WM_MOVE messages
-       window_x = CenterX;
-       window_y = CenterY;
-       rect.left += CenterX;
-       rect.right += CenterX;
-       rect.top += CenterY;
-       rect.bottom += CenterY;
+       AdjustWindowBounds(fullscreen, width, height, mode, WindowStyle, rect);
 
        pixelformat = 0;
        newpixelformat = 0;
@@ -1350,6 +1324,56 @@ qboolean VID_InitModeGL(viddef_mode_t *mode)
        return true;
 }
 
+static void AdjustWindowBounds(int fullscreen, int &width, int &height, viddef_mode_t *mode, DWORD WindowStyle, RECT &rect)
+{
+       int CenterX, CenterY;
+
+       rect.top = 0;
+       rect.left = 0;
+       rect.right = width;
+       rect.bottom = height;
+       AdjustWindowRectEx(&rect, WindowStyle, false, 0);
+
+       if (fullscreen)
+       {
+               CenterX = 0;
+               CenterY = 0;
+       }
+       else
+       {
+               RECT workArea;
+               SystemParametersInfo(SPI_GETWORKAREA, NULL, &workArea, 0);
+               int workWidth = workArea.right - workArea.left;
+               int workHeight = workArea.bottom - workArea.top;
+
+               // if height/width matches physical screen height/width, adjust it to available desktop size
+               // and allow 2 pixels on top for the title bar so the window can be moved
+               const int titleBarPixels = 2;
+               if (width == GetSystemMetrics(SM_CXSCREEN) && (height == GetSystemMetrics(SM_CYSCREEN) || height == workHeight - titleBarPixels))
+               {
+                       rect.right -= width - workWidth;
+                       width = mode->width = workWidth;
+                       rect.bottom -= height - (workHeight - titleBarPixels);
+                       height = mode->height = workHeight - titleBarPixels;
+                       CenterX = 0;
+                       CenterY = titleBarPixels;
+               }
+               else
+               {
+                       CenterX = max(0, (workWidth - width) / 2);
+                       CenterY = max(0, (workHeight - height) / 2);
+               }
+       }
+
+       // x and y may be changed by WM_MOVE messages
+       window_x = CenterX;
+       window_y = CenterY;
+       rect.left += CenterX;
+       rect.right += CenterX;
+       rect.top += CenterY;
+       rect.bottom += CenterY;
+}
+
 #ifdef SUPPORTD3D
 static D3DADAPTER_IDENTIFIER9 d3d9adapteridentifier;
 
@@ -1365,7 +1389,6 @@ qboolean VID_InitModeDX(viddef_mode_t *mode, int version)
        RECT rect;
        MSG msg;
        DWORD WindowStyle, ExWindowStyle;
-       int CenterX, CenterY;
        int bpp = mode->bitsperpixel;
        int width = mode->width;
        int height = mode->height;
@@ -1390,32 +1413,7 @@ qboolean VID_InitModeDX(viddef_mode_t *mode, int version)
                ExWindowStyle = 0;
        }
 
-       rect.top = 0;
-       rect.left = 0;
-       rect.right = width;
-       rect.bottom = height;
-       AdjustWindowRectEx(&rect, WindowStyle, false, 0);
-
-       if (fullscreen)
-       {
-               CenterX = 0;
-               CenterY = 0;
-       }
-       else
-       {
-               CenterX = (GetSystemMetrics(SM_CXSCREEN) - (rect.right - rect.left)) / 2;
-               CenterY = (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom - rect.top)) / 2;
-       }
-       CenterX = max(0, CenterX);
-       CenterY = max(0, CenterY);
-
-       // x and y may be changed by WM_MOVE messages
-       window_x = CenterX;
-       window_y = CenterY;
-       rect.left += CenterX;
-       rect.right += CenterX;
-       rect.top += CenterY;
-       rect.bottom += CenterY;
+       AdjustWindowBounds(fullscreen, width, height, mode, WindowStyle, rect);
 
        gl_extensions = "";
        gl_platformextensions = "";
@@ -1599,7 +1597,6 @@ qboolean VID_InitModeSOFT(viddef_mode_t *mode)
        MSG msg;
        int pixelformat, newpixelformat;
        DWORD WindowStyle, ExWindowStyle;
-       int CenterX, CenterY;
        int depth;
        DEVMODE thismode;
        qboolean foundmode, foundgoodmode;
@@ -1751,32 +1748,7 @@ qboolean VID_InitModeSOFT(viddef_mode_t *mode)
                ExWindowStyle = 0;
        }
 
-       rect.top = 0;
-       rect.left = 0;
-       rect.right = width;
-       rect.bottom = height;
-       AdjustWindowRectEx(&rect, WindowStyle, false, 0);
-
-       if (fullscreen)
-       {
-               CenterX = 0;
-               CenterY = 0;
-       }
-       else
-       {
-               CenterX = (GetSystemMetrics(SM_CXSCREEN) - (rect.right - rect.left)) / 2;
-               CenterY = (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom - rect.top)) / 2;
-       }
-       CenterX = max(0, CenterX);
-       CenterY = max(0, CenterY);
-
-       // x and y may be changed by WM_MOVE messages
-       window_x = CenterX;
-       window_y = CenterY;
-       rect.left += CenterX;
-       rect.right += CenterX;
-       rect.top += CenterY;
-       rect.bottom += CenterY;
+       AdjustWindowBounds(fullscreen, width, height, mode, WindowStyle, rect);
 
        pixelformat = 0;
        newpixelformat = 0;