Files
__mahjong/components/render/mac/view/RenderGLView.swift
T
2026-10-19 16:48:38 +03:00

123 lines
3.6 KiB
Swift

import AppKit
import QuartzCore
import RaylibBridge
import SwiftUI
final class RenderGLView: 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) {
guard
let format = NSOpenGLPixelFormat(attributes: RENDER_OPENGL_ATTRS)
else {
fatalError("ERROR Could not create required OpenGL 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)
otherPrintStderr("RenderGLView prepareOpenGL bounds \(bounds) backing \(convertToBacking(bounds))")
sr_init(pixelWidth, pixelHeight,
RENDER_SHADER_MAIN_VS, RENDER_SHADER_MAIN_FS,
RENDER_SHADER_SHADOW_VS, RENDER_SHADER_SHADOW_FS,
RENDER_CONFIG)
loadModel()
startTime = CACurrentMediaTime()
ready = true
startRenderTimer()
}
private func startRenderTimer() {
guard renderTimer == nil else { return }
let timer = Timer(timeInterval: 1.0 / 60.0, repeats: true) { [weak self] _ in
self?.needsDisplay = true
}
RunLoop.main.add(timer, forMode: .common)
renderTimer = timer
}
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()
otherPrintStderr("RenderGLView reshape bounds \(bounds) backing \(convertToBacking(bounds))")
needsDisplay = true
}
override func viewDidChangeBackingProperties() {
super.viewDidChangeBackingProperties()
otherPrintStderr("RenderGLView viewDidChangeBackingProperties bounds \(bounds) backing \(convertToBacking(bounds))")
needsDisplay = true
}
override func draw(_ dirtyRect: NSRect) {
guard ready, let context = openGLContext else { return }
context.makeCurrentContext()
// Update pixel size
let backing = convertToBacking(bounds)
otherPrintStderr("RenderGLView draw bounds \(bounds) backing \(backing)")
pixelWidth = Int32(max(1, backing.width))
pixelHeight = Int32(max(1, backing.height))
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()
}
}