Add SDL_GetDisplayDPI implementation on Android. (thanks Rachel!)

This commit is contained in:
Sam Lantinga 2017-10-31 13:49:59 -07:00
parent 0e032d5860
commit 4478707b0a
4 changed files with 42 additions and 1 deletions

View File

@ -18,6 +18,7 @@ import android.widget.Button;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import android.os.*; import android.os.*;
import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.util.SparseArray; import android.util.SparseArray;
import android.graphics.*; import android.graphics.*;
@ -613,6 +614,10 @@ public class SDLActivity extends Activity {
return SDL.getContext(); return SDL.getContext();
} }
public static DisplayMetrics getDisplayDPI() {
return getContext().getResources().getDisplayMetrics();
}
/** /**
* This method is called by SDL using JNI. * This method is called by SDL using JNI.
*/ */

View File

@ -214,6 +214,7 @@ static jmethodID midClipboardGetText;
static jmethodID midClipboardHasText; static jmethodID midClipboardHasText;
static jmethodID midOpenAPKExpansionInputStream; static jmethodID midOpenAPKExpansionInputStream;
static jmethodID midGetManifestEnvironmentVariable; static jmethodID midGetManifestEnvironmentVariable;
static jmethodID midGetDisplayDPI;
/* audio manager */ /* audio manager */
static jclass mAudioManagerClass; static jclass mAudioManagerClass;
@ -316,11 +317,13 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv* mEnv, jclass c
midGetManifestEnvironmentVariable = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, midGetManifestEnvironmentVariable = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
"getManifestEnvironmentVariable", "(Ljava/lang/String;)Ljava/lang/String;"); "getManifestEnvironmentVariable", "(Ljava/lang/String;)Ljava/lang/String;");
midGetDisplayDPI = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "getDisplayDPI", "()Landroid/util/DisplayMetrics;");
if (!midGetNativeSurface || if (!midGetNativeSurface ||
!midSetActivityTitle || !midSetOrientation || !midGetContext || !midInputGetInputDeviceIds || !midSetActivityTitle || !midSetOrientation || !midGetContext || !midInputGetInputDeviceIds ||
!midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown || !midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown ||
!midClipboardSetText || !midClipboardGetText || !midClipboardHasText || !midClipboardSetText || !midClipboardGetText || !midClipboardHasText ||
!midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariable) { !midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariable || !midGetDisplayDPI) {
__android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLActivity.java?"); __android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLActivity.java?");
} }
@ -1047,6 +1050,28 @@ int Android_JNI_OpenAudioDevice(int iscapture, int sampleRate, int is16Bit, int
return audioBufferFrames; return audioBufferFrames;
} }
int Android_JNI_GetDisplayDPI(float *ddpi, float *xdpi, float *ydpi)
{
JNIEnv *env = Android_JNI_GetEnv();
jobject jDisplayObj = (*env)->CallStaticObjectMethod(env, mActivityClass, midGetDisplayDPI);
jclass jDisplayClass = (*env)->GetObjectClass(env, jDisplayObj);
jfieldID fidXdpi = (*env)->GetFieldID(env, jDisplayClass, "xdpi", "F");
jfieldID fidYdpi = (*env)->GetFieldID(env, jDisplayClass, "ydpi", "F");
jfieldID fidDdpi = (*env)->GetFieldID(env, jDisplayClass, "densityDpi", "I");
float nativeXdpi = (*env)->GetFloatField(env, jDisplayObj, fidXdpi);
float nativeYdpi = (*env)->GetFloatField(env, jDisplayObj, fidYdpi);
int nativeDdpi = (*env)->GetIntField(env, jDisplayObj, fidDdpi);
*ddpi = (float)nativeDdpi;
*xdpi = nativeXdpi;
*ydpi = nativeYdpi;
return 0;
}
void * Android_JNI_GetAudioBuffer(void) void * Android_JNI_GetAudioBuffer(void)
{ {
return audioBufferPinned; return audioBufferPinned;

View File

@ -42,6 +42,8 @@ extern void Android_JNI_HideTextInput(void);
extern SDL_bool Android_JNI_IsScreenKeyboardShown(void); extern SDL_bool Android_JNI_IsScreenKeyboardShown(void);
extern ANativeWindow* Android_JNI_GetNativeWindow(void); extern ANativeWindow* Android_JNI_GetNativeWindow(void);
extern int Android_JNI_GetDisplayDPI(float *ddpi, float *xdpi, float *ydpi);
/* Audio support */ /* Audio support */
extern int Android_JNI_OpenAudioDevice(int iscapture, int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames); extern int Android_JNI_OpenAudioDevice(int iscapture, int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames);
extern void* Android_JNI_GetAudioBuffer(void); extern void* Android_JNI_GetAudioBuffer(void);

View File

@ -47,6 +47,7 @@
/* Initialization/Query functions */ /* Initialization/Query functions */
static int Android_VideoInit(_THIS); static int Android_VideoInit(_THIS);
static void Android_VideoQuit(_THIS); static void Android_VideoQuit(_THIS);
int Android_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi);
#include "../SDL_egl_c.h" #include "../SDL_egl_c.h"
#define Android_GLES_GetProcAddress SDL_EGL_GetProcAddress #define Android_GLES_GetProcAddress SDL_EGL_GetProcAddress
@ -115,6 +116,8 @@ Android_CreateDevice(int devindex)
device->VideoQuit = Android_VideoQuit; device->VideoQuit = Android_VideoQuit;
device->PumpEvents = Android_PumpEvents; device->PumpEvents = Android_PumpEvents;
device->GetDisplayDPI = Android_GetDisplayDPI;
device->CreateSDLWindow = Android_CreateWindow; device->CreateSDLWindow = Android_CreateWindow;
device->SetWindowTitle = Android_SetWindowTitle; device->SetWindowTitle = Android_SetWindowTitle;
device->DestroyWindow = Android_DestroyWindow; device->DestroyWindow = Android_DestroyWindow;
@ -198,6 +201,12 @@ Android_VideoQuit(_THIS)
Android_QuitTouch(); Android_QuitTouch();
} }
int
Android_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi)
{
return Android_JNI_GetDisplayDPI(ddpi, hdpi, vdpi);
}
void void
Android_SetScreenResolution(int width, int height, Uint32 format, float rate) Android_SetScreenResolution(int width, int height, Uint32 format, float rate)
{ {