gl view done
This commit is contained in:
1 parent
137188e618
commit
ff4613d8e6
6 files changed
+54
-45
No files matched your search
@@ -1,11 +1,7 @@
|
|||||||
import AppKit
|
import AppKit
|
||||||
import QuartzCore
|
|
||||||
import RaylibBridge
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
final class RenderGLView: NSOpenGLView {
|
final class RenderGLView: NSOpenGLView {
|
||||||
private var startTime: CFTimeInterval = 0
|
|
||||||
|
|
||||||
override init(frame frameRect: NSRect) {
|
override init(frame frameRect: NSRect) {
|
||||||
guard
|
guard
|
||||||
let format = NSOpenGLPixelFormat(attributes: RENDER_OPENGL_ATTRS)
|
let format = NSOpenGLPixelFormat(attributes: RENDER_OPENGL_ATTRS)
|
||||||
@@ -22,10 +18,7 @@ final class RenderGLView: NSOpenGLView {
|
|||||||
|
|
||||||
override func prepareOpenGL() {
|
override func prepareOpenGL() {
|
||||||
super.prepareOpenGL()
|
super.prepareOpenGL()
|
||||||
|
renderSet(F.didPrepareGL, true)
|
||||||
startTime = CACurrentMediaTime()
|
|
||||||
|
|
||||||
renderSet(F.didPrepareGLContext, true)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override func reshape() {
|
override func reshape() {
|
||||||
@@ -46,31 +39,12 @@ final class RenderGLView: NSOpenGLView {
|
|||||||
|
|
||||||
override func draw(_ dirtyRect: NSRect) {
|
override func draw(_ dirtyRect: NSRect) {
|
||||||
let vm = RenderVM.shared!
|
let vm = RenderVM.shared!
|
||||||
|
if vm.didPrepareGL {
|
||||||
guard
|
renderDraw(vm)
|
||||||
vm.didPrepareGLContext,
|
|
||||||
let context = openGLContext
|
|
||||||
else {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
context.makeCurrentContext()
|
|
||||||
|
|
||||||
sr_resize(vm.pixelSize[0], vm.pixelSize[1])
|
|
||||||
|
|
||||||
let t = Float(CACurrentMediaTime() - startTime)
|
|
||||||
|
|
||||||
sr_set_rotation(t * 6.0, t * 18.0, 0.0)
|
|
||||||
|
|
||||||
sr_render()
|
|
||||||
|
|
||||||
context.flushBuffer()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
print("ИГР RenderGV.deinit")
|
renderSet(F.willDeinit, true)
|
||||||
// TODO: renderTimer?.invalidate()
|
|
||||||
openGLContext?.makeCurrentContext()
|
|
||||||
sr_shutdown()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,16 @@
|
|||||||
import AppKit
|
import AppKit
|
||||||
|
import RaylibBridge
|
||||||
|
|
||||||
|
func renderDraw(_ vm: RenderVM) {
|
||||||
|
vm.glView.openGLContext?.makeCurrentContext()
|
||||||
|
|
||||||
|
sr_resize(vm.pixelSize[0], vm.pixelSize[1])
|
||||||
|
let t = Float(CACurrentMediaTime() - vm.startTime)
|
||||||
|
sr_set_rotation(t * 6.0, t * 18.0, 0.0)
|
||||||
|
sr_render()
|
||||||
|
|
||||||
|
vm.glView.openGLContext?.flushBuffer()
|
||||||
|
}
|
||||||
|
|
||||||
func renderPixelSize(_ view: NSView) -> [Int32] {
|
func renderPixelSize(_ view: NSView) -> [Int32] {
|
||||||
let backing = view.convertToBacking(view.bounds)
|
let backing = view.convertToBacking(view.bounds)
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import RaylibBridge
|
import RaylibBridge
|
||||||
|
import QuartzCore
|
||||||
|
|
||||||
|
func renderDeinit(_ vm: RenderVM) {
|
||||||
|
print("ИГР renderD")
|
||||||
|
vm.timer?.invalidate()
|
||||||
|
vm.glView.openGLContext?.makeCurrentContext()
|
||||||
|
sr_shutdown()
|
||||||
|
}
|
||||||
|
|
||||||
func renderInit(_ vm: RenderVM) {
|
func renderInit(_ vm: RenderVM) {
|
||||||
vm.glView.openGLContext?.makeCurrentContext()
|
vm.glView.openGLContext?.makeCurrentContext()
|
||||||
@@ -27,11 +35,14 @@ func renderLoadCubeModel(_ vm: RenderVM) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func renderStartTimer(_ vm: RenderVM) {
|
func renderStartTimer(_ vm: RenderVM) {
|
||||||
|
// Make a time mark for calculating time delta
|
||||||
|
vm.startTime = CACurrentMediaTime()
|
||||||
|
|
||||||
// Schedule repaints every once in FPS seconds
|
// Schedule repaints every once in FPS seconds
|
||||||
let timer = Timer(timeInterval: 1.0 / RENDER_FPS, repeats: true) { [weak vm] _ in
|
let timer = Timer(timeInterval: 1.0 / RENDER_FPS, repeats: true) { [weak vm] _ in
|
||||||
vm?.glView.needsDisplay = true
|
vm?.glView.needsDisplay = true
|
||||||
}
|
}
|
||||||
vm.timer = timer
|
vm.timer = timer
|
||||||
// Use .common so that holding buttons in UI does not pause this rendering timer
|
// .common means holding buttons in UI does not pause this rendering timer
|
||||||
RunLoop.main.add(timer, forMode: .common)
|
RunLoop.main.add(timer, forMode: .common)
|
||||||
}
|
}
|
||||||
@@ -2,9 +2,10 @@ import Combine
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
final class RenderVM: ObservableObject {
|
final class RenderVM: ObservableObject {
|
||||||
var didPrepareGLContext = false
|
var didPrepareGL = false
|
||||||
let glView = RenderGLView(frame: .zero)
|
let glView = RenderGLView(frame: .zero)
|
||||||
var pixelSize = [Int32]()
|
var pixelSize = [Int32]()
|
||||||
|
var startTime: CFTimeInterval = 0
|
||||||
var timer: Timer?
|
var timer: Timer?
|
||||||
|
|
||||||
static private(set) weak var shared: RenderVM?
|
static private(set) weak var shared: RenderVM?
|
||||||
|
|||||||
+6
-4
@@ -10,20 +10,22 @@ RenderContext:
|
|||||||
backingPixelSize: [Int32]
|
backingPixelSize: [Int32]
|
||||||
didClickIncrement: Bool
|
didClickIncrement: Bool
|
||||||
didLaunch: Bool
|
didLaunch: Bool
|
||||||
didPrepareGLContext: Bool
|
didPrepareGL: Bool
|
||||||
didSetup: Bool
|
didSetup: Bool
|
||||||
isReadyToRender: Bool
|
isReadyToRender: Bool
|
||||||
pixelSize: [Int32]
|
pixelSize: [Int32]
|
||||||
reshapePixelSize: [Int32]
|
reshapePixelSize: [Int32]
|
||||||
|
willDeinit: Bool
|
||||||
|
|
||||||
RenderComponent:
|
RenderComponent:
|
||||||
type: component
|
type: component
|
||||||
oneliners:
|
oneliners:
|
||||||
- didClickIncrement: renderLoadCubeModel(RenderVM.shared!)
|
- didClickIncrement: renderLoadCubeModel(RenderVM.shared!)
|
||||||
|
- didPrepareGL: renderInit(RenderVM.shared!)
|
||||||
|
- didPrepareGL: RenderVM.shared!.didPrepareGL = true
|
||||||
|
- didPrepareGL: renderStartTimer(RenderVM.shared!)
|
||||||
- pixelSize: RenderVM.shared!.pixelSize = c.pixelSize
|
- pixelSize: RenderVM.shared!.pixelSize = c.pixelSize
|
||||||
- didPrepareGLContext: renderInit(RenderVM.shared!)
|
- willDeinit: renderDeinit(RenderVM.shared!)
|
||||||
- didPrepareGLContext: RenderVM.shared!.didPrepareGLContext = true
|
|
||||||
- didPrepareGLContext: renderStartTimer(RenderVM.shared!)
|
|
||||||
shoulds:
|
shoulds:
|
||||||
didLaunch:
|
didLaunch:
|
||||||
1. Only once during the first setup:
|
1. Only once during the first setup:
|
||||||
|
|||||||
@@ -124,22 +124,24 @@ public struct F {
|
|||||||
public static let countText = "countText"
|
public static let countText = "countText"
|
||||||
public static let didClickIncrement = "didClickIncrement"
|
public static let didClickIncrement = "didClickIncrement"
|
||||||
public static let didLaunch = "didLaunch"
|
public static let didLaunch = "didLaunch"
|
||||||
public static let didPrepareGLContext = "didPrepareGLContext"
|
public static let didPrepareGL = "didPrepareGL"
|
||||||
public static let didSetup = "didSetup"
|
public static let didSetup = "didSetup"
|
||||||
public static let isReadyToRender = "isReadyToRender"
|
public static let isReadyToRender = "isReadyToRender"
|
||||||
public static let pixelSize = "pixelSize"
|
public static let pixelSize = "pixelSize"
|
||||||
public static let reshapePixelSize = "reshapePixelSize"
|
public static let reshapePixelSize = "reshapePixelSize"
|
||||||
|
public static let willDeinit = "willDeinit"
|
||||||
|
|
||||||
}
|
}
|
||||||
public struct RenderContext: DialectContext {
|
public struct RenderContext: DialectContext {
|
||||||
public var backingPixelSize = [Int32]()
|
public var backingPixelSize = [Int32]()
|
||||||
public var didClickIncrement = Bool()
|
public var didClickIncrement = Bool()
|
||||||
public var didLaunch = Bool()
|
public var didLaunch = Bool()
|
||||||
public var didPrepareGLContext = Bool()
|
public var didPrepareGL = Bool()
|
||||||
public var didSetup = Bool()
|
public var didSetup = Bool()
|
||||||
public var isReadyToRender = Bool()
|
public var isReadyToRender = Bool()
|
||||||
public var pixelSize = [Int32]()
|
public var pixelSize = [Int32]()
|
||||||
public var reshapePixelSize = [Int32]()
|
public var reshapePixelSize = [Int32]()
|
||||||
|
public var willDeinit = Bool()
|
||||||
|
|
||||||
public var recentField = ""
|
public var recentField = ""
|
||||||
|
|
||||||
@@ -153,8 +155,8 @@ public struct RenderContext: DialectContext {
|
|||||||
else if (name == "didLaunch") {
|
else if (name == "didLaunch") {
|
||||||
return didLaunch as! T
|
return didLaunch as! T
|
||||||
}
|
}
|
||||||
else if (name == "didPrepareGLContext") {
|
else if (name == "didPrepareGL") {
|
||||||
return didPrepareGLContext as! T
|
return didPrepareGL as! T
|
||||||
}
|
}
|
||||||
else if (name == "didSetup") {
|
else if (name == "didSetup") {
|
||||||
return didSetup as! T
|
return didSetup as! T
|
||||||
@@ -168,6 +170,9 @@ public struct RenderContext: DialectContext {
|
|||||||
else if (name == "reshapePixelSize") {
|
else if (name == "reshapePixelSize") {
|
||||||
return reshapePixelSize as! T
|
return reshapePixelSize as! T
|
||||||
}
|
}
|
||||||
|
else if (name == "willDeinit") {
|
||||||
|
return willDeinit as! T
|
||||||
|
}
|
||||||
|
|
||||||
return "unknown-field-name" as! T
|
return "unknown-field-name" as! T
|
||||||
}
|
}
|
||||||
@@ -185,8 +190,8 @@ public struct RenderContext: DialectContext {
|
|||||||
else if (name == "didLaunch") {
|
else if (name == "didLaunch") {
|
||||||
didLaunch = value as! Bool
|
didLaunch = value as! Bool
|
||||||
}
|
}
|
||||||
else if (name == "didPrepareGLContext") {
|
else if (name == "didPrepareGL") {
|
||||||
didPrepareGLContext = value as! Bool
|
didPrepareGL = value as! Bool
|
||||||
}
|
}
|
||||||
else if (name == "didSetup") {
|
else if (name == "didSetup") {
|
||||||
didSetup = value as! Bool
|
didSetup = value as! Bool
|
||||||
@@ -200,6 +205,9 @@ public struct RenderContext: DialectContext {
|
|||||||
else if (name == "reshapePixelSize") {
|
else if (name == "reshapePixelSize") {
|
||||||
reshapePixelSize = value as! [Int32]
|
reshapePixelSize = value as! [Int32]
|
||||||
}
|
}
|
||||||
|
else if (name == "willDeinit") {
|
||||||
|
willDeinit = value as! Bool
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -418,10 +426,11 @@ func rootRegisterShoulds(_ ctrl: DialectController) {
|
|||||||
func renderRegisterEffects(_ ctrl: DialectController) {
|
func renderRegisterEffects(_ ctrl: DialectController) {
|
||||||
let _: RenderContext? = registerOneliners(ctrl, [
|
let _: RenderContext? = registerOneliners(ctrl, [
|
||||||
F.didClickIncrement, { (c: RenderContext) in renderLoadCubeModel(RenderVM.shared!) },
|
F.didClickIncrement, { (c: RenderContext) in renderLoadCubeModel(RenderVM.shared!) },
|
||||||
|
F.didPrepareGL, { (c: RenderContext) in renderInit(RenderVM.shared!) },
|
||||||
|
F.didPrepareGL, { (c: RenderContext) in RenderVM.shared!.didPrepareGL = true },
|
||||||
|
F.didPrepareGL, { (c: RenderContext) in renderStartTimer(RenderVM.shared!) },
|
||||||
F.pixelSize, { (c: RenderContext) in RenderVM.shared!.pixelSize = c.pixelSize },
|
F.pixelSize, { (c: RenderContext) in RenderVM.shared!.pixelSize = c.pixelSize },
|
||||||
F.didPrepareGLContext, { (c: RenderContext) in renderInit(RenderVM.shared!) },
|
F.willDeinit, { (c: RenderContext) in renderDeinit(RenderVM.shared!) },
|
||||||
F.didPrepareGLContext, { (c: RenderContext) in RenderVM.shared!.didPrepareGLContext = true },
|
|
||||||
F.didPrepareGLContext, { (c: RenderContext) in renderStartTimer(RenderVM.shared!) },
|
|
||||||
|
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user