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