Skip to content

Commit

Permalink
enhance 2d animation: more friendly api and more methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Ji committed Dec 1, 2024
1 parent 4808570 commit bfd5bb9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.{
.name = "jok",
.version = "0.24.1",
.version = "0.24.2",
.paths = .{
"README.md",
"build.zig",
Expand Down
33 changes: 24 additions & 9 deletions src/j2d/AnimationSystem.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@ pub fn add(
self: *Self,
name: []const u8,
frames: []const Frame,
durations: []const f32,
loop: bool,
) !void {
assert(name.len > 0);
assert(frames.len > 0);
assert(frames.len == durations.len);
if (self.animations.get(name) != null) {
return error.NameUsed;
}
Expand All @@ -55,12 +53,10 @@ pub fn add(
.play_index = 0,
.passed_time = 0,
.is_over = false,
.is_stopped = false,
};
errdefer self.allocator.free(anim.frames);
for (anim.frames, 0..) |*f, i| {
f.data = frames[i];
f.duration = durations[i];
}
@memcpy(anim.frames, frames);
try self.animations.put(dname, anim);
}

Expand All @@ -87,6 +83,7 @@ pub fn addSimple(
.play_index = 0,
.passed_time = 0,
.is_over = false,
.is_stopped = false,
};
errdefer self.allocator.free(anim.frames);
const duration = 1.0 / fps;
Expand Down Expand Up @@ -145,14 +142,30 @@ pub fn isOver(self: Self, name: []const u8) !bool {
return error.NameNotExist;
}

/// Reset animation's status
/// Reset animation's status (clear over/stop state)
pub fn reset(self: *Self, name: []const u8) !void {
if (self.animations.getPtr(name)) |anim| {
return anim.reset();
}
return error.NameNotExist;
}

/// Set animation's stop state
pub fn setStop(self: *Self, name: []const u8, b: bool) !void {
if (self.animations.getPtr(name)) |anim| {
anim.is_stopped = b;
}
return error.NameNotExist;
}

/// Set animation's loop state
pub fn setLoop(self: *Self, name: []const u8, b: bool) !void {
if (self.animations.getPtr(name)) |anim| {
anim.loop = b;
}
return error.NameNotExist;
}

pub const Frame = struct {
pub const Data = union(enum) {
sp: Sprite,
Expand All @@ -170,19 +183,21 @@ pub const Animation = struct {
play_index: u32,
passed_time: f32,
is_over: bool,
is_stopped: bool,

pub fn reset(anim: *Animation) void {
anim.play_index = 0;
anim.passed_time = 0;
anim.is_over = false;
anim.is_stopped = false;
}

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

pub fn update(anim: *Animation, delta_tick: f32) void {
if (anim.is_over) return;
fn update(anim: *Animation, delta_tick: f32) void {
if (anim.is_over or anim.is_stopped) return;
anim.passed_time += delta_tick;
while (anim.passed_time >= anim.frames[anim.play_index].duration) {
anim.passed_time -= anim.frames[anim.play_index].duration;
Expand Down

0 comments on commit bfd5bb9

Please sign in to comment.