Skip to content

Commit

Permalink
Use 'MTKView' as 'GameBoyWindow.contentView' without proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
LiarPrincess committed Nov 9, 2020
1 parent 7a773b0 commit 3794f3d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Sources/GameBoyMac/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
right: .right,
a: .a,
b: .s,
start: .space,
select: .q
start: .q,
select: .w
)

let window = GameBoyWindow(
Expand Down
37 changes: 10 additions & 27 deletions Sources/GameBoyMac/GameBoyWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class GameBoyWindow: NSWindow, GameboyInputProvider, MTKViewDelegate {

// swiftlint:disable:next implicitly_unwrapped_optional
private(set) var gameBoy: GameBoy!
let mtkView: MTKView
let keyMap: KeyMap

let device: MTLDevice
Expand All @@ -22,8 +21,8 @@ class GameBoyWindow: NSWindow, GameboyInputProvider, MTKViewDelegate {
override var canBecomeKey: Bool { return true }
override var canBecomeMain: Bool { return true }

// swiftlint:disable:next function_body_length
init(scale: Int, bootrom: Bootrom?, cartridge: Cartridge, keyMap: KeyMap) {
self.mtkView = MTKView()
self.keyMap = keyMap

self.device = Metal.createDevice()
Expand Down Expand Up @@ -51,11 +50,16 @@ class GameBoyWindow: NSWindow, GameboyInputProvider, MTKViewDelegate {
input: self
)

self.customizeWindow(title: cartridge.header.title)
self.addMtkSubview()
}
self.contentView = {
let view = MTKView()
view.device = self.device
view.delegate = self
view.colorPixelFormat = .bgra8Unorm
view.framebufferOnly = true
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

private func customizeWindow(title: String) {
self.title = title
self.backgroundColor = .black
self.isMovableByWindowBackground = true
Expand All @@ -67,27 +71,6 @@ class GameBoyWindow: NSWindow, GameboyInputProvider, MTKViewDelegate {
// self.setContentSize(NSSize(width: width, height: height))
}

private func addMtkSubview() {
guard let contentView = self.contentView else {
print("Unable to find window content view.")
exit(1)
}

self.mtkView.device = self.device
self.mtkView.delegate = self
self.mtkView.colorPixelFormat = .bgra8Unorm
self.mtkView.framebufferOnly = true
self.mtkView.translatesAutoresizingMaskIntoConstraints = false

contentView.addSubview(self.mtkView)
NSLayoutConstraint.activate([
self.mtkView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
self.mtkView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
self.mtkView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
self.mtkView.heightAnchor.constraint(equalTo: contentView.heightAnchor)
])
}

// MARK: - Input

private var input = GameboyInput()
Expand Down

0 comments on commit 3794f3d

Please sign in to comment.