Add: [SDL2] video driver parameter to put OpenTTD on a particular display on start. By default use the display where the mouse cursor is. (#8572)

This commit is contained in:
frosch 2021-01-14 23:29:29 +01:00 committed by GitHub
parent fa60c1f8b9
commit 0e62a398c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View File

@ -272,10 +272,15 @@ bool VideoDriver_SDL::CreateMainSurface(uint w, uint h, bool resize)
flags |= SDL_WINDOW_FULLSCREEN; flags |= SDL_WINDOW_FULLSCREEN;
} }
int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED;
SDL_Rect r;
if (SDL_GetDisplayBounds(this->startup_display, &r) == 0) {
x = r.x + (r.w - w) / 2;
y = r.y + (r.h - h) / 4; // decent desktops have taskbars at the bottom
}
_sdl_window = SDL_CreateWindow( _sdl_window = SDL_CreateWindow(
caption, caption,
SDL_WINDOWPOS_UNDEFINED, x, y,
SDL_WINDOWPOS_UNDEFINED,
w, h, w, h,
flags); flags);
@ -674,6 +679,23 @@ const char *VideoDriver_SDL::Start(const StringList &parm)
} }
if (ret_code < 0) return SDL_GetError(); if (ret_code < 0) return SDL_GetError();
this->startup_display = GetDriverParamInt(parm, "display", -1);
int num_displays = SDL_GetNumVideoDisplays();
if (!IsInsideBS(this->startup_display, 0, num_displays)) {
/* Mouse position decides which display to use */
int mx, my;
SDL_GetGlobalMouseState(&mx, &my);
this->startup_display = 0; // used when mouse is on no screen...
for (int display = 0; display < num_displays; ++display) {
SDL_Rect r;
if (SDL_GetDisplayBounds(display, &r) == 0 && IsInsideBS(mx, r.x, r.w) && IsInsideBS(my, r.y, r.h)) {
DEBUG(driver, 1, "SDL2: Mouse is at (%d, %d), use display %d (%d, %d, %d, %d)", mx, my, display, r.x, r.y, r.w, r.h);
this->startup_display = display;
break;
}
}
}
this->UpdateAutoResolution(); this->UpdateAutoResolution();
GetVideoModes(); GetVideoModes();
@ -935,7 +957,7 @@ void VideoDriver_SDL::ReleaseBlitterLock()
Dimension VideoDriver_SDL::GetScreenSize() const Dimension VideoDriver_SDL::GetScreenSize() const
{ {
SDL_DisplayMode mode; SDL_DisplayMode mode;
if (SDL_GetCurrentDisplayMode(0, &mode) != 0) return VideoDriver::GetScreenSize(); if (SDL_GetCurrentDisplayMode(this->startup_display, &mode) != 0) return VideoDriver::GetScreenSize();
return { static_cast<uint>(mode.w), static_cast<uint>(mode.h) }; return { static_cast<uint>(mode.w), static_cast<uint>(mode.h) };
} }

View File

@ -64,6 +64,7 @@ private:
uint32 last_cur_ticks; uint32 last_cur_ticks;
uint32 next_tick; uint32 next_tick;
int startup_display;
std::thread draw_thread; std::thread draw_thread;
std::unique_lock<std::recursive_mutex> draw_lock; std::unique_lock<std::recursive_mutex> draw_lock;
}; };