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,6 +32,7 @@ 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) {
@ -52,7 +53,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
@ -83,7 +84,6 @@ class Render {
} }
} }
} }
}
static func getBgPalette(_ ppu: NesPPU, tileLoc: (col: Int, row: Int)) -> [UInt8] { static func getBgPalette(_ ppu: NesPPU, tileLoc: (col: Int, row: Int)) -> [UInt8] {
let attrTableIndex = tileLoc.row / 4 * 8 + tileLoc.col / 4 let attrTableIndex = tileLoc.row / 4 * 8 + tileLoc.col / 4

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)