Skip to content

Commit

Permalink
Fix GitHub Issue #43: Add option to force screen on multi-display setups
Browse files Browse the repository at this point in the history
- There's now --screen CLI option that works when in fullscreen mode.

  E.g. --screen 1
  • Loading branch information
juzzlin authored and Jussi Lind committed Nov 20, 2018
1 parent 3ffe384 commit c9ace91
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 30 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 42 additions & 22 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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];
Expand All @@ -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__
Expand All @@ -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
Expand Down Expand Up @@ -324,6 +339,11 @@ int Game::run()
return m_app.exec();
}

QScreen * Game::screen() const
{
return m_screen;
}

bool Game::loadTracks()
{
// Load track data
Expand Down
7 changes: 7 additions & 0 deletions src/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class StartlightsOverlay;
class StateMachine;
class TimingOverlay;
class TrackLoader;
class QScreen;

//! The main game class.
class Game : public QObject
Expand Down Expand Up @@ -114,6 +115,8 @@ class Game : public QObject

const std::string & fontName() const;

QScreen * screen() const;

public slots:

void exitGame();
Expand Down Expand Up @@ -162,6 +165,10 @@ private slots:

TrackLoader * m_trackLoader;

QScreen * m_screen = nullptr;

int m_screenIndex = 0;

int m_updateFps;

int m_updateDelay;
Expand Down
6 changes: 2 additions & 4 deletions src/game/menu/resolutionmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

#include <MCLogger>

#include <QApplication>
#include <QDesktopWidget>
#include <QObject> // for tr()
#include <QScreen>

Expand Down Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions src/game/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
#include <cmath>
#include <cassert>

#include <QApplication>
#include <QDesktopWidget>
#include <QDir>
#include <QFontDatabase>
#include <QIcon>
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/game/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ QString Settings::soundsKey()
return "sounds";
}

QString Settings::screenKey()
{
return "screen";
}

QString Settings::vsyncKey()
{
return "vsync";
Expand Down
6 changes: 6 additions & 0 deletions src/game/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit c9ace91

Please sign in to comment.