dev: migrate combat system & basic build execution to Zig #4

Merged
NotAShelf merged 9 commits from notashelf/push-nrzuupynktom into main 2026-04-05 21:14:12 +00:00
7 changed files with 144 additions and 52 deletions
Showing only changes of commit 7af642612b - Show all commits

build: migrate from Make to Zig build system + Just

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I7585121a5ec8e797adc43ba8e30d4ac86a6a6964
raf 2026-04-05 17:00:17 +03:00
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF

View file

@ -5,6 +5,8 @@ charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_size = 2
tab_width = 2
[*.c]
ident_style = space

10
.gitignore vendored
View file

@ -1,6 +1,12 @@
# ignore build artifacts
result
# Nix
/.direnv/
result*
# Build artifacts
build
obj
roguelike
# Zig
.zig-cache
.zig-out

20
Justfile Normal file
View file

@ -0,0 +1,20 @@
# Build the project
build:
zig build
# Build and run
dev:
zig build run
# Clean build artifacts
clean:
rm -rf zig-out .zig-cache
# Format all C source files
fmt:
clang-format -i src/*.c src/*.h
zig fmt **/*.zig
# Check formatting
fmt-check:
clang-format --dry-run --Werror src/*.c src/*.h

View file

@ -1,45 +0,0 @@
# Makefile for Roguelike Game
# Requires raylib, pkg-config
CC := cc
CFLAGS := -Wall -Wextra -O2 -std=c99 -Isrc
LDFLAGS := -lraylib -lm -lpthread -ldl -lrt
TARGET := roguelike
SRCDIR := src
OBJDIR := obj
SOURCES := $(wildcard $(SRCDIR)/*.c)
OBJECTS := $(patsubst $(SRCDIR)/%.c,$(OBJDIR)/%.o,$(SOURCES))
.PHONY: all clean format format-check
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $^ -o $@ $(LDFLAGS)
$(OBJDIR)/%.o: $(SRCDIR)/%.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -rf $(OBJDIR) $(TARGET)
# Alias for development
dev: all
./$(TARGET)
# Format all source files with clang-format
fmt:
@command -v clang-format >/dev/null 2>&1 || { echo "Error: clang-format is missing"; exit 1; }
@echo "Formatting source files..."
@clang-format -i $(SOURCES) $(wildcard $(SRCDIR)/*.h)
@echo "Done formatting."
# Check formatting without modifying files
fmt-check:
@command -v clang-format >/dev/null 2>&1 || { echo "Error: clang-format is missing"; exit 1; }
@echo "Checking formatting..."
@clang-format --dry-run --Werror \
$(SOURCES) $(wildcard $(SRCDIR)/*.h) && echo "All files properly formatted." || { echo "Formatting issues found. Run 'make fmt' to fix."; exit 1; }

73
build.zig Normal file
View file

@ -0,0 +1,73 @@
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Zig combat library
const combat_lib = b.addLibrary(.{
.name = "combat",
.root_module = b.createModule(.{
.root_source_file = b.path("libs/combat/combat.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
combat_lib.addIncludePath(b.path("src"));
// C sources (everything except combat, which is now Zig)
const c_sources = [_][]const u8{
"src/audio.c",
"src/enemy.c",
"src/items.c",
"src/main.c",
"src/map.c",
"src/player.c",
"src/render.c",
"src/rng.c",
"src/settings.c",
"src/utils.c",
};
const c_flags = [_][]const u8{
"-std=c99",
"-Wall",
"-Wextra",
"-O2",
};
// Main executable
const exe = b.addExecutable(.{
.name = "roguelike",
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
exe.addCSourceFiles(.{
.files = &c_sources,
.flags = &c_flags,
});
exe.addIncludePath(b.path("src"));
exe.linkLibrary(combat_lib);
exe.linkSystemLibrary("raylib");
exe.linkSystemLibrary("m");
exe.linkSystemLibrary("pthread");
exe.linkSystemLibrary("dl");
exe.linkSystemLibrary("rt");
b.installArtifact(exe);
// Run step
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Build and run the roguelike");
run_step.dependOn(&run_cmd.step);
}

View file

@ -1,6 +1,12 @@
{stdenv}:
{
lib,
stdenv,
zig,
raylib,
pkg-config,
}:
stdenv.mkDerivation (finalAttrs: {
pname = "sample-c-cpp";
pname = "rogged";
version = "0.0.1";
src = builtins.path {
@ -8,5 +14,32 @@ stdenv.mkDerivation (finalAttrs: {
name = finalAttrs.pname;
};
makeFlags = ["PREFIX=$(out)"];
nativeBuildInputs = [
zig
pkg-config
];
buildInputs = [raylib];
dontConfigure = true;
buildPhase = ''
runHook preBuild
export ZIG_GLOBAL_CACHE_DIR="$TMPDIR/zig-cache"
zig build --release=fast
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir -p $out/bin
cp zig-out/bin/roguelike $out/bin/
runHook postInstall
'';
meta = {
description = "A turn-based roguelike game";
license = lib.licenses.mit;
mainProgram = "roguelike";
};
})

View file

@ -2,9 +2,10 @@
mkShell,
clang-tools,
raylib,
gnumake,
pkg-config,
just,
zig,
zig-zlint,
}:
mkShell {
strictDeps = true;
@ -14,7 +15,9 @@ mkShell {
nativeBuildInputs = [
pkg-config
clang-tools
gnumake
just
zig
zig-zlint
];
}