From 699ffc02458206a876872b225ae22e7f3f62c725 Mon Sep 17 00:00:00 2001 From: Struma Date: Fri, 21 May 2021 16:28:51 -0400 Subject: [PATCH] Support Retina displays --- macos/Info.plist | 2 +- src/display/graphics.cpp | 13 ++++++++++--- src/eventthread.h | 3 +++ src/main.cpp | 6 ++++-- src/system/system.h | 1 + src/system/systemImpl.cpp | 5 +++++ src/system/systemImplApple.mm | 4 ++++ 7 files changed, 28 insertions(+), 6 deletions(-) diff --git a/macos/Info.plist b/macos/Info.plist index a7a193e..8f7965f 100644 --- a/macos/Info.plist +++ b/macos/Info.plist @@ -25,7 +25,7 @@ LSApplicationCategoryType public.app-category.role-playing-games NSHighResolutionCapable - + NSRequiresAquaSystemAppearance False diff --git a/src/display/graphics.cpp b/src/display/graphics.cpp index 97f3f7b..6d6c4d4 100644 --- a/src/display/graphics.cpp +++ b/src/display/graphics.cpp @@ -415,6 +415,10 @@ struct GraphicsPrivate { * is blitted inside the game window */ Vec2i scOffset; + // Scaling factor, used to display the screen properly + // on Retina displays + int scalingFactor; + ScreenScene screen; RGSSThreadData *threadData; SDL_GLContext glCtx; @@ -450,7 +454,7 @@ struct GraphicsPrivate { glCtx(SDL_GL_GetCurrentContext()), frameRate(DEF_FRAMERATE), frameCount(0), brightness(255), fpsLimiter(frameRate), useFrameSkip(rtData->config.frameSkip), frozen(false), - last_update(0), last_avg_update(0) { + last_update(0), last_avg_update(0), scalingFactor(rtData->scale){ avgFPSData = std::vector(); avgFPSLock = SDL_CreateMutex(); @@ -547,7 +551,10 @@ struct GraphicsPrivate { void metaBlitBufferFlippedScaled() { GLMeta::blitRectangle( IntRect(0, 0, scRes.x, scRes.y), - IntRect(scOffset.x, scSize.y + scOffset.y, scSize.x, -scSize.y), + IntRect(scOffset.x * scalingFactor, + (scSize.y + scOffset.y) * scalingFactor, + scSize.x * scalingFactor, + -scSize.y * scalingFactor), threadData->config.smoothScaling); } @@ -589,7 +596,7 @@ struct GraphicsPrivate { } double averageFPS() { - double ret; + double ret = 0; SDL_LockMutex(avgFPSLock); for (unsigned long long times : avgFPSData) ret += times; diff --git a/src/eventthread.h b/src/eventthread.h index 856ee19..57708f8 100644 --- a/src/eventthread.h +++ b/src/eventthread.h @@ -257,6 +257,7 @@ struct RGSSThreadData Vec2 sizeResoRatio; Vec2i screenOffset; + int scale; const int refreshRate; Config config; @@ -268,6 +269,7 @@ struct RGSSThreadData SDL_Window *window, ALCdevice *alcDev, int refreshRate, + int scalingFactor, const Config& newconf, SDL_GLContext ctx) : ethread(ethread), @@ -276,6 +278,7 @@ struct RGSSThreadData alcDev(alcDev), sizeResoRatio(1, 1), refreshRate(refreshRate), + scale(scalingFactor), config(newconf), glContext(ctx) {} diff --git a/src/main.cpp b/src/main.cpp index 98974db..00735c2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,6 +45,8 @@ #include "filesystem/filesystem.h" +#include "system/system.h" + #if defined(__WINDOWS__) #include "resource.h" #include @@ -191,8 +193,8 @@ int main(int argc, char *argv[]) { #ifndef WORKDIR_CURRENT char dataDir[512]{}; - char *tmp{}; #if defined(__linux__) + char *tmp{}; tmp = getenv("SRCDIR"); if (tmp) { strncpy(dataDir, tmp, sizeof(dataDir)); @@ -384,7 +386,7 @@ int main(int argc, char *argv[]) { #endif RGSSThreadData rtData(&eventThread, argv[0], win, alcDev, mode.refresh_rate, - conf, glCtx); + mkxp_sys::getScalingFactor(), conf, glCtx); int winW, winH; SDL_GetWindowSize(win, &winW, &winH); diff --git a/src/system/system.h b/src/system/system.h index 244f960..42d003f 100644 --- a/src/system/system.h +++ b/src/system/system.h @@ -13,6 +13,7 @@ namespace systemImpl { std::string getSystemLanguage(); std::string getUserName(); +int getScalingFactor(); } #ifdef MKXPZ_BUILD_XCODE diff --git a/src/system/systemImpl.cpp b/src/system/systemImpl.cpp index 28ecbb5..afcad2f 100644 --- a/src/system/systemImpl.cpp +++ b/src/system/systemImpl.cpp @@ -54,3 +54,8 @@ std::string systemImpl::getUserName() { return std::string(ret); } + +// HiDPI scaling not supported outside of macOS for now +int systemImpl::getScalingFactor() { + return 1; +} diff --git a/src/system/systemImplApple.mm b/src/system/systemImplApple.mm index d64f4c7..f19e9e4 100644 --- a/src/system/systemImplApple.mm +++ b/src/system/systemImplApple.mm @@ -19,6 +19,10 @@ std::string systemImpl::getUserName() { return std::string(NSUserName().UTF8String); } +int systemImpl::getScalingFactor() { + return NSApplication.sharedApplication.mainWindow.backingScaleFactor; +} + // constant, if it's not nil then just raise the menu instead SettingsMenu *smenu = nil;