1 //------------------------------------------------------------------------------ 2 // imgui.d 3 // 4 // Using cimgui + sokol, based on https://github.com/floooh/sokol-zig-imgui-sample 5 //------------------------------------------------------------------------------ 6 7 module examples.imgui; 8 9 private: 10 11 import sg = sokol.gfx; 12 import sgapp = sokol.glue; 13 import sapp = sokol.app; 14 import simgui = sokol.imgui; 15 import sgimgui = sokol.gfximgui; 16 import sappimgui = sokol.appimgui; 17 import log = sokol.log; 18 import imgui.cimgui; 19 20 extern (C): 21 @safe: 22 23 struct State 24 { 25 sg.PassAction pass_action = { 26 colors: [ 27 { 28 load_action: sg.LoadAction.Clear, 29 clear_value: {r: 0.0, g: 0.5, b: 1.0, a: 1.0}, 30 } 31 ] 32 }; 33 } 34 35 static State state; 36 37 void init() nothrow 38 { 39 sg.Desc gfx = { 40 environment: sgapp.environment, 41 logger: {func: &log.slog_func} 42 }; 43 sappimgui.setup; 44 sg.setup(gfx); 45 simgui.Desc imgui_desc = {0}; 46 simgui.setup(imgui_desc); 47 } 48 49 void frame() @trusted 50 { 51 simgui.FrameDesc imgui_desc = { 52 width: sapp.width(), 53 height: sapp.height(), 54 delta_time: sapp.frameDuration(), 55 dpi_scale: sapp.dpiScale(), 56 }; 57 simgui.newFrame(imgui_desc); 58 sappimgui.trackFrame; 59 60 /*=== UI CODE STARTS HERE ===*/ 61 const ImVec2 window_pos = {10, 10}; 62 const ImVec2 window_size = {400, 100}; 63 SetNextWindowPos(window_pos, ImGuiCond_.ImGuiCond_Once); 64 SetNextWindowSize(window_size, ImGuiCond_.ImGuiCond_Once); 65 Begin("Hello Dear ImGui!".ptr, null, ImGuiWindowFlags_.ImGuiWindowFlags_None); 66 const(char)* label = "Background"; 67 float[3] rgb = [ 68 state.pass_action.colors[0].clear_value.r, 69 state.pass_action.colors[0].clear_value.g, 70 state.pass_action.colors[0].clear_value.b 71 ]; 72 ColorEdit3(label, rgb.ptr, ImGuiColorEditFlags_.ImGuiColorEditFlags_None); 73 End(); 74 if (BeginMainMenuBar()) { 75 sgimgui.drawMenu("sokol-gfx"); 76 sappimgui.drawMenu("sokol-app"); 77 EndMainMenuBar(); 78 } 79 sgimgui.draw; 80 sappimgui.draw; 81 /*=== UI CODE ENDS HERE ===*/ 82 83 sg.Pass pass = {action: state.pass_action, swapchain: sgapp.swapchain}; 84 sg.beginPass(pass); 85 simgui.render; 86 sg.endPass; 87 sg.commit; 88 } 89 90 void event(const(sapp.Event)* ev) @trusted nothrow 91 { 92 sappimgui.sappimgui_track_event(ev); 93 simgui.simgui_handle_event(ev); 94 } 95 96 void cleanup() @safe nothrow 97 { 98 sappimgui.shutdown; 99 simgui.shutdown; 100 sgimgui.shutdown; 101 sg.shutdown; 102 } 103 104 void main() @safe nothrow 105 { 106 sapp.Desc runner = { 107 window_title: "imgui.d", 108 init_cb: &init, 109 frame_cb: &frame, 110 cleanup_cb: &cleanup, 111 event_cb: &event, 112 width: 800, 113 height: 600, 114 icon: {sokol_default: true}, 115 logger: {func: &log.func} 116 }; 117 sapp.run(runner); 118 } 119 120 version (WebAssembly) 121 { 122 debug 123 { 124 import emscripten.assertd; 125 } 126 }