From c773c3a66caca5685fb3061873035dd4f2078d4c Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Tue, 31 May 2016 22:32:38 -0400 Subject: [PATCH] Started trying to clean up and consolidate on the Swift side of things but time is up for the day. --- .../Documents/Atari2600Document.swift | 21 +++------ .../Documents/ElectronDocument.swift | 45 +++++-------------- .../Documents/MachineDocument.swift | 33 +++++++++++++- 3 files changed, 46 insertions(+), 53 deletions(-) diff --git a/OSBindings/Mac/Clock Signal/Documents/Atari2600Document.swift b/OSBindings/Mac/Clock Signal/Documents/Atari2600Document.swift index 6c586c360..172a9a1f2 100644 --- a/OSBindings/Mac/Clock Signal/Documents/Atari2600Document.swift +++ b/OSBindings/Mac/Clock Signal/Documents/Atari2600Document.swift @@ -10,6 +10,11 @@ import Cocoa class Atari2600Document: MachineDocument { + private var atari2600 = CSAtari2600() + override func machine() -> CSMachine? { + return atari2600 + } + // MARK: NSDocument overrides override init() { super.init() @@ -31,7 +36,6 @@ class Atari2600Document: MachineDocument { return "Atari2600Document" } - private var atari2600 = CSAtari2600() override func dataOfType(typeName: String) throws -> NSData { // Insert code here to write your document to data of the specified type. If outError != nil, ensure that you create and set an appropriate error when returning nil. // You can also choose to override fileWrapperOfType:error:, writeToURL:ofType:error:, or writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead. @@ -42,21 +46,6 @@ class Atari2600Document: MachineDocument { atari2600.setROM(data) } - override func close() { - super.close() - openGLView.invalidate() - } - - // MARK: MachineDocument overrides - - override func runForNumberOfCycles(numberOfCycles: Int32) { - atari2600.runForNumberOfCycles(numberOfCycles) - } - - override func openGLView(view: CSOpenGLView, drawViewOnlyIfDirty onlyIfDirty: Bool) { - atari2600.drawViewForPixelSize(view.backingSize, onlyIfDirty: onlyIfDirty) - } - // MARK: CSOpenGLViewResponderDelegate private func inputForKey(event: NSEvent) -> Atari2600DigitalInput? { diff --git a/OSBindings/Mac/Clock Signal/Documents/ElectronDocument.swift b/OSBindings/Mac/Clock Signal/Documents/ElectronDocument.swift index 4d08e603a..f60b93aee 100644 --- a/OSBindings/Mac/Clock Signal/Documents/ElectronDocument.swift +++ b/OSBindings/Mac/Clock Signal/Documents/ElectronDocument.swift @@ -12,21 +12,24 @@ import AudioToolbox class ElectronDocument: MachineDocument { private lazy var electron = CSElectron() + override func machine() -> CSMachine! { + return electron + } override func windowControllerDidLoadNib(aController: NSWindowController) { super.windowControllerDidLoadNib(aController) self.intendedCyclesPerSecond = 2000000 aController.window?.contentAspectRatio = NSSize(width: 11.0, height: 10.0) + if let osPath = NSBundle.mainBundle().pathForResource("os", ofType: "rom") { + self.electron.setOSROM(NSData(contentsOfFile: osPath)!) + } + if let basicPath = NSBundle.mainBundle().pathForResource("basic", ofType: "rom") { + self.electron.setBASICROM(NSData(contentsOfFile: basicPath)!) + } openGLView.performWithGLContext({ - if let osPath = NSBundle.mainBundle().pathForResource("os", ofType: "rom") { - self.electron.setOSROM(NSData(contentsOfFile: osPath)!) - } - if let basicPath = NSBundle.mainBundle().pathForResource("basic", ofType: "rom") { - self.electron.setBASICROM(NSData(contentsOfFile: basicPath)!) - } self.electron.setView(self.openGLView, aspectRatio: 11.0 / 10.0) - self.electron.audioQueue = self.audioQueue }) + self.electron.audioQueue = self.audioQueue establishStoredOptions() } @@ -58,19 +61,6 @@ class ElectronDocument: MachineDocument { electron.setROM(data, slot: 15) } - lazy var actionLock = NSLock() - lazy var drawLock = NSLock() - override func close() { - actionLock.lock() - drawLock.lock() - openGLView.invalidate() - openGLView.openGLContext!.makeCurrentContext() - actionLock.unlock() - drawLock.unlock() - - super.close() - } - // MARK: IBActions @IBOutlet var displayTypeButton: NSPopUpButton! @IBAction func setDisplayType(sender: NSPopUpButton!) { @@ -102,21 +92,6 @@ class ElectronDocument: MachineDocument { self.displayTypeButton.selectItemAtIndex(displayType) } - // MARK: CSOpenGLViewDelegate - override func runForNumberOfCycles(numberOfCycles: Int32) { - if actionLock.tryLock() { - electron.runForNumberOfCycles(numberOfCycles) - actionLock.unlock() - } - } - - override func openGLView(view: CSOpenGLView, drawViewOnlyIfDirty onlyIfDirty: Bool) { - if drawLock.tryLock() { - electron.drawViewForPixelSize(view.backingSize, onlyIfDirty: onlyIfDirty) - drawLock.unlock() - } - } - // MARK: NSWindowDelegate func windowDidResignKey(notification: NSNotification) { electron.clearAllKeys() diff --git a/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift b/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift index 473e81aad..4d93ccfea 100644 --- a/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift +++ b/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift @@ -11,6 +11,12 @@ import AudioToolbox class MachineDocument: NSDocument, CSOpenGLViewDelegate, CSOpenGLViewResponderDelegate, NSWindowDelegate { + lazy var actionLock = NSLock() + lazy var drawLock = NSLock() + func machine() -> CSMachine! { + return nil + } + @IBOutlet weak var openGLView: CSOpenGLView! { didSet { openGLView.delegate = self @@ -32,6 +38,17 @@ class MachineDocument: NSDocument, CSOpenGLViewDelegate, CSOpenGLViewResponderDe aController.window?.contentAspectRatio = NSSize(width: 4.0, height: 3.0) } + override func close() { + actionLock.lock() + drawLock.lock() + openGLView.invalidate() + openGLView.openGLContext!.makeCurrentContext() + actionLock.unlock() + drawLock.unlock() + + super.close() + } + var intendedCyclesPerSecond: Int64 = 0 private var cycleCountError: Int64 = 0 private var lastTime: CVTimeStamp? @@ -62,8 +79,20 @@ class MachineDocument: NSDocument, CSOpenGLViewDelegate, CSOpenGLViewResponderDe lastTime = time } - func openGLView(view: CSOpenGLView, drawViewOnlyIfDirty onlyIfDirty: Bool) {} - func runForNumberOfCycles(numberOfCycles: Int32) {} + // MARK: CSOpenGLViewDelegate + func runForNumberOfCycles(numberOfCycles: Int32) { + if actionLock.tryLock() { + self.machine().runForNumberOfCycles(numberOfCycles) + actionLock.unlock() + } + } + + func openGLView(view: CSOpenGLView, drawViewOnlyIfDirty onlyIfDirty: Bool) { + if drawLock.tryLock() { + self.machine().drawViewForPixelSize(view.backingSize, onlyIfDirty: onlyIfDirty) + drawLock.unlock() + } + } // MARK: CSOpenGLViewResponderDelegate func keyDown(event: NSEvent) {}