move timer
This commit is contained in:
1 parent
b068b866f0
commit
fab65fb5ad
6 files changed
+29
-13
No files matched your search
@@ -4,7 +4,6 @@ import RaylibBridge
|
||||
import SwiftUI
|
||||
|
||||
final class RenderGLView: NSOpenGLView {
|
||||
private var renderTimer: Timer?
|
||||
private var ready = false
|
||||
private var startTime: CFTimeInterval = 0
|
||||
|
||||
@@ -24,8 +23,8 @@ final class RenderGLView: NSOpenGLView {
|
||||
|
||||
override func prepareOpenGL() {
|
||||
super.prepareOpenGL()
|
||||
|
||||
guard let context = openGLContext else { return }
|
||||
|
||||
context.makeCurrentContext()
|
||||
|
||||
var swapInterval: Int32 = 1
|
||||
@@ -39,16 +38,7 @@ final class RenderGLView: NSOpenGLView {
|
||||
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
|
||||
renderSet(F.willPrepareGLContext, true)
|
||||
}
|
||||
|
||||
private func loadModel() {
|
||||
@@ -111,7 +101,8 @@ final class RenderGLView: NSOpenGLView {
|
||||
}
|
||||
|
||||
deinit {
|
||||
renderTimer?.invalidate()
|
||||
print("ИГР RenderGV.deinit")
|
||||
// TODO: renderTimer?.invalidate()
|
||||
openGLContext?.makeCurrentContext()
|
||||
sr_shutdown()
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ let RENDER_CONFIG = sr_config(
|
||||
ground_size: 40.0
|
||||
)
|
||||
|
||||
let RENDER_FPS = 60.0
|
||||
|
||||
let RENDER_OPENGL_ATTRS: [NSOpenGLPixelFormatAttribute] = [
|
||||
UInt32(NSOpenGLPFAOpenGLProfile), UInt32(NSOpenGLProfileVersion3_2Core),
|
||||
UInt32(NSOpenGLPFAColorSize), 24,
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import Foundation
|
||||
|
||||
func renderStartTimer(_ vm: RenderVM) {
|
||||
// Schedule repaints every once in FPS seconds
|
||||
let timer = Timer(timeInterval: 1.0 / RENDER_FPS, repeats: true) { [weak vm] _ in
|
||||
vm?.glView.needsDisplay = true
|
||||
}
|
||||
vm.timer = timer
|
||||
// Use .common so that holding buttons in UI does not pause this rendering timer
|
||||
RunLoop.main.add(timer, forMode: .common)
|
||||
}
|
||||
@@ -13,12 +13,14 @@ RenderContext:
|
||||
isReadyToRender: Bool
|
||||
pixelSize: [Int32]
|
||||
reshapePixelSize: [Int32]
|
||||
willPrepareGLContext: Bool
|
||||
|
||||
RenderComponent:
|
||||
type: component
|
||||
oneliners:
|
||||
- isReadyToRender: print("ИГР isRTR: '\(c.isReadyToRender)'")
|
||||
- pixelSize: RenderVM.shared!.pixelSize = c.pixelSize
|
||||
- willPrepareGLContext: renderStartTimer(RenderVM.shared!)
|
||||
shoulds:
|
||||
didLaunch:
|
||||
1. Only once during the first setup:
|
||||
|
||||
@@ -128,6 +128,7 @@ public struct F {
|
||||
public static let isReadyToRender = "isReadyToRender"
|
||||
public static let pixelSize = "pixelSize"
|
||||
public static let reshapePixelSize = "reshapePixelSize"
|
||||
public static let willPrepareGLContext = "willPrepareGLContext"
|
||||
|
||||
}
|
||||
public struct RenderContext: DialectContext {
|
||||
@@ -137,6 +138,7 @@ public struct RenderContext: DialectContext {
|
||||
public var isReadyToRender = Bool()
|
||||
public var pixelSize = [Int32]()
|
||||
public var reshapePixelSize = [Int32]()
|
||||
public var willPrepareGLContext = Bool()
|
||||
|
||||
public var recentField = ""
|
||||
|
||||
@@ -159,6 +161,9 @@ public struct RenderContext: DialectContext {
|
||||
else if (name == "reshapePixelSize") {
|
||||
return reshapePixelSize as! T
|
||||
}
|
||||
else if (name == "willPrepareGLContext") {
|
||||
return willPrepareGLContext as! T
|
||||
}
|
||||
|
||||
return "unknown-field-name" as! T
|
||||
}
|
||||
@@ -185,6 +190,9 @@ public struct RenderContext: DialectContext {
|
||||
else if (name == "reshapePixelSize") {
|
||||
reshapePixelSize = value as! [Int32]
|
||||
}
|
||||
else if (name == "willPrepareGLContext") {
|
||||
willPrepareGLContext = value as! Bool
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -404,6 +412,7 @@ func renderRegisterEffects(_ ctrl: DialectController) {
|
||||
let _: RenderContext? = registerOneliners(ctrl, [
|
||||
F.isReadyToRender, { (c: RenderContext) in print("ИГР isRTR: '\(c.isReadyToRender)'") },
|
||||
F.pixelSize, { (c: RenderContext) in RenderVM.shared!.pixelSize = c.pixelSize },
|
||||
F.willPrepareGLContext, { (c: RenderContext) in renderStartTimer(RenderVM.shared!) },
|
||||
|
||||
])
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
../../../../components/render/renderEffect.swift
|
||||
Reference in new issue
Block a user