From 48aedb239e727fd349fd70c09eed159e7f669e61 Mon Sep 17 00:00:00 2001 From: Fredia Huya-Kouadio Date: Sat, 21 Jan 2023 22:46:22 -0800 Subject: [PATCH] Update the logic to calculate the screen scale on Android Takes into account the ratio between the screen size and the default window dimensions. --- .../src/org/godotengine/godot/GodotIO.java | 1 - platform/android/os_android.cpp | 19 ++++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java b/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java index fda7a2672c1..cc0a933b202 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java +++ b/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java @@ -150,7 +150,6 @@ public class GodotIO { } else { selectedScaledDensity = 0.75f; } - Log.d(TAG, "Selected scaled density: " + selectedScaledDensity); return selectedScaledDensity; } diff --git a/platform/android/os_android.cpp b/platform/android/os_android.cpp index ae6dad88053..fb18972e5ac 100644 --- a/platform/android/os_android.cpp +++ b/platform/android/os_android.cpp @@ -55,6 +55,8 @@ #include "tts_android.h" const char *OS_Android::ANDROID_EXEC_PATH = "apk"; +static const int DEFAULT_WINDOW_WIDTH = 800; +static const int DEFAULT_WINDOW_HEIGHT = 600; String _remove_symlink(const String &dir) { // Workaround for Android 6.0+ using a symlink. @@ -530,7 +532,18 @@ int OS_Android::get_screen_dpi(int p_screen) const { } float OS_Android::get_screen_scale(int p_screen) const { - return godot_io_java->get_scaled_density(); + float screen_scale = godot_io_java->get_scaled_density(); + + // Update the scale to avoid cropping. + Vector2 screen_size = get_window_size(); + if (screen_size != Vector2()) { + float width_scale = screen_size.width / (float)DEFAULT_WINDOW_WIDTH; + float height_scale = screen_size.height / (float)DEFAULT_WINDOW_HEIGHT; + screen_scale = MIN(screen_scale, MIN(width_scale, height_scale)); + } + + print_line("Selected screen scale: " + rtos(screen_scale)); + return screen_scale; } float OS_Android::get_screen_max_scale() const { @@ -679,8 +692,8 @@ bool OS_Android::_check_internal_feature_support(const String &p_feature) { OS_Android::OS_Android(GodotJavaWrapper *p_godot_java, GodotIOJavaWrapper *p_godot_io_java, bool p_use_apk_expansion) { use_apk_expansion = p_use_apk_expansion; - default_videomode.width = 800; - default_videomode.height = 600; + default_videomode.width = DEFAULT_WINDOW_WIDTH; + default_videomode.height = DEFAULT_WINDOW_HEIGHT; default_videomode.fullscreen = true; default_videomode.resizable = false;