try my way
This commit is contained in:
1 parent
6fecc1e95e
commit
fa1cd053c8
16 files changed
+521
-2
No files matched your search
@@ -0,0 +1,60 @@
|
||||
import UIKit
|
||||
|
||||
// UIView counterpart of the macOS RenderGLView. UIKit never draws into it: the
|
||||
// EGL/ANGLE surface renders directly into this view's layer, and the timer in
|
||||
// RenderVM presents a new frame. The view only reports when it gains a window
|
||||
// and when its backing size changes.
|
||||
final class RenderGLView: UIView {
|
||||
private weak var renderVM: RenderVM?
|
||||
private var didInit = false
|
||||
private var lastPixelSize: [Int32] = [0, 0]
|
||||
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
|
||||
isOpaque = true
|
||||
// Touch handling is not wired to raylib yet, so keep the GL view
|
||||
// transparent to the SwiftUI hierarchy above it.
|
||||
isUserInteractionEnabled = false
|
||||
|
||||
contentScaleFactor = UIScreen.main.scale
|
||||
layer.contentsScale = contentScaleFactor
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
override func didMoveToWindow() {
|
||||
super.didMoveToWindow()
|
||||
|
||||
guard let window else { return }
|
||||
contentScaleFactor = window.screen.nativeScale
|
||||
layer.contentsScale = contentScaleFactor
|
||||
|
||||
guard !didInit, let vm = RenderVM.shared else { return }
|
||||
guard renderInit(vm) else { return }
|
||||
|
||||
renderVM = vm
|
||||
didInit = true
|
||||
lastPixelSize = renderPixelSize(self)
|
||||
renderStartTimer(vm)
|
||||
}
|
||||
|
||||
override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
|
||||
guard didInit else { return }
|
||||
|
||||
let size = renderPixelSize(self)
|
||||
guard size != lastPixelSize else { return }
|
||||
|
||||
lastPixelSize = size
|
||||
raybri_ios_resize(size[0], size[1])
|
||||
}
|
||||
|
||||
deinit {
|
||||
guard didInit, let vm = renderVM else { return }
|
||||
renderDeinit(vm)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import Combine
|
||||
import Foundation
|
||||
|
||||
// iOS counterpart of the macOS RenderVM: it owns the view raylib draws into
|
||||
// and the timer that drives it. The actual rendering lives in raybriIOS.mm.
|
||||
final class RenderVM: ObservableObject {
|
||||
static private(set) weak var shared: RenderVM?
|
||||
|
||||
let glView = RenderGLView(frame: .zero)
|
||||
var startTime: CFTimeInterval = 0
|
||||
var timer: Timer?
|
||||
|
||||
init() {
|
||||
Self.shared = self
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import SwiftUI
|
||||
|
||||
// SwiftUI counterpart of the macOS RenderView. It vends the shared GL view,
|
||||
// which RenderVM also keeps alive and drives with a timer.
|
||||
struct RenderView: UIViewRepresentable {
|
||||
func makeUIView(context: Context) -> RenderGLView {
|
||||
RenderVM.shared!.glView
|
||||
}
|
||||
|
||||
func updateUIView(_ uiView: RenderGLView, context: Context) {}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import QuartzCore
|
||||
import UIKit
|
||||
|
||||
// Render a single frame. The timer calls this directly: an EGL/ANGLE surface
|
||||
// presents into the view's layer, so there is no draw(_:) / needsDisplay pass
|
||||
// like in the NSOpenGLView on macOS.
|
||||
func renderDraw(_ vm: RenderVM) {
|
||||
raybri_ios_render(CACurrentMediaTime() - vm.startTime)
|
||||
}
|
||||
|
||||
// Backing-pixel size of the view, which is what EGL/ANGLE renders into.
|
||||
func renderPixelSize(_ view: UIView) -> [Int32] {
|
||||
let scale = view.contentScaleFactor
|
||||
let w = Int32(max(1, (view.bounds.width * scale).rounded()))
|
||||
let h = Int32(max(1, (view.bounds.height * scale).rounded()))
|
||||
return [w, h]
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
// iOS render constants. The macOS renderConst.swift depends on AppKit and the
|
||||
// RaylibBridge SPM module, so the two platforms keep separate files.
|
||||
let RENDER_FPS = 60.0
|
||||
@@ -0,0 +1,35 @@
|
||||
import Foundation
|
||||
import QuartzCore
|
||||
import UIKit
|
||||
|
||||
// Bring up EGL/ANGLE on the view's layer. Returns false when the surface could
|
||||
// not be created (the caller then leaves the timer stopped).
|
||||
func renderInit(_ vm: RenderVM) -> Bool {
|
||||
let size = renderPixelSize(vm.glView)
|
||||
let layer = Unmanaged.passUnretained(vm.glView.layer).toOpaque()
|
||||
let ok = raybri_ios_init(layer, size[0], size[1]) != 0
|
||||
if !ok {
|
||||
otherPrintStderr("ИГР renderInit: EGL/ANGLE initialization failed")
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
// Same shape as the macOS timer: a repeating timer on the main run loop, but
|
||||
// here it calls the renderer directly instead of flagging the view for redraw.
|
||||
func renderStartTimer(_ vm: RenderVM) {
|
||||
vm.startTime = CACurrentMediaTime()
|
||||
|
||||
let timer = Timer(timeInterval: 1.0 / RENDER_FPS, repeats: true) { [weak vm] _ in
|
||||
guard let vm else { return }
|
||||
renderDraw(vm)
|
||||
}
|
||||
vm.timer = timer
|
||||
// .common keeps rendering alive while the user holds a control.
|
||||
RunLoop.main.add(timer, forMode: .common)
|
||||
}
|
||||
|
||||
func renderDeinit(_ vm: RenderVM) {
|
||||
vm.timer?.invalidate()
|
||||
vm.timer = nil
|
||||
raybri_ios_shutdown()
|
||||
}
|
||||
Reference in new issue
Block a user