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
|
import SwiftUI
|
||||||
|
|
||||||
final class RenderGLView: NSOpenGLView {
|
final class RenderGLView: NSOpenGLView {
|
||||||
private var renderTimer: Timer?
|
|
||||||
private var ready = false
|
private var ready = false
|
||||||
private var startTime: CFTimeInterval = 0
|
private var startTime: CFTimeInterval = 0
|
||||||
|
|
||||||
@@ -24,8 +23,8 @@ final class RenderGLView: NSOpenGLView {
|
|||||||
|
|
||||||
override func prepareOpenGL() {
|
override func prepareOpenGL() {
|
||||||
super.prepareOpenGL()
|
super.prepareOpenGL()
|
||||||
|
|
||||||
guard let context = openGLContext else { return }
|
guard let context = openGLContext else { return }
|
||||||
|
|
||||||
context.makeCurrentContext()
|
context.makeCurrentContext()
|
||||||
|
|
||||||
var swapInterval: Int32 = 1
|
var swapInterval: Int32 = 1
|
||||||
@@ -39,16 +38,7 @@ final class RenderGLView: NSOpenGLView {
|
|||||||
startTime = CACurrentMediaTime()
|
startTime = CACurrentMediaTime()
|
||||||
ready = true
|
ready = true
|
||||||
|
|
||||||
startRenderTimer()
|
renderSet(F.willPrepareGLContext, true)
|
||||||
}
|
|
||||||
|
|
||||||
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() {
|
private func loadModel() {
|
||||||
@@ -111,7 +101,8 @@ final class RenderGLView: NSOpenGLView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
renderTimer?.invalidate()
|
print("ИГР RenderGV.deinit")
|
||||||
|
// TODO: renderTimer?.invalidate()
|
||||||
openGLContext?.makeCurrentContext()
|
openGLContext?.makeCurrentContext()
|
||||||
sr_shutdown()
|
sr_shutdown()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ let RENDER_CONFIG = sr_config(
|
|||||||
ground_size: 40.0
|
ground_size: 40.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
let RENDER_FPS = 60.0
|
||||||
|
|
||||||
let RENDER_OPENGL_ATTRS: [NSOpenGLPixelFormatAttribute] = [
|
let RENDER_OPENGL_ATTRS: [NSOpenGLPixelFormatAttribute] = [
|
||||||
UInt32(NSOpenGLPFAOpenGLProfile), UInt32(NSOpenGLProfileVersion3_2Core),
|
UInt32(NSOpenGLPFAOpenGLProfile), UInt32(NSOpenGLProfileVersion3_2Core),
|
||||||
UInt32(NSOpenGLPFAColorSize), 24,
|
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
|
isReadyToRender: Bool
|
||||||
pixelSize: [Int32]
|
pixelSize: [Int32]
|
||||||
reshapePixelSize: [Int32]
|
reshapePixelSize: [Int32]
|
||||||
|
willPrepareGLContext: Bool
|
||||||
|
|
||||||
RenderComponent:
|
RenderComponent:
|
||||||
type: component
|
type: component
|
||||||
oneliners:
|
oneliners:
|
||||||
- isReadyToRender: print("ИГР isRTR: '\(c.isReadyToRender)'")
|
- isReadyToRender: print("ИГР isRTR: '\(c.isReadyToRender)'")
|
||||||
- pixelSize: RenderVM.shared!.pixelSize = c.pixelSize
|
- pixelSize: RenderVM.shared!.pixelSize = c.pixelSize
|
||||||
|
- willPrepareGLContext: renderStartTimer(RenderVM.shared!)
|
||||||
shoulds:
|
shoulds:
|
||||||
didLaunch:
|
didLaunch:
|
||||||
1. Only once during the first setup:
|
1. Only once during the first setup:
|
||||||
|
|||||||
@@ -128,6 +128,7 @@ public struct F {
|
|||||||
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 willPrepareGLContext = "willPrepareGLContext"
|
||||||
|
|
||||||
}
|
}
|
||||||
public struct RenderContext: DialectContext {
|
public struct RenderContext: DialectContext {
|
||||||
@@ -137,6 +138,7 @@ public struct RenderContext: DialectContext {
|
|||||||
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 willPrepareGLContext = Bool()
|
||||||
|
|
||||||
public var recentField = ""
|
public var recentField = ""
|
||||||
|
|
||||||
@@ -159,6 +161,9 @@ public struct RenderContext: DialectContext {
|
|||||||
else if (name == "reshapePixelSize") {
|
else if (name == "reshapePixelSize") {
|
||||||
return reshapePixelSize as! T
|
return reshapePixelSize as! T
|
||||||
}
|
}
|
||||||
|
else if (name == "willPrepareGLContext") {
|
||||||
|
return willPrepareGLContext as! T
|
||||||
|
}
|
||||||
|
|
||||||
return "unknown-field-name" as! T
|
return "unknown-field-name" as! T
|
||||||
}
|
}
|
||||||
@@ -185,6 +190,9 @@ public struct RenderContext: DialectContext {
|
|||||||
else if (name == "reshapePixelSize") {
|
else if (name == "reshapePixelSize") {
|
||||||
reshapePixelSize = value as! [Int32]
|
reshapePixelSize = value as! [Int32]
|
||||||
}
|
}
|
||||||
|
else if (name == "willPrepareGLContext") {
|
||||||
|
willPrepareGLContext = value as! Bool
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -404,6 +412,7 @@ func renderRegisterEffects(_ ctrl: DialectController) {
|
|||||||
let _: RenderContext? = registerOneliners(ctrl, [
|
let _: RenderContext? = registerOneliners(ctrl, [
|
||||||
F.isReadyToRender, { (c: RenderContext) in print("ИГР isRTR: '\(c.isReadyToRender)'") },
|
F.isReadyToRender, { (c: RenderContext) in print("ИГР isRTR: '\(c.isReadyToRender)'") },
|
||||||
F.pixelSize, { (c: RenderContext) in RenderVM.shared!.pixelSize = c.pixelSize },
|
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