fix cross-platform compatability

This commit is contained in:
Andrew Glaze 2024-08-22 21:45:47 -04:00
parent df8be993f7
commit af3be8d93d

View File

@ -40,22 +40,16 @@ let keyMap = [
SDLK_s : JoypadButton.BUTTON_B SDLK_s : JoypadButton.BUTTON_B
] ]
var timer = Date.now.addingTimeInterval(0.01666)
var frame = Frame() var frame = Frame()
let time: UnsafeMutablePointer<timespec> = UnsafeMutablePointer<timespec>.allocate(capacity: 1)
clock_gettime(CLOCK_REALTIME, time)
time.pointee.tv_nsec += Int(0.01666 * 1000000000)
let bus = Bus(rom: rom, joypad1: joypad1) { ppu in let bus = Bus(rom: rom, joypad1: joypad1) { ppu in
Render.render(ppu, frame: frame) Render.render(ppu, frame: frame)
// wait here until 16.66 ms have passed
//wait here until 16.66 ms have passed // i would use clock_nanosleep but that isn't available cross platform
clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, time, nil) if timer.timeIntervalSinceNow > 0 {
var nextNSec = time.pointee.tv_nsec + Int(0.01666 * 1000000000) usleep(UInt32(timer.timeIntervalSinceNow * 1000000))
if nextNSec > 999999999 {
nextNSec -= 999999999
time.pointee.tv_sec += 1
} }
time.pointee.tv_nsec = nextNSec timer.addTimeInterval(0.01666)
SDL_UpdateTexture(texture, nil, frame.data, 256 * 3) SDL_UpdateTexture(texture, nil, frame.data, 256 * 3)
SDL_RenderCopy(canvas, texture, nil, nil) SDL_RenderCopy(canvas, texture, nil, nil)
SDL_RenderPresent(canvas) SDL_RenderPresent(canvas)
@ -88,4 +82,3 @@ let bus = Bus(rom: rom, joypad1: joypad1) { ppu in
let cpu = CPU(bus: bus) let cpu = CPU(bus: bus)
cpu.reset() cpu.reset()
cpu.run() cpu.run()
time.deallocate()