From fb81ddfdcbc3419160d9686dce6fd5eefeb35772 Mon Sep 17 00:00:00 2001 From: Jonathan De Wachter Date: Thu, 26 Jun 2014 04:56:06 +0200 Subject: [PATCH] [Android] VideoMode::getDesktopMode() returns the physical scren size in pixels --- src/SFML/Main/MainAndroid.cpp | 40 +++++++++++++++++++++++ src/SFML/System/Android/Activity.hpp | 2 ++ src/SFML/Window/Android/VideoModeImpl.cpp | 15 +-------- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/SFML/Main/MainAndroid.cpp b/src/SFML/Main/MainAndroid.cpp index 08e232ab6..83fe044ef 100644 --- a/src/SFML/Main/MainAndroid.cpp +++ b/src/SFML/Main/MainAndroid.cpp @@ -119,6 +119,44 @@ void* main(ActivityStates* states) } // namespace sf +//////////////////////////////////////////////////////////// + +void getScreenSizeInPixels(ANativeActivity* activity, int* width, int* height) +{ + // Perform the following java code: + // + // DisplayMetrics dm = new DisplayMetrics(); + // getWindowManager().getDefaultDisplay().getMetrics(dm); + + JavaVM* lJavaVM = activity->vm; + 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 = 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) { @@ -453,6 +491,8 @@ void ANativeActivity_onCreate(ANativeActivity* activity, void* savedState, size_ // Initialize the display eglInitialize(states->display, NULL, NULL); + getScreenSizeInPixels(activity, &states->screenSize.x, &states->screenSize.y); + // Launch the main thread sf::Thread* thread = new sf::Thread(sf::priv::main, states); thread->launch(); diff --git a/src/SFML/System/Android/Activity.hpp b/src/SFML/System/Android/Activity.hpp index e5c96786f..afd256b66 100644 --- a/src/SFML/System/Android/Activity.hpp +++ b/src/SFML/System/Android/Activity.hpp @@ -68,6 +68,8 @@ struct ActivityStates bool mainOver; + Vector2i screenSize; + bool initialized; bool terminated; diff --git a/src/SFML/Window/Android/VideoModeImpl.cpp b/src/SFML/Window/Android/VideoModeImpl.cpp index 8082c9ce8..d9c171c1f 100644 --- a/src/SFML/Window/Android/VideoModeImpl.cpp +++ b/src/SFML/Window/Android/VideoModeImpl.cpp @@ -55,20 +55,7 @@ VideoMode VideoModeImpl::getDesktopMode() priv::ActivityStates* states = priv::getActivity(NULL); Lock lock(states->mutex); - // Wait for a window if there's none - while (!states->window) - { - states->mutex.unlock(); - sleep(milliseconds(10)); - states->mutex.lock(); - } - - // Get size from the window - Vector2i size; - size.x = ANativeWindow_getWidth(states->window); - size.y = ANativeWindow_getHeight(states->window); - - return VideoMode(size.x, size.y); + return VideoMode(states->screenSize.x, states->screenSize.y); } } // namespace priv