From c4180d4b064fbc14e8622f30ccc533eb4634f546 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 29 Dec 2023 16:46:18 +0200 Subject: [PATCH] examples: shrink examples/gg/minimal.v even more --- examples/gg/minimal.v | 32 +++++++++++++++----------------- vlib/gg/gg.c.v | 10 ++++++++++ 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/examples/gg/minimal.v b/examples/gg/minimal.v index 682ce30cb6..cfd6d1801b 100644 --- a/examples/gg/minimal.v +++ b/examples/gg/minimal.v @@ -1,19 +1,17 @@ import gg -fn main() { - mut ctx := gg.new_context( - window_title: 'Hello' - bg_color: gg.Color{240, 240, 128, 255} - width: 320 - height: 240 - frame_fn: on_frame - ) - ctx.run() -} - -fn on_frame(ctx &gg.Context) { - ctx.begin() - ctx.draw_text(40, 100, 'GG frame: ${ctx.frame:06}', size: 30, color: gg.Color{50, 50, 255, 255}) - ctx.show_fps() - ctx.end() -} +gg.start( + window_title: 'Hello' + bg_color: gg.Color{240, 240, 128, 255} + width: 320 + height: 240 + frame_fn: fn (ctx &gg.Context) { + ctx.begin() + ctx.draw_text(40, 100, 'GG frame: ${ctx.frame:06}', + size: 30 + color: gg.Color{50, 50, 255, 255} + ) + ctx.show_fps() + ctx.end() + } +) diff --git a/vlib/gg/gg.c.v b/vlib/gg/gg.c.v index fb288ca50d..65763ea789 100644 --- a/vlib/gg/gg.c.v +++ b/vlib/gg/gg.c.v @@ -441,6 +441,16 @@ fn gg_fail_fn(msg &char, user_data voidptr) { //---- public methods +// start creates a new context and runs it right away. +// It is a convenient way to start short/throwaway gg based prototypes, +// that do not need to keep and update their own state, like simple +// animations/visualisations that depend only on the time, or the ctx.frame counter. +// Use gg.new_context() for more complex ones. +pub fn start(cfg Config) { + mut ctx := new_context(cfg) + ctx.run() +} + // new_context returns an initialized `Context` allocated on the heap. pub fn new_context(cfg Config) &Context { mut ctx := &Context{