Skip to content

Commit

Permalink
Qt/macOS: enable HiDPI ( retina display ) support
Browse files Browse the repository at this point in the history
  • Loading branch information
CarterLi committed Feb 4, 2020
1 parent 97de0ac commit 0a2aa2c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
7 changes: 6 additions & 1 deletion Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,12 @@ static bool IsWindowSmall(int pixelWidth, int pixelHeight) {
// TODO: Feels like this belongs elsewhere.
bool UpdateScreenScale(int width, int height) {
bool smallWindow;
#if PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(UWP)
#if defined(USING_QT_UI)
g_dpi = System_GetPropertyFloat(SYSPROP_DISPLAY_DPI);
float g_logical_dpi = System_GetPropertyFloat(SYSPROP_DISPLAY_LOGICAL_DPI);
g_dpi_scale_x = g_logical_dpi / g_dpi;
g_dpi_scale_y = g_logical_dpi / g_dpi;
#elif PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(UWP)
g_dpi = (float)System_GetPropertyInt(SYSPROP_DISPLAY_DPI);
g_dpi_scale_x = 96.0f / g_dpi;
g_dpi_scale_y = 96.0f / g_dpi;
Expand Down
34 changes: 19 additions & 15 deletions Qt/QtMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,24 @@ int System_GetPropertyInt(SystemProperty prop) {
#else
return DEVICE_TYPE_DESKTOP;
#endif
case SYSPROP_DISPLAY_COUNT:
return QApplication::screens().size();
default:
return -1;
}
}

int System_GetPropertyFloat(SystemProperty prop) {
switch (prop) {
case SYSPROP_DISPLAY_LOGICAL_DPI:
return QApplication::primaryScreen()->logicalDotsPerInch();
case SYSPROP_DISPLAY_DPI:
return QApplication::primaryScreen()->physicalDotsPerInch();
default:
return System_GetPropertyInt(prop);
}
}

bool System_GetPropertyBool(SystemProperty prop) {
switch (prop) {
case SYSPROP_HAS_BACK_BUTTON:
Expand Down Expand Up @@ -229,16 +242,6 @@ void LaunchBrowser(const char *url)
QDesktopServices::openUrl(QUrl(url));
}

float CalculateDPIScale()
{
// Sane default rather than check DPI
#if defined(USING_GLES2)
return 1.2f;
#else
return 1.0f;
#endif
}

static int mainInternal(QApplication &a) {
#ifdef MOBILE_DEVICE
emugl = new MainUI();
Expand Down Expand Up @@ -628,19 +631,21 @@ int main(int argc, char *argv[])
QGLFormat::setDefaultFormat(format);

QApplication a(argc, argv);
QSize res = QApplication::desktop()->screenGeometry().size();
QScreen* screen = a.primaryScreen();
QSizeF res = screen->physicalSize();
if (res.width() < res.height())
res.transpose();
pixel_xres = res.width();
pixel_yres = res.height();
g_dpi_scale_x = CalculateDPIScale();
g_dpi_scale_y = CalculateDPIScale();

g_dpi_scale_x = screen->logicalDotsPerInchX() / screen->physicalDotsPerInchX();
g_dpi_scale_y = screen->logicalDotsPerInchY() / screen->physicalDotsPerInchY();
g_dpi_scale_real_x = g_dpi_scale_x;
g_dpi_scale_real_y = g_dpi_scale_y;
dp_xres = (int)(pixel_xres * g_dpi_scale_x);
dp_yres = (int)(pixel_yres * g_dpi_scale_y);

refreshRate = (int)(a.primaryScreen()->refreshRate() * 1000);
refreshRate = (int)(screen->refreshRate() * 1000);

std::string savegame_dir = ".";
std::string external_dir = ".";
Expand Down Expand Up @@ -669,4 +674,3 @@ int main(int argc, char *argv[])
glslang::FinalizeProcess();
return ret;
}

20 changes: 15 additions & 5 deletions Qt/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ MainWindow::MainWindow(QWidget *parent, bool fullscreen) :
{
QDesktopWidget *desktop = QApplication::desktop();
int screenNum = QProcessEnvironment::systemEnvironment().value("SDL_VIDEO_FULLSCREEN_HEAD", "0").toInt();

// Move window to the center of selected screen
QRect rect = desktop->screenGeometry(screenNum);
move((rect.width()-frameGeometry().width()) / 4, (rect.height()-frameGeometry().height()) / 4);
Expand Down Expand Up @@ -295,28 +295,33 @@ void MainWindow::consoleAct()
void MainWindow::raiseTopMost()
{
setWindowState( (windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
raise();
raise();
activateWindow();
}

void MainWindow::SetFullScreen(bool fullscreen) {
if (fullscreen) {
#if !PPSSPP_PLATFORM(MAC)
menuBar()->hide();

emugl->setFixedSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
// TODO: Shouldn't this be physicalSize()?
emugl->resizeGL(emugl->size().width(), emugl->size().height());
// TODO: Won't showFullScreen do this for us?
setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
setFixedSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
#endif

showFullScreen();
InitPadLayout(dp_xres, dp_yres);

if (GetUIState() == UISTATE_INGAME && !g_Config.bShowTouchControls)
QApplication::setOverrideCursor(QCursor(Qt::BlankCursor));
} else {
#if !PPSSPP_PLATFORM(MAC)
menuBar()->show();
updateMenus();
#endif

showNormal();
SetWindowScale(-1);
Expand Down Expand Up @@ -346,7 +351,7 @@ void MainWindow::forumAct()
QDesktopServices::openUrl(QUrl("https://forums.ppsspp.org/"));
}

void MainWindow::gitAct()
void MainWindow::gitAct()
{
QDesktopServices::openUrl(QUrl("https://github.com/hrydgard/ppsspp/"));
}
Expand Down Expand Up @@ -394,9 +399,14 @@ void MainWindow::SetWindowScale(int zoom) {
g_Config.iWindowWidth = width;
g_Config.iWindowHeight = height;

#if !PPSSPP_PLATFORM(MAC)
emugl->setFixedSize(g_Config.iWindowWidth, g_Config.iWindowHeight);
// TODO: Shouldn't this be scaled size?
emugl->resizeGL(g_Config.iWindowWidth, g_Config.iWindowHeight);
setFixedSize(sizeHint());
#else
resize(g_Config.iWindowWidth, g_Config.iWindowHeight);
#endif
}

void MainWindow::SetGameTitle(QString text)
Expand Down Expand Up @@ -585,7 +595,7 @@ void MainWindow::createMenus()
}
}
langMenu->addActions(langGroup->actions());

// Help
MenuTree* helpMenu = new MenuTree(this, menuBar(), QT_TR_NOOP("&Help"));
helpMenu->add(new MenuAction(this, SLOT(websiteAct()), QT_TR_NOOP("Official &website"), QKeySequence::HelpContents));
Expand Down
2 changes: 2 additions & 0 deletions ext/native/base/NativeApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ enum SystemProperty {
SYSPROP_DISPLAY_XRES,
SYSPROP_DISPLAY_YRES,
SYSPROP_DISPLAY_REFRESH_RATE, // returns 1000*the refresh rate in Hz as it can be non-integer
SYSPROP_DISPLAY_LOGICAL_DPI,
SYSPROP_DISPLAY_DPI,
SYSPROP_DISPLAY_COUNT,
SYSPROP_MOGA_VERSION,
Expand All @@ -173,6 +174,7 @@ enum SystemProperty {

std::string System_GetProperty(SystemProperty prop);
int System_GetPropertyInt(SystemProperty prop);
int System_GetPropertyFloat(SystemProperty prop);
bool System_GetPropertyBool(SystemProperty prop);

std::vector<std::string> __cameraGetDeviceList();

0 comments on commit 0a2aa2c

Please sign in to comment.