2016-01-04 23:40:43 -05:00
|
|
|
//
|
|
|
|
// MachineDocument.swift
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 04/01/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Cocoa
|
2016-01-14 20:33:22 -05:00
|
|
|
import AudioToolbox
|
2016-01-04 23:40:43 -05:00
|
|
|
|
2016-04-19 21:29:10 -04:00
|
|
|
class MachineDocument: NSDocument, CSOpenGLViewDelegate, CSOpenGLViewResponderDelegate, NSWindowDelegate {
|
2016-01-04 23:40:43 -05:00
|
|
|
|
2016-03-05 14:45:09 -05:00
|
|
|
@IBOutlet weak var openGLView: CSOpenGLView! {
|
2016-01-04 23:40:43 -05:00
|
|
|
didSet {
|
|
|
|
openGLView.delegate = self
|
|
|
|
openGLView.responderDelegate = self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 21:43:39 -04:00
|
|
|
@IBOutlet weak var optionsPanel: NSPanel!
|
2016-04-18 08:21:00 -04:00
|
|
|
@IBAction func showOptions(sender: AnyObject!) {
|
2016-04-17 21:43:39 -04:00
|
|
|
optionsPanel?.setIsVisible(true)
|
|
|
|
}
|
|
|
|
|
2016-01-14 20:33:22 -05:00
|
|
|
lazy var audioQueue = AudioQueue()
|
|
|
|
|
2016-01-04 23:40:43 -05:00
|
|
|
override func windowControllerDidLoadNib(aController: NSWindowController) {
|
|
|
|
super.windowControllerDidLoadNib(aController)
|
|
|
|
|
|
|
|
// bind the content aspect ratio to remain 4:3 from now on
|
|
|
|
aController.window?.contentAspectRatio = NSSize(width: 4.0, height: 3.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
var intendedCyclesPerSecond: Int64 = 0
|
2016-04-06 20:46:00 -04:00
|
|
|
private var cycleCountError: Int64 = 0
|
|
|
|
private var lastTime: CVTimeStamp?
|
2016-04-20 22:35:46 -04:00
|
|
|
private var skippedFrames = 0
|
|
|
|
final func openGLView(view: CSOpenGLView, didUpdateToTime time: CVTimeStamp, didSkipPreviousUpdate : Bool, frequency : Double) {
|
2016-04-06 20:46:00 -04:00
|
|
|
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
|
2016-04-20 22:35:46 -04:00
|
|
|
var numberOfCycles = videoTimeCount / videoTimeScale64
|
2016-01-04 23:40:43 -05:00
|
|
|
|
2016-04-20 22:35:46 -04:00
|
|
|
// if the emulation has fallen behind then silently limit the request;
|
2016-03-20 18:42:37 -04:00
|
|
|
// some actions — e.g. the host computer waking after sleep — may give us a
|
|
|
|
// prohibitive backlog
|
2016-04-20 22:35:46 -04:00
|
|
|
if didSkipPreviousUpdate {
|
|
|
|
skippedFrames++
|
|
|
|
} else {
|
|
|
|
skippedFrames = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if skippedFrames > 4 {
|
|
|
|
numberOfCycles = min(numberOfCycles, Int64(Double(intendedCyclesPerSecond) * frequency))
|
|
|
|
}
|
|
|
|
runForNumberOfCycles(Int32(numberOfCycles))
|
2016-01-04 23:40:43 -05:00
|
|
|
}
|
2016-04-06 20:46:00 -04:00
|
|
|
lastTime = time
|
2016-01-04 23:40:43 -05:00
|
|
|
}
|
|
|
|
|
2016-03-05 14:45:09 -05:00
|
|
|
func openGLView(view: CSOpenGLView, drawViewOnlyIfDirty onlyIfDirty: Bool) {}
|
2016-01-04 23:40:43 -05:00
|
|
|
func runForNumberOfCycles(numberOfCycles: Int32) {}
|
|
|
|
|
|
|
|
// MARK: CSOpenGLViewResponderDelegate
|
|
|
|
func keyDown(event: NSEvent) {}
|
|
|
|
func keyUp(event: NSEvent) {}
|
|
|
|
func flagsChanged(newModifiers: NSEvent) {}
|
|
|
|
}
|