FruitMachine-Swift/FruitMachine/MainViewController.swift

71 lines
1.9 KiB
Swift
Raw Normal View History

//
// MainViewController.swift
// FruitMachine
//
// Created by Christopher Rohl on 7/26/17.
// Copyright © 2017 Christopher Rohl. All rights reserved.
//
import Cocoa
import CoreGraphics
2017-07-30 20:22:03 +00:00
import OpenGL
import GLKit
class MainViewController: NSViewController {
2017-07-27 23:22:13 +00:00
let computer = AppleI.sharedInstance
var debuggerWindowController: DebuggerWindowController!
2017-07-29 06:56:06 +00:00
var isPaused = false
var frameTimer: Timer?
2017-07-27 23:22:13 +00:00
override func viewDidLoad() {
super.viewDidLoad()
2017-07-27 23:22:13 +00:00
let debuggerStoryboard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Debugger"), bundle: nil)
debuggerWindowController = debuggerStoryboard.instantiateInitialController() as! DebuggerWindowController
debuggerWindowController.showWindow(self)
2017-07-27 23:22:13 +00:00
// Do view setup here.
self.view.addSubview(computer.emulatorView)
2017-07-30 20:22:03 +00:00
//computer.emuGLView.doSetup()
//self.view.addSubview(computer.emuGLView)
2017-07-29 06:56:06 +00:00
self.frameTimer = Timer.scheduledTimer(timeInterval: 1/60, target: self, selector: #selector(runEmulation), userInfo: nil, repeats: true)
}
@objc func runEmulation() {
2017-07-29 20:11:14 +00:00
computer.runFrame()
if(!CPU.sharedInstance.isRunning) {
self.frameTimer?.invalidate()
}
2017-07-29 06:56:06 +00:00
}
override func keyDown(with event: NSEvent) {
2017-07-29 20:11:14 +00:00
let c = returnChar(theEvent: event)
2017-07-29 06:56:06 +00:00
guard let ascii32 = c?.asciiValue else {
return
}
computer.pia["keyboard"]?.data = UInt8(ascii32 & 0x000000FF)
2017-07-29 06:56:06 +00:00
computer.pia["keyboard"]?.control |= 0x80
}
2017-07-29 20:11:14 +00:00
private func returnChar(theEvent: NSEvent) -> Character?{
let s: String = theEvent.characters!
for char in s{
return char
}
return nil
}
2017-07-27 23:22:13 +00:00
}
2017-07-29 20:11:14 +00:00
extension Character {
var asciiValue: UInt32? {
return String(self).unicodeScalars.filter{$0.isASCII}.first?.value
}
}