shithub: choc

Download patch

ref: e707a058a70c4391bdac7e54bee7be811df79b0e
parent: 99baaba60c7b24a9877db7b9726b51408713a0c5
author: Simon Howard <fraggle@soulsphere.org>
date: Sun Jan 8 15:57:18 EST 2017

video: Add video_display config option.

This new variable controls which SDL display the game window should be
shown on (currently untested as I don't have a second monitor to hand).
This only has an effect if in fullscreen mode or if window_position is
set to "center". As part of this, make that the new default for
window_position.

This is the first part of #734. See that bug for some context.

--- a/src/i_video.c
+++ b/src/i_video.c
@@ -44,14 +44,6 @@
 #include "w_wad.h"
 #include "z_zone.h"
 
-// SDL video driver name
-
-char *video_driver = "";
-
-// Window position:
-
-static char *window_position = "";
-
 // These are (1) the window (or the full screen) that our game is rendered to
 // and (2) the renderer that scales the texture (see below) into this window.
 
@@ -101,6 +93,18 @@
 
 int png_screenshots = 0;
 
+// SDL video driver name
+
+char *video_driver = "";
+
+// Window position:
+
+static char *window_position = "center";
+
+// SDL display number on which to run.
+
+int video_display = 0;
+
 // Screen width and height, from configuration file.
 
 int window_width = SCREENWIDTH * 2;
@@ -986,8 +990,46 @@
     }
 }
 
-static void GetWindowPosition(int *x, int *y)
+// Check the display bounds of the display referred to by 'video_display' and
+// set x and y to a location that places the window in the center of that
+// display.
+static void CenterWindow(int *x, int *y, int w, int h)
 {
+    SDL_Rect bounds;
+
+    // Check that video_display corresponds to a display that really exists,
+    // and if it doesn't, reset it.
+    if (video_display < 0 || video_display >= SDL_GetNumVideoDisplays())
+    {
+        fprintf(stderr,
+                "CenterWindow: We were configured to run on display #%d, but "
+                "it no longer exists (max %d). Moving to display 0.\n",
+                video_display, SDL_GetNumVideoDisplays() - 1);
+        video_display = 0;
+    }
+
+    if (SDL_GetDisplayBounds(video_display, &bounds) < 0)
+    {
+        fprintf(stderr, "CenterWindow: Failed to read display bounds "
+                        "for display #%d!\n", video_display);
+        return;
+    }
+
+    *x = bounds.x + SDL_max((bounds.w - w) / 2, 0);
+    *y = bounds.y + SDL_max((bounds.h - h) / 2, 0);
+}
+
+static void GetWindowPosition(int *x, int *y, int w, int h)
+{
+    // in fullscreen mode, the window "position" still matters, because
+    // we use it to control which display we run fullscreen on.
+
+    if (fullscreen)
+    {
+        CenterWindow(x, y, w, h);
+        return;
+    }
+
     // in windowed mode, the desired window position can be specified
     // in the configuration file.
 
@@ -997,7 +1039,10 @@
     }
     else if (!strcmp(window_position, "center"))
     {
-        *x = *y = SDL_WINDOWPOS_CENTERED;
+        // Note: SDL has a SDL_WINDOWPOS_CENTER, but this is useless for our
+        // purposes, since we also want to control which display we appear on.
+        // So we have to do this ourselves.
+        CenterWindow(x, y, w, h);
     }
     else if (sscanf(window_position, "%i,%i", x, y) != 2)
     {
@@ -1018,8 +1063,6 @@
     w = window_width;
     h = window_height;
 
-    GetWindowPosition(&x, &y);
-
     // In windowed mode, the window can be resized while the game is
     // running.
     window_flags = SDL_WINDOW_RESIZABLE;
@@ -1045,6 +1088,8 @@
         }
     }
 
+    GetWindowPosition(&x, &y, w, h);
+
     // Create window and renderer contexts. We set the window title
     // later anyway and leave the window position "undefined". If
     // "window_flags" contains the fullscreen flag (see above), then
@@ -1275,6 +1320,7 @@
 {
     M_BindIntVariable("use_mouse",                 &usemouse);
     M_BindIntVariable("fullscreen",                &fullscreen);
+    M_BindIntVariable("video_display",             &video_display);
     M_BindIntVariable("aspect_ratio_correct",      &aspect_ratio_correct);
     M_BindIntVariable("startup_delay",             &startup_delay);
     M_BindIntVariable("fullscreen_width",          &fullscreen_width);
--- a/src/m_config.c
+++ b/src/m_config.c
@@ -714,6 +714,14 @@
     CONFIG_VARIABLE_INT(fullscreen),
 
     //!
+    // Index of the display on which the game should run. This has no
+    // effect if running in windowed mode (fullscreen=0) and
+    // window_position is not set to "center".
+    //
+
+    CONFIG_VARIABLE_INT(video_display),
+
+    //!
     // If non-zero, the screen will be stretched vertically to display
     // correctly on a square pixel video mode.
     //