add viewer, the code sucks but it's 1AM and I yearn for my bed

This commit is contained in:
Hamcha 2024-05-08 01:12:05 +02:00
parent e259535320
commit 6cefd844d5
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89
2 changed files with 71 additions and 4 deletions

View file

@ -27,10 +27,28 @@ pub fn build(b: *std.Build) void {
const run_cmd = b.addRunArtifact(exe); const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep()); run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app"); const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step); run_step.dependOn(&run_cmd.step);
// Viewer debug app
const viewer = b.addExecutable(.{
.name = "binfon-viewer",
.root_source_file = b.path("src/view.zig"),
.target = target,
.optimize = optimize,
});
b.installArtifact(viewer);
const view_cmd = b.addRunArtifact(viewer);
view_cmd.step.dependOn(b.getInstallStep());
const view_step = b.step("view", "Run the app");
view_step.dependOn(&view_cmd.step);
if (b.args) |args| {
run_cmd.addArgs(args);
view_cmd.addArgs(args);
}
} }

49
src/view.zig Normal file
View file

@ -0,0 +1,49 @@
const std = @import("std");
pub fn main() !void {
// Get allocator
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
defer _ = gpa.deinit();
// Get args
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
std.debug.print("args: {s}\n", .{args});
if (args.len < 4) {
std.log.err("Usage: {s} <glyph width (eg. 8)> <glyph height (eg. 16)> <file.font.bin>", .{args[0]});
std.process.exit(1);
}
const bytesPerRow = (try std.fmt.parseInt(u8, args[1], 10)) / 8;
const glyphHeight = try std.fmt.parseInt(u8, args[2], 10);
// Open font
const file = try std.fs.cwd().openFile(args[3], .{});
defer file.close();
// Read byte by byte
var reader = file.reader();
var i: u16 = 0;
while (true) {
std.debug.print(" -- GLYPH {d} / 0x{x} --\n", .{ i, i });
for (0..glyphHeight) |y| {
for (0..bytesPerRow) |_| {
const next = try reader.readByte();
std.debug.print("{x}: ", .{y});
for (0..8) |x| {
if (@as(u1, @truncate(next >> @truncate(8 - x))) == 1) {
std.debug.print("*", .{});
} else {
std.debug.print(" ", .{});
}
}
std.debug.print(" ", .{});
}
std.debug.print("\n", .{});
}
i += 1;
}
}