v2 / vlib / gg / testdata / draw_simple_polygons.vv
82 lines · 71 sloc · 1.23 KB · 8745ab1ffffc18340ce4ea5c1a37c8fcea130937
Raw
1module main
2
3import gg
4
5struct App {
6mut:
7 gg &gg.Context = unsafe { nil }
8 rotation f32 = f32(0)
9 edge int = 3
10}
11
12fn main() {
13 println('rotation: left arrow key, right arrow key')
14 println('center polygon edge: up arrow key, down arrow key')
15
16 mut app := &App{}
17
18 app.gg = gg.new_context(
19 window_title: 'Simple Polygons'
20 width: 500
21 height: 500
22 bg_color: gg.black
23 event_fn: event
24 frame_fn: render
25 user_data: app
26 )
27
28 app.gg.run()
29}
30
31fn render(app &App) {
32 app.gg.begin()
33
34 color := gg.Color{
35 r: 175
36 g: 0
37 b: 0
38 a: 200
39 }
40 mut edge := 3
41 for i := 0; i <= 5; i++ {
42 for j := 0; j <= 5; j++ {
43 if i == 0 || j == 0 || i == 5 || j == 5 {
44 app.gg.draw_polygon_filled(50 + 80 * i, 50 + 80 * j, 30, edge, app.rotation,
45 color)
46 edge++
47 }
48 }
49 }
50
51 app.gg.draw_polygon_filled(250, 250, 150, app.edge, app.rotation, color)
52
53 app.gg.end()
54}
55
56fn event(e &gg.Event, mut app App) {
57 match e.typ {
58 .key_down {
59 match e.key_code {
60 .up {
61 app.edge++
62 }
63 .down {
64 if app.edge > 3 {
65 app.edge--
66 }
67 }
68 .right {
69 app.rotation++
70 }
71 .left {
72 app.rotation--
73 }
74 .escape {
75 app.gg.quit()
76 }
77 else {}
78 }
79 }
80 else {}
81 }
82}
83