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-10-21 05:12:56 -03:00
parent 4cf58f1faa
commit 9b0f54b111
1 changed files with 9 additions and 5 deletions

View File

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