add game_debug.c

This commit is contained in:
Rokas Puzonas 2025-12-06 00:41:35 +02:00
parent be1cb0ba9f
commit 7310056eea
5 changed files with 166 additions and 76 deletions

163
build.zig
View File

@ -6,8 +6,9 @@ pub fn build(b: *std.Build) !void {
const optimize = b.standardOptimizeOption(.{});
const tracy_enabled = b.option(bool, "tracy", "Enable tracy profiling") orelse (optimize == .Debug);
const imgui_enabled = b.option(bool, "imgui", "Enable ImGui integration") orelse (optimize == .Debug);
var targets = std.ArrayList(*std.Build.Step.Compile){};
var targets: std.ArrayList(*std.Build.Step.Compile) = .empty;
const mod = b.createModule(.{
.target = target,
@ -16,20 +17,7 @@ pub fn build(b: *std.Build) !void {
});
mod.addIncludePath(b.path("./src/"));
{
var src_dir = try std.fs.cwd().openDir("src", .{ .iterate = true });
defer src_dir.close();
var iter = src_dir.iterate();
while (try iter.next()) |entry| {
if (entry.kind == .file and std.mem.endsWith(u8, entry.name, ".c")) {
mod.addCSourceFile(.{
.file = b.path(b.pathJoin(&.{ "src", entry.name })),
.flags = &.{}
});
}
}
}
var cflags: std.ArrayList([]const u8) = .empty;
const raylib_dep = b.dependency("raylib", .{
.target = target,
@ -45,62 +33,33 @@ pub fn build(b: *std.Build) !void {
const incbin_dep = b.dependency("incbin", .{});
mod.addIncludePath(incbin_dep.path("."));
{
const imgui_mod = b.createModule(.{
if (imgui_enabled) {
if (try buildImGui(b, .{
.target = target,
.optimize = optimize,
.link_libcpp = true,
});
.raylib = raylib_dep.artifact("raylib")
})) |imgui| {
mod.linkLibrary(imgui);
try targets.append(b.allocator, imgui);
}
const imgui = b.dependency("imgui", .{});
imgui_mod.addCSourceFiles(.{
.root = imgui.path("."),
.files = &.{
"imgui.cpp",
"imgui_demo.cpp",
"imgui_draw.cpp",
"imgui_tables.cpp",
"imgui_widgets.cpp",
},
.flags = &.{
"-fno-sanitize=undefined",
"-std=c++11",
"-Wno-deprecated-declarations",
"-DNO_FONT_AWESOME",
},
});
imgui_mod.addIncludePath(imgui.path("."));
const imgui_lib = b.addLibrary(.{
.name = "imgui",
.root_module = imgui_mod
});
imgui_lib.installHeadersDirectory(imgui.path("."), "imgui", .{});
imgui_lib.installHeadersDirectory(imgui.path("."), ".", .{});
mod.linkLibrary(imgui_lib);
try cflags.append(b.allocator, "-DIMGUI_ENABLED");
}
const cimgui = b.dependency("cimgui", .{});
mod.addCSourceFile(.{
.file = cimgui.path("cimgui.cpp"),
.flags = &.{
"-fno-exceptions",
"-fno-rtti",
"-std=c++11"
},
});
mod.addIncludePath(cimgui.path("."));
{
var src_dir = try std.fs.cwd().openDir("src", .{ .iterate = true });
defer src_dir.close();
const rlimgui = b.dependency("rlimgui", .{
.target = target,
.optimize = optimize,
});
mod.addCSourceFile(.{
.file = rlimgui.path("rlImGui.cpp"),
.flags = &.{},
});
mod.addIncludePath(rlimgui.path("."));
var iter = src_dir.iterate();
while (try iter.next()) |entry| {
if (entry.kind == .file and std.mem.endsWith(u8, entry.name, ".c")) {
mod.addCSourceFile(.{
.file = b.path(b.pathJoin(&.{ "src", entry.name })),
.flags = cflags.items
});
}
}
}
const exe = b.addExecutable(.{
.name = "gaem",
@ -129,3 +88,77 @@ pub fn build(b: *std.Build) !void {
_ = zcc.createStep(b, "cdb", try targets.toOwnedSlice(b.allocator));
}
const ImGuiOptions = struct {
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
raylib: *std.Build.Step.Compile
};
fn buildImGui(b: *std.Build, opts: ImGuiOptions) !?*std.Build.Step.Compile {
const imgui = b.lazyDependency("imgui", .{}) orelse return null;
const cimgui = b.lazyDependency("cimgui", .{}) orelse return null;
const rlimgui = b.lazyDependency("rlimgui", .{}) orelse return null;
const cpp_flags = .{
"-fno-exceptions",
"-fno-rtti",
"-std=c++11",
"-DNO_FONT_AWESOME"
};
const imgui_lib = b.addLibrary(.{
.name = "imgui",
.root_module = b.createModule(.{
.target = opts.target,
.optimize = opts.optimize,
.link_libcpp = true,
})
});
imgui_lib.root_module.addCSourceFiles(.{
.root = imgui.path("."),
.files = &.{
"imgui.cpp",
"imgui_demo.cpp",
"imgui_draw.cpp",
"imgui_tables.cpp",
"imgui_widgets.cpp",
},
.flags = &cpp_flags
});
imgui_lib.root_module.addIncludePath(imgui.path("."));
// Needed for cimgui
imgui_lib.installHeadersDirectory(imgui.path("."), "imgui", .{});
// Needed for rlimgui
imgui_lib.installHeadersDirectory(imgui.path("."), ".", .{});
const lib = b.addLibrary(.{
.name = "imgui_raylib_cimgui",
.root_module = b.createModule(.{
.target = opts.target,
.optimize = opts.optimize,
.link_libcpp = true,
})
});
lib.root_module.linkLibrary(imgui_lib);
lib.root_module.addCSourceFile(.{
.file = cimgui.path("cimgui.cpp"),
.flags = &cpp_flags
});
lib.root_module.addIncludePath(cimgui.path("."));
lib.root_module.addCSourceFile(.{
.file = rlimgui.path("rlImGui.cpp"),
.flags = &cpp_flags,
});
lib.root_module.addIncludePath(rlimgui.path("."));
lib.root_module.linkLibrary(opts.raylib);
lib.installHeader(rlimgui.path("rlImGui.h"), "rlImGui.h");
lib.installHeader(cimgui.path("cimgui.h"), "cimgui.h");
return lib;
}

View File

@ -20,14 +20,17 @@
.imgui = .{
.url = "https://github.com/ocornut/imgui/archive/6d910d5487d11ca567b61c7824b0c78c569d62f0.tar.gz",
.hash = "N-V-__8AALp9cwA8tEuEno2YCZyivsMaobnF-Z7qZGY3qBOt",
.lazy = true
},
.rlimgui = .{
.url = "https://github.com/raylib-extras/rlImGui/archive/dc7f97679a024eee8f5f009e77cc311748200415.tar.gz",
.hash = "N-V-__8AAC-taQCKZ-3IjRuWJ-5Dc47QKHDPEmqlwXhvjBGq",
.lazy = true
},
.cimgui = .{
.url = "https://github.com/cimgui/cimgui/archive/bfd30140a9c5832b5e0dcf179d6e1e5c69373d5a.tar.gz",
.hash = "N-V-__8AAOY-OACWjhmHu289AXFy9Fro3w_30mGFYL8FTq71",
.lazy = true
}
},
.minimum_zig_version = "0.15.2",

58
src/game_debug.c Normal file
View File

@ -0,0 +1,58 @@
#include "main.h"
#ifdef IMGUI_ENABLED
#define NO_FONT_AWESOME
#include <rlImGui.h>
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include <cimgui.h>
void game_debug_init()
{
rlImGuiBeginInitImGui();
ImGuiIO *io = igGetIO_ContextPtr(igGetCurrentContext());
io->IniFilename = NULL;
igStyleColorsDark(NULL);
rlImGuiEndInitImGui();
}
void game_debug_deinit()
{
rlImGuiShutdown();
}
void game_debug_show(struct Game *game)
{
rlImGuiBegin();
igSetNextWindowSize((ImVec2_c){ 400, 200 }, ImGuiCond_Once);
if (igBegin("Debug", NULL, ImGuiWindowFlags_None)) {
char label[64] = { 0 };
snprintf(label, sizeof(label), "FPS: %.1f (%.2fms)", 1.0f / game->input.dt, game->input.dt * 1000);
igTextEx(label, NULL, ImGuiTextFlags_None);
igTextEx("Hello, World", NULL, ImGuiTextFlags_None);
}
igEnd();
rlImGuiEnd();
}
#else
void game_debug_init()
{
}
void game_debug_deinit()
{
}
void game_debug_show(struct Game *game)
{
}
#endif

View File

@ -86,14 +86,12 @@ int main(void)
SetWindowState(FLAG_WINDOW_RESIZABLE);
SetTargetFPS(60);
rlImGuiSetup(true);
resources_init(&g_resources);
struct Game game = { 0 };
game_init(&game);
bool open = true;
game_debug_init();
while (!WindowShouldClose())
{
@ -119,15 +117,13 @@ int main(void)
game_tick(&game);
end_window_scaling(&transform, BLACK);
rlImGuiBegin();
if (open) igShowDemoWindow(&open);
rlImGuiEnd();
game_debug_show(&game);
EndDrawing();
}
game_free(&game);
rlImGuiShutdown();
game_debug_deinit();
CloseWindow();
return 0;

View File

@ -2,7 +2,6 @@
#include <raylib.h>
#include <raymath.h>
#include <rlImGui.h>
#include <rlgl.h>
#include <TracyConfig.h>
#include <tracy/TracyC.h>
@ -15,9 +14,6 @@
#include <stdio.h>
#include <string.h>
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include <cimgui.h>
#define INCBIN_STYLE INCBIN_STYLE_SNAKE
#define INCBIN_PREFIX
#include <incbin.h>
@ -75,5 +71,9 @@ void game_init(struct Game *game);
void game_free(struct Game *game);
void game_tick(struct Game *game);
void game_debug_init();
void game_debug_deinit();
void game_debug_show(struct Game *game);
Color rgb(uint8_t r, uint8_t g, uint8_t b);
Color hex(const char *str);