Android get the display Density

main
Sylvain 2023-01-25 14:55:50 +01:00 committed by Sam Lantinga
parent 6a27188023
commit 73dc327c84
5 changed files with 13 additions and 9 deletions

View File

@ -311,7 +311,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
mNextNativeState = NativeState.INIT; mNextNativeState = NativeState.INIT;
mCurrentNativeState = NativeState.INIT; mCurrentNativeState = NativeState.INIT;
} }
protected SDLSurface createSDLSurface(Context context) { protected SDLSurface createSDLSurface(Context context) {
return new SDLSurface(context); return new SDLSurface(context);
} }
@ -909,7 +909,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
public static native void nativeResume(); public static native void nativeResume();
public static native void nativeFocusChanged(boolean hasFocus); public static native void nativeFocusChanged(boolean hasFocus);
public static native void onNativeDropFile(String filename); public static native void onNativeDropFile(String filename);
public static native void nativeSetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float rate); public static native void nativeSetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float density, float rate);
public static native void onNativeResize(); public static native void onNativeResize();
public static native void onNativeKeyDown(int keycode); public static native void onNativeKeyDown(int keycode);
public static native void onNativeKeyUp(int keycode); public static native void onNativeKeyUp(int keycode);

View File

@ -114,6 +114,7 @@ public class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
mHeight = height; mHeight = height;
int nDeviceWidth = width; int nDeviceWidth = width;
int nDeviceHeight = height; int nDeviceHeight = height;
float density = 1.0f;
try try
{ {
if (Build.VERSION.SDK_INT >= 17) { if (Build.VERSION.SDK_INT >= 17) {
@ -121,6 +122,7 @@ public class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
mDisplay.getRealMetrics( realMetrics ); mDisplay.getRealMetrics( realMetrics );
nDeviceWidth = realMetrics.widthPixels; nDeviceWidth = realMetrics.widthPixels;
nDeviceHeight = realMetrics.heightPixels; nDeviceHeight = realMetrics.heightPixels;
density = realMetrics.density;
} }
} catch(Exception ignored) { } catch(Exception ignored) {
} }
@ -132,7 +134,7 @@ public class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
Log.v("SDL", "Window size: " + width + "x" + height); Log.v("SDL", "Window size: " + width + "x" + height);
Log.v("SDL", "Device size: " + nDeviceWidth + "x" + nDeviceHeight); Log.v("SDL", "Device size: " + nDeviceWidth + "x" + nDeviceHeight);
SDLActivity.nativeSetScreenResolution(width, height, nDeviceWidth, nDeviceHeight, mDisplay.getRefreshRate()); SDLActivity.nativeSetScreenResolution(width, height, nDeviceWidth, nDeviceHeight, density, mDisplay.getRefreshRate());
SDLActivity.onNativeResize(); SDLActivity.onNativeResize();
// Prevent a screen distortion glitch, // Prevent a screen distortion glitch,

View File

@ -74,7 +74,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeDropFile)(
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetScreenResolution)( JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetScreenResolution)(
JNIEnv *env, jclass jcls, JNIEnv *env, jclass jcls,
jint surfaceWidth, jint surfaceHeight, jint surfaceWidth, jint surfaceHeight,
jint deviceWidth, jint deviceHeight, jfloat rate); jint deviceWidth, jint deviceHeight, jfloat density, jfloat rate);
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)( JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)(
JNIEnv *env, jclass cls); JNIEnv *env, jclass cls);
@ -168,7 +168,7 @@ static JNINativeMethod SDLActivity_tab[] = {
{ "nativeSetupJNI", "()I", SDL_JAVA_INTERFACE(nativeSetupJNI) }, { "nativeSetupJNI", "()I", SDL_JAVA_INTERFACE(nativeSetupJNI) },
{ "nativeRunMain", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)I", SDL_JAVA_INTERFACE(nativeRunMain) }, { "nativeRunMain", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)I", SDL_JAVA_INTERFACE(nativeRunMain) },
{ "onNativeDropFile", "(Ljava/lang/String;)V", SDL_JAVA_INTERFACE(onNativeDropFile) }, { "onNativeDropFile", "(Ljava/lang/String;)V", SDL_JAVA_INTERFACE(onNativeDropFile) },
{ "nativeSetScreenResolution", "(IIIIF)V", SDL_JAVA_INTERFACE(nativeSetScreenResolution) }, { "nativeSetScreenResolution", "(IIIIFF)V", SDL_JAVA_INTERFACE(nativeSetScreenResolution) },
{ "onNativeResize", "()V", SDL_JAVA_INTERFACE(onNativeResize) }, { "onNativeResize", "()V", SDL_JAVA_INTERFACE(onNativeResize) },
{ "onNativeSurfaceCreated", "()V", SDL_JAVA_INTERFACE(onNativeSurfaceCreated) }, { "onNativeSurfaceCreated", "()V", SDL_JAVA_INTERFACE(onNativeSurfaceCreated) },
{ "onNativeSurfaceChanged", "()V", SDL_JAVA_INTERFACE(onNativeSurfaceChanged) }, { "onNativeSurfaceChanged", "()V", SDL_JAVA_INTERFACE(onNativeSurfaceChanged) },
@ -870,11 +870,11 @@ retry:
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetScreenResolution)( JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetScreenResolution)(
JNIEnv *env, jclass jcls, JNIEnv *env, jclass jcls,
jint surfaceWidth, jint surfaceHeight, jint surfaceWidth, jint surfaceHeight,
jint deviceWidth, jint deviceHeight, jfloat rate) jint deviceWidth, jint deviceHeight, jfloat density, jfloat rate)
{ {
SDL_LockMutex(Android_ActivityMutex); SDL_LockMutex(Android_ActivityMutex);
Android_SetScreenResolution(surfaceWidth, surfaceHeight, deviceWidth, deviceHeight, rate); Android_SetScreenResolution(surfaceWidth, surfaceHeight, deviceWidth, deviceHeight, density, rate);
SDL_UnlockMutex(Android_ActivityMutex); SDL_UnlockMutex(Android_ActivityMutex);
} }

View File

@ -62,6 +62,7 @@ static int Android_DeviceWidth = 0;
static int Android_DeviceHeight = 0; static int Android_DeviceHeight = 0;
static Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_RGB565; /* Default SurfaceView format, in case this is queried before being filled */ static Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_RGB565; /* Default SurfaceView format, in case this is queried before being filled */
static float Android_ScreenRate = 0.0f; static float Android_ScreenRate = 0.0f;
static float Android_ScreenDensity = 1.0f;
SDL_sem *Android_PauseSem = NULL; SDL_sem *Android_PauseSem = NULL;
SDL_sem *Android_ResumeSem = NULL; SDL_sem *Android_ResumeSem = NULL;
SDL_mutex *Android_ActivityMutex = NULL; SDL_mutex *Android_ActivityMutex = NULL;
@ -213,13 +214,14 @@ int Android_GetDisplayPhysicalDPI(_THIS, SDL_VideoDisplay *display, float *ddpi,
return Android_JNI_GetDisplayPhysicalDPI(ddpi, hdpi, vdpi); return Android_JNI_GetDisplayPhysicalDPI(ddpi, hdpi, vdpi);
} }
void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float rate) void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float density, float rate)
{ {
Android_SurfaceWidth = surfaceWidth; Android_SurfaceWidth = surfaceWidth;
Android_SurfaceHeight = surfaceHeight; Android_SurfaceHeight = surfaceHeight;
Android_DeviceWidth = deviceWidth; Android_DeviceWidth = deviceWidth;
Android_DeviceHeight = deviceHeight; Android_DeviceHeight = deviceHeight;
Android_ScreenRate = rate; Android_ScreenRate = rate;
Android_ScreenDensity = density;
} }
static Uint32 format_to_pixelFormat(int format) static Uint32 format_to_pixelFormat(int format)

View File

@ -26,7 +26,7 @@
#include "../SDL_sysvideo.h" #include "../SDL_sysvideo.h"
/* Called by the JNI layer when the screen changes size or format */ /* Called by the JNI layer when the screen changes size or format */
extern void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float rate); extern void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float density, float rate);
extern void Android_SetFormat(int format_wanted, int format_got); extern void Android_SetFormat(int format_wanted, int format_got);
extern void Android_SendResize(SDL_Window *window); extern void Android_SendResize(SDL_Window *window);