Render: don't render all sprites for every background tile

This commit is contained in:
Andrew Glaze 2024-08-21 17:45:32 -04:00
parent 6974110b22
commit bd21449256
3 changed files with 45 additions and 49 deletions

View File

@ -20,12 +20,8 @@ class Bus {
func tick(_ cycles: UInt8) { func tick(_ cycles: UInt8) {
self.cycles += Int(cycles) self.cycles += Int(cycles)
let nmiBefore = ppu.nmiInterrupt != nil let newFrame = self.ppu.tick(cycles * 3)
//print(nmiBefore) if newFrame {
self.ppu.tick(cycles * 3)
let nmiAfter = ppu.nmiInterrupt != nil
//print(nmiAfter)
if !nmiBefore && nmiAfter {
gameloopCallback(ppu) gameloopCallback(ppu)
} }

View File

@ -13,7 +13,7 @@ class Render {
var upper = tile[tile.startIndex + y] var upper = tile[tile.startIndex + y]
var lower = tile[tile.startIndex + y + 8] var lower = tile[tile.startIndex + y + 8]
for x in (0...7).reversed() { for x in [7,6,5,4,3,2,1,0] {
let value = (1 & lower) << 1 | (1 & upper) let value = (1 & lower) << 1 | (1 & upper)
upper = upper >> 1 upper = upper >> 1
lower = lower >> 1 lower = lower >> 1
@ -32,53 +32,53 @@ class Render {
frame.setPixel((tileLoc.col * 8 + x, tileLoc.row * 8 + y), rgb) frame.setPixel((tileLoc.col * 8 + x, tileLoc.row * 8 + y), rgb)
} }
} }
}
// MARK: Draw Sprites // MARK: Draw Sprites
for i in stride(from: 0, to: ppu.oamData.count, by: 4) { for i in stride(from: 0, to: ppu.oamData.count, by: 4) {
let tileIndex = UInt16(ppu.oamData[i + 1]) let tileIndex = UInt16(ppu.oamData[i + 1])
let tileX = Int(ppu.oamData[i + 3]) let tileX = Int(ppu.oamData[i + 3])
let tileY = Int(ppu.oamData[i]) let tileY = Int(ppu.oamData[i])
let flipVert = ppu.oamData[i + 2] >> 7 & 1 == 1 let flipVert = ppu.oamData[i + 2] >> 7 & 1 == 1
let flipHori = ppu.oamData[i + 2] >> 6 & 1 == 1 let flipHori = ppu.oamData[i + 2] >> 6 & 1 == 1
let paletteIndex = ppu.oamData[i + 2] & 0b11 let paletteIndex = ppu.oamData[i + 2] & 0b11
let spritePallete = getSpritePalette(ppu, paletteIndex: paletteIndex) let spritePallete = getSpritePalette(ppu, paletteIndex: paletteIndex)
let bank = ppu.ctrl.spritePatternAddr() let bank = ppu.ctrl.spritePatternAddr()
let tile = ppu.chrRom[(bank + Int(tileIndex) * 16)...(bank + Int(tileIndex) * 16 + 15)] let tile = ppu.chrRom[(bank + Int(tileIndex) * 16)...(bank + Int(tileIndex) * 16 + 15)]
for y in 0...7 { for y in 0...7 {
var upper = tile[tile.startIndex + y] var upper = tile[tile.startIndex + y]
var lower = tile[tile.startIndex + y + 8] var lower = tile[tile.startIndex + y + 8]
for x in (0...7).reversed() { for x in [7,6,5,4,3,2,1,0] {
let value = (1 & lower) << 1 | (1 & upper) let value = (1 & lower) << 1 | (1 & upper)
upper = upper >> 1 upper = upper >> 1
lower = lower >> 1 lower = lower >> 1
if (value == 0) { if (value == 0) {
continue // skip coloring this pixel, it's transparent continue // skip coloring this pixel, it's transparent
} }
let rgb = switch value { let rgb = switch value {
case 1: case 1:
NESColor.SYSTEM_PALLETE[Int(spritePallete[1])] NESColor.SYSTEM_PALLETE[Int(spritePallete[1])]
case 2: case 2:
NESColor.SYSTEM_PALLETE[Int(spritePallete[2])] NESColor.SYSTEM_PALLETE[Int(spritePallete[2])]
case 3: case 3:
NESColor.SYSTEM_PALLETE[Int(spritePallete[3])] NESColor.SYSTEM_PALLETE[Int(spritePallete[3])]
default: default:
fatalError("Invalid Pallete Color type") fatalError("Invalid Pallete Color type")
} }
switch (flipHori, flipVert) { switch (flipHori, flipVert) {
case (false, false): case (false, false):
frame.setPixel((tileX + x, tileY + y), rgb) frame.setPixel((tileX + x, tileY + y), rgb)
case (true, false): case (true, false):
frame.setPixel((tileX + 7 - x, tileY + y), rgb) frame.setPixel((tileX + 7 - x, tileY + y), rgb)
case (false, true): case (false, true):
frame.setPixel((tileX + x, tileY + 7 - y), rgb) frame.setPixel((tileX + x, tileY + 7 - y), rgb)
case (true, true): case (true, true):
frame.setPixel((tileX + 7 - x, tileY + 7 - y), rgb) frame.setPixel((tileX + 7 - x, tileY + 7 - y), rgb)
}
} }
} }
} }

View File

@ -94,7 +94,7 @@ var quit = false
// return update // return update
// } // }
guard let bytes = NSData(contentsOfFile: "pacman.nes") else { fatalError("Rom not found") } guard let bytes = NSData(contentsOfFile: "smb1.nes") else { fatalError("Rom not found") }
var gameCode = [UInt8](repeating: 0, count: bytes.length) var gameCode = [UInt8](repeating: 0, count: bytes.length)
bytes.getBytes(&gameCode, length: bytes.length) bytes.getBytes(&gameCode, length: bytes.length)