From 2237968ec1992730635e595653e600ed0b251fe6 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 4 Jun 2020 21:09:46 +0300 Subject: [PATCH] gg: fix x11 scaling --- examples/tetris/tetris.v | 3 +-- vlib/gg/gg.v | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/examples/tetris/tetris.v b/examples/tetris/tetris.v index 702d55889a..f040835501 100644 --- a/examples/tetris/tetris.v +++ b/examples/tetris/tetris.v @@ -139,7 +139,7 @@ struct Game { const ( fpath = os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf') ) fn init_gui(mut game Game){ - x := ft.new({ font_path: fpath, scale: 2 }) or {panic(err)} + x := ft.new({ font_path: fpath, scale: sapp.dpi_scale() }) or {panic(err)} game.ft = x game.font_loaded = true } @@ -184,7 +184,6 @@ fn main() { init_fn: init_gui frame_fn: frame event_fn: on_event - scale: 2 //wait_events: true ) game.init_game() diff --git a/vlib/gg/gg.v b/vlib/gg/gg.v index c030b70bc9..cf815bba9b 100644 --- a/vlib/gg/gg.v +++ b/vlib/gg/gg.v @@ -29,7 +29,6 @@ pub: window_title string borderless_window bool always_on_top bool - scale f32 = 1.0 bg_color gx.Color init_fn FNvoidptr1 = voidptr(0) frame_fn FNvoidptr1 = voidptr(0) @@ -42,8 +41,8 @@ pub: } pub struct Context { - scale f32 = 1.0// retina = 2 , normal = 1 pub mut: + scale f32 = 1.0 // will get set to 2.0 for retina, will remain 1.0 for normal width int height int clear_pass C.sg_pass_action @@ -67,6 +66,16 @@ 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 + } + is_high_dpi := sapp.high_dpi() + fb_w := sapp.width() + fb_h := sapp.height() + println('g.scale=$g.scale is_high_dpi=$is_high_dpi fb_w=$fb_w fb_h=$fb_h') if g.config.init_fn != voidptr(0) { g.config.init_fn( g.config.user_data ) } @@ -111,7 +120,6 @@ pub fn new_context(cfg Config) &Context{ height: cfg.height clear_pass: gfx.create_clear_pass( f32(cfg.bg_color.r) / 255.0, f32(cfg.bg_color.g) / 255.0, f32(cfg.bg_color.b) / 255.0, 1.0) - scale: cfg.scale //sapp.dpi_scale()// cfg.scale config: cfg } @@ -127,11 +135,9 @@ f32(cfg.bg_color.b) / 255.0, 1.0) html5_canvas_name: cfg.window_title.str width: cfg.width height: cfg.height - high_dpi: cfg.scale > 1 + high_dpi: true fullscreen: cfg.fullscreen } - //b := sapp.high_dpi() - //println('scale=$g.scale high_dpi=$b') if cfg.use_ortho {} else {} g.window = window