chore: allocate sprite array on heap

Each Sprite optional is 10 bytes meaning I'm allocating 1.28Kb on the
stack which isn't necessary.
This commit is contained in:
Rekai Nyangadzayi Musuka 2022-08-29 01:05:33 -05:00
parent 5f8c6833f4
commit 6a798d2c9d
1 changed files with 9 additions and 5 deletions

View File

@ -43,13 +43,16 @@ pub const Ppu = struct {
framebuf: FrameBuffer,
alloc: Allocator,
scanline_sprites: [128]?Sprite,
scanline_sprites: *[128]?Sprite,
scanline: Scanline,
pub fn init(allocator: Allocator, sched: *Scheduler) !Self {
// Queue first Hblank
sched.push(.Draw, 240 * 4);
const sprites = try allocator.create([128]?Sprite);
sprites.* = [_]?Sprite{null} ** 128;
return Self{
.vram = try Vram.init(allocator),
.palette = try Palette.init(allocator),
@ -70,11 +73,12 @@ pub const Ppu = struct {
.bldy = .{ .raw = 0x0000 },
.scanline = try Scanline.init(allocator),
.scanline_sprites = [_]?Sprite{null} ** 128,
.scanline_sprites = sprites,
};
}
pub fn deinit(self: *Self) void {
self.alloc.destroy(self.scanline_sprites);
self.framebuf.deinit();
self.scanline.deinit();
self.vram.deinit();
@ -394,7 +398,7 @@ pub const Ppu = struct {
// Reset Current Scanline Pixel Buffer and list of fetched sprites
// in prep for next scanline
self.scanline.reset();
std.mem.set(?Sprite, &self.scanline_sprites, null);
std.mem.set(?Sprite, self.scanline_sprites, null);
},
0x1 => {
const fb_base = framebuf_pitch * @as(usize, scanline);
@ -421,7 +425,7 @@ pub const Ppu = struct {
// Reset Current Scanline Pixel Buffer and list of fetched sprites
// in prep for next scanline
self.scanline.reset();
std.mem.set(?Sprite, &self.scanline_sprites, null);
std.mem.set(?Sprite, self.scanline_sprites, null);
},
0x2 => {
const fb_base = framebuf_pitch * @as(usize, scanline);
@ -447,7 +451,7 @@ pub const Ppu = struct {
// Reset Current Scanline Pixel Buffer and list of fetched sprites
// in prep for next scanline
self.scanline.reset();
std.mem.set(?Sprite, &self.scanline_sprites, null);
std.mem.set(?Sprite, self.scanline_sprites, null);
},
0x3 => {
const vram_base = width * @sizeOf(u16) * @as(usize, scanline);