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(NSOpenGLProfileVersion4_1Core), 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 4.1 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) 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() updatePixelSize() needsDisplay = true } override func viewDidChangeBackingProperties() { super.viewDidChangeBackingProperties() updatePixelSize() 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 * 18.0, t * 60.0, 0.0) sr_render() context.flushBuffer() } deinit { renderTimer?.invalidate() openGLContext?.makeCurrentContext() sr_shutdown() } }