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) {
self.cycles += Int(cycles)
let nmiBefore = ppu.nmiInterrupt != nil
//print(nmiBefore)
self.ppu.tick(cycles * 3)
let nmiAfter = ppu.nmiInterrupt != nil
//print(nmiAfter)
if !nmiBefore && nmiAfter {
let newFrame = self.ppu.tick(cycles * 3)
if newFrame {
gameloopCallback(ppu)
}

View File

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

View File

@ -94,7 +94,7 @@ var quit = false
// 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)
bytes.getBytes(&gameCode, length: bytes.length)