Skip to content

Commit

Permalink
wip - cimgui wrapper
Browse files Browse the repository at this point in the history
* move module cimgui to imgui
  • Loading branch information
kassane committed Jan 17, 2025
1 parent 1f76490 commit ec69334
Show file tree
Hide file tree
Showing 8 changed files with 3,812 additions and 80 deletions.
16 changes: 8 additions & 8 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,17 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
.file = b.path(csrc_root ++ "sokol_imgui.c"),
.flags = cflags,
});
const cimgui = try buildImgui(b, .{
const imgui = try buildImgui(b, .{
.target = options.target,
.optimize = options.optimize,
.emsdk = options.emsdk,
.use_tsan = lib.root_module.sanitize_thread orelse false,
.use_ubsan = lib.root_module.sanitize_c orelse false,
});
for (cimgui.root_module.include_dirs.items) |dir| {
for (imgui.root_module.include_dirs.items) |dir| {
try lib.root_module.include_dirs.append(b.allocator, dir);
}
lib.linkLibrary(cimgui);
lib.linkLibrary(imgui);
}
return lib;
}
Expand Down Expand Up @@ -422,7 +422,7 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*std.Build.Step.InstallDi
"-i=sokol",
"-i=shaders",
"-i=handmade",
"-i=cimgui",
"-i=imgui",
});

// sokol include path
Expand Down Expand Up @@ -1149,8 +1149,8 @@ fn buildImgui(b: *Build, options: libImGuiOptions) !*CompileStep {
libimgui.root_module.sanitize_thread = options.use_tsan;

if (b.lazyDependency("imgui", .{})) |dep| {
const cimgui = dep.path(imguiver_path);
libimgui.addIncludePath(cimgui);
const imgui = dep.path(imguiver_path);
libimgui.addIncludePath(imgui);

if (options.emsdk) |emsdk| {
if (libimgui.rootModuleTarget().isWasm()) {
Expand All @@ -1168,13 +1168,13 @@ fn buildImgui(b: *Build, options: libImGuiOptions) !*CompileStep {
}
}
libimgui.addCSourceFiles(.{
.root = cimgui,
.root = imgui,
.files = &.{
"cimgui.cpp",
},
});
libimgui.addCSourceFiles(.{
.root = cimgui,
.root = imgui,
.files = &.{
"imgui.cpp",
"imgui_draw.cpp",
Expand Down
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
},
.dependencies = .{
.imgui = .{
.url = "git+https://github.com/floooh/dcimgui#4fcf465cf40a297affa887d2fb4a9342e0461b01",
.hash = "12200aded8c04f0f18420f41bd97392d67da33b74833e8474932ad58e2fedd2f905c",
.url = "git+https://github.com/floooh/dcimgui#v1.91.7",
.hash = "1220c640ad23d8800437166865e54f172c612fcfe6e000149a2b2af631c37b605f50",
.lazy = true,
},
.emsdk = .{
Expand Down
48 changes: 24 additions & 24 deletions dub.sdl

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions src/cimgui/cimgui.d

This file was deleted.

54 changes: 27 additions & 27 deletions src/examples/droptest.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import sg = sokol.gfx;
import sgapp = sokol.glue;
import sapp = sokol.app;
import sfetch = sokol.fetch;
import imgui = sokol.imgui;
import simgui = sokol.imgui;
import log = sokol.log;
import cimgui.cimgui;
import imgui;

enum MAX_FILE_SIZE = 1024 * 1024;

Expand Down Expand Up @@ -41,8 +41,8 @@ extern (C) void init() @safe @nogc nothrow
logger: {func: &log.slog_func}
};
sg.setup(gfx);
imgui.Desc imgui_desc = {0};
imgui.setup(imgui_desc);
simgui.Desc imgui_desc = {0};
simgui.setup(imgui_desc);

// ifndef emscripten
// dfmt off
Expand All @@ -66,53 +66,53 @@ extern (C) void frame() @trusted
{
sfetch.dowork;
}
imgui.FrameDesc imgui_desc = {
simgui.FrameDesc imgui_desc = {
width: sapp.width(),
height: sapp.height(),
delta_time: sapp.frameDuration(),
dpi_scale: sapp.dpiScale(),
};
// dfmt on
imgui.newFrame(imgui_desc);
simgui.newFrame(imgui_desc);

// /*=== UI CODE STARTS HERE ===*/
const ImVec2 window_pos = {10, 10};
const ImVec2 window_size = {600, 500};
igSetNextWindowPos(window_pos, ImGuiCond_.ImGuiCond_Once);
igSetNextWindowSize(window_size, ImGuiCond_.ImGuiCond_Once);
igBegin("Drop a file!".ptr, null, ImGuiWindowFlags_.ImGuiWindowFlags_None);
SetNextWindowPos(window_pos, ImGuiCond_.ImGuiCond_Once);
SetNextWindowSize(window_size, ImGuiCond_.ImGuiCond_Once);
Begin("Drop a file!".ptr, null, ImGuiWindowFlags_.ImGuiWindowFlags_None);
if (state.load_state != LoadState.Unknown)
{
igText("%s:", sapp.getDroppedFilePath(0));
Text("%s:", sapp.getDroppedFilePath(0));
}
switch (state.load_state)
{
case LoadState.Failed:
igText("LOAD FAILED!");
Text("LOAD FAILED!");
break;
case LoadState.FileTooBig:
igText("FILE TOO BIG!");
Text("FILE TOO BIG!");
break;
case LoadState.Success:
igSeparator;
Separator;
renderFileContent;
break;
default:
break;
}
igEnd();
End();
/*=== UI CODE ENDS HERE ===*/

sg.Pass pass = {swapchain: sgapp.swapchain};
sg.beginPass(pass);
imgui.render;
simgui.render;
sg.endPass;
sg.commit;
}

extern (C) void event(const(sapp.Event)* ev) @trusted @nogc nothrow
{
imgui.simgui_handle_event(ev);
simgui.simgui_handle_event(ev);
if (ev.type == sapp.EventType.Files_dropped)
{
version (Emscripten)
Expand Down Expand Up @@ -146,7 +146,7 @@ extern (C) void cleanup() @safe @nogc nothrow
sfetch.shutdown;
}
// dfmt on
imgui.shutdown;
simgui.shutdown;
sg.shutdown;
}

Expand Down Expand Up @@ -175,11 +175,11 @@ void renderFileContent()
immutable int bytes_per_line = 16; // keep this 2^N
immutable int num_lines = (state.size + (bytes_per_line - 1)) / bytes_per_line;
ImVec2 sz = {0, 0};
igBeginChild("##scrolling", sz, false, ImGuiWindowFlags_
BeginChild("##scrolling", sz, false, ImGuiWindowFlags_
.ImGuiWindowFlags_NoMove | ImGuiWindowFlags_
.ImGuiWindowFlags_NoNav);
ImGuiListClipper* clipper = null;
ImGuiListClipper_Begin(clipper, num_lines, igGetTextLineHeight());
ImGuiListClipper_Begin(clipper, num_lines, GetTextLineHeight());
ImGuiListClipper_Step(clipper);
for (int line_i = clipper.DisplayStart; line_i < clipper.DisplayEnd; line_i++)
{
Expand All @@ -189,30 +189,30 @@ void renderFileContent()
{
end_offset = state.size;
}
igText("%04X: ", start_offset);
Text("%04X: ", start_offset);
for (int i = start_offset; i < end_offset; i++)
{
igSameLineEx(0.0f, 0.0f);
igText("%02X ", state.buffer[i]);
SameLineEx(0.0f, 0.0f);
Text("%02X ", state.buffer[i]);
}
igSameLineEx((6 * 7.0f) + (bytes_per_line * 3 * 7.0f) + (2 * 7.0f), 0.0f);
SameLineEx((6 * 7.0f) + (bytes_per_line * 3 * 7.0f) + (2 * 7.0f), 0.0f);
for (int i = start_offset; i < end_offset; i++)
{
if (i != start_offset)
{
igSameLineEx(0.0f, 0.0f);
SameLineEx(0.0f, 0.0f);
}
ubyte c = state.buffer[i];
if ((c < 32) || (c > 127))
{
c = '.';
}
igText("%c", c);
Text("%c", c);
}
}
igText("EOF\n");
Text("EOF\n");
ImGuiListClipper_End(clipper);
igEndChild();
EndChild();
}

version (Emscripten)
Expand Down
28 changes: 14 additions & 14 deletions src/examples/imgui.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ private:
import sg = sokol.gfx;
import sgapp = sokol.glue;
import sapp = sokol.app;
import imgui = sokol.imgui;
import simgui = sokol.imgui;
import log = sokol.log;
import cimgui.cimgui;
import imgui;

extern (C):
@safe:
Expand All @@ -39,51 +39,51 @@ void init() nothrow
logger: {func: &log.slog_func}
};
sg.setup(gfx);
imgui.Desc imgui_desc = {0};
imgui.setup(imgui_desc);
simgui.Desc imgui_desc = {0};
simgui.setup(imgui_desc);
}

void frame() @trusted
{
imgui.FrameDesc imgui_desc = {
simgui.FrameDesc imgui_desc = {
width: sapp.width(),
height: sapp.height(),
delta_time: sapp.frameDuration(),
dpi_scale: sapp.dpiScale(),
};
imgui.newFrame(imgui_desc);
simgui.newFrame(imgui_desc);

/*=== UI CODE STARTS HERE ===*/
const ImVec2 window_pos = {10, 10};
const ImVec2 window_size = {400, 100};
igSetNextWindowPos(window_pos, ImGuiCond_.ImGuiCond_Once);
igSetNextWindowSize(window_size, ImGuiCond_.ImGuiCond_Once);
igBegin("Hello Dear ImGui!".ptr, null, ImGuiWindowFlags_.ImGuiWindowFlags_None);
SetNextWindowPos(window_pos, ImGuiCond_.ImGuiCond_Once);
SetNextWindowSize(window_size, ImGuiCond_.ImGuiCond_Once);
Begin("Hello Dear ImGui!".ptr, null, ImGuiWindowFlags_.ImGuiWindowFlags_None);
const(char)* label = "Background";
float[3] rgb = [
state.pass_action.colors[0].clear_value.r,
state.pass_action.colors[0].clear_value.g,
state.pass_action.colors[0].clear_value.b
];
igColorEdit3(label, &rgb[0], ImGuiColorEditFlags_.ImGuiColorEditFlags_None);
igEnd();
ColorEdit3(label, rgb, ImGuiColorEditFlags_.ImGuiColorEditFlags_None);
End();
/*=== UI CODE ENDS HERE ===*/

sg.Pass pass = {action: state.pass_action, swapchain: sgapp.swapchain};
sg.beginPass(pass);
imgui.render;
simgui.render;
sg.endPass;
sg.commit;
}

void event(const(sapp.Event)* ev) @trusted nothrow
{
imgui.simgui_handle_event(ev);
simgui.simgui_handle_event(ev);
}

void cleanup() @safe nothrow
{
imgui.shutdown;
simgui.shutdown;
sg.shutdown;
}

Expand Down
1 change: 0 additions & 1 deletion src/cimgui/dcimgui.c → src/imgui/dcimgui.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Use importC to import the C header files.

#ifndef DCIMGUI_H
#define DCIMGUI_H
#pragma attribute(push, nogc, nothrow) // dmd-2.110.x feature
Expand Down
Loading

0 comments on commit ec69334

Please sign in to comment.