jngl icon indicating copy to clipboard operation
jngl copied to clipboard

Retina support on macOS

Open jhasse opened this issue 3 years ago • 0 comments

Need to pass SDL_WINDOW_ALLOW_HIGHDPI when creating the window and then get the dimensions using SDL_GL_GetDrawableSize:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index e6ad008..6932460 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -188,9 +188,12 @@ if(ANDROID)
 else()
 	if(UNIX AND NOT IOS AND NOT EMSCRIPTEN)
 		find_package(PkgConfig REQUIRED)
-		pkg_check_modules(SDL2 REQUIRED sdl2)
-		target_include_directories(jngl PRIVATE ${SDL2_INCLUDE_DIRS})
-		target_link_libraries(jngl PRIVATE ${SDL2_LINK_LIBRARIES})
+		FetchContent_Declare(sdl2
+			URL https://www.libsdl.org/release/SDL2-2.0.20.zip
+			URL_HASH SHA256=cc8b16a326eb082c1f48ca30fdf471acfd2334b69bd7527e65ac58369013a1ba
+		)
+		FetchContent_MakeAvailable(sdl2)
+		target_link_libraries(jngl PRIVATE SDL2-static)
 	endif()
 
 	if(JNGL_JPEG)
diff --git a/src/jngl/init.hpp b/src/jngl/init.hpp
index 6e0b16a..cea7df8 100644
--- a/src/jngl/init.hpp
+++ b/src/jngl/init.hpp
@@ -44,9 +44,9 @@ JNGL_MAIN_BEGIN { // NOLINT
 	app.setDisplayName(params.displayName);
 	app.setPixelArt(params.pixelArt);
 	bool fullscreen = false;
-#if defined(NDEBUG) || defined(__ANDROID__)
+	// #if defined(NDEBUG) || defined(__ANDROID__)
 	fullscreen = true;
-#endif
+	// #endif
 	std::pair<int, int> minAspectRatio{ 1, 3 };
 	std::pair<int, int> maxAspectRatio{ 3, 1 };
 	if (!params.screenSize) {
@@ -75,11 +75,12 @@ JNGL_MAIN_BEGIN { // NOLINT
 	}
 	jngl::showWindow(
 	    params.displayName,
-	    fullscreen ? jngl::getDesktopWidth()
-	               : boost::math::iround(params.screenSize->x * jngl::getScaleFactor()),
-	    fullscreen ? jngl::getDesktopHeight()
-	               : boost::math::iround(params.screenSize->y * jngl::getScaleFactor()),
+	    fullscreen ? -1 : boost::math::iround(params.screenSize->x * jngl::getScaleFactor()),
+	    fullscreen ? -1 : boost::math::iround(params.screenSize->y * jngl::getScaleFactor()),
 	    fullscreen, minAspectRatio, maxAspectRatio);
+	jngl::setScaleFactor(
+	    std::min(static_cast<int>(jngl::getWindowSize()[0]) / params.screenSize->x,
+	             static_cast<int>(jngl::getWindowSize()[1]) / params.screenSize->y));
 	jngl::setWork(workFactory());
 	app.mainLoop();
 }
diff --git a/src/sdl/window.cpp b/src/sdl/window.cpp
index d2f3eb0..d0c942b 100644
--- a/src/sdl/window.cpp
+++ b/src/sdl/window.cpp
@@ -24,9 +24,10 @@ Window::Window(const std::string& title, const int width, const int height, cons
 	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
 	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
 
-	Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
+	Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI;
 	if (fullscreen) {
-		if (width == getDesktopWidth() && height == getDesktopHeight()) {
+		if (width == -1 || height == -1 ||
+		    (width == getDesktopWidth() && height == getDesktopHeight())) {
 			flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
 		} else {
 			flags |= SDL_WINDOW_FULLSCREEN;
@@ -70,8 +71,12 @@ Window::Window(const std::string& title, const int width, const int height, cons
 		}
 	}
 
+	if (width_ == -1 || height_ == -1) {
+		SDL_GL_GetDrawableSize(impl->sdlWindow, &width_, &height_);
+	}
+
 	calculateCanvasSize(minAspectRatio, maxAspectRatio);
-	Init(width, height, canvasWidth, canvasHeight);
+	Init(width_, height_, canvasWidth, canvasHeight);
 }
 
 Window::~Window() = default;

It didn't work with Homebrew's SDL and I couldn't get SDL 2.0.20 to build due to some Joystick error.

jhasse avatar Feb 05 '22 12:02 jhasse