1 module examples.user_data;
2 
3 private:
4 
5 import sg = sokol.gfx;
6 import sapp = sokol.app;
7 import log = sokol.log;
8 import sglue = sokol.glue;
9 
10 extern (C):
11 
12 struct ExampleUserData
13 {
14     ubyte data;
15     int[ubyte] map; // need druntime
16 }
17 
18 void init() @safe
19 {
20     sg.Desc gfx = {
21         environment: sglue.environment,
22         logger: {func: &log.slog_func}
23     };
24     sg.setup(gfx);
25 }
26 
27 void frame_userdata(scope void* userdata) @trusted
28 {
29     auto state = cast(ExampleUserData*) userdata;
30 
31     state.data++;
32     version (WebAssembly)
33     {
34         // TODO support
35     }
36     else
37     {
38         if (state.data % 13 == 0)
39         {
40             state.map[state.data] = state.data * 13 / 3;
41         }
42         if (state.data % 12 == 0 && state.data % 15 == 0)
43         {
44             state.map.clear();
45         }
46     }
47     debug
48     {
49         import std.stdio : writeln;
50 
51         try
52         {
53             writeln(*state);
54         }
55         catch (Exception)
56         {
57         }
58     }
59 
60     sg.Pass pass = {swapchain: sglue.swapchain};
61     sg.beginPass(pass);
62     sg.endPass();
63     sg.commit();
64 }
65 
66 void cleanup() @safe
67 {
68     sg.shutdown();
69 }
70 
71 void main()
72 {
73     auto userData = ExampleUserData(0, null);
74     sapp.Desc runner = {
75         window_title: "user-data.d",
76         init_cb: &init,
77         frame_userdata_cb: &frame_userdata,
78         cleanup_cb: &cleanup,
79         user_data: &userData,
80         width: 640,
81         height: 480,
82         sample_count: 4,
83         win32_console_attach: true,
84         icon: {sokol_default: true},
85         logger: {func: &log.func}
86     };
87     sapp.run(runner);
88 }