From c9ace91f98a1b7e68e89393bb3cc2c5b0f5198f8 Mon Sep 17 00:00:00 2001 From: Jussi Lind Date: Tue, 20 Nov 2018 16:59:16 +0200 Subject: [PATCH] Fix GitHub Issue #43: Add option to force screen on multi-display setups - There's now --screen CLI option that works when in fullscreen mode. E.g. --screen 1 --- README.md | 4 ++ src/game/game.cpp | 64 +++++++++++++++++++++----------- src/game/game.hpp | 7 ++++ src/game/menu/resolutionmenu.cpp | 6 +-- src/game/renderer.cpp | 6 +-- src/game/settings.cpp | 5 +++ src/game/settings.hpp | 6 +++ 7 files changed, 68 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 55862fe9..483b5c8a 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,10 @@ where you can place your own race tracks. E.g. `dustrac-game --lang it` +`--screen [index]` forces the screen on multi-display setups when in fullscreen mode. + +E.g. `dustrac-game --screen 1` would start the game on the second display. + ## Building the project Please refer to the `INSTALL` document for build/install instructions if you're diff --git a/src/game/game.cpp b/src/game/game.cpp index 7429d971..42f91dd8 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -59,6 +59,7 @@ Game::Game(int & argc, char ** argv) , m_renderer(nullptr) , m_scene(nullptr) , m_trackLoader(new TrackLoader) +, m_screenIndex(m_settings.loadValue(m_settings.screenKey(), 0)) , m_updateFps(60) , m_updateDelay(1000 / m_updateFps) , m_timeStep(1000 / m_updateFps) @@ -128,9 +129,10 @@ static void printHelp() std::cout << std::endl << "Dust Racing 2D version " << VERSION << std::endl; std::cout << Config::Common::COPYRIGHT << std::endl << std::endl; std::cout << "Options:" << std::endl; - std::cout << "--help Show this help." << std::endl; - std::cout << "--lang [lang] Force language: fi, fr, it, cs." << std::endl; - std::cout << "--no-vsync Force vsync off." << std::endl; + std::cout << "--help Show this help." << std::endl; + std::cout << "--screen [index] Force a certain screen on multi-display setups." << std::endl; + std::cout << "--lang [lang] Force language: fi, fr, it, cs." << std::endl; + std::cout << "--no-vsync Force vsync off." << std::endl; std::cout << std::endl; } @@ -164,6 +166,11 @@ void Game::parseArgs(int argc, char ** argv) printHelp(); throw UserException("Exit due to help."); } + else if (args[i] == "--screen" && (i + i) < args.size()) + { + m_screenIndex = args[i + 1].toInt(); + m_settings.saveValue(m_settings.screenKey(), m_screenIndex); + } else if (args[i] == "--lang" && (i + i) < args.size()) { lang = args[i + 1]; @@ -179,25 +186,6 @@ void Game::parseArgs(int argc, char ** argv) void Game::createRenderer() { - // Create the main window / renderer - int hRes, vRes; - bool fullScreen = false; - - m_settings.loadResolution(hRes, vRes, fullScreen); - - if (!hRes || !vRes) - { - hRes = QGuiApplication::primaryScreen()->geometry().width(); - vRes = QGuiApplication::primaryScreen()->geometry().height(); - } - - adjustSceneSize(hRes, vRes); - - MCLogger().info() - << "Resolution: " << hRes << " " << vRes << " " << fullScreen; - - MCLogger().info() << "Creating the renderer.."; - QSurfaceFormat format; #ifdef __MC_GL30__ @@ -222,12 +210,39 @@ void Game::createRenderer() } #endif + // Create the main window / renderer + int hRes, vRes; + bool fullScreen = false; + + m_settings.loadResolution(hRes, vRes, fullScreen); + + const auto screens = QGuiApplication::screens(); + + MCLogger().info() << "Screen: " << m_screenIndex << "/" << screens.size(); + + m_screen = m_screenIndex < screens.size() ? screens.at(m_screenIndex) : screens.at(0); + + if (!hRes || !vRes) + { + hRes = m_screen->geometry().width(); + vRes = m_screen->geometry().height(); + } + + adjustSceneSize(m_screen->geometry().width(), m_screen->geometry().height()); + + MCLogger().info() << "Screen resolution: " << m_screen->geometry().width() << " " << m_screen->geometry().height(); + + MCLogger().info() << "Virtual resolution: " << hRes << " " << vRes << " " << fullScreen; + + MCLogger().info() << "Creating the renderer.."; + m_renderer = new Renderer(hRes, vRes, fullScreen, m_world->renderer().glScene()); m_renderer->setFormat(format); m_renderer->setCursor(Qt::BlankCursor); if (fullScreen) { + m_renderer->setGeometry(m_screen->geometry()); m_renderer->showFullScreen(); } else @@ -324,6 +339,11 @@ int Game::run() return m_app.exec(); } +QScreen * Game::screen() const +{ + return m_screen; +} + bool Game::loadTracks() { // Load track data diff --git a/src/game/game.hpp b/src/game/game.hpp index e5cc9311..0c7d9f14 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -36,6 +36,7 @@ class StartlightsOverlay; class StateMachine; class TimingOverlay; class TrackLoader; +class QScreen; //! The main game class. class Game : public QObject @@ -114,6 +115,8 @@ class Game : public QObject const std::string & fontName() const; + QScreen * screen() const; + public slots: void exitGame(); @@ -162,6 +165,10 @@ private slots: TrackLoader * m_trackLoader; + QScreen * m_screen = nullptr; + + int m_screenIndex = 0; + int m_updateFps; int m_updateDelay; diff --git a/src/game/menu/resolutionmenu.cpp b/src/game/menu/resolutionmenu.cpp index 3657c5d4..a38e768e 100644 --- a/src/game/menu/resolutionmenu.cpp +++ b/src/game/menu/resolutionmenu.cpp @@ -27,8 +27,6 @@ #include -#include -#include #include // for tr() #include @@ -150,8 +148,8 @@ ResolutionMenu::ResolutionMenu( , m_confirmationMenu(confirmationMenu) { const int numResolutions = 8; - const int fullHRes = QGuiApplication::primaryScreen()->geometry().width(); - const int fullVRes = QGuiApplication::primaryScreen()->geometry().height(); + const int fullHRes = Game::instance().screen()->geometry().width(); + const int fullVRes = Game::instance().screen()->geometry().height(); const int itemHeight = height / (numResolutions + 3); int itemHRes = fullHRes; diff --git a/src/game/renderer.cpp b/src/game/renderer.cpp index b435d57c..516ccb74 100644 --- a/src/game/renderer.cpp +++ b/src/game/renderer.cpp @@ -38,8 +38,6 @@ #include #include -#include -#include #include #include #include @@ -60,8 +58,8 @@ Renderer::Renderer(int hRes, int vRes, bool fullScreen, MCGLScene & glScene) , m_enabled(false) , m_hRes(hRes) , m_vRes(vRes) -, m_fullHRes(QGuiApplication::primaryScreen()->geometry().width()) -, m_fullVRes(QGuiApplication::primaryScreen()->geometry().height()) +, m_fullHRes(Game::instance().screen()->geometry().width()) +, m_fullVRes(Game::instance().screen()->geometry().height()) , m_frameCounter(0) , m_fullScreen(fullScreen) , m_updatePending(false) diff --git a/src/game/settings.cpp b/src/game/settings.cpp index 6ce1d9bd..dea3ac28 100644 --- a/src/game/settings.cpp +++ b/src/game/settings.cpp @@ -57,6 +57,11 @@ QString Settings::soundsKey() return "sounds"; } +QString Settings::screenKey() +{ + return "screen"; +} + QString Settings::vsyncKey() { return "vsync"; diff --git a/src/game/settings.hpp b/src/game/settings.hpp index 5a89ccfd..e9612dd5 100644 --- a/src/game/settings.hpp +++ b/src/game/settings.hpp @@ -66,9 +66,15 @@ class Settings int loadValue(QString key, int defaultValue = 0); static QString difficultyKey(); + static QString fpsKey(); + static QString lapCountKey(); + static QString soundsKey(); + + static QString screenKey(); + static QString vsyncKey(); private: