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