Skip to content

Commit

Permalink
Refactor: migrated Effect functions to be method-based
Browse files Browse the repository at this point in the history
  • Loading branch information
deckarep committed Nov 1, 2024
1 parent 4572241 commit bf6943f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ This is a near exact **Zig port** of the [original DungeonRush `C-based`](https:
* Migrate to a Zig-based SDL wrapper, for a nicer SDL experience.
* Ensure all errors are accounted for, utilize `try`
* Use build.zig.zon
* Setup Github to build the project regularly
* Setup Github to build the project regularly
3. Phase 3: Code Clean-up/Refactor
* Remove duplicate code
* Make code even more idiomatic for Zig
Expand Down
9 changes: 6 additions & 3 deletions zsrc/game.zig
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,8 @@ fn freezeSnake(snake: *pl.Snake, duration: c_int) void {
var effect: ?*tps.Effect = null;
if (snake.buffs[tps.BUFF_DEFENCE] > 0) {
effect = gAllocator.create(tps.Effect) catch unreachable;
tps.copyEffect(&res.effects[res.EFFECT_VANISH30], effect.?);
res.effects[res.EFFECT_VANISH30].copyInto(effect.?);
//tps.copyEffect(&res.effects[res.EFFECT_VANISH30], effect.?);
dur = 30;
}

Expand Down Expand Up @@ -849,7 +850,8 @@ fn slowDownSnake(snake: *pl.Snake, duration: c_int) void {
var effect: ?*tps.Effect = null;
if (snake.buffs[tps.BUFF_DEFENCE] > 0) {
effect = gAllocator.create(tps.Effect) catch unreachable;
tps.copyEffect(&res.effects[res.EFFECT_VANISH30], effect.?);
res.effects[res.EFFECT_VANISH30].copyInto(effect.?);
//tps.copyEffect(&res.effects[res.EFFECT_VANISH30], effect.?);
dur = 30;
}

Expand Down Expand Up @@ -896,7 +898,8 @@ fn shieldSprite(sprite: *spr.Sprite, duration: c_int) void {
// Now it pulsates with transparency.
const effect = gAllocator.create(tps.Effect) catch unreachable;
defer effect.deinit();
tps.copyEffect(&res.effects[res.EFFECT_BLINK], effect);
res.effects[res.EFFECT_BLINK].copyInto(effect);
//tps.copyEffect(&res.effects[res.EFFECT_BLINK], effect);

const ani = ren.createAndPushAnimation(
&ren.animationsList[ren.RENDER_LIST_EFFECT_ID],
Expand Down
2 changes: 1 addition & 1 deletion zsrc/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ pub fn setEffect(texture: *tps.Texture, ef: ?*tps.Effect) void {
progress /= interval;

const prev: c.SDL_Color = effect.keys[@intCast(stage)];
const nxt: c.SDL_Color = effect.keys[@intCast(@min(stage + 1, effect.length - 1))];
const nxt: c.SDL_Color = effect.keys[@intCast(@min(@as(usize, @intCast(stage)) + 1, effect.length - 1))];

var mixed: c.SDL_Color = undefined;
mixed.r = @intFromFloat(@as(f64, @floatFromInt(prev.r)) * (1.0 - progress) + @as(f64, @floatFromInt(nxt.r)) * progress);
Expand Down
30 changes: 18 additions & 12 deletions zsrc/res.zig
Original file line number Diff line number Diff line change
Expand Up @@ -730,38 +730,44 @@ pub fn cleanup() void {
}

pub fn initCommonEffects() void {
tps.initEffect(&effects[0], 30, 4, c.SDL_BLENDMODE_BLEND);
//tps.initEffect(&effects[0], 30, 4, c.SDL_BLENDMODE_BLEND);
const deathEffect = &effects[0];
deathEffect.init(30, 4, c.SDL_BLENDMODE_BLEND);
var death: c.SDL_Color = .{ .r = 255, .g = 255, .b = 255, .a = 255 };
effects[0].keys[0] = death;
deathEffect.keys[0] = death;
death.g = 0;
death.b = 0;
death.r = 168;
effects[0].keys[1] = death;
deathEffect.keys[1] = death;
death.r = 80;
effects[0].keys[2] = death;
deathEffect.keys[2] = death;
death.r = 0;
death.a = 0;
effects[0].keys[3] = death;
deathEffect.keys[3] = death;
std.log.debug("Effect #0: Death (30frames) loaded", .{});

tps.initEffect(&effects[1], 30, 3, c.SDL_BLENDMODE_ADD);
//tps.initEffect(&effects[1], 30, 3, c.SDL_BLENDMODE_ADD);
const blinkEffect = &effects[1];
blinkEffect.init(30, 3, c.SDL_BLENDMODE_ADD);
var blink: c.SDL_Color = .{ .r = 0, .g = 0, .b = 0, .a = 255 };
effects[1].keys[0] = blink;
blinkEffect.keys[0] = blink;
blink.r = 200;
blink.g = 200;
blink.b = 200;
effects[1].keys[1] = blink;
blinkEffect.keys[1] = blink;
blink.r = 0;
blink.g = 0;
blink.b = 0;
effects[1].keys[2] = blink;
blinkEffect.keys[2] = blink;
std.log.debug("Effect #1: Blink (white) (30frames) loaded", .{});

tps.initEffect(&effects[2], 30, 2, c.SDL_BLENDMODE_BLEND);
//tps.initEffect(&effects[2], 30, 2, c.SDL_BLENDMODE_BLEND);
const vanishEffect = &effects[2];
vanishEffect.init(30, 2, c.SDL_BLENDMODE_BLEND);
var vanish: c.SDL_Color = .{ .r = 255, .g = 255, .b = 255, .a = 255 };
effects[2].keys[0] = vanish;
vanishEffect.keys[0] = vanish;
vanish.a = 0;
effects[2].keys[1] = vanish;
vanishEffect.keys[1] = vanish;
std.log.debug("Effect #2: Vanish (30frames) loaded", .{});
}

Expand Down
67 changes: 32 additions & 35 deletions zsrc/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,38 @@ pub const Animation = struct {
pub const Effect = struct {
duration: c_int,
currentFrame: c_int,
length: c_int,
//keys: [*]c.SDL_Color,
length: usize,
keys: []c.SDL_Color,
mode: c.SDL_BlendMode,

pub fn deinit(self: *Effect) void {
// TODO: migrate the destroyEffect code INTO this method.
// delete all uses of destroyEffect in favor of effect.deinit()
destroyEffect(self);
pub fn init(self: *Effect, duration: c_int, length: usize, mode: c.SDL_BlendMode) void {
self.keys = gAllocator.alloc(c.SDL_Color, length) catch unreachable;
self.duration = duration;
self.length = length;
self.currentFrame = 0;
self.mode = mode;
}

pub fn deinit(self: ?*Effect) void {
if (self) |ef| {
gAllocator.free(ef.keys);
gAllocator.destroy(ef);
}
}

/// Ensures an exact memberwise replica copy is made of the *Effect
/// while ensuring the effect.keys are deep copied. This function
/// allows the caller to choose how the dest *Effect is allocated (stack or heap).
pub fn copyInto(self: *const Effect, dest: *Effect) void {
// rc: changed from @memcpy to a memberwise copy.
dest.* = self.*;

// With a deep alloc-copy on the keys.
const len: usize = @intCast(self.length);
dest.*.keys = gAllocator.alloc(c.SDL_Color, len) catch unreachable;
for (0..len) |idx| {
dest.keys[idx] = self.keys[idx];
}
}
};

Expand Down Expand Up @@ -237,7 +260,8 @@ pub fn initAnimation(
// will deep copy effect
if (effect) |ef| {
self.effect = gAllocator.create(Effect) catch unreachable;
copyEffect(ef, self.effect.?);
//copyEffect(ef, self.effect.?);
ef.copyInto(self.effect.?);
} else {
self.effect = null;
}
Expand Down Expand Up @@ -286,7 +310,7 @@ pub fn copyAnimation(src: *const Animation, dest: *Animation) void {
dest.* = src.*;
if (src.effect) |eff| {
dest.effect = gAllocator.create(Effect) catch unreachable;
copyEffect(eff, dest.effect.?);
eff.copyInto(dest.effect.?);
}
}

Expand Down Expand Up @@ -338,33 +362,6 @@ pub fn destroyText(self: *Text) void {
gAllocator.destroy(self);
}

pub fn initEffect(self: *Effect, duration: c_int, length: c_int, mode: c.SDL_BlendMode) void {
self.keys = gAllocator.alloc(c.SDL_Color, @as(usize, @intCast(length))) catch unreachable;
self.duration = duration;
self.length = length;
self.currentFrame = 0;
self.mode = mode;
}

// deep copy
pub fn copyEffect(src: *const Effect, dest: *Effect) void {
// rc: change from memcopy to just regular ass copy.
dest.* = src.*;

const len: usize = @intCast(src.length);
dest.*.keys = gAllocator.alloc(c.SDL_Color, len) catch unreachable;
for (0..len) |idx| {
dest.keys[idx] = src.keys[idx];
}
}

pub fn destroyEffect(self: ?*Effect) void {
if (self) |ef| {
gAllocator.free(ef.keys);
gAllocator.destroy(ef);
}
}

pub fn initLinkNode(self: *ll.GenericNode) void {
self.next = null;
self.prev = null;
Expand Down

0 comments on commit bf6943f

Please sign in to comment.