From 15e001608a48e22fce24e2d8eba9ef193c294d23 Mon Sep 17 00:00:00 2001 From: acsbendi Date: Wed, 20 Feb 2019 19:21:31 +0100 Subject: [PATCH] Fixed wrong fullscreen resolution --- src/SFML/Main/MainAndroid.cpp | 43 +++++++++++++++++++++++ src/SFML/System/Android/Activity.hpp | 1 + src/SFML/Window/Android/VideoModeImpl.cpp | 10 +++++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/SFML/Main/MainAndroid.cpp b/src/SFML/Main/MainAndroid.cpp index 2f70c3083..37580f6b6 100644 --- a/src/SFML/Main/MainAndroid.cpp +++ b/src/SFML/Main/MainAndroid.cpp @@ -243,6 +243,48 @@ void getScreenSizeInPixels(ANativeActivity* activity, int* width, int* height) } +//////////////////////////////////////////////////////////// +void getFullScreenSizeInPixels(ANativeActivity* activity, int* width, int* height) +{ + // Perform the following Java code: + // + // DisplayMetrics dm = new DisplayMetrics(); + // getWindowManager().getDefaultDisplay().getRealMetrics(dm); + + JNIEnv* lJNIEnv = activity->env; + + jobject objectActivity = activity->clazz; + jclass classActivity = lJNIEnv->GetObjectClass(objectActivity); + + jclass classDisplayMetrics = lJNIEnv->FindClass("android/util/DisplayMetrics"); + jmethodID initDisplayMetrics = lJNIEnv->GetMethodID(classDisplayMetrics, "", "()V"); + jobject objectDisplayMetrics = lJNIEnv->NewObject(classDisplayMetrics, initDisplayMetrics); + + jmethodID methodGetWindowManager = lJNIEnv->GetMethodID(classActivity, "getWindowManager", "()Landroid/view/WindowManager;"); + jobject objectWindowManager = lJNIEnv->CallObjectMethod(objectActivity, methodGetWindowManager); + + jclass classWindowManager = lJNIEnv->FindClass("android/view/WindowManager"); + jmethodID methodGetDefaultDisplay = lJNIEnv->GetMethodID(classWindowManager, "getDefaultDisplay", "()Landroid/view/Display;"); + jobject objectDisplay = lJNIEnv->CallObjectMethod(objectWindowManager, methodGetDefaultDisplay); + + jclass classDisplay = lJNIEnv->FindClass("android/view/Display"); + jmethodID methodGetMetrics; + + // getRealMetrics is only supported on API level 17 and above, if we are below that, we will fall back to getMetrics + if(sf::priv::getAndroidApiLevel(activity) >= 17) + methodGetMetrics = lJNIEnv->GetMethodID(classDisplay, "getRealMetrics", "(Landroid/util/DisplayMetrics;)V"); + else + methodGetMetrics = lJNIEnv->GetMethodID(classDisplay, "getMetrics", "(Landroid/util/DisplayMetrics;)V"); + lJNIEnv->CallVoidMethod(objectDisplay, methodGetMetrics, objectDisplayMetrics); + + jfieldID fieldWidthPixels = lJNIEnv->GetFieldID(classDisplayMetrics, "widthPixels", "I"); + jfieldID fieldHeightPixels = lJNIEnv->GetFieldID(classDisplayMetrics, "heightPixels", "I"); + + *width = lJNIEnv->GetIntField(objectDisplayMetrics, fieldWidthPixels); + *height = lJNIEnv->GetIntField(objectDisplayMetrics, fieldHeightPixels); +} + + //////////////////////////////////////////////////////////// static void onStart(ANativeActivity* activity) { @@ -563,6 +605,7 @@ JNIEXPORT void ANativeActivity_onCreate(ANativeActivity* activity, void* savedSt eglInitialize(states->display, NULL, NULL); getScreenSizeInPixels(activity, &states->screenSize.x, &states->screenSize.y); + getFullScreenSizeInPixels(activity, &states->fullScreenSize.x, &states->fullScreenSize.y); // Redirect error messages to logcat sf::err().rdbuf(&states->logcat); diff --git a/src/SFML/System/Android/Activity.hpp b/src/SFML/System/Android/Activity.hpp index 0adcf1a21..911244ec5 100644 --- a/src/SFML/System/Android/Activity.hpp +++ b/src/SFML/System/Android/Activity.hpp @@ -80,6 +80,7 @@ struct ActivityStates bool mainOver; Vector2i screenSize; + Vector2i fullScreenSize; bool initialized; bool terminated; diff --git a/src/SFML/Window/Android/VideoModeImpl.cpp b/src/SFML/Window/Android/VideoModeImpl.cpp index 71535a4a3..0938c4eb3 100644 --- a/src/SFML/Window/Android/VideoModeImpl.cpp +++ b/src/SFML/Window/Android/VideoModeImpl.cpp @@ -38,7 +38,15 @@ namespace priv //////////////////////////////////////////////////////////// std::vector VideoModeImpl::getFullscreenModes() { - VideoMode desktop = getDesktopMode(); + // Get the activity states + priv::ActivityStates& states = priv::getActivity(); + + VideoMode desktop; + + { + Lock lock(states.mutex); + desktop = VideoMode(static_cast(states.fullScreenSize.x), static_cast(states.fullScreenSize.y)); + } // Return both portrait and landscape resolutions std::vector modes;