Fix up API calls for initialization

This commit is contained in:
Mitchell Hashimoto
2025-12-31 11:00:21 -08:00
parent 978400b0b0
commit 896361f426
6 changed files with 69 additions and 21 deletions

View File

@@ -133,6 +133,11 @@ pub fn build(b: *std.Build) !void {
},
.flags = flags.items,
});
lib.addCSourceFiles(.{
.root = b.path(""),
.files = &.{"ext.cpp"},
.flags = flags.items,
});
lib.installHeadersDirectory(
upstream.path(""),

30
pkg/dcimgui/ext.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include "imgui.h"
// This file contains custom extensions for functionality that isn't
// properly supported by Dear Bindings yet. Namely:
// https://github.com/dearimgui/dear_bindings/issues/55
// Wrap this in a namespace to keep it separate from the C++ API
namespace cimgui
{
#include "dcimgui.h"
}
extern "C"
{
CIMGUI_API void ImFontConfig_ImFontConfig(cimgui::ImFontConfig* self)
{
static_assert(sizeof(cimgui::ImFontConfig) == sizeof(::ImFontConfig), "ImFontConfig size mismatch");
static_assert(alignof(cimgui::ImFontConfig) == alignof(::ImFontConfig), "ImFontConfig alignment mismatch");
::ImFontConfig defaults;
*reinterpret_cast<::ImFontConfig*>(self) = defaults;
}
CIMGUI_API void ImGuiStyle_ImGuiStyle(cimgui::ImGuiStyle* self)
{
static_assert(sizeof(cimgui::ImGuiStyle) == sizeof(::ImGuiStyle), "ImGuiStyle size mismatch");
static_assert(alignof(cimgui::ImGuiStyle) == alignof(::ImGuiStyle), "ImGuiStyle alignment mismatch");
::ImGuiStyle defaults;
*reinterpret_cast<::ImGuiStyle*>(self) = defaults;
}
}

View File

@@ -28,6 +28,12 @@ pub extern fn ImGui_DockBuilderDockWindow(window_name: [*:0]const u8, node_id: c
pub extern fn ImGui_DockBuilderSplitNode(node_id: c.ImGuiID, split_dir: c.ImGuiDir, size_ratio_for_node_at_dir: f32, out_id_at_dir: *c.ImGuiID, out_id_at_opposite_dir: *c.ImGuiID) callconv(.c) c.ImGuiID;
pub extern fn ImGui_DockBuilderFinish(node_id: c.ImGuiID) callconv(.c) void;
// Extension functions from ext.cpp
pub const ext = struct {
pub extern fn ImFontConfig_ImFontConfig(self: *c.ImFontConfig) callconv(.c) void;
pub extern fn ImGuiStyle_ImGuiStyle(self: *c.ImGuiStyle) callconv(.c) void;
};
test {
_ = c;
}

View File

@@ -1070,8 +1070,11 @@ pub const Inspector = struct {
// Cache our scale because we use it for cursor position calculations.
self.content_scale = x;
// Setup a new style and scale it appropriately.
var style: cimgui.c.ImGuiStyle = .{};
// Setup a new style and scale it appropriately. We must use the
// ImGuiStyle constructor to get proper default values (e.g.,
// CurveTessellationTol) rather than zero-initialized values.
var style: cimgui.c.ImGuiStyle = undefined;
cimgui.ext.ImGuiStyle_ImGuiStyle(&style);
cimgui.c.ImGuiStyle_ScaleAllSizes(&style, @floatCast(x));
const active_style = cimgui.c.ImGui_GetStyle();
active_style.* = style;

View File

@@ -255,8 +255,11 @@ pub const ImguiWidget = extern struct {
io.DisplaySize = .{ .x = @floatFromInt(width), .y = @floatFromInt(height) };
io.DisplayFramebufferScale = .{ .x = 1, .y = 1 };
// Setup a new style and scale it appropriately.
var style: cimgui.c.ImGuiStyle = .{};
// Setup a new style and scale it appropriately. We must use the
// ImGuiStyle constructor to get proper default values (e.g.,
// CurveTessellationTol) rather than zero-initialized values.
var style: cimgui.c.ImGuiStyle = undefined;
cimgui.ext.ImGuiStyle_ImGuiStyle(&style);
cimgui.c.ImGuiStyle_ScaleAllSizes(&style, @floatFromInt(scale_factor));
const active_style = cimgui.c.ImGui_GetStyle();
active_style.* = style;

View File

@@ -138,23 +138,24 @@ pub fn setup() void {
io.IniFilename = null;
io.LogFilename = null;
// Use our own embedded font
{
// TODO: This will have to be recalculated for different screen DPIs.
// This is currently hardcoded to a 2x content scale.
const font_size = 16 * 2;
var font_config: cimgui.c.ImFontConfig = .{};
font_config.FontDataOwnedByAtlas = false;
_ = cimgui.c.ImFontAtlas_AddFontFromMemoryTTF(
io.Fonts,
@ptrCast(@constCast(font.embedded.regular)),
font.embedded.regular.len,
font_size,
&font_config,
null,
);
}
// // Use our own embedded font
// {
// // TODO: This will have to be recalculated for different screen DPIs.
// // This is currently hardcoded to a 2x content scale.
// const font_size = 16 * 2;
//
// var font_config: cimgui.c.ImFontConfig = .{};
// cimgui.ext.ImFontConfig_ImFontConfig(&font_config);
// font_config.FontDataOwnedByAtlas = false;
// _ = cimgui.c.ImFontAtlas_AddFontFromMemoryTTF(
// io.Fonts,
// @ptrCast(@constCast(font.embedded.regular.ptr)),
// @intCast(font.embedded.regular.len),
// font_size,
// &font_config,
// null,
// );
// }
}
pub fn init(surface: *Surface) !Inspector {