mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-04-21 14:55:20 +00:00
Imgui (#20)
* vendor/cimgui * Add a "dev mode" window which for now is just imgui demo
This commit is contained in:
committed by
GitHub
parent
ddfb1dec4b
commit
f29393bca6
93
pkg/imgui/build.zig
Normal file
93
pkg/imgui/build.zig
Normal file
@@ -0,0 +1,93 @@
|
||||
const std = @import("std");
|
||||
|
||||
/// Directories with our includes.
|
||||
const root = thisDir() ++ "../../../vendor/cimgui/";
|
||||
pub const include_paths = [_][]const u8{
|
||||
root,
|
||||
root ++ "imgui",
|
||||
root ++ "imgui/backends",
|
||||
};
|
||||
|
||||
pub const pkg = std.build.Pkg{
|
||||
.name = "imgui",
|
||||
.source = .{ .path = thisDir() ++ "/main.zig" },
|
||||
};
|
||||
|
||||
fn thisDir() []const u8 {
|
||||
return std.fs.path.dirname(@src().file) orelse ".";
|
||||
}
|
||||
|
||||
pub const Options = struct {
|
||||
backends: ?[]const []const u8 = null,
|
||||
};
|
||||
|
||||
pub fn link(
|
||||
b: *std.build.Builder,
|
||||
step: *std.build.LibExeObjStep,
|
||||
opt: Options,
|
||||
) !*std.build.LibExeObjStep {
|
||||
const lib = try buildImgui(b, step, opt);
|
||||
step.linkLibrary(lib);
|
||||
inline for (include_paths) |path| step.addIncludePath(path);
|
||||
return lib;
|
||||
}
|
||||
|
||||
pub fn buildImgui(
|
||||
b: *std.build.Builder,
|
||||
step: *std.build.LibExeObjStep,
|
||||
opt: Options,
|
||||
) !*std.build.LibExeObjStep {
|
||||
const target = step.target;
|
||||
const lib = b.addStaticLibrary("imgui", null);
|
||||
lib.setTarget(step.target);
|
||||
lib.setBuildMode(step.build_mode);
|
||||
|
||||
// Include
|
||||
inline for (include_paths) |path| lib.addIncludePath(path);
|
||||
|
||||
// Link
|
||||
lib.linkLibC();
|
||||
|
||||
// Compile
|
||||
var flags = std.ArrayList([]const u8).init(b.allocator);
|
||||
defer flags.deinit();
|
||||
try flags.appendSlice(&.{
|
||||
"-DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1",
|
||||
|
||||
//"-fno-sanitize=undefined",
|
||||
});
|
||||
switch (target.getOsTag()) {
|
||||
.windows => try flags.appendSlice(&.{
|
||||
"-DIMGUI_IMPL_API=extern\t\"C\"\t__declspec(dllexport)",
|
||||
}),
|
||||
else => try flags.appendSlice(&.{
|
||||
"-DIMGUI_IMPL_API=extern\t\"C\"\t",
|
||||
}),
|
||||
}
|
||||
|
||||
// C files
|
||||
lib.addCSourceFiles(srcs, flags.items);
|
||||
if (opt.backends) |backends| {
|
||||
for (backends) |backend| {
|
||||
var buf: [4096]u8 = undefined;
|
||||
const path = try std.fmt.bufPrint(
|
||||
&buf,
|
||||
"{s}imgui/backends/imgui_impl_{s}.cpp",
|
||||
.{ root, backend },
|
||||
);
|
||||
|
||||
lib.addCSourceFile(path, flags.items);
|
||||
}
|
||||
}
|
||||
|
||||
return lib;
|
||||
}
|
||||
|
||||
const srcs = &.{
|
||||
root ++ "cimgui.cpp",
|
||||
root ++ "imgui/imgui.cpp",
|
||||
root ++ "imgui/imgui_demo.cpp",
|
||||
root ++ "imgui/imgui_draw.cpp",
|
||||
root ++ "imgui/imgui_tables.cpp",
|
||||
root ++ "imgui/imgui_widgets.cpp",
|
||||
};
|
||||
4
pkg/imgui/c.zig
Normal file
4
pkg/imgui/c.zig
Normal file
@@ -0,0 +1,4 @@
|
||||
pub usingnamespace @cImport({
|
||||
@cDefine("CIMGUI_DEFINE_ENUMS_AND_STRUCTS", "");
|
||||
@cInclude("cimgui.h");
|
||||
});
|
||||
28
pkg/imgui/context.zig
Normal file
28
pkg/imgui/context.zig
Normal file
@@ -0,0 +1,28 @@
|
||||
const std = @import("std");
|
||||
const c = @import("c.zig");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
pub const Context = opaque {
|
||||
pub fn create() Allocator.Error!*Context {
|
||||
return @ptrCast(
|
||||
?*Context,
|
||||
c.igCreateContext(null),
|
||||
) orelse Allocator.Error.OutOfMemory;
|
||||
}
|
||||
|
||||
pub fn destroy(self: *Context) void {
|
||||
c.igDestroyContext(self.cval());
|
||||
}
|
||||
|
||||
pub inline fn cval(self: *Context) *c.ImGuiContext {
|
||||
return @ptrCast(
|
||||
*c.ImGuiContext,
|
||||
@alignCast(@alignOf(c.ImGuiContext), self),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
test {
|
||||
var ctx = try Context.create();
|
||||
defer ctx.destroy();
|
||||
}
|
||||
20
pkg/imgui/core.zig
Normal file
20
pkg/imgui/core.zig
Normal file
@@ -0,0 +1,20 @@
|
||||
const std = @import("std");
|
||||
const c = @import("c.zig");
|
||||
const imgui = @import("main.zig");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
pub fn newFrame() void {
|
||||
c.igNewFrame();
|
||||
}
|
||||
|
||||
pub fn endFrame() void {
|
||||
c.igEndFrame();
|
||||
}
|
||||
|
||||
pub fn render() void {
|
||||
c.igRender();
|
||||
}
|
||||
|
||||
pub fn showDemoWindow(open: ?*bool) void {
|
||||
c.igShowDemoWindow(@ptrCast([*c]bool, if (open) |v| v else null));
|
||||
}
|
||||
20
pkg/imgui/draw_data.zig
Normal file
20
pkg/imgui/draw_data.zig
Normal file
@@ -0,0 +1,20 @@
|
||||
const std = @import("std");
|
||||
const c = @import("c.zig");
|
||||
const imgui = @import("main.zig");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
pub const DrawData = opaque {
|
||||
pub fn get() Allocator.Error!*DrawData {
|
||||
return @ptrCast(
|
||||
?*DrawData,
|
||||
c.igGetDrawData(),
|
||||
) orelse Allocator.Error.OutOfMemory;
|
||||
}
|
||||
|
||||
pub inline fn cval(self: *DrawData) *c.ImGuiDrawData {
|
||||
return @ptrCast(
|
||||
*c.ImGuiDrawData,
|
||||
@alignCast(@alignOf(c.ImGuiDrawData), self),
|
||||
);
|
||||
}
|
||||
};
|
||||
28
pkg/imgui/impl_glfw.zig
Normal file
28
pkg/imgui/impl_glfw.zig
Normal file
@@ -0,0 +1,28 @@
|
||||
const std = @import("std");
|
||||
const c = @import("c.zig");
|
||||
const imgui = @import("main.zig");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
pub const ImplGlfw = struct {
|
||||
pub const GLFWWindow = opaque {};
|
||||
|
||||
pub fn initForOpenGL(win: *GLFWWindow, install_callbacks: bool) bool {
|
||||
// https://github.com/ocornut/imgui/issues/5785
|
||||
defer _ = glfwGetError(null);
|
||||
|
||||
return ImGui_ImplGlfw_InitForOpenGL(win, install_callbacks);
|
||||
}
|
||||
|
||||
pub fn shutdown() void {
|
||||
return ImGui_ImplGlfw_Shutdown();
|
||||
}
|
||||
|
||||
pub fn newFrame() void {
|
||||
return ImGui_ImplGlfw_NewFrame();
|
||||
}
|
||||
|
||||
extern "c" fn glfwGetError(?*const anyopaque) c_int;
|
||||
extern "c" fn ImGui_ImplGlfw_InitForOpenGL(*GLFWWindow, bool) bool;
|
||||
extern "c" fn ImGui_ImplGlfw_Shutdown() void;
|
||||
extern "c" fn ImGui_ImplGlfw_NewFrame() void;
|
||||
};
|
||||
30
pkg/imgui/impl_opengl3.zig
Normal file
30
pkg/imgui/impl_opengl3.zig
Normal file
@@ -0,0 +1,30 @@
|
||||
const std = @import("std");
|
||||
const c = @import("c.zig");
|
||||
const imgui = @import("main.zig");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
pub const ImplOpenGL3 = struct {
|
||||
pub fn init(glsl_version: ?[:0]const u8) bool {
|
||||
return ImGui_ImplOpenGL3_Init(
|
||||
if (glsl_version) |s| s.ptr else null,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn shutdown() void {
|
||||
return ImGui_ImplOpenGL3_Shutdown();
|
||||
}
|
||||
|
||||
pub fn newFrame() void {
|
||||
return ImGui_ImplOpenGL3_NewFrame();
|
||||
}
|
||||
|
||||
pub fn renderDrawData(data: *imgui.DrawData) void {
|
||||
ImGui_ImplOpenGL3_RenderDrawData(data);
|
||||
}
|
||||
|
||||
extern "c" fn glfwGetError(?*const anyopaque) c_int;
|
||||
extern "c" fn ImGui_ImplOpenGL3_Init([*c]const u8) bool;
|
||||
extern "c" fn ImGui_ImplOpenGL3_Shutdown() void;
|
||||
extern "c" fn ImGui_ImplOpenGL3_NewFrame() void;
|
||||
extern "c" fn ImGui_ImplOpenGL3_RenderDrawData(*imgui.DrawData) void;
|
||||
};
|
||||
26
pkg/imgui/io.zig
Normal file
26
pkg/imgui/io.zig
Normal file
@@ -0,0 +1,26 @@
|
||||
const std = @import("std");
|
||||
const c = @import("c.zig");
|
||||
const imgui = @import("main.zig");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
pub const IO = opaque {
|
||||
pub fn get() Allocator.Error!*IO {
|
||||
return @ptrCast(
|
||||
?*IO,
|
||||
c.igGetIO(),
|
||||
) orelse Allocator.Error.OutOfMemory;
|
||||
}
|
||||
|
||||
pub inline fn cval(self: *IO) *c.ImGuiIO {
|
||||
return @ptrCast(
|
||||
*c.ImGuiIO,
|
||||
@alignCast(@alignOf(c.ImGuiIO), self),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
test {
|
||||
const ctx = try imgui.Context.create();
|
||||
defer ctx.destroy();
|
||||
_ = try IO.get();
|
||||
}
|
||||
13
pkg/imgui/main.zig
Normal file
13
pkg/imgui/main.zig
Normal file
@@ -0,0 +1,13 @@
|
||||
pub const c = @import("c.zig");
|
||||
pub usingnamespace @import("context.zig");
|
||||
pub usingnamespace @import("core.zig");
|
||||
pub usingnamespace @import("draw_data.zig");
|
||||
pub usingnamespace @import("io.zig");
|
||||
pub usingnamespace @import("style.zig");
|
||||
|
||||
pub usingnamespace @import("impl_glfw.zig");
|
||||
pub usingnamespace @import("impl_opengl3.zig");
|
||||
|
||||
test {
|
||||
@import("std").testing.refAllDecls(@This());
|
||||
}
|
||||
35
pkg/imgui/style.zig
Normal file
35
pkg/imgui/style.zig
Normal file
@@ -0,0 +1,35 @@
|
||||
const std = @import("std");
|
||||
const c = @import("c.zig");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
pub const Style = opaque {
|
||||
pub fn get() Allocator.Error!*Style {
|
||||
return @ptrCast(
|
||||
?*Style,
|
||||
c.igGetStyle(),
|
||||
) orelse Allocator.Error.OutOfMemory;
|
||||
}
|
||||
|
||||
pub fn colorsDark(self: *Style) void {
|
||||
c.igStyleColorsDark(self.cval());
|
||||
}
|
||||
|
||||
pub fn colorsLight(self: *Style) void {
|
||||
c.igStyleColorsLight(self.cval());
|
||||
}
|
||||
|
||||
pub fn colorsClassic(self: *Style) void {
|
||||
c.igStyleColorsClassic(self.cval());
|
||||
}
|
||||
|
||||
pub fn scaleAllSizes(self: *Style, factor: f32) void {
|
||||
c.ImGuiStyle_ScaleAllSizes(self.cval(), factor);
|
||||
}
|
||||
|
||||
pub inline fn cval(self: *Style) *c.ImGuiStyle {
|
||||
return @ptrCast(
|
||||
*c.ImGuiStyle,
|
||||
@alignCast(@alignOf(c.ImGuiStyle), self),
|
||||
);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user