v / examples / gg
Raw file | 68 loc (56 sloc) | 1.14 KB | Latest commit hash 8b962f844
1module main
2
3import gg
4import gx
5
6const rate = f32(1) / 60 * 10
7
8struct App {
9mut:
10 gg &gg.Context = unsafe { nil }
11 anim &Anim = unsafe { nil }
12}
13
14struct Anim {
15mut:
16 time f32
17 reverse bool
18}
19
20fn (mut anim Anim) advance() {
21 if anim.reverse {
22 anim.time -= 1 * rate
23 } else {
24 anim.time += 1 * rate
25 }
26 // Use some arbitrary value that fits 60 fps
27 if anim.time > 80 * rate || anim.time < -80 * rate {
28 anim.reverse = !anim.reverse
29 }
30}
31
32fn main() {
33 mut app := &App{
34 gg: 0
35 anim: &Anim{}
36 }
37 app.gg = gg.new_context(
38 bg_color: gx.rgb(174, 198, 255)
39 width: 600
40 height: 400
41 window_title: 'Animated cubic Bézier curve'
42 frame_fn: frame
43 user_data: app
44 )
45 app.gg.run()
46}
47
48fn frame(mut app App) {
49 time := app.anim.time
50
51 p1_x := f32(200.0)
52 p1_y := f32(200.0) + (10 * time)
53
54 p2_x := f32(400.0)
55 p2_y := f32(200.0) + (10 * time)
56
57 ctrl_p1_x := f32(200.0) + (40 * time)
58 ctrl_p1_y := f32(100.0)
59 ctrl_p2_x := f32(400.0) + (-40 * time)
60 ctrl_p2_y := f32(100.0)
61
62 points := [p1_x, p1_y, ctrl_p1_x, ctrl_p1_y, ctrl_p2_x, ctrl_p2_y, p2_x, p2_y]
63
64 app.gg.begin()
65 app.gg.draw_cubic_bezier(points, gx.blue)
66 app.gg.end()
67 app.anim.advance()
68}