mirror of
https://github.com/SFML/SFML.git
synced 2024-11-28 22:31:09 +08:00
[Android] VideoMode::getDesktopMode() returns the physical scren size in pixels
This commit is contained in:
parent
d73418261b
commit
fb81ddfdcb
@ -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, "<init>", "()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();
|
||||
|
@ -68,6 +68,8 @@ struct ActivityStates
|
||||
|
||||
bool mainOver;
|
||||
|
||||
Vector2i screenSize;
|
||||
|
||||
bool initialized;
|
||||
bool terminated;
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user