Compare commits
No commits in common. "d1c823df41e5597ec81d55c3a8d589b3d46dc9fc" and "bb1d383ccb9d355aa6c227c1b534e214e3211fdf" have entirely different histories.
d1c823df41
...
bb1d383ccb
|
@ -5,7 +5,7 @@
|
||||||
"name": "Runner",
|
"name": "Runner",
|
||||||
"type": "gdb",
|
"type": "gdb",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"target": "${workspaceRoot}/demos/canvas.out",
|
"target": "${workspaceRoot}/demos/effects.out",
|
||||||
"cwd": "${workspaceRoot}/demos/",
|
"cwd": "${workspaceRoot}/demos/",
|
||||||
"valuesFormatting": "prettyPrinters",
|
"valuesFormatting": "prettyPrinters",
|
||||||
"preLaunchTask": "Build All"
|
"preLaunchTask": "Build All"
|
||||||
|
|
14
build.zig
14
build.zig
|
@ -254,20 +254,6 @@ pub fn build(b: *std.Build) !void {
|
||||||
|
|
||||||
gfx_module.link_libc = true;
|
gfx_module.link_libc = true;
|
||||||
|
|
||||||
_ = try project.add_module(b, "gui", .{
|
|
||||||
.imports = &.{
|
|
||||||
.{
|
|
||||||
.name = "ona",
|
|
||||||
.module = ona_module,
|
|
||||||
},
|
|
||||||
|
|
||||||
.{
|
|
||||||
.name = "gfx",
|
|
||||||
.module = gfx_module,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
project.find_demos(b.step("demos", "Build demos"));
|
project.find_demos(b.step("demos", "Build demos"));
|
||||||
project.find_tests(b.step("tests", "Build and run tests"));
|
project.find_tests(b.step("tests", "Build and run tests"));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,44 +0,0 @@
|
||||||
const gfx = @import("gfx");
|
|
||||||
|
|
||||||
const gui = @import("gui");
|
|
||||||
|
|
||||||
const hid = @import("hid");
|
|
||||||
|
|
||||||
const ona = @import("ona");
|
|
||||||
|
|
||||||
pub const main = ona.App.game
|
|
||||||
.with_module(gfx)
|
|
||||||
.with_module(hid)
|
|
||||||
.with_module(gui)
|
|
||||||
.with_system(.load, ona.system_fn(load), .{ .label = "Hello Ona" }).build();
|
|
||||||
|
|
||||||
fn load(canvas: ona.Write(gui.Canvas)) !void {
|
|
||||||
const item = try canvas.state.append("", .{
|
|
||||||
.x = 0,
|
|
||||||
.y = 0,
|
|
||||||
.width = 256,
|
|
||||||
.height = 256,
|
|
||||||
});
|
|
||||||
|
|
||||||
try canvas.state.set_block(item, .{
|
|
||||||
.has_color = gfx.colors.purple,
|
|
||||||
});
|
|
||||||
|
|
||||||
try canvas.state.set_label(item, .{
|
|
||||||
.has_text = "Hello, world",
|
|
||||||
});
|
|
||||||
|
|
||||||
const item2 = try canvas.state.append("", .{
|
|
||||||
.x = 128,
|
|
||||||
.y = 128,
|
|
||||||
.width = 256,
|
|
||||||
.height = 256,
|
|
||||||
});
|
|
||||||
|
|
||||||
try canvas.state.set_block(item2, .{
|
|
||||||
.has_color = .{0, 1, 0, 1},
|
|
||||||
});
|
|
||||||
|
|
||||||
canvas.state.set_parent(item2, item);
|
|
||||||
}
|
|
||||||
|
|
|
@ -56,23 +56,23 @@ fn load(display: ona.Write(gfx.Display), effects: ona.Write(Effects), assets: on
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const main = ona.App.game
|
pub const main = ona.App.setup.
|
||||||
.with_module(gfx)
|
with_module(gfx).
|
||||||
.with_state(Effects{})
|
with_state(Effects{}).
|
||||||
.with_system(.load, ona.system_fn(load), .{.label = "load effects"})
|
with_system(.load, ona.system_fn(load), .{.label = "load effects"}).
|
||||||
.with_system(.update, ona.system_fn(update), .{.label = "update effects"})
|
with_system(.update, ona.system_fn(update), .{.label = "update effects"}).
|
||||||
.with_system(.render, ona.system_fn(render), .{.label = "render effects"}).build();
|
with_system(.render, ona.system_fn(render), .{.label = "render effects"}).build();
|
||||||
|
|
||||||
fn update(effects: ona.Write(Effects), loop: ona.Read(ona.Loop)) void {
|
fn update(effects: ona.Write(Effects), app: ona.Read(ona.App)) void {
|
||||||
const update_seconds = 5;
|
const update_seconds = 5;
|
||||||
|
|
||||||
if ((loop.state.elapsed_time - effects.state.last_time) > update_seconds) {
|
if ((app.state.elapsed_time - effects.state.last_time) > update_seconds) {
|
||||||
effects.state.image_index = (effects.state.image_index + 1) % effects.state.image_textures.len;
|
effects.state.image_index = (effects.state.image_index + 1) % effects.state.image_textures.len;
|
||||||
effects.state.last_time = loop.state.elapsed_time;
|
effects.state.last_time = app.state.elapsed_time;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render(commands: gfx.Commands, effects: ona.Write(Effects), loop: ona.Read(ona.Loop), display: ona.Write(gfx.Display)) !void {
|
fn render(commands: gfx.Commands, effects: ona.Write(Effects), app: ona.Read(ona.App), display: ona.Write(gfx.Display)) !void {
|
||||||
try commands.set_target(.{
|
try commands.set_target(.{
|
||||||
.texture = effects.state.render_texture,
|
.texture = effects.state.render_texture,
|
||||||
.clear_color = gfx.colors.black,
|
.clear_color = gfx.colors.black,
|
||||||
|
@ -80,21 +80,26 @@ fn render(commands: gfx.Commands, effects: ona.Write(Effects), loop: ona.Read(on
|
||||||
.clear_stencil = 0,
|
.clear_stencil = 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
const width: f32 = @floatFromInt(display.state.width);
|
const display_width: f32 = @floatFromInt(display.state.width);
|
||||||
const height: f32 = @floatFromInt(display.state.height);
|
const display_height: f32 = @floatFromInt(display.state.height);
|
||||||
|
|
||||||
|
const display_transform = gfx.transform_2d(.{
|
||||||
|
.translation = .{display_width / 2, display_height / 2},
|
||||||
|
});
|
||||||
|
|
||||||
try commands.draw_texture(.{
|
try commands.draw_texture(.{
|
||||||
.texture = effects.state.image_textures[effects.state.image_index],
|
.texture = effects.state.image_textures[effects.state.image_index],
|
||||||
.size = .{width, height},
|
.transform = display_transform,
|
||||||
|
.resolution = .{display.state.width, display.state.height},
|
||||||
});
|
});
|
||||||
|
|
||||||
try commands.set_effect(.{
|
try commands.set_effect(.{
|
||||||
.effect = effects.state.crt_effect,
|
.effect = effects.state.crt_effect,
|
||||||
|
|
||||||
.properties = std.mem.asBytes(&CRT{
|
.properties = std.mem.asBytes(&CRT{
|
||||||
.width = width,
|
.width = display_width,
|
||||||
.height = height,
|
.height = display_height,
|
||||||
.time = @floatCast(loop.state.elapsed_time),
|
.time = @floatCast(app.state.elapsed_time),
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -106,7 +111,7 @@ fn render(commands: gfx.Commands, effects: ona.Write(Effects), loop: ona.Read(on
|
||||||
|
|
||||||
try commands.draw_texture(.{
|
try commands.draw_texture(.{
|
||||||
.texture = effects.state.render_texture,
|
.texture = effects.state.render_texture,
|
||||||
.size = .{width, height},
|
.transform = display_transform,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
115
demos/inputs.zig
115
demos/inputs.zig
|
@ -6,68 +6,80 @@ const ona = @import("ona");
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
|
const Spawned = struct {
|
||||||
|
visual: struct {
|
||||||
|
color: gfx.Color,
|
||||||
|
transform: gfx.Transform2D,
|
||||||
|
},
|
||||||
|
|
||||||
|
lifetime_seconds: f32,
|
||||||
|
};
|
||||||
|
|
||||||
const Visuals = struct {
|
const Visuals = struct {
|
||||||
spawned: SpawnMap = SpawnMap.init(ona.heap.allocator),
|
spawned: ona.stack.Parallel(Spawned) = .{},
|
||||||
random: std.Random.Xoroshiro128,
|
random: std.Random.Xoroshiro128,
|
||||||
mouse_position: @Vector(2, f32) = @splat(0),
|
mouse_position: @Vector(2, f32) = @splat(0),
|
||||||
|
|
||||||
const SpawnMap = ona.SlotMap(struct {
|
|
||||||
color: gfx.Color,
|
|
||||||
position: gfx.Vector2,
|
|
||||||
rotation: f32,
|
|
||||||
lifetime_seconds: f32,
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
fn cleanup(visuals: ona.Write(Visuals)) !void {
|
fn cleanup(visuals: ona.Write(Visuals)) !void {
|
||||||
visuals.state.spawned.deinit();
|
visuals.state.spawned.deinit();
|
||||||
|
visuals.state.spawned.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const main = ona.App.game
|
fn load(visuals: ona.Write(Visuals)) !void {
|
||||||
.with_module(gfx)
|
const initial_spawn_capacity = 1024;
|
||||||
.with_module(hid)
|
|
||||||
.with_state(Visuals{.random = std.Random.Xoroshiro128.init(47342563891212)})
|
try visuals.state.spawned.grow(initial_spawn_capacity);
|
||||||
.with_system(.update, ona.system_fn(update), .{.label = "spawn visuals"})
|
}
|
||||||
.with_system(.render, ona.system_fn(render), .{.label = "render visuals"})
|
|
||||||
.with_system(.exit, ona.system_fn(cleanup), .{.label = "clean up visuals"}).build();
|
pub const main = ona.App.setup.
|
||||||
|
with_module(gfx).
|
||||||
|
with_module(hid).
|
||||||
|
with_state(Visuals{.random = std.Random.Xoroshiro128.init(47342563891212)}).
|
||||||
|
with_system(.load, ona.system_fn(load), .{.label = "load visuals"}).
|
||||||
|
with_system(.update, ona.system_fn(update), .{.label = "spawn visuals"}).
|
||||||
|
with_system(.render, ona.system_fn(render), .{.label = "render visuals"}).
|
||||||
|
with_system(.exit, ona.system_fn(cleanup), .{.label = "clean up visuals"}).build();
|
||||||
|
|
||||||
fn update(visuals: ona.Write(Visuals), events: ona.Receive(hid.Event), display: ona.Read(gfx.Display)) !void {
|
fn update(visuals: ona.Write(Visuals), events: ona.Receive(hid.Event), display: ona.Read(gfx.Display)) !void {
|
||||||
const float_speed = 6;
|
update_spawned(&visuals.state.spawned);
|
||||||
|
|
||||||
for (0 .. visuals.state.spawned.next) |i| {
|
|
||||||
const spawned = visuals.state.spawned.get(i) orelse {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
spawned.lifetime_seconds -= 1.0 / 60.0;
|
|
||||||
spawned.position -= .{0, float_speed};
|
|
||||||
|
|
||||||
if (spawned.lifetime_seconds <= 0) {
|
|
||||||
std.debug.assert(visuals.state.spawned.remove(i) != null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const random = visuals.state.random.random();
|
const random = visuals.state.random.random();
|
||||||
const width: f32 = @floatFromInt(display.state.width);
|
const width: f32 = @floatFromInt(display.state.width);
|
||||||
const height: f32 = @floatFromInt(display.state.height);
|
const height: f32 = @floatFromInt(display.state.height);
|
||||||
|
const icon_scale = .{8, 8};
|
||||||
|
|
||||||
for (events.messages()) |event| {
|
for (events.messages()) |event| {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.key_down => {
|
.key_down => {
|
||||||
_ = try visuals.state.spawned.insert(.{
|
try visuals.state.spawned.push_grow(.{
|
||||||
.lifetime_seconds = 2.5 + (5 * random.float(f32)),
|
.lifetime_seconds = 2.5 + (5 * random.float(f32)),
|
||||||
|
|
||||||
|
.visual = .{
|
||||||
.color = .{random.float(f32), random.float(f32), random.float(f32), random.float(f32)},
|
.color = .{random.float(f32), random.float(f32), random.float(f32), random.float(f32)},
|
||||||
.position = .{width * random.float(f32), height},
|
|
||||||
|
.transform = gfx.transform_2d(.{
|
||||||
|
.translation = .{width * random.float(f32), height},
|
||||||
.rotation = std.math.pi * random.float(f32),
|
.rotation = std.math.pi * random.float(f32),
|
||||||
|
.scale = icon_scale,
|
||||||
|
}),
|
||||||
|
},
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
.mouse_down => {
|
.mouse_down => {
|
||||||
_ = try visuals.state.spawned.insert(.{
|
try visuals.state.spawned.push_grow(.{
|
||||||
.lifetime_seconds = 2.5 + (5 * random.float(f32)),
|
.lifetime_seconds = 2.5 + (5 * random.float(f32)),
|
||||||
|
|
||||||
|
.visual = .{
|
||||||
.color = .{random.float(f32), random.float(f32), random.float(f32), random.float(f32)},
|
.color = .{random.float(f32), random.float(f32), random.float(f32), random.float(f32)},
|
||||||
.position = visuals.state.mouse_position,
|
|
||||||
|
.transform = gfx.transform_2d(.{
|
||||||
|
.translation = visuals.state.mouse_position,
|
||||||
.rotation = std.math.pi * random.float(f32),
|
.rotation = std.math.pi * random.float(f32),
|
||||||
|
.scale = icon_scale,
|
||||||
|
}),
|
||||||
|
},
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -80,13 +92,38 @@ fn update(visuals: ona.Write(Visuals), events: ona.Receive(hid.Event), display:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn update_spawned(spawned: *ona.stack.Parallel(Spawned)) void {
|
||||||
|
const float_speed = 6;
|
||||||
|
|
||||||
|
for (spawned.values.slice(.visual)) |*visual| {
|
||||||
|
visual.transform = visual.transform.translated(.{0, -float_speed});
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var range = spawned.len();
|
||||||
|
var index: usize = 0;
|
||||||
|
|
||||||
|
while (index < range) : (index += 1) {
|
||||||
|
const lifetime_seconds = spawned.values.get(.lifetime_seconds, index).?;
|
||||||
|
|
||||||
|
lifetime_seconds.* -= 1.0 / 60.0;
|
||||||
|
|
||||||
|
if (lifetime_seconds.* <= 0) {
|
||||||
|
range -= 1;
|
||||||
|
|
||||||
|
std.mem.swap(f32, lifetime_seconds, spawned.values.get(.lifetime_seconds, range).?);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std.debug.assert(spawned.pop_many(spawned.len() - range));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn render(visuals: ona.Write(Visuals), commands: gfx.Commands) !void {
|
fn render(visuals: ona.Write(Visuals), commands: gfx.Commands) !void {
|
||||||
for (visuals.state.spawned.values()) |visual| {
|
for (visuals.state.spawned.values.slice(.visual)) |visual| {
|
||||||
try commands.draw_texture(.{
|
try commands.draw_rect(.{
|
||||||
.anchor = @splat(0.5),
|
.transform = visual.transform,
|
||||||
.position = visual.position,
|
.color = visual.color,
|
||||||
.tint = visual.color,
|
|
||||||
.size = @as(gfx.Vector2, @splat(64)),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ pub const Effect = struct {
|
||||||
},
|
},
|
||||||
|
|
||||||
.vertex_source = .{
|
.vertex_source = .{
|
||||||
.ops = &spirv.to_ops(@embedFile("./shaders/draw_texture.vert.spv")),
|
.ops = &spirv.to_ops(@embedFile("./shaders/2d_default.vert.spv")),
|
||||||
},
|
},
|
||||||
|
|
||||||
.fragment_source = .{
|
.fragment_source = .{
|
||||||
|
@ -197,12 +197,12 @@ pub const Effect = struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
attrs[2] = .{
|
attrs[2] = .{
|
||||||
.format = .FLOAT,
|
.format = .FLOAT2,
|
||||||
.buffer_index = 1,
|
.buffer_index = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
attrs[3] = .{
|
attrs[3] = .{
|
||||||
.format = .FLOAT,
|
.format = .FLOAT2,
|
||||||
.buffer_index = 1,
|
.buffer_index = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -212,12 +212,12 @@ pub const Effect = struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
attrs[5] = .{
|
attrs[5] = .{
|
||||||
.format = .FLOAT2,
|
.format = .UBYTE4N,
|
||||||
.buffer_index = 1,
|
.buffer_index = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
attrs[6] = .{
|
attrs[6] = .{
|
||||||
.format = .FLOAT2,
|
.format = .FLOAT,
|
||||||
.buffer_index = 1,
|
.buffer_index = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -226,11 +226,6 @@ pub const Effect = struct {
|
||||||
.buffer_index = 1,
|
.buffer_index = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
attrs[8] = .{
|
|
||||||
.format = .FLOAT4,
|
|
||||||
.buffer_index = 1,
|
|
||||||
};
|
|
||||||
|
|
||||||
break: get attrs;
|
break: get attrs;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -471,7 +466,7 @@ pub fn init() !Self {
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.is_default_handle(try pools.create_effect(.{
|
assert.is_default_handle(try pools.create_effect(.{
|
||||||
.fragment_spirv_ops = &spirv.to_ops(@embedFile("./shaders/draw_texture.frag.spv")),
|
.fragment_spirv_ops = &spirv.to_ops(@embedFile("./shaders/2d_default.frag.spv")),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
assert.is_default_handle(try pools.create_texture(try descs.solid_texture(.{
|
assert.is_default_handle(try pools.create_texture(try descs.solid_texture(.{
|
||||||
|
|
270
src/gfx/gfx.zig
270
src/gfx/gfx.zig
|
@ -27,7 +27,40 @@ pub const Assets = struct {
|
||||||
load_file: *const fn (*std.heap.ArenaAllocator, ona.files.Storage, []const u8) LoadFileError!Texture.Desc,
|
load_file: *const fn (*std.heap.ArenaAllocator, ona.files.Storage, []const u8) LoadFileError!Texture.Desc,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const WorkQueue = ona.asyncio.BlockingQueue(1024, Work);
|
pub const WorkQueue = ona.asyncio.BlockingQueue(1024, union (enum) {
|
||||||
|
load_effect: LoadEffectWork,
|
||||||
|
load_texture: LoadTextureWork,
|
||||||
|
render_frame: RenderFrameWork,
|
||||||
|
shutdown,
|
||||||
|
unload_effect: UnloadEffectWork,
|
||||||
|
unload_texture: UnloadTextureWork,
|
||||||
|
|
||||||
|
const LoadEffectWork = struct {
|
||||||
|
desc: Effect.Desc,
|
||||||
|
loaded: *ona.asyncio.Future(std.mem.Allocator.Error!Effect),
|
||||||
|
};
|
||||||
|
|
||||||
|
const LoadTextureWork = struct {
|
||||||
|
desc: Texture.Desc,
|
||||||
|
loaded: *ona.asyncio.Future(std.mem.Allocator.Error!Texture),
|
||||||
|
};
|
||||||
|
|
||||||
|
const RenderFrameWork = struct {
|
||||||
|
clear_color: Color,
|
||||||
|
width: u16,
|
||||||
|
height: u16,
|
||||||
|
finished: *std.Thread.ResetEvent,
|
||||||
|
has_command_params: ?*ona.Params(Commands).Node,
|
||||||
|
};
|
||||||
|
|
||||||
|
const UnloadEffectWork = struct {
|
||||||
|
handle: Effect,
|
||||||
|
};
|
||||||
|
|
||||||
|
const UnloadTextureWork = struct {
|
||||||
|
handle: Texture,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
fn deinit(self: *Assets) void {
|
fn deinit(self: *Assets) void {
|
||||||
self.pending_work.enqueue(.shutdown);
|
self.pending_work.enqueue(.shutdown);
|
||||||
|
@ -39,10 +72,12 @@ pub const Assets = struct {
|
||||||
self.* = undefined;
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init(width: u16, height: u16) !Assets {
|
fn init() !Assets {
|
||||||
const window = create: {
|
const window = create: {
|
||||||
const position = ext.SDL_WINDOWPOS_CENTERED;
|
const position = ext.SDL_WINDOWPOS_CENTERED;
|
||||||
const flags = ext.SDL_WINDOW_OPENGL;
|
const flags = ext.SDL_WINDOW_OPENGL;
|
||||||
|
const width = 640;
|
||||||
|
const height = 480;
|
||||||
|
|
||||||
break: create ext.SDL_CreateWindow("Ona", position, position, width, height, flags) orelse {
|
break: create ext.SDL_CreateWindow("Ona", position, position, width, height, flags) orelse {
|
||||||
return error.Unsupported;
|
return error.Unsupported;
|
||||||
|
@ -103,60 +138,42 @@ pub const Assets = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const thread_restriction = .main;
|
pub const thread_restriction = .main;
|
||||||
|
|
||||||
pub fn unload_effect(self: *Assets, handle: Effect) void {
|
|
||||||
self.pending_work.enqueue(.{.unload_effect = handle});
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn unload_texture(self: *Assets, handle: Texture) void {
|
|
||||||
self.pending_work.enqueue(.{.unload_texture = handle});
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Color = @Vector(4, f32);
|
pub const Color = @Vector(4, f32);
|
||||||
|
|
||||||
pub const Command = union (enum) {
|
pub const Commands = struct {
|
||||||
draw_font: DrawFont,
|
pending: *List,
|
||||||
draw_texture: DrawTexture,
|
|
||||||
set_effect: SetEffect,
|
|
||||||
set_scissor_rect: ?Rect,
|
|
||||||
set_target: SetTarget,
|
|
||||||
|
|
||||||
pub const DrawFont = struct {
|
pub const Command = union (enum) {
|
||||||
position: Vector2 = @splat(0),
|
draw_rect: DrawRectCommand,
|
||||||
size: Vector2,
|
draw_texture: DrawTextureCommand,
|
||||||
text: []const u8,
|
set_effect: SetEffectCommand,
|
||||||
font: Font,
|
set_target: SetTargetCommand,
|
||||||
tint: Color,
|
|
||||||
depth: f32,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const DrawTexture = struct {
|
pub const DrawRectCommand = struct {
|
||||||
anchor: Vector2 = @splat(0),
|
color: Color,
|
||||||
size: ?Vector2 = null,
|
transform: Transform2D,
|
||||||
source: Rect = .{.left = 0, .top = 0, .right = 1, .bottom = 1},
|
|
||||||
position: Vector2 = @splat(0),
|
|
||||||
tint: Color = colors.white,
|
|
||||||
texture: Texture = .default,
|
|
||||||
rotation: f32 = 0,
|
|
||||||
depth: f32 = 0,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const SetEffect = struct {
|
pub const DrawTextureCommand = struct {
|
||||||
|
texture: Texture,
|
||||||
|
transform: Transform2D,
|
||||||
|
resolution: ?@Vector(2, u16) = null,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const SetEffectCommand = struct {
|
||||||
effect: Effect,
|
effect: Effect,
|
||||||
properties: []const u8,
|
properties: []const u8,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const SetTarget = struct {
|
pub const SetTargetCommand = struct {
|
||||||
texture: ?Texture = null,
|
texture: ?Texture = null,
|
||||||
clear_color: ?Color,
|
clear_color: ?Color,
|
||||||
clear_depth: ?f32,
|
clear_depth: ?f32,
|
||||||
clear_stencil: ?u8,
|
clear_stencil: ?u8,
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
pub const Commands = struct {
|
|
||||||
pending: *List,
|
|
||||||
|
|
||||||
pub const List = struct {
|
pub const List = struct {
|
||||||
arena: std.heap.ArenaAllocator,
|
arena: std.heap.ArenaAllocator,
|
||||||
|
@ -214,8 +231,6 @@ pub const Commands = struct {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const Self = @This();
|
|
||||||
|
|
||||||
pub fn bind(_: ona.World.BindContext) std.mem.Allocator.Error!Param {
|
pub fn bind(_: ona.World.BindContext) std.mem.Allocator.Error!Param {
|
||||||
return .{
|
return .{
|
||||||
.swap_lists = .{
|
.swap_lists = .{
|
||||||
|
@ -225,30 +240,21 @@ pub const Commands = struct {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(param: *Param) Self {
|
pub fn init(param: *Param) Commands {
|
||||||
return .{
|
return .{
|
||||||
.pending = param.pending_list(),
|
.pending = param.pending_list(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_font(self: Self, command: Command.DrawFont) std.mem.Allocator.Error!void {
|
pub fn draw_rect(self: Commands, command: DrawRectCommand) std.mem.Allocator.Error!void {
|
||||||
try self.pending.stack.push_grow(.{
|
try self.pending.stack.push_grow(.{.draw_rect = command});
|
||||||
.draw_font = .{
|
|
||||||
.text = try self.pending.arena.allocator().dupe(u8, command.text),
|
|
||||||
.position = command.position,
|
|
||||||
.size = command.size,
|
|
||||||
.font = command.font,
|
|
||||||
.tint = command.tint,
|
|
||||||
.depth = command.depth,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_texture(self: Self, command: Command.DrawTexture) std.mem.Allocator.Error!void {
|
pub fn draw_texture(self: Commands, command: DrawTextureCommand) std.mem.Allocator.Error!void {
|
||||||
try self.pending.stack.push_grow(.{.draw_texture = command});
|
try self.pending.stack.push_grow(.{.draw_texture = command});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_effect(self: Self, command: Command.SetEffect) std.mem.Allocator.Error!void {
|
pub fn set_effect(self: Commands, command: SetEffectCommand) std.mem.Allocator.Error!void {
|
||||||
try self.pending.stack.push_grow(.{
|
try self.pending.stack.push_grow(.{
|
||||||
.set_effect = .{
|
.set_effect = .{
|
||||||
.properties = try self.pending.arena.allocator().dupe(u8, command.properties),
|
.properties = try self.pending.arena.allocator().dupe(u8, command.properties),
|
||||||
|
@ -257,17 +263,13 @@ pub const Commands = struct {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_scissor_rect(self: Self, has_rect: Rect) std.mem.Allocator.Error!void {
|
|
||||||
try self.pending.stack.push_grow(.{.set_scissor_rect = has_rect});
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_target(self: Self, command: Command.SetTarget) std.mem.Allocator.Error!void {
|
|
||||||
try self.pending.stack.push_grow(.{.set_target = command});
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn unbind(param: *Param, _: ona.World.UnbindContext) void {
|
pub fn unbind(param: *Param, _: ona.World.UnbindContext) void {
|
||||||
param.deinit();
|
param.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_target(self: Commands, command: SetTargetCommand) std.mem.Allocator.Error!void {
|
||||||
|
try self.pending.stack.push_grow(.{.set_target = command});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Descs = struct {
|
pub const Descs = struct {
|
||||||
|
@ -429,37 +431,11 @@ pub const Effect = enum (u32) {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Font = enum (u32) {
|
pub const Rect = struct {
|
||||||
default,
|
|
||||||
_,
|
|
||||||
|
|
||||||
pub const Desc = struct {
|
|
||||||
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const Rect = extern struct {
|
|
||||||
left: f32,
|
left: f32,
|
||||||
top: f32,
|
top: f32,
|
||||||
right: f32,
|
right: f32,
|
||||||
bottom: f32,
|
bottom: f32,
|
||||||
|
|
||||||
pub fn area(self: Rect) f32 {
|
|
||||||
const width, const height = self.size();
|
|
||||||
|
|
||||||
return width * height;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn size(self: Rect) Vector2 {
|
|
||||||
return .{self.right - self.left, self.bottom - self.top};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const zero = Rect{
|
|
||||||
.left = 0,
|
|
||||||
.top = 0,
|
|
||||||
.right = 0,
|
|
||||||
.bottom = 0,
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Texture = enum (u32) {
|
pub const Texture = enum (u32) {
|
||||||
|
@ -498,41 +474,58 @@ pub const Texture = enum (u32) {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Vector2 = @Vector(2, f32);
|
pub const Transform2D = extern struct {
|
||||||
|
xbasis: Vector = .{1, 0},
|
||||||
|
ybasis: Vector = .{0, 1},
|
||||||
|
origin: Vector = @splat(0),
|
||||||
|
|
||||||
const Work = union (enum) {
|
pub const Simplified = struct {
|
||||||
load_effect: LoadEffect,
|
translation: Vector = @splat(0),
|
||||||
load_texture: LoadTexture,
|
rotation: f32 = 0,
|
||||||
render_frame: RenderFrame,
|
scale: Vector = @splat(1),
|
||||||
shutdown,
|
skew: f32 = 0,
|
||||||
unload_effect: UnloadEffect,
|
|
||||||
unload_texture: UnloadTexture,
|
|
||||||
|
|
||||||
const LoadEffect = struct {
|
|
||||||
desc: Effect.Desc,
|
|
||||||
loaded: *ona.asyncio.Future(std.mem.Allocator.Error!Effect),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const LoadTexture = struct {
|
pub const Vector = @Vector(2, f32);
|
||||||
desc: Texture.Desc,
|
|
||||||
loaded: *ona.asyncio.Future(std.mem.Allocator.Error!Texture),
|
|
||||||
};
|
|
||||||
|
|
||||||
const RenderFrame = struct {
|
pub fn scaled(self: Transform2D, scale: Vector) Transform2D {
|
||||||
clear_color: Color,
|
var transform = self;
|
||||||
width: u16,
|
const scale_x, const scale_y = scale;
|
||||||
height: u16,
|
|
||||||
finished: *std.Thread.ResetEvent,
|
|
||||||
has_commands: ?*ona.Params(Commands).Node,
|
|
||||||
};
|
|
||||||
|
|
||||||
const UnloadEffect = struct {
|
transform.xbasis *= @splat(scale_x);
|
||||||
handle: Effect,
|
transform.ybasis *= @splat(scale_y);
|
||||||
};
|
|
||||||
|
|
||||||
const UnloadTexture = struct {
|
return transform;
|
||||||
handle: Texture,
|
}
|
||||||
|
|
||||||
|
pub fn transformed(self: Transform2D, other: Transform2D) Transform2D {
|
||||||
|
const xbasis_x, const xbasis_y = other.xbasis;
|
||||||
|
const ybasis_x, const ybasis_y = other.ybasis;
|
||||||
|
const origin_x, const origin_y = other.origin;
|
||||||
|
|
||||||
|
return .{
|
||||||
|
.xbasis =
|
||||||
|
(self.xbasis * @as(Vector, @splat(xbasis_x))) +
|
||||||
|
(self.ybasis * @as(Vector, @splat(xbasis_y))),
|
||||||
|
|
||||||
|
.ybasis =
|
||||||
|
(self.xbasis * @as(Vector, @splat(ybasis_x))) +
|
||||||
|
(self.ybasis * @as(Vector, @splat(ybasis_y))),
|
||||||
|
|
||||||
|
.origin =
|
||||||
|
(self.xbasis * @as(Vector, @splat(origin_x))) +
|
||||||
|
(self.ybasis * @as(Vector, @splat(origin_y))) +
|
||||||
|
self.origin,
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn translated(self: Transform2D, translation: Vector) Transform2D {
|
||||||
|
var transform = self;
|
||||||
|
|
||||||
|
transform.origin += translation;
|
||||||
|
|
||||||
|
return transform;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const colors = struct {
|
pub const colors = struct {
|
||||||
|
@ -553,8 +546,6 @@ pub const colors = struct {
|
||||||
|
|
||||||
pub const purple = rgb(0.5, 0, 0.5);
|
pub const purple = rgb(0.5, 0, 0.5);
|
||||||
|
|
||||||
pub const red = rgb(1, 0, 0);
|
|
||||||
|
|
||||||
pub fn rgb(r: f32, g: f32, b: f32) Color {
|
pub fn rgb(r: f32, g: f32, b: f32) Color {
|
||||||
return .{r, g, b, 1};
|
return .{r, g, b, 1};
|
||||||
}
|
}
|
||||||
|
@ -562,13 +553,13 @@ pub const colors = struct {
|
||||||
pub const white = greyscale(1);
|
pub const white = greyscale(1);
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn poll(loop: ona.Write(ona.Loop), events: ona.Send(hid.Event)) !void {
|
pub fn poll(app: ona.Write(ona.App), events: ona.Send(hid.Event)) !void {
|
||||||
var event = @as(ext.SDL_Event, undefined);
|
var event = @as(ext.SDL_Event, undefined);
|
||||||
|
|
||||||
while (ext.SDL_PollEvent(&event) != 0) {
|
while (ext.SDL_PollEvent(&event) != 0) {
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
ext.SDL_QUIT => {
|
ext.SDL_QUIT => {
|
||||||
loop.state.quit();
|
app.state.quit();
|
||||||
},
|
},
|
||||||
|
|
||||||
ext.SDL_KEYUP => {
|
ext.SDL_KEYUP => {
|
||||||
|
@ -615,15 +606,13 @@ pub fn poll(loop: ona.Write(ona.Loop), events: ona.Send(hid.Event)) !void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn setup(world: *ona.World) !void {
|
pub fn setup(world: *ona.World, events: ona.App.Events) (error {Unsupported} || std.Thread.SpawnError || std.mem.Allocator.Error)!void {
|
||||||
if (ext.SDL_Init(ext.SDL_INIT_VIDEO | ext.SDL_INIT_EVENTS) != 0) {
|
if (ext.SDL_Init(ext.SDL_INIT_VIDEO | ext.SDL_INIT_EVENTS) != 0) {
|
||||||
return error.Unsupported;
|
return error.Unsupported;
|
||||||
}
|
}
|
||||||
|
|
||||||
const display = try world.set_get_state(Display{});
|
|
||||||
|
|
||||||
const assets = create: {
|
const assets = create: {
|
||||||
var assets = try Assets.init(display.width, display.height);
|
var assets = try Assets.init();
|
||||||
|
|
||||||
errdefer {
|
errdefer {
|
||||||
assets.deinit();
|
assets.deinit();
|
||||||
|
@ -643,9 +632,10 @@ pub fn setup(world: *ona.World) !void {
|
||||||
assets.window,
|
assets.window,
|
||||||
});
|
});
|
||||||
|
|
||||||
try world.on_event(.pre_update, ona.system_fn(poll), .{.label = "poll gfx"});
|
try world.set_state(Display{});
|
||||||
try world.on_event(.exit, ona.system_fn(stop), .{.label = "stop gfx"});
|
try world.on_event(events.pre_update, ona.system_fn(poll), .{.label = "poll gfx"});
|
||||||
try world.on_event(.finish, ona.system_fn(synchronize), .{.label = "synchronize gfx"});
|
try world.on_event(events.exit, ona.system_fn(stop), .{.label = "stop gfx"});
|
||||||
|
try world.on_event(events.finish, ona.system_fn(synchronize), .{.label = "synchronize gfx"});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stop(assets: ona.Write(Assets)) void {
|
pub fn stop(assets: ona.Write(Assets)) void {
|
||||||
|
@ -658,11 +648,13 @@ pub fn synchronize(exclusive: ona.Exclusive(&.{Assets, Display})) !void {
|
||||||
assets.frame_rendered.wait();
|
assets.frame_rendered.wait();
|
||||||
assets.frame_rendered.reset();
|
assets.frame_rendered.reset();
|
||||||
|
|
||||||
|
{
|
||||||
var has_command_param = exclusive.world.get_params(Commands).has_head;
|
var has_command_param = exclusive.world.get_params(Commands).has_head;
|
||||||
|
|
||||||
while (has_command_param) |command_param| : (has_command_param = command_param.has_next) {
|
while (has_command_param) |command_param| : (has_command_param = command_param.has_next) {
|
||||||
command_param.param.rotate();
|
command_param.param.rotate();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var display_width, var display_height = [_]c_int{0, 0};
|
var display_width, var display_height = [_]c_int{0, 0};
|
||||||
|
|
||||||
|
@ -672,10 +664,10 @@ pub fn synchronize(exclusive: ona.Exclusive(&.{Assets, Display})) !void {
|
||||||
ext.SDL_SetWindowSize(assets.window, display.width, display.height);
|
ext.SDL_SetWindowSize(assets.window, display.width, display.height);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (exclusive.world.get_params(Commands).has_head) |has_commands| {
|
if (exclusive.world.get_params(Commands).has_head) |command_param| {
|
||||||
assets.pending_work.enqueue(.{
|
assets.pending_work.enqueue(.{
|
||||||
.render_frame = .{
|
.render_frame = .{
|
||||||
.has_commands = has_commands,
|
.has_command_params = command_param,
|
||||||
.width = display.width,
|
.width = display.width,
|
||||||
.height = display.height,
|
.height = display.height,
|
||||||
.clear_color = display.clear_color,
|
.clear_color = display.clear_color,
|
||||||
|
@ -686,3 +678,21 @@ pub fn synchronize(exclusive: ona.Exclusive(&.{Assets, Display})) !void {
|
||||||
assets.frame_rendered.set();
|
assets.frame_rendered.set();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn transform_2d(simplified: Transform2D.Simplified) Transform2D {
|
||||||
|
const rotation_skew = simplified.rotation + simplified.skew;
|
||||||
|
|
||||||
|
return .{
|
||||||
|
.xbasis = simplified.scale * Transform2D.Vector{
|
||||||
|
std.math.cos(simplified.rotation),
|
||||||
|
std.math.sin(simplified.rotation),
|
||||||
|
},
|
||||||
|
|
||||||
|
.ybasis = simplified.scale * Transform2D.Vector{
|
||||||
|
-std.math.sin(rotation_skew),
|
||||||
|
std.math.cos(rotation_skew),
|
||||||
|
},
|
||||||
|
|
||||||
|
.origin = simplified.translation,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
@ -23,23 +23,15 @@ const Frame = struct {
|
||||||
current_source_texture: gfx.Texture = .default,
|
current_source_texture: gfx.Texture = .default,
|
||||||
current_target_texture: ?gfx.Texture = null,
|
current_target_texture: ?gfx.Texture = null,
|
||||||
current_effect: gfx.Effect = .default,
|
current_effect: gfx.Effect = .default,
|
||||||
transform_matrix: Matrix = identity_matrix,
|
|
||||||
width: u16 = 0,
|
width: u16 = 0,
|
||||||
height: u16 = 0,
|
height: u16 = 0,
|
||||||
|
|
||||||
const DrawTexture = extern struct {
|
const DrawTexture = extern struct {
|
||||||
rotation: f32,
|
transform: gfx.Transform2D,
|
||||||
depth: f32,
|
tint: @Vector(4, u8) = @splat(std.math.maxInt(u8)),
|
||||||
position: gfx.Vector2,
|
depth: f32 = 0,
|
||||||
size: gfx.Vector2,
|
texture_offset: @Vector(2, f32) = @splat(0),
|
||||||
anchor: gfx.Vector2,
|
texture_size: @Vector(2, f32) = @splat(1),
|
||||||
source_clip: gfx.Rect,
|
|
||||||
tint: gfx.Color,
|
|
||||||
};
|
|
||||||
|
|
||||||
const View = extern struct {
|
|
||||||
projection_matrix: Matrix,
|
|
||||||
transform_matrix: Matrix,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const batches_per_buffer = 512;
|
const batches_per_buffer = 512;
|
||||||
|
@ -83,13 +75,35 @@ const Frame = struct {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_font(self: *Frame, resources: *Resources, command: gfx.Command.DrawFont) !void {
|
pub fn draw_rect(self: *Frame, resources: *Resources, command: gfx.Commands.DrawRectCommand) !void {
|
||||||
_ = self;
|
if (self.current_source_texture != .default) {
|
||||||
_ = resources;
|
self.flush(resources);
|
||||||
_ = command;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_texture(self: *Frame, resources: *Resources, command: gfx.Command.DrawTexture) !void {
|
self.current_source_texture = .default;
|
||||||
|
|
||||||
|
const has_filled_current_buffer = (self.drawn_count % batches_per_buffer) == 0;
|
||||||
|
const buffer_count = self.drawn_count / batches_per_buffer;
|
||||||
|
|
||||||
|
if (has_filled_current_buffer and buffer_count == self.texture_batch_buffers.len()) {
|
||||||
|
const instance_buffer = sokol.gfx.makeBuffer(.{
|
||||||
|
.size = @sizeOf(DrawTexture) * batches_per_buffer,
|
||||||
|
.usage = .STREAM,
|
||||||
|
});
|
||||||
|
|
||||||
|
errdefer sokol.gfx.destroyBuffer(instance_buffer);
|
||||||
|
|
||||||
|
try self.texture_batch_buffers.push_grow(instance_buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = sokol.gfx.appendBuffer(self.texture_batch_buffers.get().?.*, sokol.gfx.asRange(&DrawTexture{
|
||||||
|
.transform = command.transform,
|
||||||
|
}));
|
||||||
|
|
||||||
|
self.drawn_count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw_texture(self: *Frame, resources: *Resources, command: gfx.Commands.DrawTextureCommand) !void {
|
||||||
if (self.current_source_texture != command.texture) {
|
if (self.current_source_texture != command.texture) {
|
||||||
self.flush(resources);
|
self.flush(resources);
|
||||||
}
|
}
|
||||||
|
@ -113,13 +127,10 @@ const Frame = struct {
|
||||||
const texture = resources.get_texture(command.texture).?;
|
const texture = resources.get_texture(command.texture).?;
|
||||||
|
|
||||||
_ = sokol.gfx.appendBuffer(self.texture_batch_buffers.get().?.*, sokol.gfx.asRange(&DrawTexture{
|
_ = sokol.gfx.appendBuffer(self.texture_batch_buffers.get().?.*, sokol.gfx.asRange(&DrawTexture{
|
||||||
.size = command.size orelse .{@floatFromInt(texture.width), @floatFromInt(texture.height)},
|
.transform = command.transform.scaled(@floatFromInt(command.resolution orelse .{
|
||||||
.anchor = command.anchor,
|
texture.width,
|
||||||
.rotation = command.rotation,
|
texture.height,
|
||||||
.depth = command.depth,
|
})),
|
||||||
.source_clip = command.source,
|
|
||||||
.tint = command.tint,
|
|
||||||
.position = command.position,
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
self.drawn_count += 1;
|
self.drawn_count += 1;
|
||||||
|
@ -167,27 +178,19 @@ const Frame = struct {
|
||||||
if (self.current_target_texture) |target_texture| {
|
if (self.current_target_texture) |target_texture| {
|
||||||
const texture = resources.get_texture(target_texture).?;
|
const texture = resources.get_texture(target_texture).?;
|
||||||
|
|
||||||
sokol.gfx.applyUniforms(.VS, 0, sokol.gfx.asRange(&View{
|
sokol.gfx.applyUniforms(.VS, 0, sokol.gfx.asRange(&orthographic_projection(-1.0, 1.0, .{
|
||||||
.projection_matrix = orthographic_projection(-1.0, 1.0, .{
|
|
||||||
.left = 0,
|
.left = 0,
|
||||||
.top = 0,
|
.top = 0,
|
||||||
.right = @floatFromInt(texture.width),
|
.right = @floatFromInt(texture.width),
|
||||||
.bottom = @floatFromInt(texture.height),
|
.bottom = @floatFromInt(texture.height),
|
||||||
}),
|
})));
|
||||||
|
|
||||||
.transform_matrix = self.transform_matrix,
|
|
||||||
}));
|
|
||||||
} else {
|
} else {
|
||||||
sokol.gfx.applyUniforms(.VS, 0, sokol.gfx.asRange(&View{
|
sokol.gfx.applyUniforms(.VS, 0, sokol.gfx.asRange(&orthographic_projection(-1.0, 1.0, .{
|
||||||
.projection_matrix = orthographic_projection(-1.0, 1.0, .{
|
|
||||||
.left = 0,
|
.left = 0,
|
||||||
.top = 0,
|
.top = 0,
|
||||||
.right = @floatFromInt(self.width),
|
.right = @floatFromInt(self.width),
|
||||||
.bottom = @floatFromInt(self.height),
|
.bottom = @floatFromInt(self.height),
|
||||||
}),
|
})));
|
||||||
|
|
||||||
.transform_matrix = self.transform_matrix,
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (effect.properties.len != 0) {
|
if (effect.properties.len != 0) {
|
||||||
|
@ -212,7 +215,7 @@ const Frame = struct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_effect(self: *Frame, resources: *Resources, command: gfx.Command.SetEffect) void {
|
pub fn set_effect(self: *Frame, resources: *Resources, command: gfx.Commands.SetEffectCommand) void {
|
||||||
if (command.effect != self.current_effect) {
|
if (command.effect != self.current_effect) {
|
||||||
self.flush(resources);
|
self.flush(resources);
|
||||||
}
|
}
|
||||||
|
@ -224,25 +227,7 @@ const Frame = struct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_scissor_rect(self: *Frame, resources: *Resources, has_rect: ?gfx.Rect) void {
|
pub fn set_target(self: *Frame, resources: *Resources, command: gfx.Commands.SetTargetCommand) void {
|
||||||
self.flush(resources);
|
|
||||||
|
|
||||||
if (has_rect) |rect| {
|
|
||||||
const width, const height = rect.size();
|
|
||||||
|
|
||||||
sokol.gfx.applyScissorRect(
|
|
||||||
@intFromFloat(rect.left),
|
|
||||||
@intFromFloat(rect.top),
|
|
||||||
@intFromFloat(width),
|
|
||||||
@intFromFloat(height),
|
|
||||||
true,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
sokol.gfx.applyScissorRect(0, 0, self.width, self.height, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_target(self: *Frame, resources: *Resources, command: gfx.Command.SetTarget) void {
|
|
||||||
sokol.gfx.endPass();
|
sokol.gfx.endPass();
|
||||||
|
|
||||||
var pass = sokol.gfx.Pass{
|
var pass = sokol.gfx.Pass{
|
||||||
|
@ -320,23 +305,18 @@ const Frame = struct {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const Matrix = [4]@Vector(4, f32);
|
fn Matrix(comptime n: usize, comptime Element: type) type {
|
||||||
|
return [n]@Vector(n, Element);
|
||||||
|
}
|
||||||
|
|
||||||
var default_sampler: sokol.gfx.Sampler = undefined;
|
var default_sampler: sokol.gfx.Sampler = undefined;
|
||||||
|
|
||||||
const identity_matrix = Matrix{
|
|
||||||
.{1, 0, 0, 0},
|
|
||||||
.{0, 1, 0, 0},
|
|
||||||
.{0, 0, 1, 0},
|
|
||||||
.{0, 0, 0, 1},
|
|
||||||
};
|
|
||||||
|
|
||||||
const vertex_indices = .{
|
const vertex_indices = .{
|
||||||
.mesh = 0,
|
.mesh = 0,
|
||||||
.instance = 1,
|
.instance = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn orthographic_projection(near: f32, far: f32, viewport: gfx.Rect) Matrix {
|
fn orthographic_projection(near: f32, far: f32, viewport: gfx.Rect) Matrix(4, f32) {
|
||||||
const width = viewport.right - viewport.left;
|
const width = viewport.right - viewport.left;
|
||||||
const height = viewport.bottom - viewport.top;
|
const height = viewport.bottom - viewport.top;
|
||||||
|
|
||||||
|
@ -417,18 +397,17 @@ pub fn process_work(pending_work: *gfx.Assets.WorkQueue, window: *ext.SDL_Window
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
.render_frame => |*render_frame| {
|
.render_frame => |render_frame| {
|
||||||
frame.start(render_frame.width, render_frame.height);
|
frame.start(render_frame.width, render_frame.height);
|
||||||
|
|
||||||
var has_commands = render_frame.has_commands;
|
var has_command_params = render_frame.has_command_params;
|
||||||
|
|
||||||
while (has_commands) |commands| : (has_commands = commands.has_next) {
|
while (has_command_params) |command_params| : (has_command_params = command_params.has_next) {
|
||||||
for (commands.param.submitted_commands()) |command| {
|
for (command_params.param.submitted_commands()) |command| {
|
||||||
try switch (command) {
|
try switch (command) {
|
||||||
.draw_font => |draw_font| frame.draw_font(&resources, draw_font),
|
.draw_rect => |draw_rect| frame.draw_rect(&resources, draw_rect),
|
||||||
.draw_texture => |draw_texture| frame.draw_texture(&resources, draw_texture),
|
.draw_texture => |draw_texture| frame.draw_texture(&resources, draw_texture),
|
||||||
.set_effect => |set_effect| frame.set_effect(&resources, set_effect),
|
.set_effect => |set_effect| frame.set_effect(&resources, set_effect),
|
||||||
.set_scissor_rect => |has_rect| frame.set_scissor_rect(&resources, has_rect),
|
|
||||||
.set_target => |set_target| frame.set_target(&resources, set_target),
|
.set_target => |set_target| frame.set_target(&resources, set_target),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
#version 430
|
||||||
|
|
||||||
|
layout (location = 0) in vec2 mesh_xy;
|
||||||
|
layout (location = 1) in vec2 mesh_uv;
|
||||||
|
|
||||||
|
layout (location = 2) in vec2 instance_xbasis;
|
||||||
|
layout (location = 3) in vec2 instance_ybasis;
|
||||||
|
layout (location = 4) in vec2 instance_origin;
|
||||||
|
layout (location = 5) in vec4 instance_tint;
|
||||||
|
layout (location = 6) in float instance_depth;
|
||||||
|
layout (location = 7) in vec4 instance_clip;
|
||||||
|
|
||||||
|
layout (location = 0) out vec4 color;
|
||||||
|
layout (location = 1) out vec2 uv;
|
||||||
|
|
||||||
|
layout (binding = 0) uniform View {
|
||||||
|
mat4 projection_matrix;
|
||||||
|
};
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
const vec2 world_position = instance_origin + mesh_xy.x * instance_xbasis + mesh_xy.y * instance_ybasis;
|
||||||
|
const vec2 projected_position = (projection_matrix * vec4(world_position, 0, 1)).xy;
|
||||||
|
const vec2 rect_size = instance_clip.zw - instance_clip.xy;
|
||||||
|
const vec4 screen_position = vec4(projected_position, instance_depth, 1.0);
|
||||||
|
|
||||||
|
gl_Position = screen_position;
|
||||||
|
color = instance_tint;
|
||||||
|
uv = instance_clip.xy + (mesh_uv * rect_size);
|
||||||
|
}
|
|
@ -1,42 +0,0 @@
|
||||||
#version 430
|
|
||||||
|
|
||||||
layout (location = 0) in vec2 batch_xy;
|
|
||||||
layout (location = 1) in vec2 batch_uv;
|
|
||||||
|
|
||||||
layout (location = 2) in float instance_rotation;
|
|
||||||
layout (location = 3) in float instance_depth;
|
|
||||||
layout (location = 4) in vec2 instance_position;
|
|
||||||
layout (location = 5) in vec2 instance_scale;
|
|
||||||
layout (location = 6) in vec2 instance_anchor;
|
|
||||||
layout (location = 7) in vec4 instance_clip;
|
|
||||||
layout (location = 8) in vec4 instance_tint;
|
|
||||||
|
|
||||||
layout (location = 0) out vec4 color;
|
|
||||||
layout (location = 1) out vec2 uv;
|
|
||||||
|
|
||||||
layout (binding = 0) uniform View {
|
|
||||||
mat4 projection_matrix;
|
|
||||||
mat4 transform_matrix;
|
|
||||||
};
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
const vec2 normalized_position = (batch_xy + 0.5) - instance_anchor;
|
|
||||||
const float cos_theta = cos(instance_rotation);
|
|
||||||
const float sin_theta = sin(instance_rotation);
|
|
||||||
|
|
||||||
const mat2 rotation_matrix = mat2(
|
|
||||||
cos_theta, -sin_theta,
|
|
||||||
sin_theta, cos_theta
|
|
||||||
);
|
|
||||||
|
|
||||||
const vec2 rotated_position = rotation_matrix * (normalized_position * instance_scale);
|
|
||||||
const vec2 world_position = instance_position + rotated_position;
|
|
||||||
const vec4 clip_space_position = projection_matrix * transform_matrix * vec4(world_position, 0.0, 1.0);
|
|
||||||
|
|
||||||
gl_Position = vec4(clip_space_position.xy, instance_depth, 1.0);
|
|
||||||
|
|
||||||
const vec2 clip_size = instance_clip.zw - instance_clip.xy;
|
|
||||||
|
|
||||||
uv = instance_clip.xy + (batch_uv * clip_size);
|
|
||||||
color = instance_tint;
|
|
||||||
}
|
|
|
@ -232,7 +232,9 @@ pub const Stage = struct {
|
||||||
else => error.UnsupportedSPIRV,
|
else => error.UnsupportedSPIRV,
|
||||||
},
|
},
|
||||||
|
|
||||||
.len = std.math.cast(u16, ext.spvc_type_get_array_dimension(member_type_handle, 0)) orelse 0,
|
.len = std.math.cast(u16, ext.spvc_type_get_array_dimension(member_type_handle, 0)) orelse {
|
||||||
|
return error.UnsupportedSPIRV;
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
234
src/gui/gui.zig
234
src/gui/gui.zig
|
@ -1,234 +0,0 @@
|
||||||
const gfx = @import("gfx");
|
|
||||||
|
|
||||||
const ona = @import("ona");
|
|
||||||
|
|
||||||
const std = @import("std");
|
|
||||||
|
|
||||||
pub const Box = struct {
|
|
||||||
x: f32,
|
|
||||||
y: f32,
|
|
||||||
width: f32,
|
|
||||||
height: f32,
|
|
||||||
|
|
||||||
pub fn rect(self: Box) gfx.Rect {
|
|
||||||
return .{
|
|
||||||
.left = self.x,
|
|
||||||
.top = self.y,
|
|
||||||
.right = self.x + self.width,
|
|
||||||
.bottom = self.y + self.height,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const Canvas = struct {
|
|
||||||
nodes: NodeMap = NodeMap.init(ona.heap.allocator),
|
|
||||||
drawable_blocks: DrawableBlockMap = DrawableBlockMap.init(ona.heap.allocator),
|
|
||||||
drawable_labels: DrawableLabelMap = DrawableLabelMap.init(ona.heap.allocator),
|
|
||||||
|
|
||||||
fn Drawable(comptime State: type) type {
|
|
||||||
return struct {
|
|
||||||
clip: gfx.Rect,
|
|
||||||
box: Box,
|
|
||||||
depth: f32,
|
|
||||||
state: State,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const DrawableBlockMap = ona.SlotMap(Drawable(struct {
|
|
||||||
background: gfx.Texture = .default,
|
|
||||||
color: gfx.Color = gfx.colors.white,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const DrawableLabelMap = ona.SlotMap(Drawable(struct {
|
|
||||||
text: ona.stack.Sequential(u8) = .{},
|
|
||||||
color: gfx.Color = gfx.colors.black,
|
|
||||||
font: gfx.Font = .default,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const NodeMap = ona.SlotMap(struct {
|
|
||||||
name: []u8,
|
|
||||||
box: Box,
|
|
||||||
parent_index: u32 = unused_index,
|
|
||||||
block_index: u32 = unused_index,
|
|
||||||
label_index: u32 = unused_index,
|
|
||||||
|
|
||||||
const Self = @This();
|
|
||||||
|
|
||||||
fn clip(self: Self, nodes: NodeMap) gfx.Rect {
|
|
||||||
if (self.parent_index == unused_index) {
|
|
||||||
return self.box.rect();
|
|
||||||
}
|
|
||||||
|
|
||||||
return nodes.get(self.parent_index).?.box.rect();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn depth(self: Self, nodes: NodeMap) f32 {
|
|
||||||
var parent_index = self.parent_index;
|
|
||||||
var value: f32 = 0;
|
|
||||||
|
|
||||||
while (parent_index != unused_index) : (parent_index = nodes.get(parent_index).?.parent_index) {
|
|
||||||
value += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
pub fn append(self: *Canvas, name: []const u8, box: Box) std.mem.Allocator.Error!Item {
|
|
||||||
const duped_name = try ona.heap.allocator.dupe(u8, name);
|
|
||||||
|
|
||||||
errdefer {
|
|
||||||
ona.heap.allocator.free(duped_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
return @enumFromInt(try self.nodes.insert(.{
|
|
||||||
.name = duped_name,
|
|
||||||
.box = box,
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn deinit(self: *Canvas) void {
|
|
||||||
self.nodes.deinit();
|
|
||||||
self.drawable_blocks.deinit();
|
|
||||||
|
|
||||||
for (self.drawable_labels.values()) |*drawable_label| {
|
|
||||||
drawable_label.state.text.deinit();
|
|
||||||
}
|
|
||||||
|
|
||||||
self.drawable_labels.deinit();
|
|
||||||
|
|
||||||
self.* = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_block(self: *Canvas, item: Item, set: SetBlock) std.mem.Allocator.Error!void {
|
|
||||||
const node = self.nodes.get(@intFromEnum(item)).?;
|
|
||||||
|
|
||||||
if (node.block_index == unused_index) {
|
|
||||||
node.block_index = @intCast(try self.drawable_blocks.insert(.{
|
|
||||||
.clip = node.clip(self.nodes),
|
|
||||||
.depth = node.depth(self.nodes),
|
|
||||||
.box = node.box,
|
|
||||||
.state = .{},
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
const block = self.drawable_blocks.get(node.block_index).?;
|
|
||||||
|
|
||||||
if (set.has_background) |background| {
|
|
||||||
block.state.background = background;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (set.has_color) |color| {
|
|
||||||
block.state.color = color;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_label(self: *Canvas, item: Item, set: SetLabel) std.mem.Allocator.Error!void {
|
|
||||||
const node = self.nodes.get(@intFromEnum(item)).?;
|
|
||||||
|
|
||||||
if (node.label_index == unused_index) {
|
|
||||||
node.label_index = @intCast(try self.drawable_labels.insert(.{
|
|
||||||
.clip = node.clip(self.nodes),
|
|
||||||
.depth = node.depth(self.nodes),
|
|
||||||
.box = node.box,
|
|
||||||
.state = .{},
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
const label = self.drawable_labels.get(node.label_index).?;
|
|
||||||
|
|
||||||
if (set.has_text) |text| {
|
|
||||||
label.state.text.pop_all();
|
|
||||||
|
|
||||||
try label.state.text.grow(ona.scalars.sub(text.len, label.state.text.cap) orelse 0);
|
|
||||||
|
|
||||||
std.debug.assert(label.state.text.push_all(text));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (set.has_font) |font| {
|
|
||||||
label.state.font = font;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (set.has_color) |color| {
|
|
||||||
label.state.color = color;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_parent(self: *Canvas, item: Item, has_parent: ?Item) void {
|
|
||||||
const node = self.nodes.get(@intFromEnum(item)).?;
|
|
||||||
|
|
||||||
node.parent_index = if (has_parent) |parent| @intFromEnum(parent) else unused_index;
|
|
||||||
|
|
||||||
const depth = node.depth(self.nodes);
|
|
||||||
const clip = node.clip(self.nodes);
|
|
||||||
|
|
||||||
if (node.block_index != unused_index) {
|
|
||||||
const block = self.drawable_blocks.get(node.block_index).?;
|
|
||||||
|
|
||||||
block.depth = depth;
|
|
||||||
block.clip = clip;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (node.label_index != unused_index) {
|
|
||||||
const label = self.drawable_labels.get(node.label_index).?;
|
|
||||||
|
|
||||||
label.depth = depth;
|
|
||||||
label.clip = clip;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const unused_index = std.math.maxInt(u32);
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const Item = enum (u32) {
|
|
||||||
_,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const SetBlock = struct {
|
|
||||||
has_background: ?gfx.Texture = null,
|
|
||||||
has_color: ?gfx.Color = null,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const SetLabel = struct {
|
|
||||||
has_text: ?[]const u8 = null,
|
|
||||||
has_color: ?gfx.Color = null,
|
|
||||||
has_font: ?gfx.Font = null,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn exit(canvas: ona.Write(Canvas)) !void {
|
|
||||||
canvas.state.deinit();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn render(commands: gfx.Commands, canvas: ona.Read(Canvas)) !void {
|
|
||||||
// TODO: Investigate if scissor rects are necessary for clipping content that overflows.
|
|
||||||
for (canvas.state.drawable_blocks.values()) |block| {
|
|
||||||
try commands.set_scissor_rect(block.clip);
|
|
||||||
|
|
||||||
try commands.draw_texture(.{
|
|
||||||
.size = .{block.box.width, block.box.height},
|
|
||||||
.position = .{block.box.x, block.box.y},
|
|
||||||
.depth = block.depth,
|
|
||||||
.texture = block.state.background,
|
|
||||||
.tint = block.state.color,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
for (canvas.state.drawable_labels.values()) |label| {
|
|
||||||
try commands.set_scissor_rect(label.clip);
|
|
||||||
|
|
||||||
try commands.draw_font(.{
|
|
||||||
.size = .{label.box.width, label.box.height},
|
|
||||||
.depth = label.depth,
|
|
||||||
.text = label.state.text.values,
|
|
||||||
.position = .{label.box.x, label.box.y},
|
|
||||||
.tint = label.state.color,
|
|
||||||
.font = label.state.font,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn setup(world: *ona.World) !void {
|
|
||||||
try world.set_state(Canvas{});
|
|
||||||
try world.on_event(.render, ona.system_fn(render), .{.label = "render gui"});
|
|
||||||
try world.on_event(.exit, ona.system_fn(exit), .{.label = "cleanup gui"});
|
|
||||||
}
|
|
|
@ -293,10 +293,10 @@ test "mapping values" {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn setup(world: *ona.World) std.mem.Allocator.Error!void {
|
pub fn setup(world: *ona.World, events: ona.App.Events) std.mem.Allocator.Error!void {
|
||||||
try world.set_state(Mapping{});
|
try world.set_state(Mapping{});
|
||||||
|
|
||||||
try world.on_event(.pre_update, ona.system_fn(update), .{
|
try world.on_event(events.pre_update, ona.system_fn(update), .{
|
||||||
.label = "update actions",
|
.label = "update actions",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ const std = @import("std");
|
||||||
|
|
||||||
thread_pool: ?*std.Thread.Pool = null,
|
thread_pool: ?*std.Thread.Pool = null,
|
||||||
thread_restricted_resources: [std.enums.values(ona.ThreadRestriction).len]StateTable,
|
thread_restricted_resources: [std.enums.values(ona.ThreadRestriction).len]StateTable,
|
||||||
event_schedules: ona.map.Hashed([]const u8, *Schedule, ona.map.string_traits),
|
event_systems: ona.stack.Sequential(Schedule),
|
||||||
|
|
||||||
pub const BindContext = struct {
|
pub const BindContext = struct {
|
||||||
node: ona.dag.Node,
|
node: ona.dag.Node,
|
||||||
|
@ -110,6 +110,8 @@ const Graph = ona.dag.Graph(struct {
|
||||||
resource_accesses: ona.stack.Sequential(StateAccess),
|
resource_accesses: ona.stack.Sequential(StateAccess),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
pub const Event = enum (usize) { _ };
|
||||||
|
|
||||||
const ParallelNodeBundles = ona.stack.Sequential(NodeBundle);
|
const ParallelNodeBundles = ona.stack.Sequential(NodeBundle);
|
||||||
|
|
||||||
const NodeBundle = ona.stack.Sequential(ona.dag.Node);
|
const NodeBundle = ona.stack.Sequential(ona.dag.Node);
|
||||||
|
@ -365,7 +367,7 @@ const Schedule = struct {
|
||||||
|
|
||||||
errdefer {
|
errdefer {
|
||||||
bundle.deinit();
|
bundle.deinit();
|
||||||
std.debug.assert(schedule.parallel_work_bundles.pop() != null);
|
std.debug.assert(schedule.parallel_work_bundles.pop());
|
||||||
}
|
}
|
||||||
|
|
||||||
try populate_bundle(bundle, &schedule.graph, node);
|
try populate_bundle(bundle, &schedule.graph, node);
|
||||||
|
@ -440,16 +442,22 @@ const Schedule = struct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(comptime label: [:0]const u8) std.mem.Allocator.Error!Schedule {
|
pub fn init(label: []const u8) std.mem.Allocator.Error!Schedule {
|
||||||
|
var arena = std.heap.ArenaAllocator.init(ona.heap.allocator);
|
||||||
|
|
||||||
|
errdefer arena.deinit();
|
||||||
|
|
||||||
|
const duped_label = try arena.allocator().dupeZ(u8, label);
|
||||||
|
|
||||||
return .{
|
return .{
|
||||||
.graph = Graph.init(ona.heap.allocator),
|
.graph = Graph.init(ona.heap.allocator),
|
||||||
.arena = std.heap.ArenaAllocator.init(ona.heap.allocator),
|
.label = duped_label,
|
||||||
.label = label,
|
.arena = arena,
|
||||||
.system_id_nodes = .{},
|
.system_id_nodes = .{.allocator = ona.heap.allocator},
|
||||||
.read_write_resource_id_nodes = .{},
|
.read_write_resource_id_nodes = .{.allocator = ona.heap.allocator},
|
||||||
.read_only_resource_id_nodes = .{},
|
.read_only_resource_id_nodes = .{.allocator = ona.heap.allocator},
|
||||||
.parallel_work_bundles = .{},
|
.parallel_work_bundles = .{.allocator = ona.heap.allocator},
|
||||||
.blocking_work = .{},
|
.blocking_work = .{.allocator = ona.heap.allocator},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -536,12 +544,21 @@ pub const UnbindContext = struct {
|
||||||
world: *Self,
|
world: *Self,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn deinit(self: *Self) void {
|
pub fn create_event(self: *Self, label: []const u8) std.mem.Allocator.Error!Event {
|
||||||
var event_schedules = self.event_schedules.entries();
|
var systems = try Schedule.init(label);
|
||||||
|
|
||||||
while (event_schedules.next()) |schedule| {
|
errdefer systems.deinit(self);
|
||||||
schedule.value.deinit(self);
|
|
||||||
ona.heap.allocator.destroy(schedule.value);
|
const index = self.event_systems.len();
|
||||||
|
|
||||||
|
try self.event_systems.push_grow(systems);
|
||||||
|
|
||||||
|
return @enumFromInt(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *Self) void {
|
||||||
|
for (self.event_systems.values) |*schedule| {
|
||||||
|
schedule.deinit(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (&self.thread_restricted_resources) |*resources| {
|
for (&self.thread_restricted_resources) |*resources| {
|
||||||
|
@ -553,7 +570,7 @@ pub fn deinit(self: *Self) void {
|
||||||
ona.heap.allocator.destroy(thread_pool);
|
ona.heap.allocator.destroy(thread_pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.event_schedules.deinit();
|
self.event_systems.deinit();
|
||||||
|
|
||||||
self.* = undefined;
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
@ -577,7 +594,7 @@ pub fn set_get_state(self: *Self, value: anytype) std.mem.Allocator.Error!*@Type
|
||||||
pub fn init(thread_count: u32) std.Thread.SpawnError!Self {
|
pub fn init(thread_count: u32) std.Thread.SpawnError!Self {
|
||||||
var world = Self{
|
var world = Self{
|
||||||
.thread_restricted_resources = .{StateTable.init(), StateTable.init()},
|
.thread_restricted_resources = .{StateTable.init(), StateTable.init()},
|
||||||
.event_schedules = .{.allocator = ona.heap.allocator},
|
.event_systems = .{.allocator = ona.heap.allocator},
|
||||||
};
|
};
|
||||||
|
|
||||||
if (thread_count != 0 and !builtin.single_threaded) {
|
if (thread_count != 0 and !builtin.single_threaded) {
|
||||||
|
@ -594,34 +611,12 @@ pub fn init(thread_count: u32) std.Thread.SpawnError!Self {
|
||||||
return world;
|
return world;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn on_event(self: *Self, comptime event: anytype, info: *const ona.SystemInfo, order: ona.SystemOrder) std.mem.Allocator.Error!void {
|
pub fn on_event(self: *Self, event: Event, action: *const ona.SystemInfo, order: ona.SystemOrder) std.mem.Allocator.Error!void {
|
||||||
const event_name = @tagName(event);
|
try self.event_systems.values[@intFromEnum(event)].insert(action, order);
|
||||||
|
|
||||||
if (self.event_schedules.get(event_name)) |schedule| {
|
|
||||||
try schedule.*.insert(info, order);
|
|
||||||
} else {
|
|
||||||
const schedule = try ona.heap.allocator.create(Schedule);
|
|
||||||
|
|
||||||
errdefer {
|
|
||||||
ona.heap.allocator.destroy(schedule);
|
|
||||||
}
|
|
||||||
|
|
||||||
schedule.* = try Schedule.init(event_name);
|
|
||||||
|
|
||||||
errdefer {
|
|
||||||
schedule.deinit(self);
|
|
||||||
}
|
|
||||||
|
|
||||||
try schedule.insert(info, order);
|
|
||||||
|
|
||||||
std.debug.assert(try self.event_schedules.emplace(event_name, schedule));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_event(self: *Self, comptime event: anytype) anyerror!void {
|
pub fn run_event(self: *Self, event: Event) anyerror!void {
|
||||||
if (self.event_schedules.get(@tagName(event))) |schedule| {
|
try self.event_systems.values[@intFromEnum(event)].run(self);
|
||||||
try schedule.*.run(self);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_state(self: *Self, value: anytype) std.mem.Allocator.Error!void {
|
pub fn set_state(self: *Self, value: anytype) std.mem.Allocator.Error!void {
|
||||||
|
|
|
@ -30,13 +30,13 @@ pub fn Graph(comptime Payload: type) type {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear_edges(self: *Self) void {
|
pub fn clear_edges(self: *Self) void {
|
||||||
for (self.table.values.get(.edges)) |*edges| {
|
for (self.table.values.slice(.edges)) |*edges| {
|
||||||
edges.clear();
|
edges.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
for (self.table.values.get(.edges)) |*edges| {
|
for (self.table.values.slice(.edges)) |*edges| {
|
||||||
edges.deinit();
|
edges.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,17 +50,15 @@ pub fn Graph(comptime Payload: type) type {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return self.table.values.get(.edges)[@intFromEnum(node)].values;
|
return self.table.values.get(.edges, @intFromEnum(node)).?.values;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exists(self: Self, node: Node) bool {
|
pub fn exists(self: Self, node: Node) bool {
|
||||||
const node_index = @intFromEnum(node);
|
if (self.table.values.get(.is_occupied, @intFromEnum(node))) |is_occupied| {
|
||||||
|
return is_occupied.*;
|
||||||
if (node_index >= self.table.values.len) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return self.table.values.get(.is_occupied)[node_index];
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(self: Self, node: Node) ?*Payload {
|
pub fn get(self: Self, node: Node) ?*Payload {
|
||||||
|
@ -68,7 +66,7 @@ pub fn Graph(comptime Payload: type) type {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return &self.table.values.get(.payload)[@intFromEnum(node)];
|
return self.table.values.get(.payload, @intFromEnum(node)).?;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(allocator: std.mem.Allocator) Self {
|
pub fn init(allocator: std.mem.Allocator) Self {
|
||||||
|
@ -78,11 +76,13 @@ pub fn Graph(comptime Payload: type) type {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_edge(self: *Self, dependant_node: Node, edge_node: Node) std.mem.Allocator.Error!bool {
|
pub fn insert_edge(self: *Self, dependant_node: Node, edge_node: Node) std.mem.Allocator.Error!bool {
|
||||||
if (!self.exists(edge_node) or !self.exists(dependant_node)) {
|
if (!self.exists(edge_node)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const edges = &self.table.values.get(.edges)[@intFromEnum(dependant_node)];
|
const edges = self.table.values.get(.edges, @intFromEnum(dependant_node)) orelse {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
if (std.mem.indexOfScalar(Node, edges.values, edge_node) == null) {
|
if (std.mem.indexOfScalar(Node, edges.values, edge_node) == null) {
|
||||||
try edges.push_grow(edge_node);
|
try edges.push_grow(edge_node);
|
||||||
|
@ -97,7 +97,7 @@ pub fn Graph(comptime Payload: type) type {
|
||||||
|
|
||||||
pub fn nodes(self: *const Self) Nodes {
|
pub fn nodes(self: *const Self) Nodes {
|
||||||
return .{
|
return .{
|
||||||
.occupied_table = self.table.values.get(.is_occupied),
|
.occupied_table = self.table.values.slice(.is_occupied),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,7 +106,7 @@ pub fn Graph(comptime Payload: type) type {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.table.values.get(.is_visited)[@intFromEnum(node)] = true;
|
std.debug.assert(self.table.values.set(.is_visited, @intFromEnum(node), true));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -118,14 +118,14 @@ pub fn Graph(comptime Payload: type) type {
|
||||||
|
|
||||||
const node_index = @intFromEnum(node);
|
const node_index = @intFromEnum(node);
|
||||||
|
|
||||||
self.table.values.get(.is_occupied)[node_index] = false;
|
self.table.values.get(.is_occupied, node_index).?.* = false;
|
||||||
self.node_count -= 1;
|
self.node_count -= 1;
|
||||||
|
|
||||||
return self.table.values.get(.payload)[node_index];
|
return self.table.values.get(.payload, node_index).?.*;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reset_visited(self: *Self) void {
|
pub fn reset_visited(self: *Self) void {
|
||||||
@memset(self.table.values.get(.is_visited), false);
|
@memset(self.table.values.slice(.is_visited), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn visited(self: Self, node: Node) ?bool {
|
pub fn visited(self: Self, node: Node) ?bool {
|
||||||
|
@ -133,7 +133,7 @@ pub fn Graph(comptime Payload: type) type {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return self.table.values.get(.is_visited)[@intFromEnum(node)];
|
return self.table.values.get(.is_visited, @intFromEnum(node)).?.*;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ pub fn Hashed(comptime Key: type, comptime Value: type, comptime traits: Traits(
|
||||||
const max_int = std.math.maxInt(usize);
|
const max_int = std.math.maxInt(usize);
|
||||||
|
|
||||||
return struct {
|
return struct {
|
||||||
allocator: std.mem.Allocator = ona.heap.allocator,
|
allocator: std.mem.Allocator,
|
||||||
entry_map: []?Entry = &.{},
|
entry_map: []?Entry = &.{},
|
||||||
len: usize = 0,
|
len: usize = 0,
|
||||||
|
|
||||||
|
|
299
src/ona/ona.zig
299
src/ona/ona.zig
|
@ -29,11 +29,37 @@ pub const World = @import("./World.zig");
|
||||||
pub const lina = @import("./lina.zig");
|
pub const lina = @import("./lina.zig");
|
||||||
|
|
||||||
pub const App = struct {
|
pub const App = struct {
|
||||||
step: fn (*World) anyerror!void = noop,
|
events: *const Events,
|
||||||
|
target_frame_time: f64,
|
||||||
|
elapsed_time: f64,
|
||||||
|
is_running: bool,
|
||||||
|
|
||||||
fn noop(_: *World) !void {}
|
pub const Events = struct {
|
||||||
|
load: World.Event,
|
||||||
|
pre_update: World.Event,
|
||||||
|
update: World.Event,
|
||||||
|
post_update: World.Event,
|
||||||
|
render: World.Event,
|
||||||
|
finish: World.Event,
|
||||||
|
exit: World.Event,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn build(self: App) fn () anyerror!void {
|
pub const Setup = struct {
|
||||||
|
pub const Event = enum {
|
||||||
|
load,
|
||||||
|
pre_update,
|
||||||
|
update,
|
||||||
|
post_update,
|
||||||
|
render,
|
||||||
|
finish,
|
||||||
|
exit,
|
||||||
|
};
|
||||||
|
|
||||||
|
step: fn (*World, Events) anyerror!void = noop,
|
||||||
|
|
||||||
|
fn noop(_: *World, _: Events) !void {}
|
||||||
|
|
||||||
|
pub fn build(self: Setup) fn () anyerror!void {
|
||||||
const Start = struct {
|
const Start = struct {
|
||||||
fn main() anyerror!void {
|
fn main() anyerror!void {
|
||||||
defer {
|
defer {
|
||||||
|
@ -74,60 +100,66 @@ pub const App = struct {
|
||||||
|
|
||||||
defer world.deinit();
|
defer world.deinit();
|
||||||
|
|
||||||
try self.step(&world);
|
const events = App.Events{
|
||||||
|
.load = try world.create_event("load"),
|
||||||
|
.pre_update = try world.create_event("pre-update"),
|
||||||
|
.update = try world.create_event("update"),
|
||||||
|
.post_update = try world.create_event("post-update"),
|
||||||
|
.render = try world.create_event("render"),
|
||||||
|
.finish = try world.create_event("finish"),
|
||||||
|
.exit = try world.create_event("exit"),
|
||||||
|
};
|
||||||
|
|
||||||
|
const app = try world.set_get_state(App{
|
||||||
|
.events = &events,
|
||||||
|
.target_frame_time = 1.0 / 60.0,
|
||||||
|
.elapsed_time = 0,
|
||||||
|
.is_running = true,
|
||||||
|
});
|
||||||
|
|
||||||
|
try self.step(&world, events);
|
||||||
|
try world.run_event(events.load);
|
||||||
|
|
||||||
|
const ticks_initial = std.time.milliTimestamp();
|
||||||
|
var ticks_previous = ticks_initial;
|
||||||
|
var accumulated_time = @as(f64, 0);
|
||||||
|
|
||||||
|
while (app.is_running) {
|
||||||
|
const ticks_current = std.time.milliTimestamp();
|
||||||
|
const milliseconds_per_second = 1000.0;
|
||||||
|
const delta_time = @as(f64, @floatFromInt(ticks_current - ticks_previous)) / milliseconds_per_second;
|
||||||
|
|
||||||
|
app.elapsed_time = @as(f64, @floatFromInt(ticks_current - ticks_initial)) / milliseconds_per_second;
|
||||||
|
ticks_previous = ticks_current;
|
||||||
|
accumulated_time += delta_time;
|
||||||
|
|
||||||
|
try world.run_event(events.pre_update);
|
||||||
|
|
||||||
|
while (accumulated_time >= app.target_frame_time) : (accumulated_time -= app.target_frame_time) {
|
||||||
|
try world.run_event(events.update);
|
||||||
|
}
|
||||||
|
|
||||||
|
try world.run_event(events.post_update);
|
||||||
|
try world.run_event(events.render);
|
||||||
|
try world.run_event(events.finish);
|
||||||
|
}
|
||||||
|
|
||||||
|
try world.run_event(events.exit);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return Start.main;
|
return Start.main;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const game = App{.step = run_game};
|
pub fn with_module(self: Setup, comptime Module: type) Setup {
|
||||||
|
|
||||||
fn run_game(world: *World) !void {
|
|
||||||
const loop = try world.set_get_state(Loop{
|
|
||||||
.target_frame_time = 1.0 / 60.0,
|
|
||||||
.elapsed_time = 0,
|
|
||||||
.is_running = true,
|
|
||||||
});
|
|
||||||
|
|
||||||
try world.run_event(.load);
|
|
||||||
|
|
||||||
const ticks_initial = std.time.milliTimestamp();
|
|
||||||
var ticks_previous = ticks_initial;
|
|
||||||
var accumulated_time = @as(f64, 0);
|
|
||||||
|
|
||||||
while (loop.is_running) {
|
|
||||||
const ticks_current = std.time.milliTimestamp();
|
|
||||||
const milliseconds_per_second = 1000.0;
|
|
||||||
const delta_time = @as(f64, @floatFromInt(ticks_current - ticks_previous)) / milliseconds_per_second;
|
|
||||||
|
|
||||||
loop.elapsed_time = @as(f64, @floatFromInt(ticks_current - ticks_initial)) / milliseconds_per_second;
|
|
||||||
ticks_previous = ticks_current;
|
|
||||||
accumulated_time += delta_time;
|
|
||||||
|
|
||||||
try world.run_event(.pre_update);
|
|
||||||
|
|
||||||
while (accumulated_time >= loop.target_frame_time) : (accumulated_time -= loop.target_frame_time) {
|
|
||||||
try world.run_event(.update);
|
|
||||||
}
|
|
||||||
|
|
||||||
try world.run_event(.post_update);
|
|
||||||
try world.run_event(.render);
|
|
||||||
try world.run_event(.finish);
|
|
||||||
}
|
|
||||||
|
|
||||||
try world.run_event(.exit);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn with_module(self: App, comptime Module: type) App {
|
|
||||||
if (!@hasDecl(Module, "setup")) {
|
if (!@hasDecl(Module, "setup")) {
|
||||||
@compileError("`Module` argument must have a setup fn declaration");
|
@compileError("`Module` argument must have a setup fn declaration");
|
||||||
}
|
}
|
||||||
|
|
||||||
const WithState = struct {
|
const WithState = struct {
|
||||||
fn step(world: *World) !void {
|
fn step(world: *World, events: App.Events) !void {
|
||||||
try Module.setup(world);
|
try Module.setup(world, events);
|
||||||
try self.step(world);
|
try self.step(world, events);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -136,11 +168,11 @@ pub const App = struct {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_state(self: App, comptime state: anytype) App {
|
pub fn with_state(self: Setup, comptime state: anytype) Setup {
|
||||||
const WithState = struct {
|
const WithState = struct {
|
||||||
fn step(world: *World) !void {
|
fn step(world: *World, events: App.Events) !void {
|
||||||
try world.set_state(state);
|
try world.set_state(state);
|
||||||
try self.step(world);
|
try self.step(world, events);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -149,11 +181,21 @@ pub const App = struct {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_system(self: App, comptime event: anytype, comptime info: *const SystemInfo, comptime order: SystemOrder) App {
|
pub fn with_system(self: Setup, comptime event: Event, comptime info: *const SystemInfo, comptime order: SystemOrder) Setup {
|
||||||
const WithState = struct {
|
const WithState = struct {
|
||||||
fn step(world: *World) !void {
|
fn step(world: *World, events: App.Events) !void {
|
||||||
try world.on_event(event, info, order);
|
const app_event = switch (event) {
|
||||||
try self.step(world);
|
.load => events.load,
|
||||||
|
.pre_update => events.pre_update,
|
||||||
|
.update => events.update,
|
||||||
|
.post_update => events.post_update,
|
||||||
|
.render => events.render,
|
||||||
|
.finish => events.finish,
|
||||||
|
.exit => events.exit,
|
||||||
|
};
|
||||||
|
|
||||||
|
try world.on_event(app_event, info, order);
|
||||||
|
try self.step(world, events);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -161,14 +203,11 @@ pub const App = struct {
|
||||||
.step = WithState.step,
|
.step = WithState.step,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Loop = struct {
|
pub const setup = Setup{};
|
||||||
target_frame_time: f64,
|
|
||||||
elapsed_time: f64,
|
|
||||||
is_running: bool,
|
|
||||||
|
|
||||||
pub fn quit(self: *Loop) void {
|
pub fn quit(self: *App) void {
|
||||||
self.is_running = false;
|
self.is_running = false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -316,10 +355,7 @@ pub const LaunchFlag = enum {
|
||||||
|
|
||||||
pub fn Params(comptime Value: type) type {
|
pub fn Params(comptime Value: type) type {
|
||||||
if (!@hasDecl(Value, "Param")) {
|
if (!@hasDecl(Value, "Param")) {
|
||||||
@compileError(@typeName(Value) ++
|
@compileError("System parameters must have a Params type declaration");
|
||||||
\\ is missing a public `Params` type declaration.
|
|
||||||
\\This is required for a type to be used as a system parameter
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return struct {
|
return struct {
|
||||||
|
@ -483,11 +519,15 @@ pub fn Receive(comptime Message: type) type {
|
||||||
.channel = (try context.register_readable_state_access(TypedChannel)) orelse set: {
|
.channel = (try context.register_readable_state_access(TypedChannel)) orelse set: {
|
||||||
try context.world.set_state(TypedChannel.init(heap.allocator));
|
try context.world.set_state(TypedChannel.init(heap.allocator));
|
||||||
|
|
||||||
try context.world.on_event(.post_update, system_fn(TypedChannel.swap), .{
|
const app = context.world.get_state(App) orelse {
|
||||||
|
@panic("Send system parameters depend on a " ++ @typeName(App) ++ " state to work");
|
||||||
|
};
|
||||||
|
|
||||||
|
try context.world.on_event(app.events.post_update, system_fn(TypedChannel.swap), .{
|
||||||
.label = "swap channel of " ++ @typeName(Message),
|
.label = "swap channel of " ++ @typeName(Message),
|
||||||
});
|
});
|
||||||
|
|
||||||
try context.world.on_event(.exit, system_fn(TypedChannel.cleanup), .{
|
try context.world.on_event(app.events.exit, system_fn(TypedChannel.cleanup), .{
|
||||||
.label = "clean up channel of " ++ @typeName(Message),
|
.label = "clean up channel of " ++ @typeName(Message),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -525,11 +565,15 @@ pub fn Send(comptime Message: type) type {
|
||||||
.channel = (try context.register_writable_state_access(TypedChannel)) orelse set: {
|
.channel = (try context.register_writable_state_access(TypedChannel)) orelse set: {
|
||||||
try context.world.set_state(TypedChannel.init(heap.allocator));
|
try context.world.set_state(TypedChannel.init(heap.allocator));
|
||||||
|
|
||||||
try context.world.on_event(.post_update, system_fn(TypedChannel.swap), .{
|
const app = context.world.get_state(App) orelse {
|
||||||
|
@panic("Send system parameters depend on a " ++ @typeName(App) ++ " state to work");
|
||||||
|
};
|
||||||
|
|
||||||
|
try context.world.on_event(app.events.post_update, system_fn(TypedChannel.swap), .{
|
||||||
.label = "swap channel of " ++ @typeName(Message),
|
.label = "swap channel of " ++ @typeName(Message),
|
||||||
});
|
});
|
||||||
|
|
||||||
try context.world.on_event(.exit, system_fn(TypedChannel.cleanup), .{
|
try context.world.on_event(app.events.exit, system_fn(TypedChannel.cleanup), .{
|
||||||
.label = "clean up channel of " ++ @typeName(Message),
|
.label = "clean up channel of " ++ @typeName(Message),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -635,127 +679,6 @@ pub fn Shared(comptime Value: type, comptime info: ShareInfo) type {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn SlotMap(comptime Value: type) type {
|
|
||||||
const Index = @Type(.{.Int = .{
|
|
||||||
.bits = @bitSizeOf(usize) - 1,
|
|
||||||
.signedness = .unsigned,
|
|
||||||
}});
|
|
||||||
|
|
||||||
const Slot = packed struct {
|
|
||||||
index: Index,
|
|
||||||
is_occupied: bool,
|
|
||||||
};
|
|
||||||
|
|
||||||
comptime std.debug.assert(@sizeOf(Slot) == @sizeOf(usize));
|
|
||||||
|
|
||||||
return struct {
|
|
||||||
slots: stack.Sequential(Slot),
|
|
||||||
entries: stack.Parallel(Entry),
|
|
||||||
next: Index = 0,
|
|
||||||
|
|
||||||
const Entry = struct {
|
|
||||||
value: Value,
|
|
||||||
erased: usize,
|
|
||||||
};
|
|
||||||
|
|
||||||
const Self = @This();
|
|
||||||
|
|
||||||
pub fn deinit(self: *Self) void {
|
|
||||||
self.slots.deinit();
|
|
||||||
self.entries.deinit();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get(self: Self, index: usize) ?*Value {
|
|
||||||
if (index >= self.slots.len()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const slot = self.slots.values[index];
|
|
||||||
|
|
||||||
if (!slot.is_occupied) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return &self.entries.values.get(.value)[slot.index];
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init(allocator: std.mem.Allocator) Self {
|
|
||||||
return .{
|
|
||||||
.slots = .{.allocator = allocator},
|
|
||||||
.entries = .{.allocator = allocator},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn insert(self: *Self, value: Value) std.mem.Allocator.Error!usize {
|
|
||||||
const index = self.next;
|
|
||||||
const slots_len: Index = @intCast(self.slots.len());
|
|
||||||
|
|
||||||
if (self.next == slots_len) {
|
|
||||||
try self.slots.push_grow(.{
|
|
||||||
.index = slots_len + 1,
|
|
||||||
.is_occupied = false,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const slot = &self.slots.values[index];
|
|
||||||
|
|
||||||
std.debug.assert(!slot.is_occupied);
|
|
||||||
|
|
||||||
self.next = slot.index;
|
|
||||||
|
|
||||||
slot.* = .{
|
|
||||||
.index = index,
|
|
||||||
.is_occupied = true,
|
|
||||||
};
|
|
||||||
|
|
||||||
try self.entries.push_grow(.{
|
|
||||||
.value = value,
|
|
||||||
.erased = index,
|
|
||||||
});
|
|
||||||
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remove(self: *Self, index: usize) ?Value {
|
|
||||||
if (index >= self.slots.len()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const slot = self.slots.values[index];
|
|
||||||
|
|
||||||
if (!slot.is_occupied) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const value = self.entries.values.get(.value)[slot.index];
|
|
||||||
|
|
||||||
std.debug.assert(self.entries.values.write(slot.index, .{
|
|
||||||
.value = self.entries.get(.value).?.*,
|
|
||||||
.erased = self.entries.get(.erased).?.*,
|
|
||||||
}));
|
|
||||||
|
|
||||||
std.debug.assert(self.entries.pop());
|
|
||||||
|
|
||||||
if (!self.entries.is_empty()) {
|
|
||||||
self.slots.values[self.entries.values.get(.erased)[slot.index]] = slot;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.slots.values[index] = .{
|
|
||||||
.index = self.next,
|
|
||||||
.is_occupied = false,
|
|
||||||
};
|
|
||||||
|
|
||||||
self.next = @intCast(index);
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn values(self: Self) []Value {
|
|
||||||
return self.entries.values.get(.value);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const SystemInfo = struct {
|
pub const SystemInfo = struct {
|
||||||
execute: *const fn ([]const *const Parameter, *const [max_parameters]*anyopaque) anyerror!void,
|
execute: *const fn ([]const *const Parameter, *const [max_parameters]*anyopaque) anyerror!void,
|
||||||
parameters: [max_parameters]*const Parameter = undefined,
|
parameters: [max_parameters]*const Parameter = undefined,
|
||||||
|
|
|
@ -35,15 +35,15 @@ pub fn Parallel(comptime Type: type) type {
|
||||||
|
|
||||||
const all_fields = std.enums.values(Field);
|
const all_fields = std.enums.values(Field);
|
||||||
|
|
||||||
pub fn get(self: Self, comptime field: Field) []align (alignment) Element(field) {
|
|
||||||
return self.ptr(field)[0 .. self.len];
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn ptr(self: Self, comptime field: Field) [*]align (alignment) Element(field) {
|
pub fn ptr(self: Self, comptime field: Field) [*]align (alignment) Element(field) {
|
||||||
return @as([*]align (alignment) Element(field), @ptrCast(self.ptrs[@intFromEnum(field)]));
|
return @as([*]align (alignment) Element(field), @ptrCast(self.ptrs[@intFromEnum(field)]));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn slice(self: Self, off: usize, len: usize) ?Self {
|
pub fn slice(self: Self, comptime field: Field) []align (alignment) Element(field) {
|
||||||
|
return self.ptr(field)[0 .. self.len];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn slice_all(self: Self, off: usize, len: usize) ?Self {
|
||||||
if (len > self.len or off > len) {
|
if (len > self.len or off > len) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -57,13 +57,31 @@ pub fn Parallel(comptime Type: type) type {
|
||||||
return sliced;
|
return sliced;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(self: Self, index: usize, value: Type) bool {
|
pub fn get(self: Self, comptime field: Field, index: usize) ?*Element(field) {
|
||||||
|
if (index >= self.len) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &self.ptr(field)[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set(self: Self, comptime field: Field, index: usize, value: Element(field)) bool {
|
||||||
|
if (index >= self.len) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.slice(field)[index] = value;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_all(self: Self, index: usize, value: Type) bool {
|
||||||
if (index >= self.len) {
|
if (index >= self.len) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline for (0 .. fields.len) |i| {
|
inline for (0 .. fields.len) |i| {
|
||||||
self.get(all_fields[i])[index] = @field(value, fields[i].name);
|
self.slice(all_fields[i])[index] = @field(value, fields[i].name);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -105,12 +123,12 @@ pub fn parallel_alloc(comptime Element: type, allocator: std.mem.Allocator, n: u
|
||||||
|
|
||||||
pub fn parallel_copy(comptime Element: type, target: Parallel(Element), origin: Parallel(Element)) void {
|
pub fn parallel_copy(comptime Element: type, target: Parallel(Element), origin: Parallel(Element)) void {
|
||||||
inline for (comptime std.enums.values(Parallel(Element).Field)) |field| {
|
inline for (comptime std.enums.values(Parallel(Element).Field)) |field| {
|
||||||
@memcpy(target.get(field), origin.get(field));
|
@memcpy(target.slice(field), origin.slice(field));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parallel_free(comptime Element: type, allocator: std.mem.Allocator, buffers: Parallel(Element)) void {
|
pub fn parallel_free(comptime Element: type, allocator: std.mem.Allocator, buffers: Parallel(Element)) void {
|
||||||
inline for (comptime std.enums.values(Parallel(Element).Field)) |field| {
|
inline for (comptime std.enums.values(Parallel(Element).Field)) |field| {
|
||||||
allocator.free(buffers.get(field));
|
allocator.free(buffers.slice(field));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,12 +25,11 @@ pub fn Sequential(comptime Value: type) type {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn grow(self: *Self, additional: usize) std.mem.Allocator.Error!void {
|
pub fn grow(self: *Self, additional: usize) std.mem.Allocator.Error!void {
|
||||||
const grown_capacity = self.values.len + additional;
|
if (additional == 0) {
|
||||||
|
|
||||||
if (grown_capacity <= self.cap) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const grown_capacity = self.cap + additional;
|
||||||
const buffer = try self.allocator.alloc(Value, grown_capacity);
|
const buffer = try self.allocator.alloc(Value, grown_capacity);
|
||||||
|
|
||||||
errdefer self.allocator.deallocate(buffer);
|
errdefer self.allocator.deallocate(buffer);
|
||||||
|
@ -60,20 +59,14 @@ pub fn Sequential(comptime Value: type) type {
|
||||||
return self.values.len;
|
return self.values.len;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pop(self: *Self) ?Value {
|
pub fn pop(self: *Self) bool {
|
||||||
if (self.values.len == 0) {
|
if (self.values.len == 0) {
|
||||||
return null;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const tail_index = self.values.len - 1;
|
self.values = self.values[0 .. self.values.len - 1];
|
||||||
|
|
||||||
defer self.values = self.values[0 .. tail_index];
|
return true;
|
||||||
|
|
||||||
return self.values[tail_index];
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn pop_all(self: *Self) void {
|
|
||||||
self.values = self.values[0 .. 0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pop_many(self: *Self, n: usize) bool {
|
pub fn pop_many(self: *Self, n: usize) bool {
|
||||||
|
@ -119,7 +112,9 @@ pub fn Sequential(comptime Value: type) type {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push_grow(self: *Self, value: Value) std.mem.Allocator.Error!void {
|
pub fn push_grow(self: *Self, value: Value) std.mem.Allocator.Error!void {
|
||||||
try self.grow(@max(1, self.values.len));
|
if (self.values.len == self.cap) {
|
||||||
|
try self.grow(@max(1, self.cap));
|
||||||
|
}
|
||||||
|
|
||||||
const offset_index = self.values.len;
|
const offset_index = self.values.len;
|
||||||
|
|
||||||
|
@ -232,63 +227,46 @@ pub fn Parallel(comptime Value: type) type {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return @alignCast(&self.values.get(field)[self.len() - 1]);
|
return &self.slices.field_slice(field)[self.len() - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn grow(self: *Self, additional: usize) std.mem.Allocator.Error!void {
|
pub fn grow(self: *Self, additional: usize) std.mem.Allocator.Error!void {
|
||||||
const grown_capacity = self.values.len + additional;
|
const grown_capacity = self.cap + additional;
|
||||||
|
|
||||||
if (grown_capacity <= self.cap) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const buffer = try ona.slices.parallel_alloc(Value, self.allocator, grown_capacity);
|
const buffer = try ona.slices.parallel_alloc(Value, self.allocator, grown_capacity);
|
||||||
|
|
||||||
if (self.cap != 0) {
|
if (self.cap != 0) {
|
||||||
ona.slices.parallel_copy(Value, buffer.slice(0, self.values.len).?, self.values);
|
ona.slices.parallel_copy(Value, buffer.slice_all(0, self.values.len).?, self.values);
|
||||||
ona.slices.parallel_free(Value, self.allocator, self.values.slice(0, self.cap).?);
|
ona.slices.parallel_free(Value, self.allocator, self.values.slice_all(0, self.cap).?);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.cap = grown_capacity;
|
self.cap = grown_capacity;
|
||||||
self.values = buffer.slice(0, self.values.len).?;
|
self.values = buffer.slice_all(0, self.values.len).?;
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_empty(self: Self) bool {
|
|
||||||
return self.values.len == 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn len(self: Self) usize {
|
pub fn len(self: Self) usize {
|
||||||
return self.values.len;
|
return self.values.len;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pop(self: *Self) bool {
|
|
||||||
if (self.values.len == 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.values = self.values.slice(0, self.values.len - 1).?;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn pop_many(self: *Self, n: usize) bool {
|
pub fn pop_many(self: *Self, n: usize) bool {
|
||||||
const new_length = ona.scalars.sub(self.values.len, n) orelse {
|
const new_length = ona.scalars.sub(self.values.len, n) orelse {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
self.values = self.values.slice(0, new_length).?;
|
self.values = self.values.slice_all(0, new_length).?;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push_grow(self: *Self, value: Value) std.mem.Allocator.Error!void {
|
pub fn push_grow(self: *Self, value: Value) std.mem.Allocator.Error!void {
|
||||||
try self.grow(@max(1, self.values.len));
|
if (self.len() == self.cap) {
|
||||||
|
try self.grow(@max(1, self.cap));
|
||||||
|
}
|
||||||
|
|
||||||
const tail_index = self.values.len;
|
const tail_index = self.values.len;
|
||||||
|
|
||||||
self.values.len += 1;
|
self.values.len += 1;
|
||||||
|
|
||||||
std.debug.assert(self.values.write(tail_index, value));
|
std.debug.assert(self.values.set_all(tail_index, value));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue