artificer/gui/main.zig
2024-09-07 16:37:05 +03:00

77 lines
2.2 KiB
Zig

const std = @import("std");
const Artificer = @import("artificer");
const rl = @import("raylib");
const Allocator = std.mem.Allocator;
const srcery = @import("./srcery.zig");
const UI = @import("./ui.zig");
const UIStack = @import("./ui_stack.zig");
const RectUtils = @import("./rect_utils.zig");
fn getAPITokenFromArgs(allocator: Allocator) !?[]u8 {
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
if (args.len < 2) {
return null;
}
const filename = args[1];
const cwd = std.fs.cwd();
var token_buffer: [256]u8 = undefined;
const token = try cwd.readFile(filename, &token_buffer);
return try allocator.dupe(u8, std.mem.trim(u8,token,"\n\t "));
}
fn drawCharacterInfo(ui: *UI, rect: rl.Rectangle, brain: Artificer.Brain) void {
const name_height = 20;
UI.drawTextCentered(ui.font, brain.name, .{
.x = RectUtils.center(rect).x,
.y = rect.y + name_height/2
}, 20, 2, srcery.bright_white);
}
pub fn main() anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const token = (try getAPITokenFromArgs(allocator)) orelse return error.MissingToken;
defer allocator.free(token);
var artificer = try Artificer.init(allocator, token);
defer artificer.deinit();
rl.initWindow(800, 450, "Artificer");
defer rl.closeWindow();
rl.setTargetFPS(60);
var ui = UI.init();
defer ui.deinit();
while (!rl.windowShouldClose()) {
if (std.time.timestamp() > artificer.nextStepAt()) {
try artificer.step();
}
const screen_size = rl.Vector2.init(
@floatFromInt(rl.getScreenWidth()),
@floatFromInt(rl.getScreenHeight())
);
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(srcery.black);
var info_stack = UIStack.init(rl.Rectangle.init(0, 0, screen_size.x, screen_size.y), .left_to_right);
for (artificer.characters.items) |brain| {
const info_width = screen_size.x / @as(f32, @floatFromInt(artificer.characters.items.len));
drawCharacterInfo(&ui, info_stack.next(info_width), brain);
}
}
}