diff --git a/build.zig b/build.zig index 79b1194..a827521 100644 --- a/build.zig +++ b/build.zig @@ -30,8 +30,9 @@ pub fn build(b: *std.Build) void { .optimize = optimize, .link_libc = true, }); + renderer.addIncludePath(b.path("ext")); renderer.addCSourceFile(.{ .file = b.path("ext/stb_image.c") }); - renderer.addImport("sideros", sideros); + //renderer.addImport("sideros", sideros); renderer.addImport("math", math); renderer.addImport("ecs", ecs); // TODO(ernesto): ecs and renderer should be decoupled @@ -39,11 +40,13 @@ pub fn build(b: *std.Build) void { compileAllShaders(b, renderer); - const sideros = b.addStaticLibrary(.{ + const sideros = b.addLibrary(.{ .name = "sideros", - .root_source_file = b.path("src/sideros.zig"), - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("src/sideros.zig"), + .target = target, + .optimize = optimize, + }), }); sideros.addIncludePath(b.path("ext")); sideros.addIncludePath(b.path("src")); diff --git a/src/ecs/entities.zig b/src/ecs/entities.zig index a848108..df28ae3 100644 --- a/src/ecs/entities.zig +++ b/src/ecs/entities.zig @@ -26,7 +26,7 @@ pub const Human = struct { // TODO(ernesto): Move pool to its own file pub const Pool = struct { humans: std.MultiArrayList(Human), - resources: *Resources, + resources: Resources, allocator: Allocator, system_groups: std.ArrayList(SystemGroup), sync_groups: std.ArrayList(SyncGroup), @@ -34,7 +34,7 @@ pub const Pool = struct { wait_group: std.Thread.WaitGroup, mutex: std.Thread.Mutex, - pub fn init(allocator: Allocator, resources: *Resources) !@This() { + pub fn init(allocator: Allocator, resources: Resources) !@This() { var pool = @This(){ .humans = .{}, .resources = resources, diff --git a/src/main.zig b/src/main.zig deleted file mode 100644 index 6b33a1c..0000000 --- a/src/main.zig +++ /dev/null @@ -1,68 +0,0 @@ -const std = @import("std"); -const config = @import("sideros").config; -const math = @import("sideros").math; -const Input = @import("sideros").Input; -const mods = @import("sideros").mods; -const ecs = @import("sideros").ecs; -const builtin = @import("builtin"); -const Renderer = @import("sideros").Renderer; - -const platform = if (builtin.target.os.tag == .linux) (if (config.wayland) @import("wayland.zig") else @import("xorg.zig")) else @import("xorg.zig"); - -//fn testSystem2(pool: *ecs.Pool) void { -// std.debug.print("{any}\n", .{pool.resources.input.isKeyDown(.a)}); -//} - -pub fn main() !void { - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - const allocator = gpa.allocator(); - defer if (gpa.deinit() != .ok) @panic("Leaked memory"); - - // var global_runtime = mods.GlobalRuntime.init(allocator); - // defer global_runtime.deinit(); - // try global_runtime.addFunction("debug", mods.Wasm.debug); - - // // const file = try std.fs.cwd().openFile("assets/core.wasm", .{}); - // const file = try std.fs.cwd().openFile("./test.wasm", .{}); - // const all = try file.readToEndAlloc(allocator, 1_000_000); // 1 MB - // defer allocator.free(all); - // var parser = try mods.Parser.init(allocator, all); - // defer parser.deinit(); - // parser.parseModule() catch |err| { - // std.debug.print("[ERROR]: error at byte {x}(0x{x})\n", .{ parser.byte_idx, parser.bytes[parser.byte_idx] }); - // return err; - // }; - // const module = parser.module(); - // defer module.deinit(allocator); - - // for (0..parser.globalTypes.len) |i| { - // try global_runtime.addGlobal(@intCast(i), parser.globalTypes[i], parser.globalValues[i]); - // } - - // var runtime = try mods.Runtime.init(allocator, module, &global_runtime); - // defer runtime.deinit(allocator); - - // var parameters = [_]mods.VM.Value{.{ .i32 = 17 }}; - // try runtime.callExternal(allocator, .preinit, ¶meters); - // const result = runtime.stack.pop().?; - // std.debug.print("Result of preinit: {any}\n", .{result}); - - //var w = try Renderer.Window.create(800, 600, "sideros"); - //defer w.destroy(); - - var resources = ecs.Resources{ - .camera = .{ - .position = .{30.0, 30.0, 30.0}, - .target = .{0.0, 0.0, 0.0}, - }, - .renderer = undefined, - .input = .{ .key_pressed = .{false} ** @intFromEnum(Input.KeyCode.menu) }, - }; - var pool = try ecs.Pool.init(allocator, &resources); - defer pool.deinit(); - try pool.addSystemGroup(&[_]ecs.System{ - Renderer.render, - }, true); - - try platform.init(allocator, &pool); -} diff --git a/src/renderer/Mesh.zig b/src/renderer/Mesh.zig index 3c082e1..fd0974b 100644 --- a/src/renderer/Mesh.zig +++ b/src/renderer/Mesh.zig @@ -2,6 +2,7 @@ const std = @import("std"); const vk = @import("vulkan.zig"); const gltf = @import("gltf.zig"); const Allocator = std.mem.Allocator; +const c = vk.c; const Mesh = @This(); diff --git a/src/renderer/Texture.zig b/src/renderer/Texture.zig index b4bb5da..9002c51 100644 --- a/src/renderer/Texture.zig +++ b/src/renderer/Texture.zig @@ -1,6 +1,9 @@ const Texture = @This(); -const c = @import("sideros").c; const vk = @import("vulkan.zig"); +const c = vk.c; +pub const stb = @cImport({ + @cInclude("stb_image.h"); +}); image: c.VkImage, image_memory: c.VkDeviceMemory, @@ -11,8 +14,8 @@ pub fn init(path: [:0]const u8, device: anytype) !Texture { var height: i32 = 0; var channels: i32 = 0; - const pixels = c.stbi_load(path, &width, &height, &channels, c.STBI_rgb_alpha); - defer c.stbi_image_free(pixels); + const pixels = stb.stbi_load(path, &width, &height, &channels, stb.STBI_rgb_alpha); + defer stb.stbi_image_free(pixels); const size: c.VkDeviceSize = @as(u64, @intCast(width)) * @as(u64, @intCast(height)) * 4; const image_buffer = try device.createBuffer(vk.BufferUsage{ .transfer_src = true }, vk.BufferFlags{ .host_visible = true, .host_coherent = true }, size); diff --git a/src/sideros.zig b/src/sideros.zig index ed4ba5a..44f334b 100644 --- a/src/sideros.zig +++ b/src/sideros.zig @@ -12,10 +12,10 @@ const allocator = gpa.allocator(); var pool: ecs.Pool = undefined; var renderer: Renderer = undefined; -export fn sideros_init(init: api.GameInit) callconv(.C) void { +export fn sideros_init(init: api.GameInit) callconv(.c) void { pool = ecs.Pool.init(allocator, .{ .camera = .{ - .position = .{ 0.0, 0.0, 1.0 }, + .position = .{ 30.0, 30.0, 30.0 }, .target = .{ 0.0, 0.0, 0.0 }, }, .renderer = undefined, @@ -24,16 +24,16 @@ export fn sideros_init(init: api.GameInit) callconv(.C) void { // 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.resources.renderer = &renderer; pool.tick(); } -export fn sideros_update(gameUpdate: api.GameUpdate) callconv(.C) void { +export fn sideros_update(gameUpdate: api.GameUpdate) callconv(.c) void { _ = gameUpdate; pool.tick(); } -export fn sideros_cleanup() callconv(.C) void { +export fn sideros_cleanup() callconv(.c) void { renderer.deinit(); pool.deinit(); if (gpa.deinit() != .ok) @panic("Memory leaked"); diff --git a/src/sideros_api.h b/src/sideros_api.h index 7319e07..c07091e 100644 --- a/src/sideros_api.h +++ b/src/sideros_api.h @@ -6,6 +6,7 @@ typedef struct { } GameInit; typedef struct { + double dt; } GameUpdate; void sideros_init(GameInit init); diff --git a/src/wayland.zig b/src/wayland.zig index b75eaba..c1dc1a4 100644 --- a/src/wayland.zig +++ b/src/wayland.zig @@ -190,11 +190,10 @@ fn toplevelHandleConfigure(data: ?*anyopaque, toplevel: ?*c.xdg_toplevel, width: } fn toplevelHandleClose(data: ?*anyopaque, toplevel: ?*c.xdg_toplevel) callconv(.c) void { - const state: *State = @alignCast(@ptrCast(data)); + _ = date; _ = toplevel; quit = true; - state.pool.resources.renderer.deinit(); } fn toplevelHandleConfigureBounds(data: ?*anyopaque, toplevel: ?*c.xdg_toplevel, width: i32, height: i32) callconv(.c) void {