Skip to content

Commit

Permalink
imgui example
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Jan 16, 2024
1 parent a49d52c commit 92c4f73
Show file tree
Hide file tree
Showing 6 changed files with 8,195 additions and 21 deletions.
63 changes: 46 additions & 17 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,19 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
csrc_root ++ "sokol_glue.c",
csrc_root ++ "sokol_debugtext.c",
csrc_root ++ "sokol_shape.c",
csrc_root ++ "sokol_imgui.c",
};
for (csources) |csrc| {
lib.addCSourceFile(.{
.file = .{ .path = csrc },
.flags = cflags,
});
}
const cimgui = buildImgui(b, .{ .target = options.target, .optimize = options.optimize });
for (cimgui.root_module.include_dirs.items) |dir| {
try lib.root_module.include_dirs.append(b.allocator, dir);
}
lib.linkLibrary(cimgui);
if (sharedlib)
b.installArtifact(lib);
return lib;
Expand Down Expand Up @@ -219,6 +225,7 @@ pub fn build(b: *Build) !void {
// "debugtext-userfont",
// "shapes",
"user-data",
"imgui",
};
b.getInstallStep().name = "sokol library";
inline for (examples) |example| {
Expand All @@ -239,8 +246,6 @@ pub fn build(b: *Build) !void {
b.getInstallStep().dependOn(&ldc.step);
}
buildShaders(b);
const ll = buildImgui(b, .{ .target = target, .optimize = optimize });
b.installArtifact(ll);
}

// a separate step to compile shaders, expects the shader compiler in ../sokol-tools-bin/
Expand Down Expand Up @@ -502,6 +507,22 @@ fn ldcBuild(b: *Build, lib_sokol: *CompileStep, options: DCompileStep) !*RunStep
// run the command
var ldc_exec = b.addSystemCommand(cmds.items);
ldc_exec.addArtifactArg(lib_sokol);
var it = lib_sokol.root_module.iterateDependencies(lib_sokol, false);
while (it.next()) |item| {
for (item.module.link_objects.items) |link_object| {
switch (link_object) {
.other_step => |compile_step| {
switch (compile_step.kind) {
.lib => {
ldc_exec.addArtifactArg(compile_step);
},
else => {},
}
},
else => {},
}
}
}
ldc_exec.setName(options.name);

const example_run = b.addSystemCommand(&.{b.pathJoin(&.{ b.install_path, "bin", options.name })});
Expand Down Expand Up @@ -546,41 +567,49 @@ fn buildImgui(b: *Build, options: struct { target: Build.ResolvedTarget, optimiz
const imgui_cpp = b.dependency("imgui", .{});
const imgui_cpp_dir = imgui_cpp.path("").getPath(b);
const cimgui = b.dependency("cimgui", .{});
// const cimgui_dir = cimgui.path("").getPath(b);
const cimgui_dir = cimgui.path("").getPath(b);

const lib = b.addStaticLibrary(.{
.name = "imgui",
const libimgui = b.addStaticLibrary(.{
.name = "cimgui",
.target = options.target,
.optimize = options.optimize,
});
lib.addIncludePath(imgui_cpp.path(""));
lib.addIncludePath(cimgui.path(""));
lib.addCSourceFiles(.{
if (libimgui.linkage == .static)
libimgui.pie = true
else
libimgui.root_module.pic = true;
libimgui.addIncludePath(.{ .path = cimgui_dir });
libimgui.addIncludePath(.{ .path = imgui_cpp_dir });
libimgui.addCSourceFiles(.{
.files = &.{
b.pathJoin(&.{ cimgui_dir, "cimgui.cpp" }),
b.pathJoin(&.{ imgui_cpp_dir, "imgui.cpp" }),
b.pathJoin(&.{ imgui_cpp_dir, "imgui_draw.cpp" }),
b.pathJoin(&.{ imgui_cpp_dir, "imgui_demo.cpp" }),
b.pathJoin(&.{ imgui_cpp_dir, "imgui_widgets.cpp" }),
b.pathJoin(&.{ imgui_cpp_dir, "imgui_tables.cpp" }),
// b.pathJoin(&.{ cimgui_dir, "cimgui.cpp" }),
},
.flags = &.{
"-Wall",
"-Wformat",
"-Wpedantic",
"-Wextra",
"-fno-exceptions",
"-Wformat",
"-fno-rtti",
"-fno-exceptions",
"-Werror",
"-DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1",
"-fno-threadsafe-statics",
},
});
lib.root_module.sanitize_c = false;
libimgui.root_module.sanitize_c = false;
if (libimgui.rootModuleTarget().os.tag == .windows)
libimgui.linkSystemLibrary2("imm32", .{ .use_pkg_config = .no });

// https://github.com/ziglang/zig/issues/5312
if (lib.rootModuleTarget().abi != .msvc) {
if (libimgui.rootModuleTarget().abi != .msvc) {
// llvm-libcxx + llvm-libunwind + os-libc
lib.linkLibCpp();
libimgui.linkLibCpp();
} else {
lib.linkLibC();
libimgui.linkLibC();
}
return lib;
return libimgui;
}
8 changes: 4 additions & 4 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
// .hash = "1220194ac6476adaed11b30299a8abdd593eb7ae6d368bf6832d7fe6a604f12cae4f",
// },
.cimgui = .{
.url = "git+https://github.com/cimgui/cimgui#831f155f605c0ef3ad2e2bc20dae298cdb6f5779",
.hash = "1220abb48ac358308cc4db0603dc92dcc1452b9a99026c7dbca0766f88424a4725c6",
.url = "git+https://github.com/kassane/cimgui#f5b64afa65549d5e5ca65b003a443030ead23c9b",
.hash = "1220e6183fb9559dbfafe58d7fab20f1c330bee32342205815aa10553134ff9f9302",
},
.imgui = .{
.url = "git+https://github.com/ocornut/imgui#6228c2e1ec7ef21ca1809579c055ed34540dedb0",
.hash = "12205a78270c1c77b8996639cb35adb6b63265aa31143f51ee207cb4fe90afc7d55d",
.url = "git+https://github.com/ocornut/imgui#5fdcdf7080aade03b30f742dd9369172460fcfd7",
.hash = "12202af0db41b79a4c7904950269b5e33b7d48f50aae042e334cb8d717a5a2caf48d",
},
},
}
91 changes: 91 additions & 0 deletions src/examples/imgui.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//------------------------------------------------------------------------------
// imgui.d
//
// Using cimgui+sokol, based on https://github.com/floooh/cimgui-sokol-starterkit
//------------------------------------------------------------------------------

module examples.imgui;

import sg = sokol.gfx;
import sgapp = sokol.glue;
import sapp = sokol.app;
import imgui = sokol.imgui;
import log = sokol.log;

extern (C):

struct State{
sg.PassAction pass_action = {
colors: [{
load_action: sg.LoadAction.Clear,
clear_value: { r: 0.0, g: 0.5, b: 1.0, a: 1.0 },
}]
};
}
static State state;

void init()
{
sg.Desc gfx = {
context: sgapp.context(),
logger: {func: &log.slog_func}
};
sg.setup(gfx);
imgui.simgui_desc_t imgui_desc = {0};
imgui.simgui_setup(&imgui_desc);
}

void frame()
{
imgui.simgui_frame_desc_t imgui_desc = {
width: sapp.width(),
height: sapp.height(),
delta_time: sapp.frameDuration(),
dpi_scale: sapp.dpiScale(),
};
imgui.simgui_new_frame(&imgui_desc);

/*=== UI CODE STARTS HERE ===*/
const imgui.ImVec2 window_pos = {0,0};
const imgui.ImVec2 window_pos_pivot = {0,0};
const imgui.ImVec2 window_size = {400, 100};
imgui.igSetNextWindowPos(window_pos, imgui.ImGuiCond_.ImGuiCond_Once, window_pos_pivot);
imgui.igSetNextWindowSize(window_size, imgui.ImGuiCond_.ImGuiCond_Once);
imgui.igBegin("Hello Dear ImGui!".ptr, null, imgui.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];
imgui.igColorEdit3(label, rgb, imgui.ImGuiColorEditFlags_.ImGuiColorEditFlags_None);
imgui.igEnd();
/*=== UI CODE ENDS HERE ===*/

sg.beginDefaultPass(state.pass_action, sapp.width(), sapp.height());
sg.endPass();
sg.commit();
}

void event(const(sapp.Event)* ev)
{
imgui.simgui_handle_event(ev);
}

void cleanup()
{
imgui.simgui_shutdown();
sg.shutdown();
}

void main()
{
sapp.Desc runner = {
window_title: "imgui.d",
init_cb: &init,
frame_cb: &frame,
cleanup_cb: &cleanup,
event_cb: &event,
width: 800,
height: 600,
icon: {sokol_default: true},
logger: {func: &log.func}
};
sapp.run(runner);
}
10 changes: 10 additions & 0 deletions src/sokol/c/sokol_imgui.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#if defined(IMPL)
#define SOKOL_IMGUI_IMPL
#endif
#include "sokol_app.h"
#include "sokol_gfx.h"
#include "sokol_log.h"
#include "sokol_glue.h"
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include <cimgui.h>
#include "sokol_imgui.h"
Loading

0 comments on commit 92c4f73

Please sign in to comment.