WIP flecs-zig-ble new system init

wip/new-system-init
copygirl 5 months ago
parent b1895bbef8
commit 8caf363d22
  1. 5
      .gitignore
  2. 4
      build.zig
  3. 41
      src/App.zig
  4. 21
      src/Renderer.zig
  5. 2
      src/primitives.zig

5
.gitignore vendored

@ -3,5 +3,6 @@
/zig-out/
# Dependencies (cloned manually)
/libs/flecs-zig-ble/
/libs/zig-gamedev/
# Could be symlinks, so not including trailing slash.
/libs/flecs-zig-ble
/libs/zig-gamedev

@ -18,7 +18,7 @@ pub fn build(b: *std.Build) !void {
const app = try mach.CoreApp.init(b, mach_dep.builder, .{
.name = "zig-bloxel-game",
.src = "src/main.zig",
.src = "src/App.zig",
.target = target,
.optimize = optimize,
.deps = &.{
@ -36,7 +36,7 @@ pub fn build(b: *std.Build) !void {
run_step.dependOn(&app.run.step);
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.root_source_file = .{ .path = "src/App.zig" },
.target = target,
.optimize = optimize,
});

@ -2,18 +2,15 @@ const std = @import("std");
const GeneralPurposeAllocator = std.heap.GeneralPurposeAllocator(.{});
const core = @import("mach").core;
const Renderer = @import("./renderer.zig");
const Renderer = @import("./Renderer.zig");
const flecszigble = @import("flecs-zig-ble");
const flecs = flecszigble.flecs;
const Context = flecszigble.Context(void);
const World = Context.World;
const Iter = Context.Iter;
const flecs = flecszigble.flecs;
const OnLoad = flecs.pipeline.OnLoad;
const OnUpdate = flecs.pipeline.OnUpdate;
const OnStore = flecs.pipeline.OnStore;
pub const App = @This();
gpa: GeneralPurposeAllocator,
@ -57,14 +54,10 @@ pub fn init(app: *App) !void {
app.world = world;
// Create a singleton component for accessing the `App` from ECS.
_ = try world.singleton("App", *App, app);
// TODO: The way we register systems using flecs-zig-ble is still very WIP.
_ = try world.system("PollEvents", pollEvents, OnLoad, "App");
_ = try world.singleton(*App, app);
const s = try world.system("UpdateWindowTitle", updateWindowTitle, OnStore, "");
// Set the update interval of the `UpdateWindowTitle` system to 1 second.
_ = flecszigble.c.ecs_set_interval(world.raw, s.raw, 1.0);
_ = try world.system(PollEvents);
_ = try world.system(UpdateWindowTitle);
app.renderer = try Renderer.init(app);
}
@ -84,30 +77,36 @@ pub fn update(app: *App) !bool {
return !app.world.progress(0.0);
}
/// Read events from the OS such as input.
pub fn pollEvents(it: Iter) void {
const app = it.field(*App, 1)[0];
/// System that reads events from the OS such as input.
pub const PollEvents = struct {
pub const phase = flecs.pipeline.OnLoad;
pub const expr = "App($)";
pub fn callback(world: *World, app: *const *App) void {
var pollIter = core.pollEvents();
while (pollIter.next()) |event| {
switch (event) {
// Allow the renderer to act on the window being resized.
// This is required so we can resize necessary buffers.
.framebuffer_resize => |_| app.renderer.resize(),
.framebuffer_resize => |_| app.*.renderer.resize(),
// Close the window when requested, such as when
// pressing the X button in the window title bar.
.close => it.world.quit(),
.close => world.quit(),
else => {},
}
}
}
};
/// Update the window title to show FPS and input frequency.
pub fn updateWindowTitle(_: Iter) void {
/// System that updates the window title to show FPS and input frequency.
pub const UpdateWindowTitle = struct {
pub const phase = flecs.pipeline.OnStore;
pub const interval = 1.0; // Run only once a second.
pub fn callback(_: Iter) void {
core.printTitle(
"Triangle [ {d}fps ] [ Input {d}hz ]",
.{ core.frameRate(), core.inputRate() },
) catch @panic("Title too long!");
}
};

@ -11,20 +11,19 @@ const zm = @import("zmath");
const vec = zm.f32x4;
const Mat = zm.Mat;
const App = @import("./main.zig");
const App = @import("./App.zig");
const primitives = @import("./primitives.zig");
const VertexData = primitives.VertexData;
const PrimitiveData = primitives.PrimitiveData;
const flecszigble = @import("flecs-zig-ble");
const flecs = flecszigble.flecs;
const Context = flecszigble.Context(void);
const Entity = Context.Entity;
const Iter = Context.Iter;
const flecs = flecszigble.flecs;
const OnStore = flecs.pipeline.OnStore;
const Transform = struct { value: Mat };
const CameraPerspective = struct {
/// Vertical field of view (in degrees).
@ -231,8 +230,8 @@ pub fn init(app: *App) !*Renderer {
}
// Register components necessary for the camera.
_ = try app.world.component("Transform", Transform);
_ = try app.world.component("CameraPerspective", CameraPerspective);
_ = try app.world.component(Transform);
_ = try app.world.component(CameraPerspective);
const camera_entity = try app.world.entity(
.{ .name = "Camera", .symbol = "Camera" },
@ -244,8 +243,7 @@ pub fn init(app: *App) !*Renderer {
.far_plane = 80.0,
});
const render_expr = "App, [in] CameraPerspective(Camera), [out] Transform(Camera)";
_ = try app.world.system("Render", render, OnStore, render_expr);
_ = try app.world.system(Render);
const result = try app.allocator.create(Renderer);
result.* = .{
@ -293,7 +291,11 @@ pub fn resize(self: *Renderer) void {
self.recreateDepthTexture();
}
pub fn render(it: Iter) void {
/// System which renders the game world from the camera entity's perspective.
pub const Render = struct {
pub const phase = flecs.pipeline.OnStore;
pub const expr = "App($), [in] CameraPerspective(Camera), [out] Transform(Camera)";
pub fn callback(it: Iter) void {
const app = it.field(*App, 1)[0];
const camera_perspective = it.field(CameraPerspective, 2)[0];
const camera_transform = &it.field(Transform, 3)[0];
@ -392,6 +394,7 @@ pub fn render(it: Iter) void {
// Submit the command(s) to the GPU.
core.queue.submit(&.{command});
}
};
/// Loads a texture from the provided buffer and uploads it to the GPU.
pub fn loadTexture(allocator: std.mem.Allocator, buffer: []const u8) !*gpu.TextureView {

@ -3,7 +3,7 @@ const tau = std.math.tau;
const gpu = @import("mach").core.gpu;
const Renderer = @import("./renderer.zig");
const Renderer = @import("./Renderer.zig");
const createAndWriteBuffer = Renderer.createAndWriteBuffer;
/// Describes the layout of each vertex that a primitive is made of.

Loading…
Cancel
Save