5

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.

Adrian Heine
  • 4,051
  • 2
  • 30
  • 43
Hari Krishna Ganji
  • 1,647
  • 2
  • 20
  • 33
  • Does this answer it for you? http://stackoverflow.com/q/7594527/517561 – Sparky Jul 23 '12 at 11:11
  • @Sparky: I am afraid not. I am aware of the DisplayMatrics class to access the appropriate values. I wanted to know if there is a native way of accessing the same. Thanks! – Hari Krishna Ganji Jul 23 '12 at 11:34
  • I have improved the question a little bit. – Hari Krishna Ganji Jul 23 '12 at 12:13
  • 1
    Technically, yes, there are config files on the device and similar you could parse, but you aren't supposed to do that as it's not a stable API and could change. Arguably you could also avoid jni using socket communication, but that's just being silly. Passing config info from java to your native code on activity creation is really not a big enough concern to try to work around. It's when you have to do something repeatedly with tight timing, or supply a callback, or things like that where having a native API would be nice. – Chris Stratton Jul 23 '12 at 12:55
  • @ChrisStratton Okay. makes sense! So, to summarize - other than the hacks you just discussed, there is no proper Native API that one could possibly use? – Hari Krishna Ganji Jul 23 '12 at 13:24
  • 1
    @HariKrishnaGanji the NDK is so neglected... one year later and this still isn't possible. – krsteeve Aug 27 '13 at 12:49

1 Answers1

4

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.

Pavulon
  • 161
  • 1
  • 6