Refactored compilation and startup. Now everything is orchestrated through a simple API described in sideros_api.h. Also refactored some of the code to get rid of global C imports.

Signed-off-by: Lorenzo Torres <torres@sideros.org>
This commit is contained in:
Ernesto Lanchares 2025-08-05 18:05:17 +00:00 committed by Lorenzo Torres
parent 5b51a3d571
commit b1bd949db5
12 changed files with 420 additions and 212 deletions

View file

@ -1,7 +1,40 @@
pub const math = @import("math.zig");
pub const Input = @import("Input.zig");
pub const mods = @import("mods");
pub const ecs = @import("ecs");
pub const Renderer = @import("renderer");
pub const config = @import("config");
pub const c = @import("c.zig").c;
const api = @cImport({
@cInclude("sideros_api.h");
});
const std = @import("std");
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
var pool: ecs.Pool = undefined;
var renderer: Renderer = undefined;
export fn sideros_init(init: api.GameInit) callconv(.C) void {
pool = ecs.Pool.init(allocator, .{
.camera = .{
.position = .{ 0.0, 0.0, 1.0 },
.target = .{ 0.0, 0.0, 0.0 },
},
.renderer = undefined,
.input = .{ .key_pressed = .{false} ** @intFromEnum(ecs.Input.KeyCode.menu) },
}) catch @panic("TODO: Gracefully handle error");
// TODO(ernesto): I think this @ptrCast are unavoidable but maybe not?
renderer = Renderer.init(allocator, @ptrCast(init.instance), @ptrCast(init.surface)) catch @panic("TODO: Gracefully handle error");
pool.addSystemGroup(&[_]ecs.System{Renderer.render}, true) catch @panic("TODO: Gracefuly handle error");
pool.resources.renderer = renderer;
pool.tick();
}
export fn sideros_update(gameUpdate: api.GameUpdate) callconv(.C) void {
_ = gameUpdate;
pool.tick();
}
export fn sideros_cleanup() callconv(.C) void {
renderer.deinit();
pool.deinit();
if (gpa.deinit() != .ok) @panic("Memory leaked");
}