My android game uses OpenGL and Android NDK. I am currently passing the necessary dpi values as parameters, by making JNI native call from my Activity/GLSurfaceView.
Currently I am using :
// request an instance of DisplayMetrics, say 'dm' from android.
float densityFactor = dm.density;
// pass this as a parameter using JNI call
myCustomNativeMethod(densityFactor); # implementation in a 'game.c' file
I was just wondering if one can access the android environment parameters without using JNI to receive them (the parameters). May be like, importing some low level NDK header files (.h) that will help me communicate directly with the UI system or something?
I mean like:
#import <android_displayparams.h>
...
float densityfactor = getDisplayParamDensity(); // <- Is this possible?
Note: This is a question out of curiosity, and I am aware that such a mechanism might not be a good practice.
With your setup with Java Activity / GLSurfaceView passing DPI as parameters looks like the best solution.
If you would use NativeActivity you could get DisplayMetrics.densityDpi using:
#include <android/configuration.h>
#include <android_native_app_glue.h>
#include <android/native_activity.h>
int32_t getDensityDpi(android_app* app) {
AConfiguration* config = AConfiguration_new();
AConfiguration_fromAssetManager(config, app->activity->assetManager);
int32_t density = AConfiguration_getDensity(config);
AConfiguration_delete(config);
return density;
}
Unfortunately there is no API to get DisplayMetrics.xdpi and DisplayMetrics.ydpi.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With