Skip to content

Commit d0bea8f

Browse files
authored
ImGUI wrapper (#49)
* move module cimgui to imgui * zig-build: sokol-flags changed * zig-build: ldmd2 boundcheck impl for wasm32
1 parent d233212 commit d0bea8f

File tree

8 files changed

+3903
-133
lines changed

8 files changed

+3903
-133
lines changed

build.zig

+98-60
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,24 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
9191
}
9292

9393
// resolve .auto backend into specific backend by platform
94+
var cflags = try std.BoundedArray([]const u8, 64).init(0);
95+
try cflags.append("-DIMPL");
96+
if (options.optimize != .Debug) {
97+
try cflags.append("-DNDEBUG");
98+
}
9499
const backend = resolveSokolBackend(options.backend, lib.rootModuleTarget());
95-
const backend_cflags = switch (backend) {
96-
.d3d11 => "-DSOKOL_D3D11",
97-
.metal => "-DSOKOL_METAL",
98-
.gl => "-DSOKOL_GLCORE",
99-
.gles3 => "-DSOKOL_GLES3",
100-
.wgpu => "-DSOKOL_WGPU",
100+
switch (backend) {
101+
.d3d11 => try cflags.append("-DSOKOL_D3D11"),
102+
.metal => try cflags.append("-DSOKOL_METAL"),
103+
.gl => try cflags.append("-DSOKOL_GLCORE"),
104+
.gles3 => try cflags.append("-DSOKOL_GLES3"),
105+
.wgpu => try cflags.append("-DSOKOL_WGPU"),
101106
else => @panic("unknown sokol backend"),
102-
};
107+
}
103108

104109
// platform specific compile and link options
105-
var cflags: []const []const u8 = &.{ "-DIMPL", backend_cflags };
106110
if (lib.rootModuleTarget().isDarwin()) {
107-
cflags = &.{ "-ObjC", "-DIMPL", backend_cflags };
111+
try cflags.append("-ObjC");
108112
lib.linkFramework("Foundation");
109113
lib.linkFramework("AudioToolbox");
110114
if (.metal == backend) {
@@ -134,11 +138,10 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
134138
lib.linkSystemLibrary("android");
135139
lib.linkSystemLibrary("log");
136140
} else if (lib.rootModuleTarget().os.tag == .linux) {
137-
const egl_cflags = if (options.use_egl) "-DSOKOL_FORCE_EGL " else "";
138-
const x11_cflags = if (!options.use_x11) "-DSOKOL_DISABLE_X11 " else "";
139-
const wayland_cflags = if (!options.use_wayland) "-DSOKOL_DISABLE_WAYLAND" else "";
141+
if (options.use_egl) try cflags.append("-DSOKOL_FORCE_EGL");
142+
if (!options.use_x11) try cflags.append("-DSOKOL_DISABLE_X11");
143+
if (!options.use_wayland) try cflags.append("-DSOKOL_DISABLE_WAYLAND");
140144
const link_egl = options.use_egl or options.use_wayland;
141-
cflags = &.{ "-DIMPL", backend_cflags, egl_cflags, x11_cflags, wayland_cflags };
142145
lib.linkSystemLibrary("asound");
143146
lib.linkSystemLibrary("GL");
144147
if (options.use_x11) {
@@ -153,7 +156,7 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
153156
lib.linkSystemLibrary("xkbcommon");
154157
}
155158
if (link_egl) {
156-
lib.linkSystemLibrary("egl");
159+
lib.linkSystemLibrary("EGL");
157160
}
158161
} else if (lib.rootModuleTarget().os.tag == .windows) {
159162
lib.linkSystemLibrary("kernel32");
@@ -166,44 +169,45 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
166169
}
167170
}
168171

169-
// finally add the C source files
170172
const csrc_root = "src/sokol/c/";
171173
const csources = [_][]const u8{
172-
csrc_root ++ "sokol_log.c",
173-
csrc_root ++ "sokol_app.c",
174-
csrc_root ++ "sokol_gfx.c",
175-
csrc_root ++ "sokol_time.c",
176-
csrc_root ++ "sokol_audio.c",
177-
csrc_root ++ "sokol_gl.c",
178-
csrc_root ++ "sokol_debugtext.c",
179-
csrc_root ++ "sokol_shape.c",
180-
csrc_root ++ "sokol_fetch.c",
181-
csrc_root ++ "sokol_glue.c",
182-
csrc_root ++ "sokol_memtrack.c",
174+
"sokol_log.c",
175+
"sokol_app.c",
176+
"sokol_gfx.c",
177+
"sokol_time.c",
178+
"sokol_audio.c",
179+
"sokol_gl.c",
180+
"sokol_debugtext.c",
181+
"sokol_shape.c",
182+
"sokol_glue.c",
183+
"sokol_fetch.c",
184+
"sokol_memtrack.c",
183185
};
184-
for (csources) |csrc| {
186+
187+
// finally add the C source files
188+
inline for (csources) |csrc| {
185189
lib.addCSourceFile(.{
186-
.file = b.path(csrc),
187-
.flags = cflags,
190+
.file = b.path(csrc_root ++ csrc),
191+
.flags = cflags.slice(),
188192
});
189193
}
190194

191195
if (options.with_sokol_imgui) {
192196
lib.addCSourceFile(.{
193197
.file = b.path(csrc_root ++ "sokol_imgui.c"),
194-
.flags = cflags,
198+
.flags = cflags.slice(),
195199
});
196-
const cimgui = try buildImgui(b, .{
200+
const imgui = try buildImgui(b, .{
197201
.target = options.target,
198202
.optimize = options.optimize,
199203
.emsdk = options.emsdk,
200204
.use_tsan = lib.root_module.sanitize_thread orelse false,
201205
.use_ubsan = lib.root_module.sanitize_c orelse false,
202206
});
203-
for (cimgui.root_module.include_dirs.items) |dir| {
207+
for (imgui.root_module.include_dirs.items) |dir| {
204208
try lib.root_module.include_dirs.append(b.allocator, dir);
205209
}
206-
lib.linkLibrary(cimgui);
210+
lib.linkLibrary(imgui);
207211
}
208212
return lib;
209213
}
@@ -320,7 +324,10 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*std.Build.Step.InstallDi
320324
// set kind of build
321325
switch (options.kind) {
322326
.@"test" => {
323-
ldc_exec.addArgs(&.{ "-unittest", "-main" });
327+
ldc_exec.addArgs(&.{
328+
"-unittest",
329+
"-main",
330+
});
324331
},
325332
.obj => ldc_exec.addArg("-c"),
326333
else => {},
@@ -367,27 +374,45 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*std.Build.Step.InstallDi
367374
if (options.betterC)
368375
ldc_exec.addArg("-betterC");
369376

377+
// verbose error messages
378+
ldc_exec.addArg("-verrors=context");
379+
370380
switch (options.optimize) {
371381
.Debug => {
372-
ldc_exec.addArg("-debug");
373-
ldc_exec.addArg("-d-debug");
374-
ldc_exec.addArg("-gc"); // debuginfo for non D dbg
375-
ldc_exec.addArg("-g"); // debuginfo for D dbg
376-
ldc_exec.addArg("-gf");
377-
ldc_exec.addArg("-gs");
378-
ldc_exec.addArg("-vgc");
379-
ldc_exec.addArg("-vtls");
380-
ldc_exec.addArg("-verrors=context");
381-
ldc_exec.addArg("-boundscheck=on");
382+
ldc_exec.addArgs(&.{
383+
"-debug",
384+
"-d-debug",
385+
"--gc",
386+
"-g",
387+
"-gf",
388+
"-gs",
389+
"-vgc",
390+
"-vtls",
391+
"-boundscheck=on",
392+
"--link-debuglib",
393+
});
382394
},
383395
.ReleaseSafe => {
384-
ldc_exec.addArgs(&.{ "-O2", "-boundscheck=safeonly" });
396+
ldc_exec.addArgs(&.{
397+
"-O",
398+
"-boundscheck=safeonly",
399+
});
385400
},
386401
.ReleaseFast => {
387-
ldc_exec.addArgs(&.{ "-O3", "-boundscheck=off", "--enable-asserts=false" });
402+
ldc_exec.addArgs(&.{
403+
"-O",
404+
"-boundscheck=off",
405+
"--enable-asserts=false",
406+
"--strip-debug",
407+
});
388408
},
389409
.ReleaseSmall => {
390-
ldc_exec.addArgs(&.{ "-Oz", "-boundscheck=off", "--enable-asserts=false" });
410+
ldc_exec.addArgs(&.{
411+
"-Oz",
412+
"-boundscheck=off",
413+
"--enable-asserts=false",
414+
"--strip-debug",
415+
});
391416
},
392417
}
393418

@@ -422,7 +447,7 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*std.Build.Step.InstallDi
422447
"-i=sokol",
423448
"-i=shaders",
424449
"-i=handmade",
425-
"-i=cimgui",
450+
"-i=imgui",
426451
});
427452

428453
// sokol include path
@@ -507,6 +532,13 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*std.Build.Step.InstallDi
507532
\\
508533
\\ pragma(printf)
509534
\\ int fprintf(FILE* __restrict, scope const(char)* __restrict, scope...) @nogc nothrow;
535+
\\
536+
\\ // boundchecking
537+
\\ void _d_arraybounds_index(string file, uint line, size_t index, size_t length) @nogc nothrow
538+
\\ {
539+
\\ if (index >= length)
540+
\\ __assert("Array index out of bounds".ptr, file.ptr, line);
541+
\\ }
510542
\\ }
511543
,
512544
),
@@ -1148,9 +1180,22 @@ fn buildImgui(b: *Build, options: libImGuiOptions) !*CompileStep {
11481180
libimgui.root_module.sanitize_c = options.use_ubsan;
11491181
libimgui.root_module.sanitize_thread = options.use_tsan;
11501182

1183+
var cflags = try std.BoundedArray([]const u8, 64).init(0);
1184+
if (options.optimize != .Debug) {
1185+
try cflags.append("-DNDEBUG");
1186+
}
1187+
try cflags.appendSlice(&.{
1188+
"-Wall",
1189+
"-Wextra",
1190+
"-fno-exceptions",
1191+
"-Wno-unused-parameter",
1192+
"-Wno-missing-field-initializers",
1193+
"-fno-threadsafe-statics",
1194+
});
1195+
11511196
if (b.lazyDependency("imgui", .{})) |dep| {
1152-
const cimgui = dep.path(imguiver_path);
1153-
libimgui.addIncludePath(cimgui);
1197+
const imgui = dep.path(imguiver_path);
1198+
libimgui.addIncludePath(imgui);
11541199

11551200
if (options.emsdk) |emsdk| {
11561201
if (libimgui.rootModuleTarget().isWasm()) {
@@ -1168,28 +1213,21 @@ fn buildImgui(b: *Build, options: libImGuiOptions) !*CompileStep {
11681213
}
11691214
}
11701215
libimgui.addCSourceFiles(.{
1171-
.root = cimgui,
1216+
.root = imgui,
11721217
.files = &.{
11731218
"cimgui.cpp",
11741219
},
11751220
});
11761221
libimgui.addCSourceFiles(.{
1177-
.root = cimgui,
1222+
.root = imgui,
11781223
.files = &.{
11791224
"imgui.cpp",
11801225
"imgui_draw.cpp",
11811226
"imgui_demo.cpp",
11821227
"imgui_widgets.cpp",
11831228
"imgui_tables.cpp",
11841229
},
1185-
.flags = &.{
1186-
"-Wall",
1187-
"-Wextra",
1188-
"-fno-exceptions",
1189-
"-Wno-unused-parameter",
1190-
"-Wno-missing-field-initializers",
1191-
"-fno-threadsafe-statics",
1192-
},
1230+
.flags = cflags.slice(),
11931231
});
11941232
libimgui.root_module.sanitize_c = false;
11951233
if (libimgui.rootModuleTarget().os.tag == .windows)

build.zig.zon

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
},
1313
.dependencies = .{
1414
.imgui = .{
15-
.url = "git+https://github.com/floooh/dcimgui#4fcf465cf40a297affa887d2fb4a9342e0461b01",
16-
.hash = "12200aded8c04f0f18420f41bd97392d67da33b74833e8474932ad58e2fedd2f905c",
15+
.url = "git+https://github.com/floooh/dcimgui#v1.91.7",
16+
.hash = "1220c640ad23d8800437166865e54f172c612fcfe6e000149a2b2af631c37b605f50",
1717
.lazy = true,
1818
},
1919
.emsdk = .{

0 commit comments

Comments
 (0)