Skip to content

Commit

Permalink
examples: add an animated spirograph.v using gg
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Jul 5, 2024
1 parent a67bfeb commit 811ac12
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
64 changes: 64 additions & 0 deletions examples/gg/spirograph.v
Original file line number Diff line number Diff line change
@@ -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()
}
)
2 changes: 1 addition & 1 deletion thirdparty/sokol/util/sokol_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 811ac12

Please sign in to comment.