v / examples / sokol / drawing.v
72 lines · 65 sloc · 1.42 KB · 5d6de17f708abcf79877f70e677d60a4e23bb727
Raw
1// vtest build: !solaris
2import sokol.sapp
3import sokol.gfx
4import sokol.sgl
5
6struct AppState {
7 pass_action gfx.PassAction
8}
9
10fn main() {
11 state := &AppState{
12 pass_action: gfx.create_clear_pass_action(0.1, 0.1, 0.1, 1.0)
13 }
14 title := 'Sokol Drawing Template'
15 desc := sapp.Desc{
16 width: 640
17 height: 480
18 user_data: state
19 init_userdata_cb: init
20 frame_userdata_cb: frame
21 window_title: &char(title.str)
22 }
23 sapp.run(&desc)
24}
25
26fn init(_user_data voidptr) {
27 desc := sapp.create_desc() // gfx.Desc{
28 gfx.setup(&desc)
29 sgl_desc := sgl.Desc{}
30 sgl.setup(&sgl_desc)
31}
32
33fn frame(state &AppState) {
34 // println('frame')
35 draw()
36 pass := sapp.create_default_pass(state.pass_action)
37 gfx.begin_pass(&pass)
38 sgl.draw()
39 gfx.end_pass()
40 gfx.commit()
41}
42
43fn draw() {
44 // first, reset and setup ortho projection
45 sgl.defaults()
46 sgl.matrix_mode_projection()
47 sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
48 sgl.c4b(255, 0, 0, 128)
49 draw_hollow_rect(220, 140, 200, 200)
50 sgl.c4b(25, 150, 255, 128)
51 draw_filled_rect(270, 190, 100, 100)
52 // line(0, 0, 500, 500)
53}
54
55fn draw_hollow_rect(x f32, y f32, w f32, h f32) {
56 sgl.begin_line_strip()
57 sgl.v2f(x, y)
58 sgl.v2f(x + w, y)
59 sgl.v2f(x + w, y + h)
60 sgl.v2f(x, y + h)
61 sgl.v2f(x, y)
62 sgl.end()
63}
64
65fn draw_filled_rect(x f32, y f32, w f32, h f32) {
66 sgl.begin_quads()
67 sgl.v2f(x, y)
68 sgl.v2f(x + w, y)
69 sgl.v2f(x + w, y + h)
70 sgl.v2f(x, y + h)
71 sgl.end()
72}
73