//! ---------------------------------------------------- //! # Game Template Library //! //! Low *cortisol* library for games and game engines //! //! # Features //! //! **dynamic** //! | SlotMap //! | KeyedSlotMap //! | Handle(T) **Generational** //! //! **fixed** //! | FixedArrayList //! | FixedSlotMap //! | RingBuffer //! ---------------------------------------------------- // // PRIVATE // const std = @import("std"); // // PUBLIC // pub const log = @import("log.zig"); pub const ansi = @import("ansi.zig"); pub const Handle = @import("handle.zig").Handle; pub const TypeID = @import("type_id.zig").of; // --- ARRAYS --- pub const SlotMap = @import("slot_map.zig").SlotMap; pub const KeyedSlotMap = @import("keyed_slot_map.zig").KeyedSlotMap; pub const FixedArrayList = @import("fixed_array_list.zig").FixedArrayList; pub const FixedSlotMap = @import("fixed_slot_map.zig").FixedSlotMap; pub const RingBuffer = @import("ring_buffer.zig").RingBuffer; // // STRUCTS // // --- SHADER --- const ShaderID = union(enum) { pbr, unlit, custom: Handle(ShaderID) }; const GPUShader = struct { raw: *anyopaque }; // --- PIPELINE --- const PipelineKey = struct { shader: ShaderID }; const GPUPipeline = struct { raw: *anyopaque }; /// ---------------------------------------------------- /// ---------------------------------------------------- const Material = struct { /// ---------------------------------------------------- /// ---------------------------------------------------- pub const Style = struct { albedo: @Vector(4, f32) = .{ 1, 1, 1, 1 }, metallic: f32 = 0.0, roughness: f32 = 0.5, }; /// ---------------------------------------------------- /// ---------------------------------------------------- pub const Unique = struct { shader: ShaderID = .pbr, front_face: enum { cw, ccw } = .ccw, cull_mode: enum { back, front, none } = .back, topology: enum { triangle_list, point_list, line_list } = .triangle_list, blend_mode: enum { none, alpha, additive, multiply } = .none, }; // // FIELDS // style: Style = .{}, unique: Unique = .{}, }; test "SlotMap" { // --- ALLOCATOR --- const alloc = std.testing.allocator; std.debug.print("{s}━━━ SLOTMAP ━━━{s}\n", .{ ansi.blue, ansi.reset }); // --- MATERIALS --- var materials: SlotMap(Material) = .init(alloc); defer materials.deinit(); // --- ADD MATERIALS --- _ = try materials.put(.{ .style = .{ .albedo = .{ 0.8, 0.2, 0.2, 1.0 } } }); _ = try materials.put(.{ .unique = .{ .topology = .line_list } }); // --- # --- if (log.print(.debug, "MATERIALS", ansi.blue)) |a| { defer a.end(); // --- # --- for (materials.items()) |material| { if (a.section(.debug, "", ansi.cyan)) |b| { defer b.end(); // --- STYLE --- if (b.section(.debug, "STYLE", ansi.yellow)) |style| { defer style.end(); style.write("Albedo: rgba({}, {}, {}, {})", .{ material.style.albedo[0], material.style.albedo[1], material.style.albedo[2], material.style.albedo[3] }); style.write("Metallic: {}", .{material.style.metallic}); style.write("Roughness: {}", .{material.style.roughness}); } // --- STYLE --- if (b.section(.debug, "UNIQUE", ansi.red)) |unique| { defer unique.end(); unique.write("Shader: {s}", .{@tagName(material.unique.shader)}); unique.write("Topology: {s}", .{@tagName(material.unique.topology)}); unique.write("Cull Mode: {s}", .{@tagName(material.unique.cull_mode)}); } } } } } test "SlotMap-DEP" { // TODO: Yes... { // --- LOG --- log.warn("...", .{}, @src()); log.err("...", .{}, @src()); log.info("...", .{}, @src()); log.debug("...", .{}, @src()); log.trace("...", .{}, @src()); // --- PRINT --- if (log.print(.debug, "DEVICE INFO", ansi.bold ++ ansi.red)) |a| { defer a.end(); // --- VALUES --- a.write("device -> {s}", .{"GTX"}); a.write("vendor -> {s}", .{"Nvidia"}); // --- SECTION --- if (a.section(.err, "MEMORY", ansi.green)) |b| { defer b.end(); b.write("VRAM -> {}GB", .{24}); } } log.set(.{ .style = .rounded }); // --- PRINT --- if (log.print(.debug, "DEVICE INFO", ansi.bold ++ ansi.red)) |a| { defer a.end(); // --- VALUES --- a.write("device -> {s}", .{"GTX"}); a.write("vendor -> {s}", .{"Nvidia"}); // --- SECTION --- if (a.section(.err, "MEMORY", ansi.green)) |b| { defer b.end(); b.write("VRAM -> {}GB", .{24}); } } log.set(.{ .style = .pipe }); // --- PRINT --- if (log.print(.debug, "DEVICE INFO", ansi.bold ++ ansi.red)) |a| { defer a.end(); // --- VALUES --- a.write("device -> {s}", .{"GTX"}); a.write("vendor -> {s}", .{"Nvidia"}); // --- SECTION --- if (a.section(.err, "MEMORY", ansi.green)) |b| { defer b.end(); // --- VALUES --- b.write("VRAM -> {}GB", .{24}); } } } } test "RingBuffer" { std.debug.print("{s}━━━ RING BUFFER ━━━{s}\n", .{ ansi.blue, ansi.reset }); var prev_inputs: RingBuffer([]const u8, 256) = .empty; try prev_inputs.push("w"); try prev_inputs.push("s"); try prev_inputs.push("a"); try prev_inputs.push("d"); var i: usize = 0; while (prev_inputs.pop()) |data| { defer i += 1; std.debug.print("[{}][PreviousInputs] -> \"{s}\"\n", .{ i, data }); } } test "FixedArrayList" { std.debug.print("{s}━━━ FIXED ARRAY LIST ━━━{s}\n", .{ ansi.blue, ansi.reset }); var entities: FixedArrayList(u32, 4) = .empty; try entities.append(1); try entities.append(2); try entities.append(3); if (entities.getPtr(0)) |v| v.* = 69; for (entities.items()) |item| { std.debug.print("Entity: {}\n", .{item}); } } test "FixedSlotMap" { std.debug.print("{s}━━━ FIXED SLOT MAP ━━━{s}\n", .{ ansi.blue, ansi.reset }); var materials: FixedSlotMap(Material, 3) = .empty; _ = try materials.put(.{}); _ = try materials.put(.{}); const handle = try materials.put(.{ .unique = .{ .topology = .line_list } }); if (materials.get(handle)) |v| { std.debug.print("Material: {}\n", .{v.unique.topology}); } }