working SwiftUI mac example
This commit is contained in:
parent
dffc91c5e0
commit
1ee3bdd159
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,4 +5,5 @@ out/
|
||||
build/
|
||||
CMakeLists.txt.user
|
||||
**/.DS_Store
|
||||
xcuserdata/
|
||||
.swiftpm/
|
||||
|
||||
@ -15,7 +15,7 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
@property CGImageRef frame;
|
||||
|
||||
- (instancetype)init;
|
||||
- (void)runRom:(NSString*)path;
|
||||
- (void)runRom:(NSURL*)url;
|
||||
- (void)stepToNextFrame;
|
||||
|
||||
@end
|
||||
|
||||
@ -30,7 +30,11 @@
|
||||
}
|
||||
|
||||
- (void)runRom:(NSURL*)url {
|
||||
|
||||
[url startAccessingSecurityScopedResource];
|
||||
_system->insertCartridge([url fileSystemRepresentation]);
|
||||
[url stopAccessingSecurityScopedResource];
|
||||
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
while(TRUE) {
|
||||
[_condition lock];
|
||||
|
||||
@ -10,14 +10,10 @@ import NesKitCpp
|
||||
|
||||
class NesSystemSwift {
|
||||
|
||||
var nesSystem: nes.System?
|
||||
|
||||
init() {
|
||||
|
||||
let frameBufferSize = nes.Ppu.SCREEN_WIDTH * nes.Ppu.SCREEN_HEIGHT * MemoryLayout<nes.Pixel>.stride
|
||||
|
||||
nesSystem?.setNewFrameCallback({ frameBuffer in
|
||||
|
||||
})
|
||||
}
|
||||
// var nesSystem: nes.System?
|
||||
//
|
||||
// init() {
|
||||
//
|
||||
// let frameBufferSize = nes.Ppu.SCREEN_WIDTH * nes.Ppu.SCREEN_HEIGHT * MemoryLayout<nes.Pixel>.stride
|
||||
// }
|
||||
}
|
||||
|
||||
Binary file not shown.
@ -6,16 +6,36 @@
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import NesKit
|
||||
import UniformTypeIdentifiers
|
||||
|
||||
struct ContentView: View {
|
||||
|
||||
@State var system = NesSystem()
|
||||
@State var showFileImporter = false
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
Image(systemName: "globe")
|
||||
.imageScale(.large)
|
||||
.foregroundStyle(.tint)
|
||||
Text("Hello, world!")
|
||||
}
|
||||
.padding()
|
||||
NesView(system: system)
|
||||
.fileImporter(
|
||||
isPresented: $showFileImporter,
|
||||
allowedContentTypes: [.init(filenameExtension: "nes") ?? .data],
|
||||
allowsMultipleSelection: false) { result in
|
||||
switch result {
|
||||
case .success(let urls):
|
||||
if let url = urls.first {
|
||||
system.runRom(url)
|
||||
}
|
||||
case .failure(let error):
|
||||
print("error: \(error)")
|
||||
}
|
||||
}
|
||||
.toolbar {
|
||||
Button {
|
||||
showFileImporter = true
|
||||
} label: {
|
||||
Image(systemName: "document.badge.plus")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
23
examples/NesApp/NesApp/NesView/NesSystemView.swift
Normal file
23
examples/NesApp/NesApp/NesView/NesSystemView.swift
Normal file
@ -0,0 +1,23 @@
|
||||
//
|
||||
// NesSystemView.swift
|
||||
// NesApp
|
||||
//
|
||||
// Created by Selim Mustafaev on 04.10.2025.
|
||||
//
|
||||
|
||||
import AppKit
|
||||
import NesKit
|
||||
|
||||
class NesSystemView: NSView {
|
||||
|
||||
init(system: NesSystem) {
|
||||
super.init(frame: .zero)
|
||||
|
||||
self.layer = NesLayer(nesSystem: system)
|
||||
self.wantsLayer = true
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
}
|
||||
@ -5,27 +5,20 @@
|
||||
// Created by Selim Mustafaev on 04.10.2025.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
import NesKit
|
||||
|
||||
class NesView: UIView {
|
||||
struct NesView: NSViewRepresentable {
|
||||
|
||||
let nesLayer: NesLayer
|
||||
@State var system: NesSystem
|
||||
|
||||
init(system: NesSystem) {
|
||||
self.nesLayer = NesLayer(nesSystem: system)
|
||||
super.init(frame: .zero)
|
||||
func makeNSView(context: Context) -> NesSystemView {
|
||||
|
||||
self.layer.addSublayer(self.nesLayer)
|
||||
NesSystemView(system: system)
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
func updateNSView(_ nsView: NesSystemView, context: Context) {
|
||||
|
||||
self.nesLayer.frame = self.layer.frame
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user