2026-08-02 13:23:12 +01:00
|
|
|
//! ----------------------------------------------------
|
|
|
|
|
//! # Game Template Library
|
|
|
|
|
//!
|
|
|
|
|
//! Low *cortisol* library for games and game engines
|
|
|
|
|
//!
|
|
|
|
|
//! # Features
|
|
|
|
|
//!
|
|
|
|
|
//! **dynamic**
|
|
|
|
|
//! | SlotMap
|
|
|
|
|
//! | KeyedSlotMap
|
|
|
|
|
//! | Handle(T) **Generational**
|
|
|
|
|
//!
|
|
|
|
|
//! **fixed**
|
|
|
|
|
//! | FixedArrayList
|
|
|
|
|
//! | FixedSlotMap
|
|
|
|
|
//! | RingBuffer
|
|
|
|
|
//! ----------------------------------------------------
|
|
|
|
|
|
2026-07-12 19:46:40 +01:00
|
|
|
//
|
|
|
|
|
// 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;
|
2026-08-01 18:18:53 +01:00
|
|
|
pub const TypeID = @import("type_id.zig").of;
|
|
|
|
|
|
|
|
|
|
// --- ARRAYS ---
|
2026-07-12 19:46:40 +01:00
|
|
|
pub const SlotMap = @import("slot_map.zig").SlotMap;
|
2026-07-12 21:22:53 +01:00
|
|
|
pub const KeyedSlotMap = @import("keyed_slot_map.zig").KeyedSlotMap;
|
2026-08-01 18:18:53 +01:00
|
|
|
pub const FixedArrayList = @import("fixed_array_list.zig").FixedArrayList;
|
|
|
|
|
pub const FixedSlotMap = @import("fixed_slot_map.zig").FixedSlotMap;
|
2026-07-13 16:41:14 +01:00
|
|
|
pub const RingBuffer = @import("ring_buffer.zig").RingBuffer;
|
2026-07-12 19:46:40 +01:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// STRUCTS
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
// --- SHADER ---
|
2026-08-01 16:58:58 +01:00
|
|
|
const ShaderID = union(enum) { pbr, unlit, custom: Handle(ShaderID) };
|
2026-07-12 19:46:40 +01:00
|
|
|
const GPUShader = struct { raw: *anyopaque };
|
|
|
|
|
|
|
|
|
|
// --- PIPELINE ---
|
|
|
|
|
const PipelineKey = struct { shader: ShaderID };
|
|
|
|
|
const GPUPipeline = struct { raw: *anyopaque };
|
|
|
|
|
|
2026-08-01 16:58:58 +01:00
|
|
|
/// ----------------------------------------------------
|
|
|
|
|
/// ----------------------------------------------------
|
|
|
|
|
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,
|
|
|
|
|
};
|
2026-07-12 19:46:40 +01:00
|
|
|
|
|
|
|
|
//
|
2026-08-01 16:58:58 +01:00
|
|
|
// FIELDS
|
2026-07-12 19:46:40 +01:00
|
|
|
//
|
|
|
|
|
|
2026-08-01 16:58:58 +01:00
|
|
|
style: Style = .{},
|
|
|
|
|
unique: Unique = .{},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
test "SlotMap" {
|
|
|
|
|
// --- ALLOCATOR ---
|
|
|
|
|
const alloc = std.testing.allocator;
|
|
|
|
|
std.debug.print("{s}━━━ SLOTMAP ━━━{s}\n", .{ ansi.blue, ansi.reset });
|
|
|
|
|
|
2026-07-12 19:46:40 +01:00
|
|
|
// --- MATERIALS ---
|
2026-08-01 16:58:58 +01:00
|
|
|
var materials: SlotMap(Material) = .init(alloc);
|
|
|
|
|
defer materials.deinit();
|
2026-07-12 19:46:40 +01:00
|
|
|
|
2026-08-01 16:58:58 +01:00
|
|
|
// --- ADD MATERIALS ---
|
|
|
|
|
_ = try materials.put(.{ .style = .{ .albedo = .{ 0.8, 0.2, 0.2, 1.0 } } });
|
|
|
|
|
_ = try materials.put(.{ .unique = .{ .topology = .line_list } });
|
2026-08-03 16:30:26 +01:00
|
|
|
const c = try materials.put(.{ .unique = .{ .topology = .line_list } });
|
|
|
|
|
_ = try materials.put(.{ .unique = .{ .topology = .line_list } });
|
|
|
|
|
|
|
|
|
|
try materials.remove(c);
|
2026-07-12 19:46:40 +01:00
|
|
|
|
2026-08-01 16:58:58 +01:00
|
|
|
// --- # ---
|
|
|
|
|
if (log.print(.debug, "MATERIALS", ansi.blue)) |a| {
|
|
|
|
|
defer a.end();
|
2026-07-12 19:46:40 +01:00
|
|
|
|
2026-08-01 16:58:58 +01:00
|
|
|
// --- # ---
|
2026-08-03 16:30:26 +01:00
|
|
|
for (materials.values.items) |material| {
|
2026-08-01 16:58:58 +01:00
|
|
|
if (a.section(.debug, "", ansi.cyan)) |b| {
|
|
|
|
|
defer b.end();
|
2026-07-12 19:46:40 +01:00
|
|
|
|
2026-08-01 16:58:58 +01:00
|
|
|
// --- STYLE ---
|
|
|
|
|
if (b.section(.debug, "STYLE", ansi.yellow)) |style| {
|
|
|
|
|
defer style.end();
|
2026-07-12 19:46:40 +01:00
|
|
|
|
2026-08-01 16:58:58 +01:00
|
|
|
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});
|
|
|
|
|
}
|
2026-07-12 19:46:40 +01:00
|
|
|
|
2026-08-01 16:58:58 +01:00
|
|
|
// --- STYLE ---
|
|
|
|
|
if (b.section(.debug, "UNIQUE", ansi.red)) |unique| {
|
|
|
|
|
defer unique.end();
|
2026-07-12 19:46:40 +01:00
|
|
|
|
2026-08-01 16:58:58 +01:00
|
|
|
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)});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-12 19:46:40 +01:00
|
|
|
}
|
2026-08-01 16:58:58 +01:00
|
|
|
}
|
2026-07-12 19:46:40 +01:00
|
|
|
|
2026-08-03 16:30:26 +01:00
|
|
|
test "KeyedSlotMap" {
|
|
|
|
|
std.debug.print("{s}━━━ KEYED SLOTMAP ━━━{s}\n", .{ ansi.blue, ansi.reset });
|
2026-07-31 23:57:45 +01:00
|
|
|
|
2026-08-03 16:30:26 +01:00
|
|
|
// --- # ---
|
|
|
|
|
var pipelines: KeyedSlotMap(Material.Unique, u32) = .init(std.testing.allocator);
|
|
|
|
|
defer pipelines.deinit();
|
|
|
|
|
|
|
|
|
|
// --- ADD PIPELINES ---
|
|
|
|
|
_ = try pipelines.put(.{}, 0);
|
|
|
|
|
_ = try pipelines.put(.{ .topology = .line_list }, 0);
|
|
|
|
|
|
|
|
|
|
// --- ITERATOR ---
|
|
|
|
|
var it = pipelines.iterator();
|
|
|
|
|
while (it.next()) |entry| {
|
|
|
|
|
std.debug.print("[{}] {}\n", .{
|
|
|
|
|
entry.value,
|
|
|
|
|
entry.key.topology,
|
|
|
|
|
});
|
2026-07-12 19:46:40 +01:00
|
|
|
}
|
2026-08-01 18:18:53 +01:00
|
|
|
}
|
2026-07-13 16:41:14 +01:00
|
|
|
|
2026-08-01 18:18:53 +01:00
|
|
|
test "RingBuffer" {
|
|
|
|
|
std.debug.print("{s}━━━ RING BUFFER ━━━{s}\n", .{ ansi.blue, ansi.reset });
|
2026-08-01 16:58:58 +01:00
|
|
|
|
2026-08-02 13:23:12 +01:00
|
|
|
var prev_inputs: RingBuffer([]const u8, 256) = .empty;
|
2026-07-13 16:41:14 +01:00
|
|
|
|
2026-08-01 16:58:58 +01:00
|
|
|
try prev_inputs.push("w");
|
|
|
|
|
try prev_inputs.push("s");
|
|
|
|
|
try prev_inputs.push("a");
|
|
|
|
|
try prev_inputs.push("d");
|
2026-07-13 16:41:14 +01:00
|
|
|
|
|
|
|
|
var i: usize = 0;
|
2026-08-01 16:58:58 +01:00
|
|
|
while (prev_inputs.pop()) |data| {
|
|
|
|
|
defer i += 1;
|
2026-08-01 18:18:53 +01:00
|
|
|
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});
|
2026-07-13 16:41:14 +01:00
|
|
|
}
|
2026-07-12 19:46:40 +01:00
|
|
|
}
|