From 811ac125fa62948fd7363e5e4826398ef64a7c23 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 5 Jul 2024 11:29:47 +0300 Subject: [PATCH] examples: add an animated spirograph.v using gg --- examples/gg/spirograph.v | 64 ++++++++++++++++++++++++++++++++ thirdparty/sokol/util/sokol_gl.h | 2 +- 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 examples/gg/spirograph.v diff --git a/examples/gg/spirograph.v b/examples/gg/spirograph.v new file mode 100644 index 0000000000..3eee74a019 --- /dev/null +++ b/examples/gg/spirograph.v @@ -0,0 +1,64 @@ +import gg +import math + +const pixel_count = 15000 +const p = 3 +const s = math.pi * 2 / f32(pixel_count) +const color = gg.Color{255, 255, 255, 255} +const background = gg.Color{20, 20, 64, 255} +const colors = [ + gg.Color{255, 255, 255, 255}, + gg.Color{255, 0, 255, 255}, + gg.Color{255, 255, 0, 255}, + gg.Color{0, 255, 255, 255}, + gg.Color{0, 0, 255, 255}, + gg.Color{0, 0, 0, 255}, +]! + +mut k := 497 +mut scale := 200 +gg.start( + window_title: 'Spirograph' + bg_color: background + width: 900 + height: 950 + sample_count: 2 + frame_fn: fn [mut k, mut scale] (ctx &gg.Context) { + wsize := gg.window_size() + ctx.begin() + d := math.sin(f64(ctx.frame) / 300) + if math.abs(d) < 0.0015 { + k++ + } + if ctx.pressed_keys[int(gg.KeyCode.left)] { + k-- + } + if ctx.pressed_keys[int(gg.KeyCode.right)] { + k++ + } + if ctx.pressed_keys[int(gg.KeyCode.up)] { + scale++ + } + if ctx.pressed_keys[int(gg.KeyCode.down)] { + scale-- + } + d600 := math.sin(f64(ctx.frame) / 60) + cd600 := math.cos(d600) + mut pdj, mut pkj := [p]f64{}, [p]f64{} + for j := 0; j < p; j++ { + pdj[j], pkj[j] = math.pow(d, j), math.pow(k, j) + } + for i := 0; i < pixel_count; i++ { + mut x, mut y := 0.0, 0.0 + for j := 0; j < p; j++ { + a := pkj[j] * i * s + x += pdj[j] * math.sin(a) * cd600 + y += pdj[j] * math.cos(a) * cd600 + } + ctx.draw_rect_filled(f32(wsize.width / 2 + scale * x), f32(wsize.height / 2 + scale * y), + 1, 1, colors[i % colors.len]) + } + ctx.draw_text(wsize.width / 2 - 170, wsize.height - 15, 'frame: ${ctx.frame:06}, k: ${k:4}, scale: ${scale:4}, arrows to change') + ctx.end() + } +) diff --git a/thirdparty/sokol/util/sokol_gl.h b/thirdparty/sokol/util/sokol_gl.h index f72f0b103f..ed75f6649a 100644 --- a/thirdparty/sokol/util/sokol_gl.h +++ b/thirdparty/sokol/util/sokol_gl.h @@ -2309,7 +2309,7 @@ typedef struct { #define _SGL_DEFAULT_CONTEXT_POOL_SIZE (4) #define _SGL_DEFAULT_PIPELINE_POOL_SIZE (64) // __v_ start -#define _SGL_DEFAULT_MAX_VERTICES (1<<17) +#define _SGL_DEFAULT_MAX_VERTICES (1<<22) #define _SGL_DEFAULT_MAX_COMMANDS (1<<15) // __v_ end #define _SGL_SLOT_SHIFT (16)