Panda3DS/src/pandios/Pandios/ContentView.swift
wheremyfoodat ce356c6e61
Some checks are pending
Android Build / x64 (release) (push) Waiting to run
Android Build / arm64 (release) (push) Waiting to run
HTTP Server Build / build (push) Waiting to run
Hydra Core Build / Windows (push) Waiting to run
Hydra Core Build / MacOS (push) Waiting to run
Hydra Core Build / Linux (push) Waiting to run
Hydra Core Build / Android-x64 (push) Waiting to run
Hydra Core Build / ARM-Libretro (push) Waiting to run
Linux AppImage Build / build (push) Waiting to run
Linux Build / build (push) Waiting to run
MacOS Build / MacOS-arm64 (push) Waiting to run
MacOS Build / MacOS-x86_64 (push) Waiting to run
MacOS Build / MacOS-Universal (push) Blocked by required conditions
Qt Build / Windows (push) Waiting to run
Qt Build / MacOS-arm64 (push) Waiting to run
Qt Build / MacOS-x86_64 (push) Waiting to run
Qt Build / MacOS-Universal (push) Blocked by required conditions
Qt Build / Linux (push) Waiting to run
Windows Build / build (push) Waiting to run
iOS Simulator Build / build (push) Waiting to run
iOS: Remove debug prints
2025-06-29 23:33:02 +03:00

116 lines
3.3 KiB
Swift

import AlberDriver
import SwiftUI
import MetalKit
import Darwin
final class DrawableSize {
var width: UInt32 = 0
var height: UInt32 = 0
var sizeChanged = false
}
var emulatorLock = NSLock()
var drawableSize = DrawableSize()
class ResizeAwareMTKView: MTKView {
var onResize: ((CGSize) -> Void)?
override func layoutSubviews() {
super.layoutSubviews()
onResize?(self.drawableSize)
}
}
class DocumentViewController: UIViewController, DocumentDelegate {
var documentPicker: DocumentPicker!
override func viewDidLoad() {
super.viewDidLoad()
documentPicker = DocumentPicker(presentationController: self, delegate: self)
/// When the view loads (ie user opens the app) show the file picker
show()
}
/// Callback from the document picker
func didPickDocument(document: Document?) {
if let pickedDoc = document {
let fileURL = pickedDoc.fileURL
print("Loading ROM", fileURL)
emulatorLock.lock()
iosLoadROM(fileURL.path(percentEncoded: false))
emulatorLock.unlock()
}
}
func show() {
documentPicker.displayPicker()
}
}
struct DocumentView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> DocumentViewController {
DocumentViewController()
}
func updateUIViewController(_ uiViewController: DocumentViewController, context: Context) {}
}
struct ContentView: UIViewRepresentable {
func makeUIView(context: Context) -> ResizeAwareMTKView {
let mtkView = ResizeAwareMTKView()
mtkView.preferredFramesPerSecond = 60
mtkView.enableSetNeedsDisplay = true
mtkView.isPaused = true
if let metalDevice = MTLCreateSystemDefaultDevice() {
mtkView.device = metalDevice
}
mtkView.framebufferOnly = false
mtkView.drawableSize = mtkView.frame.size
mtkView.onResize = { newDrawableSize in
let newWidth = UInt32(newDrawableSize.width)
let newHeight = UInt32(newDrawableSize.height)
emulatorLock.lock()
if drawableSize.width != newWidth || drawableSize.height != newHeight {
drawableSize.width = newWidth
drawableSize.height = newHeight
drawableSize.sizeChanged = true
}
emulatorLock.unlock()
}
let dispatchQueue = DispatchQueue(label: "QueueIdentification", qos: .background)
let metalLayer = mtkView.layer as! CAMetalLayer
dispatchQueue.async {
iosCreateEmulator()
while (true) {
emulatorLock.lock()
if drawableSize.sizeChanged {
drawableSize.sizeChanged = false
iosSetOutputSize(drawableSize.width, drawableSize.height)
}
iosRunFrame(metalLayer)
emulatorLock.unlock()
}
}
return mtkView
}
func updateUIView(_ uiView: ResizeAwareMTKView, context: Context) {}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
DocumentView()
ContentView()
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}