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 log = sokol.log;
16 import imgui;
17 
18 extern (C):
19 @safe:
20 
21 struct State
22 {
23     sg.PassAction pass_action = {
24         colors: [
25             {
26                 load_action: sg.LoadAction.Clear,
27                 clear_value: {r: 0.0, g: 0.5, b: 1.0, a: 1.0},
28             }
29         ]
30     };
31 }
32 
33 static State state;
34 
35 void init() nothrow
36 {
37     sg.Desc gfx = {
38         environment: sgapp.environment,
39         logger: {func: &log.slog_func}
40     };
41     sg.setup(gfx);
42     simgui.Desc imgui_desc = {0};
43     simgui.setup(imgui_desc);
44 }
45 
46 void frame() @trusted
47 {
48     simgui.FrameDesc imgui_desc = {
49         width: sapp.width(),
50         height: sapp.height(),
51         delta_time: sapp.frameDuration(),
52         dpi_scale: sapp.dpiScale(),
53     };
54     simgui.newFrame(imgui_desc);
55 
56     /*=== UI CODE STARTS HERE ===*/
57     const ImVec2 window_pos = {10, 10};
58     const ImVec2 window_size = {400, 100};
59     SetNextWindowPos(window_pos, ImGuiCond_.ImGuiCond_Once);
60     SetNextWindowSize(window_size, ImGuiCond_.ImGuiCond_Once);
61     Begin("Hello Dear ImGui!".ptr, null, ImGuiWindowFlags_.ImGuiWindowFlags_None);
62     const(char)* label = "Background";
63     float[3] rgb = [
64         state.pass_action.colors[0].clear_value.r,
65         state.pass_action.colors[0].clear_value.g,
66         state.pass_action.colors[0].clear_value.b
67     ];
68     ColorEdit3(label, rgb, ImGuiColorEditFlags_.ImGuiColorEditFlags_None);
69     End();
70     /*=== UI CODE ENDS HERE ===*/
71 
72     sg.Pass pass = {action: state.pass_action, swapchain: sgapp.swapchain};
73     sg.beginPass(pass);
74     simgui.render;
75     sg.endPass;
76     sg.commit;
77 }
78 
79 void event(const(sapp.Event)* ev) @trusted nothrow
80 {
81     simgui.simgui_handle_event(ev);
82 }
83 
84 void cleanup() @safe nothrow
85 {
86     simgui.shutdown;
87     sg.shutdown;
88 }
89 
90 void main() @safe nothrow
91 {
92     sapp.Desc runner = {
93         window_title: "imgui.d",
94         init_cb: &init,
95         frame_cb: &frame,
96         cleanup_cb: &cleanup,
97         event_cb: &event,
98         width: 800,
99         height: 600,
100         icon: {sokol_default: true},
101         logger: {func: &log.func}
102     };
103     sapp.run(runner);
104 }