Reduce pointer usage

This commit is contained in:
Chris Thrasher 2024-11-19 13:13:15 -07:00
parent dab1800f61
commit 3c084bf661
2 changed files with 86 additions and 86 deletions

View File

@ -26,53 +26,53 @@
int vibrate(sf::Time duration) int vibrate(sf::Time duration)
{ {
// First we'll need the native activity handle // First we'll need the native activity handle
ANativeActivity* activity = sf::getNativeActivity(); ANativeActivity& activity = *sf::getNativeActivity();
// Retrieve the JVM and JNI environment // Retrieve the JVM and JNI environment
JavaVM* vm = activity->vm; JavaVM& vm = *activity.vm;
JNIEnv* env = activity->env; JNIEnv& env = *activity.env;
// First, attach this thread to the main thread // First, attach this thread to the main thread
JavaVMAttachArgs attachargs; JavaVMAttachArgs attachargs;
attachargs.version = JNI_VERSION_1_6; attachargs.version = JNI_VERSION_1_6;
attachargs.name = "NativeThread"; attachargs.name = "NativeThread";
attachargs.group = nullptr; attachargs.group = nullptr;
jint res = vm->AttachCurrentThread(&env, &attachargs); jint res = vm.AttachCurrentThread(&env, &attachargs);
if (res == JNI_ERR) if (res == JNI_ERR)
return EXIT_FAILURE; return EXIT_FAILURE;
// Retrieve class information // Retrieve class information
jclass natact = env->FindClass("android/app/NativeActivity"); jclass natact = env.FindClass("android/app/NativeActivity");
jclass context = env->FindClass("android/content/Context"); jclass context = env.FindClass("android/content/Context");
// Get the value of a constant // Get the value of a constant
jfieldID fid = env->GetStaticFieldID(context, "VIBRATOR_SERVICE", "Ljava/lang/String;"); jfieldID fid = env.GetStaticFieldID(context, "VIBRATOR_SERVICE", "Ljava/lang/String;");
jobject svcstr = env->GetStaticObjectField(context, fid); jobject svcstr = env.GetStaticObjectField(context, fid);
// Get the method 'getSystemService' and call it // Get the method 'getSystemService' and call it
jmethodID getss = env->GetMethodID(natact, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;"); jmethodID getss = env.GetMethodID(natact, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
jobject vib_obj = env->CallObjectMethod(activity->clazz, getss, svcstr); jobject vib_obj = env.CallObjectMethod(activity.clazz, getss, svcstr);
// Get the object's class and retrieve the member name // Get the object's class and retrieve the member name
jclass vib_cls = env->GetObjectClass(vib_obj); jclass vib_cls = env.GetObjectClass(vib_obj);
jmethodID vibrate = env->GetMethodID(vib_cls, "vibrate", "(J)V"); jmethodID vibrate = env.GetMethodID(vib_cls, "vibrate", "(J)V");
// Determine the timeframe // Determine the timeframe
jlong length = duration.asMilliseconds(); jlong length = duration.asMilliseconds();
// Bzzz! // Bzzz!
env->CallVoidMethod(vib_obj, vibrate, length); env.CallVoidMethod(vib_obj, vibrate, length);
// Free references // Free references
env->DeleteLocalRef(vib_obj); env.DeleteLocalRef(vib_obj);
env->DeleteLocalRef(vib_cls); env.DeleteLocalRef(vib_cls);
env->DeleteLocalRef(svcstr); env.DeleteLocalRef(svcstr);
env->DeleteLocalRef(context); env.DeleteLocalRef(context);
env->DeleteLocalRef(natact); env.DeleteLocalRef(natact);
// Detach thread again // Detach thread again
vm->DetachCurrentThread(); vm.DetachCurrentThread();
} }
#endif #endif

View File

@ -100,18 +100,18 @@ void onStart(ANativeActivity* /* activity */)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
int getAndroidApiLevel(ANativeActivity& activity) int getAndroidApiLevel(ANativeActivity& activity)
{ {
JNIEnv* lJNIEnv = activity.env; JNIEnv& lJNIEnv = *activity.env;
jclass versionClass = lJNIEnv->FindClass("android/os/Build$VERSION"); jclass versionClass = lJNIEnv.FindClass("android/os/Build$VERSION");
if (versionClass == nullptr) if (versionClass == nullptr)
return 0; return 0;
jfieldID sdkIntFieldID = lJNIEnv->GetStaticFieldID(versionClass, "SDK_INT", "I"); jfieldID sdkIntFieldID = lJNIEnv.GetStaticFieldID(versionClass, "SDK_INT", "I");
if (sdkIntFieldID == nullptr) if (sdkIntFieldID == nullptr)
return 0; return 0;
jint sdkInt = 0; jint sdkInt = 0;
sdkInt = lJNIEnv->GetStaticIntField(versionClass, sdkIntFieldID); sdkInt = lJNIEnv.GetStaticIntField(versionClass, sdkIntFieldID);
return sdkInt; return sdkInt;
} }
@ -133,19 +133,19 @@ void goToFullscreenMode(ANativeActivity& activity)
ANativeActivity_setWindowFlags(&activity, AWINDOW_FLAG_FULLSCREEN, AWINDOW_FLAG_FULLSCREEN); ANativeActivity_setWindowFlags(&activity, AWINDOW_FLAG_FULLSCREEN, AWINDOW_FLAG_FULLSCREEN);
// Hide the navigation bar // Hide the navigation bar
JNIEnv* lJNIEnv = activity.env; JNIEnv& lJNIEnv = *activity.env;
jobject objectActivity = activity.clazz; jobject objectActivity = activity.clazz;
jclass classActivity = lJNIEnv->GetObjectClass(objectActivity); jclass classActivity = lJNIEnv.GetObjectClass(objectActivity);
jmethodID methodGetWindow = lJNIEnv->GetMethodID(classActivity, "getWindow", "()Landroid/view/Window;"); jmethodID methodGetWindow = lJNIEnv.GetMethodID(classActivity, "getWindow", "()Landroid/view/Window;");
jobject objectWindow = lJNIEnv->CallObjectMethod(objectActivity, methodGetWindow); jobject objectWindow = lJNIEnv.CallObjectMethod(objectActivity, methodGetWindow);
jclass classWindow = lJNIEnv->FindClass("android/view/Window"); jclass classWindow = lJNIEnv.FindClass("android/view/Window");
jmethodID methodGetDecorView = lJNIEnv->GetMethodID(classWindow, "getDecorView", "()Landroid/view/View;"); jmethodID methodGetDecorView = lJNIEnv.GetMethodID(classWindow, "getDecorView", "()Landroid/view/View;");
jobject objectDecorView = lJNIEnv->CallObjectMethod(objectWindow, methodGetDecorView); jobject objectDecorView = lJNIEnv.CallObjectMethod(objectWindow, methodGetDecorView);
jclass classView = lJNIEnv->FindClass("android/view/View"); jclass classView = lJNIEnv.FindClass("android/view/View");
// Default flags // Default flags
jint flags = 0; jint flags = 0;
@ -153,33 +153,33 @@ void goToFullscreenMode(ANativeActivity& activity)
// API Level 14 // API Level 14
if (apiLevel >= 14) if (apiLevel >= 14)
{ {
jfieldID fieldSystemUiFlagLowProfile = lJNIEnv->GetStaticFieldID(classView, jfieldID fieldSystemUiFlagLowProfile = lJNIEnv.GetStaticFieldID(classView,
"SYSTEM_UI_FLAG_HIDE_NAVIGATION", "SYSTEM_UI_FLAG_HIDE_NAVIGATION",
"I"); "I");
const jint systemUiFlagLowProfile = lJNIEnv->GetStaticIntField(classView, fieldSystemUiFlagLowProfile); const jint systemUiFlagLowProfile = lJNIEnv.GetStaticIntField(classView, fieldSystemUiFlagLowProfile);
flags |= systemUiFlagLowProfile; flags |= systemUiFlagLowProfile;
} }
// API Level 16 // API Level 16
if (apiLevel >= 16) if (apiLevel >= 16)
{ {
jfieldID fieldSystemUiFlagFullscreen = lJNIEnv->GetStaticFieldID(classView, "SYSTEM_UI_FLAG_FULLSCREEN", "I"); jfieldID fieldSystemUiFlagFullscreen = lJNIEnv.GetStaticFieldID(classView, "SYSTEM_UI_FLAG_FULLSCREEN", "I");
const jint systemUiFlagFullscreen = lJNIEnv->GetStaticIntField(classView, fieldSystemUiFlagFullscreen); const jint systemUiFlagFullscreen = lJNIEnv.GetStaticIntField(classView, fieldSystemUiFlagFullscreen);
flags |= systemUiFlagFullscreen; flags |= systemUiFlagFullscreen;
} }
// API Level 19 // API Level 19
if (apiLevel >= 19) if (apiLevel >= 19)
{ {
jfieldID fieldSystemUiFlagImmersiveSticky = lJNIEnv->GetStaticFieldID(classView, jfieldID fieldSystemUiFlagImmersiveSticky = lJNIEnv.GetStaticFieldID(classView,
"SYSTEM_UI_FLAG_IMMERSIVE_STICKY", "SYSTEM_UI_FLAG_IMMERSIVE_STICKY",
"I"); "I");
const jint systemUiFlagImmersiveSticky = lJNIEnv->GetStaticIntField(classView, fieldSystemUiFlagImmersiveSticky); const jint systemUiFlagImmersiveSticky = lJNIEnv.GetStaticIntField(classView, fieldSystemUiFlagImmersiveSticky);
flags |= systemUiFlagImmersiveSticky; flags |= systemUiFlagImmersiveSticky;
} }
jmethodID methodsetSystemUiVisibility = lJNIEnv->GetMethodID(classView, "setSystemUiVisibility", "(I)V"); jmethodID methodsetSystemUiVisibility = lJNIEnv.GetMethodID(classView, "setSystemUiVisibility", "(I)V");
lJNIEnv->CallVoidMethod(objectDecorView, methodsetSystemUiVisibility, flags); lJNIEnv.CallVoidMethod(objectDecorView, methodsetSystemUiVisibility, flags);
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
@ -190,35 +190,35 @@ void getScreenSizeInPixels(ANativeActivity& activity, int& width, int& height)
// DisplayMetrics dm = new DisplayMetrics(); // DisplayMetrics dm = new DisplayMetrics();
// getWindowManager().getDefaultDisplay().getMetrics(dm); // getWindowManager().getDefaultDisplay().getMetrics(dm);
JNIEnv* lJNIEnv = activity.env; JNIEnv& lJNIEnv = *activity.env;
jobject objectActivity = activity.clazz; jobject objectActivity = activity.clazz;
jclass classActivity = lJNIEnv->GetObjectClass(objectActivity); jclass classActivity = lJNIEnv.GetObjectClass(objectActivity);
jclass classDisplayMetrics = lJNIEnv->FindClass("android/util/DisplayMetrics"); jclass classDisplayMetrics = lJNIEnv.FindClass("android/util/DisplayMetrics");
jmethodID initDisplayMetrics = lJNIEnv->GetMethodID(classDisplayMetrics, "<init>", "()V"); jmethodID initDisplayMetrics = lJNIEnv.GetMethodID(classDisplayMetrics, "<init>", "()V");
jobject objectDisplayMetrics = lJNIEnv->NewObject(classDisplayMetrics, initDisplayMetrics); jobject objectDisplayMetrics = lJNIEnv.NewObject(classDisplayMetrics, initDisplayMetrics);
jmethodID methodGetWindowManager = lJNIEnv->GetMethodID(classActivity, jmethodID methodGetWindowManager = lJNIEnv.GetMethodID(classActivity,
"getWindowManager", "getWindowManager",
"()Landroid/view/WindowManager;"); "()Landroid/view/WindowManager;");
jobject objectWindowManager = lJNIEnv->CallObjectMethod(objectActivity, methodGetWindowManager); jobject objectWindowManager = lJNIEnv.CallObjectMethod(objectActivity, methodGetWindowManager);
jclass classWindowManager = lJNIEnv->FindClass("android/view/WindowManager"); jclass classWindowManager = lJNIEnv.FindClass("android/view/WindowManager");
jmethodID methodGetDefaultDisplay = lJNIEnv->GetMethodID(classWindowManager, jmethodID methodGetDefaultDisplay = lJNIEnv.GetMethodID(classWindowManager,
"getDefaultDisplay", "getDefaultDisplay",
"()Landroid/view/Display;"); "()Landroid/view/Display;");
jobject objectDisplay = lJNIEnv->CallObjectMethod(objectWindowManager, methodGetDefaultDisplay); jobject objectDisplay = lJNIEnv.CallObjectMethod(objectWindowManager, methodGetDefaultDisplay);
jclass classDisplay = lJNIEnv->FindClass("android/view/Display"); jclass classDisplay = lJNIEnv.FindClass("android/view/Display");
jmethodID methodGetMetrics = lJNIEnv->GetMethodID(classDisplay, "getMetrics", "(Landroid/util/DisplayMetrics;)V"); jmethodID methodGetMetrics = lJNIEnv.GetMethodID(classDisplay, "getMetrics", "(Landroid/util/DisplayMetrics;)V");
lJNIEnv->CallVoidMethod(objectDisplay, methodGetMetrics, objectDisplayMetrics); lJNIEnv.CallVoidMethod(objectDisplay, methodGetMetrics, objectDisplayMetrics);
jfieldID fieldWidthPixels = lJNIEnv->GetFieldID(classDisplayMetrics, "widthPixels", "I"); jfieldID fieldWidthPixels = lJNIEnv.GetFieldID(classDisplayMetrics, "widthPixels", "I");
jfieldID fieldHeightPixels = lJNIEnv->GetFieldID(classDisplayMetrics, "heightPixels", "I"); jfieldID fieldHeightPixels = lJNIEnv.GetFieldID(classDisplayMetrics, "heightPixels", "I");
width = lJNIEnv->GetIntField(objectDisplayMetrics, fieldWidthPixels); width = lJNIEnv.GetIntField(objectDisplayMetrics, fieldWidthPixels);
height = lJNIEnv->GetIntField(objectDisplayMetrics, fieldHeightPixels); height = lJNIEnv.GetIntField(objectDisplayMetrics, fieldHeightPixels);
} }
@ -230,41 +230,41 @@ void getFullScreenSizeInPixels(ANativeActivity& activity, int& width, int& heigh
// DisplayMetrics dm = new DisplayMetrics(); // DisplayMetrics dm = new DisplayMetrics();
// getWindowManager().getDefaultDisplay().getRealMetrics(dm); // getWindowManager().getDefaultDisplay().getRealMetrics(dm);
JNIEnv* lJNIEnv = activity.env; JNIEnv& lJNIEnv = *activity.env;
jobject objectActivity = activity.clazz; jobject objectActivity = activity.clazz;
jclass classActivity = lJNIEnv->GetObjectClass(objectActivity); jclass classActivity = lJNIEnv.GetObjectClass(objectActivity);
jclass classDisplayMetrics = lJNIEnv->FindClass("android/util/DisplayMetrics"); jclass classDisplayMetrics = lJNIEnv.FindClass("android/util/DisplayMetrics");
jmethodID initDisplayMetrics = lJNIEnv->GetMethodID(classDisplayMetrics, "<init>", "()V"); jmethodID initDisplayMetrics = lJNIEnv.GetMethodID(classDisplayMetrics, "<init>", "()V");
jobject objectDisplayMetrics = lJNIEnv->NewObject(classDisplayMetrics, initDisplayMetrics); jobject objectDisplayMetrics = lJNIEnv.NewObject(classDisplayMetrics, initDisplayMetrics);
jmethodID methodGetWindowManager = lJNIEnv->GetMethodID(classActivity, jmethodID methodGetWindowManager = lJNIEnv.GetMethodID(classActivity,
"getWindowManager", "getWindowManager",
"()Landroid/view/WindowManager;"); "()Landroid/view/WindowManager;");
jobject objectWindowManager = lJNIEnv->CallObjectMethod(objectActivity, methodGetWindowManager); jobject objectWindowManager = lJNIEnv.CallObjectMethod(objectActivity, methodGetWindowManager);
jclass classWindowManager = lJNIEnv->FindClass("android/view/WindowManager"); jclass classWindowManager = lJNIEnv.FindClass("android/view/WindowManager");
jmethodID methodGetDefaultDisplay = lJNIEnv->GetMethodID(classWindowManager, jmethodID methodGetDefaultDisplay = lJNIEnv.GetMethodID(classWindowManager,
"getDefaultDisplay", "getDefaultDisplay",
"()Landroid/view/Display;"); "()Landroid/view/Display;");
jobject objectDisplay = lJNIEnv->CallObjectMethod(objectWindowManager, methodGetDefaultDisplay); jobject objectDisplay = lJNIEnv.CallObjectMethod(objectWindowManager, methodGetDefaultDisplay);
jclass classDisplay = lJNIEnv->FindClass("android/view/Display"); jclass classDisplay = lJNIEnv.FindClass("android/view/Display");
jmethodID methodGetMetrics = nullptr; jmethodID methodGetMetrics = nullptr;
// getRealMetrics is only supported on API level 17 and above, if we are below that, we will fall back to getMetrics // getRealMetrics is only supported on API level 17 and above, if we are below that, we will fall back to getMetrics
if (getAndroidApiLevel(activity) >= 17) if (getAndroidApiLevel(activity) >= 17)
methodGetMetrics = lJNIEnv->GetMethodID(classDisplay, "getRealMetrics", "(Landroid/util/DisplayMetrics;)V"); methodGetMetrics = lJNIEnv.GetMethodID(classDisplay, "getRealMetrics", "(Landroid/util/DisplayMetrics;)V");
else else
methodGetMetrics = lJNIEnv->GetMethodID(classDisplay, "getMetrics", "(Landroid/util/DisplayMetrics;)V"); methodGetMetrics = lJNIEnv.GetMethodID(classDisplay, "getMetrics", "(Landroid/util/DisplayMetrics;)V");
lJNIEnv->CallVoidMethod(objectDisplay, methodGetMetrics, objectDisplayMetrics); lJNIEnv.CallVoidMethod(objectDisplay, methodGetMetrics, objectDisplayMetrics);
jfieldID fieldWidthPixels = lJNIEnv->GetFieldID(classDisplayMetrics, "widthPixels", "I"); jfieldID fieldWidthPixels = lJNIEnv.GetFieldID(classDisplayMetrics, "widthPixels", "I");
jfieldID fieldHeightPixels = lJNIEnv->GetFieldID(classDisplayMetrics, "heightPixels", "I"); jfieldID fieldHeightPixels = lJNIEnv.GetFieldID(classDisplayMetrics, "heightPixels", "I");
width = lJNIEnv->GetIntField(objectDisplayMetrics, fieldWidthPixels); width = lJNIEnv.GetIntField(objectDisplayMetrics, fieldWidthPixels);
height = lJNIEnv->GetIntField(objectDisplayMetrics, fieldHeightPixels); height = lJNIEnv.GetIntField(objectDisplayMetrics, fieldHeightPixels);
} }