Added the concept of display natural orientation

Also renamed SDL_GetDisplayOrientation() SDL_GetDisplayCurrentOrientation()

The natural orientation of the primary display is the frame of reference for accelerometer and gyro sensor readings.
This commit is contained in:
Sam Lantinga 2023-06-17 00:52:40 -07:00
parent 8de6ce7e92
commit e6d1ba2a17
20 changed files with 213 additions and 79 deletions

View file

@ -155,10 +155,14 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetenv)(
JNIEnv *env, jclass cls,
jstring name, jstring value);
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeOrientationChanged)(
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetNaturalOrientation)(
JNIEnv *env, jclass cls,
jint orientation);
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeRotationChanged)(
JNIEnv *env, jclass cls,
jint rotation);
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeAddTouch)(
JNIEnv *env, jclass cls,
jint touchId, jstring name);
@ -202,7 +206,8 @@ static JNINativeMethod SDLActivity_tab[] = {
{ "nativeGetHint", "(Ljava/lang/String;)Ljava/lang/String;", SDL_JAVA_INTERFACE(nativeGetHint) },
{ "nativeGetHintBoolean", "(Ljava/lang/String;Z)Z", SDL_JAVA_INTERFACE(nativeGetHintBoolean) },
{ "nativeSetenv", "(Ljava/lang/String;Ljava/lang/String;)V", SDL_JAVA_INTERFACE(nativeSetenv) },
{ "onNativeOrientationChanged", "(I)V", SDL_JAVA_INTERFACE(onNativeOrientationChanged) },
{ "nativeSetNaturalOrientation", "(I)V", SDL_JAVA_INTERFACE(nativeSetNaturalOrientation) },
{ "onNativeRotationChanged", "(I)V", SDL_JAVA_INTERFACE(onNativeRotationChanged) },
{ "nativeAddTouch", "(ILjava/lang/String;)V", SDL_JAVA_INTERFACE(nativeAddTouch) },
{ "nativePermissionResult", "(IZ)V", SDL_JAVA_INTERFACE(nativePermissionResult) },
{ "nativeAllowRecreateActivity", "()Z", SDL_JAVA_INTERFACE(nativeAllowRecreateActivity) },
@ -369,7 +374,8 @@ static jmethodID midHapticRun;
static jmethodID midHapticStop;
/* Accelerometer data storage */
static SDL_DisplayOrientation displayOrientation;
static SDL_DisplayOrientation displayNaturalOrientation;
static SDL_DisplayOrientation displayCurrentOrientation;
static float fLastAccelerometer[3];
static SDL_bool bHasNewData;
@ -934,17 +940,44 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)(
SDL_UnlockMutex(Android_ActivityMutex);
}
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeOrientationChanged)(
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetNaturalOrientation)(
JNIEnv *env, jclass jcls,
jint orientation)
{
displayNaturalOrientation = (SDL_DisplayOrientation)orientation;
}
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeRotationChanged)(
JNIEnv *env, jclass jcls,
jint rotation)
{
SDL_LockMutex(Android_ActivityMutex);
displayOrientation = (SDL_DisplayOrientation)orientation;
if (displayNaturalOrientation == SDL_ORIENTATION_LANDSCAPE) {
rotation += 90;
}
switch (rotation % 360) {
case 0:
displayCurrentOrientation = SDL_ORIENTATION_PORTRAIT;
break;
case 90:
displayCurrentOrientation = SDL_ORIENTATION_LANDSCAPE;
break;
case 180:
displayCurrentOrientation = SDL_ORIENTATION_PORTRAIT_FLIPPED;
break;
case 270:
displayCurrentOrientation = SDL_ORIENTATION_LANDSCAPE_FLIPPED;
break;
default:
displayCurrentOrientation = SDL_ORIENTATION_UNKNOWN;
break;
}
if (Android_Window) {
SDL_VideoDisplay *display = SDL_GetVideoDisplay(SDL_GetPrimaryDisplay());
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_ORIENTATION, orientation);
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_ORIENTATION, displayCurrentOrientation);
}
SDL_UnlockMutex(Android_ActivityMutex);
@ -1706,9 +1739,14 @@ int Android_JNI_OpenAudioDevice(int iscapture, int device_id, SDL_AudioSpec *spe
return 0;
}
SDL_DisplayOrientation Android_JNI_GetDisplayOrientation(void)
SDL_DisplayOrientation Android_JNI_GetDisplayNaturalOrientation(void)
{
return displayOrientation;
return displayNaturalOrientation;
}
SDL_DisplayOrientation Android_JNI_GetDisplayCurrentOrientation(void)
{
return displayCurrentOrientation;
}
void *Android_JNI_GetAudioBuffer(void)