From 28e65172346fd6992d370302b7a3faf469980013 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Wed, 6 Apr 2016 20:46:00 -0400 Subject: [PATCH] Switched away from tracking absolute time to tracking relative. --- .../Documents/MachineDocument.swift | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift b/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift index a499de76f..65ea23402 100644 --- a/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift +++ b/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift @@ -28,25 +28,23 @@ class MachineDocument: NSDocument, CSOpenGLViewDelegate, CSOpenGLViewResponderDe } var intendedCyclesPerSecond: Int64 = 0 - private var lastCycleCount: Int64? + private var cycleCountError: Int64 = 0 + private var lastTime: CVTimeStamp? final func openGLView(view: CSOpenGLView, didUpdateToTime time: CVTimeStamp) { - // TODO: treat time as a delta from old time, work out how many cycles that is plus error + if let lastTime = lastTime { + // perform (time passed in seconds) * (intended cycles per second), converting and + // maintaining an error count to deal with underflow + let videoTimeScale64 = Int64(time.videoTimeScale) + let videoTimeCount = ((time.videoTime - lastTime.videoTime) * intendedCyclesPerSecond) + cycleCountError + cycleCountError = videoTimeCount % videoTimeScale64 + let numberOfCycles = videoTimeCount / videoTimeScale64 - // this slightly elaborate dance is to avoid overflow - let videoTimeScale64 = Int64(time.videoTimeScale) - - let cycleCountLow = ((time.videoTime % videoTimeScale64) * intendedCyclesPerSecond) / videoTimeScale64 - let cycleCountHigh = (time.videoTime / videoTimeScale64) * intendedCyclesPerSecond - - let cycleCount = cycleCountLow + cycleCountHigh - if let lastCycleCount = lastCycleCount { - let elapsedTime = cycleCount - lastCycleCount // if the emulation has fallen too far behind then silently limit the request; // some actions — e.g. the host computer waking after sleep — may give us a // prohibitive backlog - runForNumberOfCycles(min(Int32(elapsedTime), Int32(intendedCyclesPerSecond / 25))) + runForNumberOfCycles(min(Int32(numberOfCycles), Int32(intendedCyclesPerSecond / 25))) } - lastCycleCount = cycleCount + lastTime = time } func openGLView(view: CSOpenGLView, drawViewOnlyIfDirty onlyIfDirty: Bool) {}