Remove memory leaks from switching levels

master
Louis Pearson 2022-08-06 17:37:25 -06:00
parent 6bfead54cc
commit 376e001a69
2 changed files with 53 additions and 50 deletions

View File

@ -243,10 +243,9 @@ pub fn toggle(this: *@This(), c: Cell) void {
pub fn clearMap(this: *@This()) void { pub fn clearMap(this: *@This()) void {
this.clear(); this.clear();
std.mem.set(u8, this.map, 0); std.mem.set(u8, this.map, 0);
for (this.doors.items) |*door| { this.doors.reset();
door.enabled = false;
}
this.bridges.reset(); this.bridges.reset();
this.sources.reset();
} }
pub fn clear(this: *@This()) void { pub fn clear(this: *@This()) void {

View File

@ -218,6 +218,57 @@ fn loadLevel(lvl: usize) !void {
.switch_off = world.Tiles.SwitchesOff, .switch_off = world.Tiles.SwitchesOff,
.switch_on = world.Tiles.SwitchesOn, .switch_on = world.Tiles.SwitchesOn,
}); });
const tile_size = Vec2{ 8, 8 };
{
_ = try wires.resize(0);
var a: usize = 0;
while (level.getWire(a)) |wire| : (a += 1) {
const p1 = util.vec2ToVec2f(Vec2{ wire[0].x, wire[0].y } * tile_size + Vec2{ 4, 4 });
const p2 = util.vec2ToVec2f(Vec2{ wire[1].x, wire[1].y } * tile_size + Vec2{ 4, 4 });
var w = try wires.addOne();
_ = try w.nodes.resize(0);
// const divisions = wire.divisions;
const divisions = 10;
var i: usize = 0;
while (i <= divisions) : (i += 1) {
try w.nodes.append(Pos.init(p1));
}
w.begin().pos = p1;
w.end().pos = p2;
w.begin().pinned = wire[0].kind == world.EntityKind.WireAnchor;
w.end().pinned = wire[1].kind == world.EntityKind.WireEndAnchor;
w.straighten();
}
}
{
var i: usize = 0;
while (level.getDoor(i)) |door| : (i += 1) {
try circuit.addDoor(Vec2{ door.x, door.y });
}
}
try coins.resize(0);
// if (!try Disk.load()) {
var i: usize = 0;
while (level.getCoin(i)) |coin| : (i += 1) {
try coins.append(.{
.pos = Pos.init(util.vec2ToVec2f(Vec2{ coin.x, coin.y } * tile_size)),
.sprite = .{ .offset = .{ 0, 0 }, .size = .{ 8, 8 }, .index = 4, .flags = .{ .bpp = .b2 } },
.anim = Anim{ .anim = &anim_store.coin },
.area = .{ .pos = .{ 0, 0 }, .size = .{ 8, 8 } },
});
}
// }
try updateCircuit();
} }
fn moveLevel(direction: enum { L, R, U, D }) !void { fn moveLevel(direction: enum { L, R, U, D }) !void {
@ -300,53 +351,6 @@ pub fn start() !void {
}, },
.kinematic = .{ .col = .{ .pos = .{ -3, -6 }, .size = .{ 5, 5 } } }, .kinematic = .{ .col = .{ .pos = .{ -3, -6 }, .size = .{ 5, 5 } } },
}; };
{
_ = try wires.resize(0);
var a: usize = 0;
while (level.getWire(a)) |wire| : (a += 1) {
const p1 = util.vec2ToVec2f(Vec2{ wire[0].x, wire[0].y } * tile_size + Vec2{ 4, 4 });
const p2 = util.vec2ToVec2f(Vec2{ wire[1].x, wire[1].y } * tile_size + Vec2{ 4, 4 });
var w = try wires.addOne();
_ = try w.nodes.resize(0);
// const divisions = wire.divisions;
const divisions = 10;
var i: usize = 0;
while (i <= divisions) : (i += 1) {
try w.nodes.append(Pos.init(p1));
}
w.begin().pos = p1;
w.end().pos = p2;
w.begin().pinned = wire[0].kind == world.EntityKind.WireAnchor;
w.end().pinned = wire[1].kind == world.EntityKind.WireEndAnchor;
w.straighten();
}
}
{
var i: usize = 0;
while (level.getDoor(i)) |door| : (i += 1) {
try circuit.addDoor(Vec2{ door.x, door.y });
}
}
try coins.resize(0);
// if (!try Disk.load()) {
var i: usize = 0;
while (level.getCoin(i)) |coin| : (i += 1) {
try coins.append(.{
.pos = Pos.init(util.vec2ToVec2f(Vec2{ coin.x, coin.y } * tile_size)),
.sprite = .{ .offset = .{ 0, 0 }, .size = .{ 8, 8 }, .index = 4, .flags = .{ .bpp = .b2 } },
.anim = Anim{ .anim = &anim_store.coin },
.area = .{ .pos = .{ 0, 0 }, .size = .{ 8, 8 } },
});
}
// }
try updateCircuit();
} }
var indicator: ?Interaction = null; var indicator: ?Interaction = null;