2026-07-27 22:40:15 +01:00
|
|
|
//! ----------------------------------------------------
|
|
|
|
|
//! ----------------------------------------------------
|
|
|
|
|
|
2026-08-01 22:44:49 +01:00
|
|
|
//
|
|
|
|
|
// PRIVATE
|
|
|
|
|
//
|
|
|
|
|
|
2026-07-09 22:30:04 +01:00
|
|
|
const std = @import("std");
|
2026-08-01 22:44:49 +01:00
|
|
|
const template = @import("template/root.zig");
|
|
|
|
|
const Self = @This();
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// PUBLIC
|
|
|
|
|
//
|
2026-08-01 18:54:24 +01:00
|
|
|
|
2026-07-09 22:30:04 +01:00
|
|
|
pub const event = @import("event/root.zig");
|
|
|
|
|
pub const plugin = @import("plugin/root.zig");
|
|
|
|
|
pub const resource = @import("resource/root.zig");
|
|
|
|
|
pub const scheduler = @import("scheduler/root.zig");
|
2026-08-01 18:54:24 +01:00
|
|
|
|
|
|
|
|
pub const Plugin = plugin.Plugin;
|
2026-08-01 22:44:49 +01:00
|
|
|
pub const AppExit = event.AppExit;
|
2026-07-09 22:30:04 +01:00
|
|
|
|
2026-07-10 16:36:30 +01:00
|
|
|
/// ----------------------------------------------------
|
|
|
|
|
/// Default app stages
|
|
|
|
|
/// ----------------------------------------------------
|
2026-07-09 22:30:04 +01:00
|
|
|
pub const stage = struct {
|
2026-07-14 15:48:44 +01:00
|
|
|
// --- LIFETIME ---
|
2026-07-10 16:36:30 +01:00
|
|
|
pub const init = struct {};
|
|
|
|
|
pub const deinit = struct {};
|
|
|
|
|
|
2026-07-14 15:48:44 +01:00
|
|
|
// --- FRAME ---
|
2026-07-12 20:04:35 +01:00
|
|
|
pub const begin_frame = struct {};
|
|
|
|
|
pub const end_frame = struct {};
|
|
|
|
|
|
2026-07-26 12:47:49 +00:00
|
|
|
// --- CORE ---
|
2026-07-10 16:36:30 +01:00
|
|
|
pub const inputs = struct {};
|
|
|
|
|
pub const fixed_update = struct {};
|
|
|
|
|
pub const update = struct {};
|
|
|
|
|
pub const draw = struct {};
|
2026-07-09 22:30:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// FIELDS
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
events: event.Storage,
|
|
|
|
|
plugins: plugin.Storage,
|
|
|
|
|
resources: resource.Storage,
|
|
|
|
|
schedules: scheduler.Storage,
|
|
|
|
|
alloc: std.mem.Allocator,
|
|
|
|
|
|
|
|
|
|
/// ----------------------------------------------------
|
|
|
|
|
/// ----------------------------------------------------
|
|
|
|
|
pub fn init(alloc: std.mem.Allocator) Self {
|
|
|
|
|
return .{
|
|
|
|
|
.events = .init(alloc),
|
|
|
|
|
.plugins = .init(alloc),
|
|
|
|
|
.resources = .init(alloc),
|
|
|
|
|
.schedules = .init(alloc),
|
|
|
|
|
.alloc = alloc,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// ----------------------------------------------------
|
|
|
|
|
/// ----------------------------------------------------
|
|
|
|
|
pub fn deinit(self: *Self) void {
|
|
|
|
|
self.events.deinit();
|
|
|
|
|
self.plugins.deinit();
|
|
|
|
|
self.resources.deinit();
|
|
|
|
|
self.schedules.deinit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2026-08-01 22:44:49 +01:00
|
|
|
// TESTS
|
2026-07-09 22:30:04 +01:00
|
|
|
//
|
|
|
|
|
|
2026-08-01 22:44:49 +01:00
|
|
|
const Window = struct { raw: u32 };
|
|
|
|
|
const Renderer = struct { raw: u32 };
|
2026-07-09 22:30:04 +01:00
|
|
|
|
2026-08-01 22:44:49 +01:00
|
|
|
const window_plugin: Plugin = .{
|
|
|
|
|
.name = "Window",
|
|
|
|
|
.description = "SDL3 Window",
|
|
|
|
|
.build = build,
|
|
|
|
|
};
|
2026-07-09 22:30:04 +01:00
|
|
|
|
2026-08-01 22:44:49 +01:00
|
|
|
fn ensureStuff(app: *Self) !void {
|
|
|
|
|
try app.resources.set(Window{ .raw = 0 });
|
|
|
|
|
try app.resources.set(Renderer{ .raw = 0 });
|
2026-07-10 22:41:36 +01:00
|
|
|
|
2026-08-01 22:44:49 +01:00
|
|
|
if (app.resources.getPtr(Window)) |win| {
|
|
|
|
|
std.debug.print("[WINDOW] {}\n", .{win.raw});
|
2026-07-10 22:41:36 +01:00
|
|
|
}
|
|
|
|
|
|
2026-08-01 22:44:49 +01:00
|
|
|
const res = try app.resources.getMany(struct {
|
|
|
|
|
window: *Window,
|
|
|
|
|
renderer: ?*Renderer,
|
|
|
|
|
});
|
2026-07-09 22:30:04 +01:00
|
|
|
|
2026-08-01 22:44:49 +01:00
|
|
|
if (res.renderer) |ren| {
|
|
|
|
|
std.debug.print("[RENDERER] {}\n", .{ren.raw});
|
2026-07-09 22:30:04 +01:00
|
|
|
}
|
2026-07-10 22:27:43 +01:00
|
|
|
|
2026-08-01 22:44:49 +01:00
|
|
|
try app.events.send(
|
|
|
|
|
AppExit{
|
|
|
|
|
.reason = "Why Not?",
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (app.events.consume(AppExit)) |app_exit| {
|
|
|
|
|
std.debug.print("[APP_EXIT] {s}\n", .{app_exit.reason});
|
2026-07-09 22:30:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-08-01 22:44:49 +01:00
|
|
|
|
|
|
|
|
fn build(
|
|
|
|
|
self: *Plugin,
|
|
|
|
|
app: *Self,
|
|
|
|
|
) !void {
|
|
|
|
|
std.debug.print(
|
|
|
|
|
"[WINDOW_PLUGIN] {s} - {s}\n",
|
|
|
|
|
.{
|
|
|
|
|
self.name,
|
|
|
|
|
self.description,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
try app.schedules.addMany(stage.init, &.{
|
|
|
|
|
ensureStuff,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test "main" {
|
|
|
|
|
// --- APP ---
|
|
|
|
|
var app: Self = .init(std.testing.allocator);
|
|
|
|
|
defer app.deinit();
|
|
|
|
|
|
|
|
|
|
// --- ADD PLUGINS ---
|
|
|
|
|
try app.plugins.addMany(&.{
|
|
|
|
|
window_plugin,
|
|
|
|
|
});
|
|
|
|
|
try app.plugins.build(&app);
|
|
|
|
|
|
|
|
|
|
// --- RUN SYSTEMS ---
|
|
|
|
|
try app.schedules.runMany(
|
|
|
|
|
&.{
|
|
|
|
|
stage.init,
|
|
|
|
|
stage.deinit,
|
|
|
|
|
},
|
|
|
|
|
&app,
|
|
|
|
|
);
|
|
|
|
|
}
|