Skip to content

Commit

Permalink
enhance 2d animation: support draw command as frame data
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Ji committed Nov 30, 2024
1 parent e6f9f45 commit 513f8ee
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
26 changes: 13 additions & 13 deletions examples/animation_2d.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,30 @@ pub fn init(ctx: jok.Context) !void {
const player = sheet.getSpriteByName("player").?;
try as.addSimple(
"player_left_right",
&[_]j2d.Sprite{
player.getSubSprite(4 * 16, 0, 16, 16),
player.getSubSprite(5 * 16, 0, 16, 16),
player.getSubSprite(3 * 16, 0, 16, 16),
&.{
.{ .sp = player.getSubSprite(4 * 16, 0, 16, 16) },
.{ .sp = player.getSubSprite(5 * 16, 0, 16, 16) },
.{ .sp = player.getSubSprite(3 * 16, 0, 16, 16) },
},
6,
false,
);
try as.addSimple(
"player_up",
&[_]j2d.Sprite{
player.getSubSprite(7 * 16, 0, 16, 16),
player.getSubSprite(8 * 16, 0, 16, 16),
player.getSubSprite(6 * 16, 0, 16, 16),
&.{
.{ .sp = player.getSubSprite(7 * 16, 0, 16, 16) },
.{ .sp = player.getSubSprite(8 * 16, 0, 16, 16) },
.{ .sp = player.getSubSprite(6 * 16, 0, 16, 16) },
},
6,
false,
);
try as.addSimple(
"player_down",
&[_]j2d.Sprite{
player.getSubSprite(1 * 16, 0, 16, 16),
player.getSubSprite(2 * 16, 0, 16, 16),
player.getSubSprite(0 * 16, 0, 16, 16),
&.{
.{ .sp = player.getSubSprite(1 * 16, 0, 16, 16) },
.{ .sp = player.getSubSprite(2 * 16, 0, 16, 16) },
.{ .sp = player.getSubSprite(0 * 16, 0, 16, 16) },
},
6,
false,
Expand Down Expand Up @@ -110,7 +110,7 @@ pub fn draw(ctx: jok.Context) !void {
},
);
try b.sprite(
try as.getCurrentFrame(animation),
(try as.getCurrentFrame(animation)).sp,
.{
.pos = pos,
.flip_h = flip_h,
Expand Down
31 changes: 18 additions & 13 deletions src/j2d/AnimationSystem.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const assert = std.debug.assert;
const jok = @import("../jok.zig");
const internal = @import("internal.zig");
const Sprite = @import("Sprite.zig");
const Self = @This();

Expand Down Expand Up @@ -35,29 +36,29 @@ pub fn destroy(self: *Self) void {
pub fn add(
self: *Self,
name: []const u8,
sprites: []const Sprite,
frames: []const Frame,
durations: []const f32,
loop: bool,
) !void {
assert(name.len > 0);
assert(sprites.len > 0);
assert(sprites.len == durations.len);
assert(frames.len > 0);
assert(frames.len == durations.len);
if (self.animations.get(name) != null) {
return error.NameUsed;
}
const dname = try self.allocator.dupe(u8, name);
errdefer self.allocator.free(dname);
const anim = Animation{
.name = dname,
.frames = try self.allocator.alloc(Frame, sprites.len),
.frames = try self.allocator.alloc(Frame, frames.len),
.loop = loop,
.play_index = 0,
.passed_time = 0,
.is_over = false,
};
errdefer self.allocator.free(anim.frames);
for (anim.frames, 0..) |*f, i| {
f.sp = sprites[i];
f.data = frames[i];
f.duration = durations[i];
}
try self.animations.put(dname, anim);
Expand All @@ -67,12 +68,12 @@ pub fn add(
pub fn addSimple(
self: *Self,
name: []const u8,
sprites: []const Sprite,
frames: []const Frame.Data,
fps: f32,
loop: bool,
) !void {
assert(name.len > 0);
assert(sprites.len > 0);
assert(frames.len > 0);
assert(fps > 0);
if (self.animations.get(name) != null) {
return error.NameUsed;
Expand All @@ -81,7 +82,7 @@ pub fn addSimple(
errdefer self.allocator.free(dname);
const anim = Animation{
.name = dname,
.frames = try self.allocator.alloc(Frame, sprites.len),
.frames = try self.allocator.alloc(Frame, frames.len),
.loop = loop,
.play_index = 0,
.passed_time = 0,
Expand All @@ -90,7 +91,7 @@ pub fn addSimple(
errdefer self.allocator.free(anim.frames);
const duration = 1.0 / fps;
for (anim.frames, 0..) |*f, i| {
f.sp = sprites[i];
f.data = frames[i];
f.duration = duration;
}
try self.animations.put(dname, anim);
Expand Down Expand Up @@ -129,7 +130,7 @@ pub fn update(self: *Self, delta_tick: f32) void {
}

/// Get animation's current frame
pub fn getCurrentFrame(self: Self, name: []const u8) !Sprite {
pub fn getCurrentFrame(self: Self, name: []const u8) !Frame.Data {
if (self.animations.get(name)) |anim| {
return anim.getCurrentFrame();
}
Expand All @@ -153,7 +154,11 @@ pub fn reset(self: *Self, name: []const u8) !void {
}

pub const Frame = struct {
sp: Sprite,
pub const Data = union(enum) {
sp: Sprite,
dcmd: internal.DrawCmd,
};
data: Data,
duration: f32,
};

Expand All @@ -172,8 +177,8 @@ pub const Animation = struct {
anim.is_over = false;
}

pub fn getCurrentFrame(self: Animation) Sprite {
return self.frames[self.play_index].sp;
pub fn getCurrentFrame(self: Animation) Frame.Data {
return self.frames[self.play_index].data;
}

pub fn update(anim: *Animation, delta_tick: f32) void {
Expand Down

0 comments on commit 513f8ee

Please sign in to comment.