Display a textured cube with Raylib (#2)
This commit was merged in pull request #2.
This commit is contained in:
1 parent
184454cf59
commit
1515a9483b
13 files changed
+982
-12
No files matched your search
@@ -0,0 +1,138 @@
|
||||
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()
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -8,17 +8,22 @@ struct RootView: View {
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 16) {
|
||||
Text("Hello, World!")
|
||||
.font(.largeTitle)
|
||||
ZStack {
|
||||
RaylibGLView()
|
||||
.ignoresSafeArea()
|
||||
|
||||
Text(vm.countText)
|
||||
.font(.title3.monospacedDigit())
|
||||
VStack(spacing: 16) {
|
||||
Text("Hello, World!")
|
||||
.font(.largeTitle)
|
||||
|
||||
Button("Increment") {
|
||||
rootSet(F.didClickIncrement, true)
|
||||
Text(vm.countText)
|
||||
.font(.title3.monospacedDigit())
|
||||
|
||||
Button("Increment") {
|
||||
rootSet(F.didClickIncrement, true)
|
||||
}
|
||||
}
|
||||
.padding(40)
|
||||
}
|
||||
.padding(40)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user