From 7e06203da84b2af980d7787116a9a03bd944774e Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 12 Jun 2022 14:20:26 +0300 Subject: [PATCH] gg: support `-d show_fps` for all gg apps --- vlib/gg/gg.c.v | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/vlib/gg/gg.c.v b/vlib/gg/gg.c.v index bd619ba5a1..f368061f68 100644 --- a/vlib/gg/gg.c.v +++ b/vlib/gg/gg.c.v @@ -142,6 +142,7 @@ pub mut: pressed_keys [key_code_max]bool // an array representing all currently pressed keys pressed_keys_edge [key_code_max]bool // true when the previous state of pressed_keys, // *before* the current event was different + fps FPSConfig } fn gg_init_sokol_window(user_data voidptr) { @@ -490,6 +491,13 @@ pub fn (ctx &Context) begin() { // end finishes drawing for the context. pub fn (ctx &Context) end() { + $if show_fps ? { + ctx.show_fps() + } $else { + if ctx.fps.show { + ctx.show_fps() + } + } gfx.begin_default_pass(ctx.clear_pass, sapp.width(), sapp.height()) sgl.draw() gfx.end_pass() @@ -502,6 +510,33 @@ pub fn (ctx &Context) end() { */ } +pub struct FPSConfig { + x int + y int + text_config gx.TextCfg = gx.TextCfg{ + color: gx.yellow + } + background_color gx.Color = gx.Color{ + r: 0 + g: 0 + b: 0 + a: 128 + } + show bool // do not show by default, use `-d show_fps` or set it manually in your app to override with: `app.gg.fps.show = true` +} + +pub fn (ctx &Context) show_fps() { + if !ctx.font_inited { + return + } + frame_duration := sapp.frame_duration() + fps_text := '${1 / frame_duration:2.0f}' + ctx.set_cfg(ctx.fps.text_config) + fps_width, fps_height := ctx.text_size('00') // maximum size; prevents blinking on variable width fonts + ctx.draw_rect_filled(ctx.fps.x, ctx.fps.y, fps_width + 5, fps_height + 7, ctx.fps.background_color) + ctx.draw_text(ctx.fps.x + 2, ctx.fps.y + 1, fps_text, ctx.fps.text_config) +} + fn (mut ctx Context) set_scale() { mut s := sapp.dpi_scale() $if android {