1
0

include all ROMs into final binary at comptime

This commit is contained in:
Rokas Puzonas 2023-11-09 00:20:28 +02:00
parent 441a3df8ea
commit cdfd81fc91
3 changed files with 37 additions and 1 deletions

View File

@ -32,6 +32,24 @@ pub fn build(b: *std.Build) !void {
const exe = b.addExecutable(.{ .name = "chip8-zig", .root_source_file = .{ .path = "src/main.zig" }, .optimize = optimize, .target = target });
// Provide filenames of all files in 'src/ROMs' to program as options
{
var files = std.ArrayList([]const u8).init(b.allocator);
defer files.deinit();
var options = b.addOptions();
var dir = try std.fs.cwd().openIterableDir("src/ROMs", .{ });
var it = dir.iterate();
while (try it.next()) |file| {
if (file.kind != .file) {
continue;
}
try files.append(b.pathJoin(&.{"ROMs", file.name}));
}
options.addOption([]const []const u8, "roms", files.items);
exe.addOptions("options", options);
}
raylib.addTo(b, exe, target, optimize);
// rl.link(b, exe, target, optimize);

BIN
src/ROMs/snek.ch8 Normal file

Binary file not shown.

View File

@ -7,11 +7,17 @@ const ChipContext = @import("chip.zig");
const RaylibChip = @import("raylib-chip.zig");
const GlobalContext = @import("./global-context.zig");
const MainScene = @import("./main-scene.zig");
const options = @import("options");
fn megabytes(amount: usize) usize {
return amount * 1024 * 1024;
}
const ROM = struct {
name: []const u8,
data: []const u8,
};
pub fn main() anyerror!void {
const memory = try std.heap.page_allocator.alloc(u8, megabytes(5));
var fba = std.heap.FixedBufferAllocator.init(memory);
@ -30,7 +36,19 @@ pub fn main() anyerror!void {
var main_scene = try MainScene.init(allocator, &ctx);
defer main_scene.deinit();
main_scene.chip.set_memory(0x200, @embedFile("ROMs/br8kout.ch8"));
comptime var roms = [1]ROM{ undefined } ** options.roms.len;
comptime {
var i = 0;
for (options.roms) |file| {
roms[i] = ROM{
.name = file,
.data = @embedFile(file)
};
i += 1;
}
}
main_scene.chip.set_memory(0x200, roms[3].data);
const font_size = 24;
const font_ttf_default_numchars = 95; // TTF font generation default charset: 95 glyphs (ASCII 32..126)