v2 / examples / gg / many_thousands_of_circles.v
55 lines · 48 sloc · 1.64 KB · bbb61ab3687afe512a1fa12492c876d011626107
Raw
1module main
2
3import gg
4import rand
5
6const max_circles_per_pass = 1000
7
8// prepare some random colors ahead of time
9const colors = []gg.Color{len: max_circles_per_pass, init: gg.Color{
10 r: u8(index * 0 + rand.u8())
11 g: u8(index * 0 + rand.u8())
12 b: u8(index * 0 + rand.u8())
13}}
14
15fn frame(mut ctx gg.Context) {
16 // First pass, just clears the background:
17 ctx.begin()
18 ctx.end()
19
20 // We want to draw thousands of circles, but sokol has a limit for how
21 // many primitives can be in a single pass, and if you reach that limit
22 // you will not see *anything at all* for that pass.
23 // For the circles below, that limit is ~2520 circles per pass.
24 // To overcome this, we will use several passes instead, where each one
25 // will draw just 1000 circles.
26 // In other words, in total we will have 4 * 1000 = 4000 circles, drawn with
27 // 4 passes.
28 for i := 0; i < 4 * max_circles_per_pass; i += max_circles_per_pass {
29 ctx.begin()
30 for c in 0 .. max_circles_per_pass {
31 rx := rand.int_in_range(0, ctx.window.width) or { 0 }
32 ry := rand.int_in_range(0, ctx.window.height) or { 0 }
33 ctx.draw_circle_filled(rx, ry, 10, colors[c])
34 }
35 ctx.end(how: .passthru)
36 }
37
38 // The last pass, is for the fps overlay, that should be *always on top of everything*.
39 // Drawing it in a separate pass, guarantees, that it *will* be drawn, even if the drawing
40 // of all the other passes fail. Try increasing max_circles_per_pass to 3000 for example.
41 ctx.begin()
42 ctx.show_fps()
43 ctx.end(how: .passthru)
44}
45
46fn main() {
47 mut ctx := gg.new_context(
48 window_title: 'Many Thousands of Circles'
49 bg_color: gg.black
50 width: 600
51 height: 400
52 frame_fn: frame
53 )
54 ctx.run()
55}
56