daq-view/src/assets.zig
2025-02-02 00:59:24 +02:00

88 lines
2.6 KiB
Zig

const std = @import("std");
const rl = @import("raylib");
const srcery = @import("./srcery.zig");
const FontFace = @import("./font-face.zig");
const Aseprite = @import("./aseprite.zig");
const assert = std.debug.assert;
pub const FontId = enum {
text
};
var loaded_fonts: std.BoundedArray(rl.Font, 32) = .{};
const FontArray = std.EnumArray(FontId, FontFace);
var fonts: FontArray = FontArray.initUndefined();
pub var grab_texture: struct {
normal: rl.Texture2D,
hot: rl.Texture2D,
active: rl.Texture2D,
} = undefined;
pub fn font(font_id: FontId) FontFace {
return fonts.get(font_id);
}
pub fn init(allocator: std.mem.Allocator) !void {
const roboto_regular = @embedFile("./assets/fonts/roboto/Roboto-Regular.ttf");
const default_font = try loadFont(roboto_regular, 16);
fonts = FontArray.init(.{
.text = FontFace{ .font = default_font, .line_height = 1.2 }
});
const grab_ase = try Aseprite.init(allocator, @embedFile("./assets/grab-marker.ase"));
defer grab_ase.deinit();
const grab_normal_image = grab_ase.getTagImage(grab_ase.getTag("normal") orelse return error.TagNotFound);
defer grab_normal_image.unload();
const grab_normal_texture = rl.loadTextureFromImage(grab_normal_image);
errdefer grab_normal_texture.unload();
const grab_hot_image = grab_ase.getTagImage(grab_ase.getTag("hot") orelse return error.TagNotFound);
defer grab_hot_image.unload();
const grab_hot_texture = rl.loadTextureFromImage(grab_hot_image);
errdefer grab_hot_texture.unload();
const grab_active_image = grab_ase.getTagImage(grab_ase.getTag("active") orelse return error.TagNotFound);
defer grab_active_image.unload();
const grab_active_texture = rl.loadTextureFromImage(grab_active_image);
errdefer grab_active_texture.unload();
grab_texture = .{
.normal = grab_normal_texture,
.hot = grab_hot_texture,
.active = grab_active_texture
};
}
fn loadFont(ttf_data: []const u8, font_size: u32) !rl.Font {
var codepoints: [95]i32 = undefined;
for (0..codepoints.len) |i| {
codepoints[i] = @as(i32, @intCast(i)) + 32;
}
const loaded_font = rl.loadFontFromMemory(".ttf", ttf_data, @intCast(font_size), &codepoints);
if (!loaded_font.isReady()) {
return error.LoadFontFromMemory;
}
loaded_fonts.appendAssumeCapacity(loaded_font);
return loaded_font;
}
pub fn deinit(allocator: std.mem.Allocator) void {
_ = allocator;
for (loaded_fonts.slice()) |loaded_font| {
loaded_font.unload();
}
grab_texture.active.unload();
grab_texture.hot.unload();
grab_texture.normal.unload();
}