About Monkey 2 › Forums › Monkey 2 Code Library › DPI for Android
This topic contains 2 replies, has 3 voices, and was last updated by
Amon
1 year, 3 months ago.
-
AuthorPosts
-
January 10, 2018 at 10:37 pm #12800
SDL2, or the current version used in Monkey2, does not support DPI for Android. So if you’ve been frustrated trying to come up with a solution but weren’t satisfied, this is the way to go!
Why would I want this?
Because you can now have elements displayed at the same size across desktops and Android (unsure about ios) based on inches instead of pixels – which is important in itself, and even more important with UI..
.
Java FileJava1234567891011121314151617181920package com.monkey2.lib;import android.util.DisplayMetrics;import android.view.WindowManager;public class DisplayUtility {private static final String TAG = "DisplayUtility";static public int getDPI( boolean vertical ) {DisplayMetrics metrics = new DisplayMetrics();Monkey2Activity.mSingleton.getWindowManager().getDefaultDisplay().getMetrics(metrics);if( vertical ){return (int)metrics.ydpi;} else {return (int)metrics.xdpi;}}}Store the Java file somewhere logical in your app. Say in a folder called “native”. The floats are cast to ints for the time being. There aren’t externs for float methods in Monkey2 at this time.
.
.
Monkey 2 ImplementationMonkey1234567891011121314151617181920212223242526272829#If __TARGET__="android"#Import "src/native/DisplayUtility.java"#End#Import "<sdl2>"Using std..Using sdl2..#If __TARGET__="android"#Rem monkeydocs Returns the vertical(true) or horizontal(false) DPI for Android.#EndFunction GetDPI:Int( vertical:Bool )Local env:=sdl2.Android_JNI_GetEnv()Local cls:=env.FindClass( "com/monkey2/lib/DisplayUtility" )Local mth:=env.GetStaticMethodID( cls, "getDPI", "(Z)I" )Return env.CallStaticIntMethod( cls, mth, New Variant[]( vertical ) )End#ElseFunction GetDPI:Int( vertical:Bool )Return 0End#Endif.
.
How To Use itMonkey12345678910111213Function Main()Local hdpi:FloatLocal vdpi:FloatLocal result:= SDL_GetDisplayDPI(0, Null, Varptr(hdpi), Varptr(vdpi))' SDL fails if not 0, so use our backup planIf result<>0 Thenvdpi = GetDPI( True )hdpi = GetDPI( False )EndEndJanuary 11, 2018 at 7:35 am #12803Cool! Didnt know you could use Java in the externs for Monkey2!
January 11, 2018 at 7:39 am #12804Yep, deffo cool.
-
AuthorPosts
You must be logged in to reply to this topic.