Skip to content

Commit

Permalink
Migrate create, and init functions to be methods on the Sprite struct…
Browse files Browse the repository at this point in the history
… itself
  • Loading branch information
deckarep committed Nov 9, 2024
1 parent d671c0e commit 71460af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion zsrc/game.zig
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub fn appendSpriteToSnake(
newY += delta;
}
}
const sprite = spr.createSprite(&res.commonSprites[@intCast(spriteId)], newX, newY);
const sprite = spr.Sprite.create(&res.commonSprites[@intCast(spriteId)], newX, newY);
sprite.direction = direction;
if (direction == .LEFT) {
sprite.face = .LEFT;
Expand Down
42 changes: 21 additions & 21 deletions zsrc/sprite.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,29 @@ pub const Sprite = struct {
// Timestamp of the last attack
lastAttack: c_int,
dropRate: f64,

pub fn create(model: *Sprite, x: c_int, y: c_int) *Sprite {
const self = gAllocator.create(Sprite) catch unreachable;
self.init(model, x, y);
return self;
}

pub fn init(self: *Sprite, model: *const Sprite, x: c_int, y: c_int) void {
// r.c.: 1. I made the model a const pointer.
// 2. Original C code used a memcpy, hoping the line below is fine.
self.* = model.*;

self.x = x;
self.y = y;
self.posQueue = PositionBufferQueue.init();

const ani = gAllocator.create(tps.Animation) catch unreachable;
tps.copyAnimation(model.ani, ani);
self.ani = ani;
ren.updateAnimationOfSprite(self);
}
};

pub inline fn pushToPositionBuffer(q: *PositionBufferQueue, slot: PositionBufferSlot) void {
q.enqueue(slot);
}

pub fn initSprite(model: *const Sprite, self: *Sprite, x: c_int, y: c_int) void {
// r.c.: 1. I made the model a const pointer.
// 2. Original C code used a memcpy, hoping the line below is fine.
self.* = model.*;

self.x = x;
self.y = y;
self.posQueue = PositionBufferQueue.init();

const ani = gAllocator.create(tps.Animation) catch unreachable;
tps.copyAnimation(model.ani, ani);
self.ani = ani;
ren.updateAnimationOfSprite(self);
}

pub fn createSprite(model: *Sprite, x: c_int, y: c_int) *Sprite {
const self = gAllocator.create(Sprite) catch unreachable;
initSprite(model, self, x, y);
return self;
}

0 comments on commit 71460af

Please sign in to comment.