module: change state to avoid undefined, remove global allocator

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-04-05 17:05:48 -07:00 committed by Stephen Gutekanst
parent 5dbf9ece3b
commit cf883f5544
11 changed files with 271 additions and 222 deletions

View file

@ -15,6 +15,8 @@ const Vec3 = math.Vec3;
const Mat3x3 = math.Mat3x3;
const Mat4x4 = math.Mat4x4;
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
timer: mach.Timer,
player: mach.ecs.EntityID,
direction: Vec2 = vec2(0, 0),
@ -25,6 +27,7 @@ frame_count: usize,
sprites: usize,
rand: std.rand.DefaultPrng,
time: f32,
allocator: std.mem.Allocator,
const d0 = 0.000001;
@ -68,13 +71,14 @@ fn init(
try sprite_mod.set(player, .uv_transform, Mat3x3.translate(vec2(0, 0)));
try sprite_mod.set(player, .pipeline, @intFromEnum(Pipeline.default));
const allocator = gpa.allocator();
sprite_mod.send(.init_pipeline, .{Sprite.PipelineOptions{
.pipeline = @intFromEnum(Pipeline.default),
.texture = try loadTexture(engine),
.texture = try loadTexture(engine, allocator),
}});
sprite_mod.send(.updated, .{@intFromEnum(Pipeline.default)});
game.state = .{
game.init(.{
.timer = try mach.Timer.start(),
.spawn_timer = try mach.Timer.start(),
.player = player,
@ -83,7 +87,8 @@ fn init(
.sprites = 0,
.rand = std.rand.DefaultPrng.init(1337),
.time = 0,
};
.allocator = allocator,
});
}
fn tick(
@ -93,8 +98,8 @@ fn tick(
) !void {
// TODO(engine): event polling should occur in mach.Engine module and get fired as ECS events.
var iter = core.pollEvents();
var direction = game.state.direction;
var spawning = game.state.spawning;
var direction = game.state().direction;
var spawning = game.state().spawning;
while (iter.next()) |event| {
switch (event) {
.key_press => |ev| {
@ -121,30 +126,30 @@ fn tick(
else => {},
}
}
game.state.direction = direction;
game.state.spawning = spawning;
game.state().direction = direction;
game.state().spawning = spawning;
var player_transform = sprite_mod.get(game.state.player, .transform).?;
var player_transform = sprite_mod.get(game.state().player, .transform).?;
var player_pos = player_transform.translation();
if (spawning and game.state.spawn_timer.read() > 1.0 / 60.0) {
if (spawning and game.state().spawn_timer.read() > 1.0 / 60.0) {
// Spawn new entities
_ = game.state.spawn_timer.lap();
_ = game.state().spawn_timer.lap();
for (0..100) |_| {
var new_pos = player_pos;
new_pos.v[0] += game.state.rand.random().floatNorm(f32) * 25;
new_pos.v[1] += game.state.rand.random().floatNorm(f32) * 25;
new_pos.v[0] += game.state().rand.random().floatNorm(f32) * 25;
new_pos.v[1] += game.state().rand.random().floatNorm(f32) * 25;
const new_entity = try engine.newEntity();
try sprite_mod.set(new_entity, .transform, Mat4x4.translate(new_pos).mul(&Mat4x4.scale(Vec3.splat(0.3))));
try sprite_mod.set(new_entity, .size, vec2(32, 32));
try sprite_mod.set(new_entity, .uv_transform, Mat3x3.translate(vec2(0, 0)));
try sprite_mod.set(new_entity, .pipeline, @intFromEnum(Pipeline.default));
game.state.sprites += 1;
game.state().sprites += 1;
}
}
// Multiply by delta_time to ensure that movement is the same speed regardless of the frame rate.
const delta_time = game.state.timer.lap();
const delta_time = game.state().timer.lap();
// Rotate entities
var archetypes_iter = engine.entities.query(.{ .all = &.{
@ -161,8 +166,8 @@ fn tick(
// transform = transform.mul(&Mat4x4.translate(location));
var transform = Mat4x4.ident;
transform = transform.mul(&Mat4x4.translate(location));
transform = transform.mul(&Mat4x4.rotateZ(2 * math.pi * game.state.time));
transform = transform.mul(&Mat4x4.scaleScalar(@min(math.cos(game.state.time / 2.0), 0.5)));
transform = transform.mul(&Mat4x4.rotateZ(2 * math.pi * game.state().time));
transform = transform.mul(&Mat4x4.scaleScalar(@min(math.cos(game.state().time / 2.0), 0.5)));
// TODO: .set() API is substantially slower due to internals
// try sprite_mod.set(id, .transform, transform);
@ -175,7 +180,7 @@ fn tick(
const speed = 200.0;
player_pos.v[0] += direction.x() * speed * delta_time;
player_pos.v[1] += direction.y() * speed * delta_time;
try sprite_mod.set(game.state.player, .transform, Mat4x4.translate(player_pos));
try sprite_mod.set(game.state().player, .transform, Mat4x4.translate(player_pos));
sprite_mod.send(.updated, .{@intFromEnum(Pipeline.default)});
// Perform pre-render work
@ -188,24 +193,22 @@ fn tick(
engine.send(.present, .{}); // Present the frame
// Every second, update the window title with the FPS
if (game.state.fps_timer.read() >= 1.0) {
try core.printTitle("gfx.Sprite example [ FPS: {d} ] [ Sprites: {d} ]", .{ game.state.frame_count, game.state.sprites });
game.state.fps_timer.reset();
game.state.frame_count = 0;
if (game.state().fps_timer.read() >= 1.0) {
try core.printTitle("gfx.Sprite example [ FPS: {d} ] [ Sprites: {d} ]", .{ game.state().frame_count, game.state().sprites });
game.state().fps_timer.reset();
game.state().frame_count = 0;
}
game.state.frame_count += 1;
game.state.time += delta_time;
game.state().frame_count += 1;
game.state().time += delta_time;
}
// TODO: move this helper into gfx module
fn loadTexture(
engine: *mach.Engine.Mod,
) !*gpu.Texture {
const device = engine.state.device;
fn loadTexture(engine: *mach.Engine.Mod, allocator: std.mem.Allocator) !*gpu.Texture {
const device = engine.state().device;
const queue = device.getQueue();
// Load the image from memory
var img = try zigimg.Image.fromMemory(engine.allocator, assets.sprites_sheet_png);
var img = try zigimg.Image.fromMemory(allocator, assets.sprites_sheet_png);
defer img.deinit();
const img_size = gpu.Extent3D{ .width = @as(u32, @intCast(img.width)), .height = @as(u32, @intCast(img.height)) };
@ -226,8 +229,8 @@ fn loadTexture(
switch (img.pixels) {
.rgba32 => |pixels| queue.writeTexture(&.{ .texture = texture }, &data_layout, &img_size, pixels),
.rgb24 => |pixels| {
const data = try rgb24ToRgba32(engine.allocator, pixels);
defer data.deinit(engine.allocator);
const data = try rgb24ToRgba32(allocator, pixels);
defer data.deinit(allocator);
queue.writeTexture(&.{ .texture = texture }, &data_layout, &img_size, data.rgba32);
},
else => @panic("unsupported image color format"),