From b4dc6c83cdb9e5ad2d86ff5ec996cf87a7bf0a7d Mon Sep 17 00:00:00 2001 From: spaceface Date: Mon, 22 Feb 2021 20:24:18 +0100 Subject: [PATCH] gg: handle Android's own native dpi scaling factor (#8908) --- examples/2048/2048.v | 8 ++++---- vlib/gg/gg.v | 25 +++++++++++++------------ vlib/gg/gg_android.c.v | 25 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 16 deletions(-) create mode 100644 vlib/gg/gg_android.c.v diff --git a/examples/2048/2048.v b/examples/2048/2048.v index 39b6043d95..709b58a2bf 100644 --- a/examples/2048/2048.v +++ b/examples/2048/2048.v @@ -255,7 +255,7 @@ fn (b Board) hmirror() Board { // GCC optimization bug; inlining fails when compiled with -prod [no_inline] fn (t TileLine) to_left() TileLine { - right_border_idx := 5 + right_border_idx := 4 mut res := t mut zeros := 0 mut nonzeros := 0 @@ -729,11 +729,11 @@ fn (mut app App) handle_tap() { avgx, avgy := avg(s.pos.x, e.pos.x), avg(s.pos.y, e.pos.y) // TODO: Replace "touch spots" with actual buttons // bottom left -> change theme - if avgx < 200 && h - avgy < 200 { + if avgx < 50 && h - avgy < 50 { app.next_theme() } // bottom right -> change tile format - if w - avgx < 200 && h - avgy < 200 { + if w - avgx < 50 && h - avgy < 50 { app.next_tile_format() } if app.state == .victory { @@ -767,7 +767,7 @@ fn (mut app App) handle_swipe() { dmax := if max(adx, ady) > 0 { max(adx, ady) } else { 1 } tdiff := int(e.time.unix_time_milli() - s.time.unix_time_milli()) // TODO: make this calculation more accurate (don't use arbitrary numbers) - min_swipe_distance := int(math.sqrt(min(w, h) * tdiff / 60)) + 20 + min_swipe_distance := int(math.sqrt(min(w, h) * tdiff / 100)) + 20 if dmax < min_swipe_distance { return } diff --git a/vlib/gg/gg.v b/vlib/gg/gg.v index 1652d7bb0c..75563f358e 100644 --- a/vlib/gg/gg.v +++ b/vlib/gg/gg.v @@ -148,12 +148,7 @@ fn gg_init_sokol_window(user_data voidptr) { gfx.setup(&desc) sgl_desc := C.sgl_desc_t{} sgl.setup(&sgl_desc) - g.scale = sapp.dpi_scale() - // NB: on older X11, `Xft.dpi` from ~/.Xresources, that sokol uses, - // may not be set which leads to sapp.dpi_scale reporting incorrectly 0.0 - if g.scale < 0.1 { - g.scale = 1.0 - } + g.scale = dpi_scale() // is_high_dpi := sapp.high_dpi() // fb_w := sapp.width() // fb_h := sapp.height() @@ -166,7 +161,7 @@ fn gg_init_sokol_window(user_data voidptr) { g.ft = new_ft( font_path: g.config.font_path custom_bold_font_path: g.config.custom_bold_font_path - scale: sapp.dpi_scale() + scale: dpi_scale() ) or { panic(err) } // println('FT took ${time.ticks()-t} ms') g.font_inited = true @@ -761,15 +756,21 @@ pub fn screen_size() Size { // window_size returns the `Size` of the active window pub fn window_size() Size { - mut s := sapp.dpi_scale() - if s == 0 { - s = 1. - } + s := dpi_scale() return Size{int(sapp.width() / s), int(sapp.height() / s)} } pub fn dpi_scale() f32 { - return sapp.dpi_scale() + mut s := sapp.dpi_scale() + $if android { + s *= android_dpi_scale() + } + // NB: on older X11, `Xft.dpi` from ~/.Xresources, that sokol uses, + // may not be set which leads to sapp.dpi_scale reporting incorrectly 0.0 + if s < 0.1 { + s = 1. + } + return s } pub fn high_dpi() bool { diff --git a/vlib/gg/gg_android.c.v b/vlib/gg/gg_android.c.v new file mode 100644 index 0000000000..7c28932f31 --- /dev/null +++ b/vlib/gg/gg_android.c.v @@ -0,0 +1,25 @@ +module gg + +import sokol.sapp + +#include +#include + +fn C.AConfiguration_new() voidptr +fn C.AConfiguration_fromAssetManager(voidptr, voidptr) +fn C.AConfiguration_getDensity(voidptr) u32 +fn C.AConfiguration_delete(voidptr) + +struct C.AAssetManager {} +struct C.ANativeActivity { + assetManager voidptr +} + +pub fn android_dpi_scale() f32 { + config := C.AConfiguration_new() + activity := &C.ANativeActivity(sapp.android_get_native_activity()) + C.AConfiguration_fromAssetManager(config, activity.assetManager) + density := C.AConfiguration_getDensity(config) + C.AConfiguration_delete(config) + return f32(density) / 160 +}