141 lines
4.1 KiB
Swift
141 lines
4.1 KiB
Swift
import SwiftUI
|
|
import AppKit
|
|
import QuartzCore
|
|
import RaylibBridge
|
|
|
|
/// A raylib-driven 3D scene (rendered through rlgl into an NSOpenGLView) that
|
|
/// sits behind the SwiftUI interface.
|
|
struct RaylibGLView: NSViewRepresentable {
|
|
func makeNSView(context: Context) -> RaylibGLNSView {
|
|
RaylibGLNSView(frame: .zero)
|
|
}
|
|
|
|
func updateNSView(_ nsView: RaylibGLNSView, context: Context) {}
|
|
}
|
|
|
|
/// An OpenGL view whose context is rendered directly by raylib's rlgl layer,
|
|
/// without letting raylib create its own window. Mirrors the reference sample
|
|
/// (`swiftraylib`), but drives a single cube that is rotated by the application
|
|
/// every frame.
|
|
final class RaylibGLNSView: NSOpenGLView {
|
|
private var renderTimer: Timer?
|
|
private var ready = false
|
|
private var pixelWidth: Int32 = 1
|
|
private var pixelHeight: Int32 = 1
|
|
private var startTime: CFTimeInterval = 0
|
|
|
|
override init(frame frameRect: NSRect) {
|
|
let attributes: [NSOpenGLPixelFormatAttribute] = [
|
|
UInt32(NSOpenGLPFAOpenGLProfile), UInt32(NSOpenGLProfileVersion3_2Core),
|
|
UInt32(NSOpenGLPFAColorSize), 24,
|
|
UInt32(NSOpenGLPFAAlphaSize), 8,
|
|
UInt32(NSOpenGLPFADepthSize), 24,
|
|
UInt32(NSOpenGLPFADoubleBuffer),
|
|
UInt32(NSOpenGLPFAAccelerated),
|
|
0
|
|
]
|
|
|
|
guard let format = NSOpenGLPixelFormat(attributes: attributes) else {
|
|
fatalError("Unable to create an OpenGL 3.2 core pixel format")
|
|
}
|
|
|
|
super.init(frame: frameRect, pixelFormat: format)!
|
|
wantsBestResolutionOpenGLSurface = true
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
override func prepareOpenGL() {
|
|
super.prepareOpenGL()
|
|
|
|
guard let context = openGLContext else { return }
|
|
context.makeCurrentContext()
|
|
|
|
var swapInterval: Int32 = 1
|
|
context.setValues(&swapInterval, for: .swapInterval)
|
|
|
|
updatePixelSize()
|
|
sr_init(pixelWidth, pixelHeight,
|
|
RAYLIB_SHADER_MAIN_VS, RAYLIB_SHADER_MAIN_FS,
|
|
RAYLIB_SHADER_SHADOW_VS, RAYLIB_SHADER_SHADOW_FS)
|
|
loadModel()
|
|
startTime = CACurrentMediaTime()
|
|
ready = true
|
|
|
|
startRenderTimer()
|
|
}
|
|
|
|
private func startRenderTimer() {
|
|
guard renderTimer == nil else { return }
|
|
renderTimer = Timer.scheduledTimer(withTimeInterval: 1.0 / 60.0, repeats: true) { [weak self] _ in
|
|
self?.needsDisplay = true
|
|
}
|
|
}
|
|
|
|
private func loadModel() {
|
|
for path in modelCandidates() where FileManager.default.fileExists(atPath: path) {
|
|
sr_load_model(path)
|
|
return
|
|
}
|
|
}
|
|
|
|
private func modelCandidates() -> [String] {
|
|
var paths: [String] = []
|
|
|
|
if let resources = Bundle.main.resourceURL {
|
|
paths.append(resources.appendingPathComponent("assets/cube.gltf").path)
|
|
}
|
|
|
|
let cwd = FileManager.default.currentDirectoryPath
|
|
paths.append("\(cwd)/assets/cube.gltf")
|
|
|
|
if let exe = Bundle.main.executableURL {
|
|
let dir = exe.deletingLastPathComponent()
|
|
paths.append(dir.appendingPathComponent("assets/cube.gltf").path)
|
|
}
|
|
|
|
return paths
|
|
}
|
|
|
|
override func reshape() {
|
|
super.reshape()
|
|
needsDisplay = true
|
|
}
|
|
|
|
override func viewDidChangeBackingProperties() {
|
|
super.viewDidChangeBackingProperties()
|
|
needsDisplay = true
|
|
}
|
|
|
|
private func updatePixelSize() {
|
|
let backing = convertToBacking(bounds)
|
|
pixelWidth = Int32(max(1, backing.width))
|
|
pixelHeight = Int32(max(1, backing.height))
|
|
}
|
|
|
|
override func draw(_ dirtyRect: NSRect) {
|
|
guard ready, let context = openGLContext else { return }
|
|
|
|
context.makeCurrentContext()
|
|
updatePixelSize()
|
|
|
|
sr_resize(pixelWidth, pixelHeight)
|
|
|
|
let t = Float(CACurrentMediaTime() - startTime)
|
|
|
|
sr_set_rotation(t * 6.0, t * 18.0, 0.0)
|
|
|
|
sr_render()
|
|
|
|
context.flushBuffer()
|
|
}
|
|
|
|
deinit {
|
|
renderTimer?.invalidate()
|
|
openGLContext?.makeCurrentContext()
|
|
sr_shutdown()
|
|
}
|
|
}
|