diff --git a/components/render/mac/view/RenderGLView.swift b/components/render/mac/view/RenderGLView.swift index 3100500..0f9de48 100644 --- a/components/render/mac/view/RenderGLView.swift +++ b/components/render/mac/view/RenderGLView.swift @@ -11,20 +11,11 @@ final class RenderGLView: NSOpenGLView { private var startTime: CFTimeInterval = 0 override init(frame frameRect: NSRect) { - let attributes: [NSOpenGLPixelFormatAttribute] = [ - UInt32(NSOpenGLPFAOpenGLProfile), UInt32(NSOpenGLProfileVersion3_2Core), - 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 3.2 core pixel format") + 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 } diff --git a/components/render/render.swift b/components/render/render.swift new file mode 100644 index 0000000..a676446 --- /dev/null +++ b/components/render/render.swift @@ -0,0 +1,14 @@ +class RenderComponent { + let ctrl = DialectController(RenderContext()) + static private(set) weak var singleton: RenderComponent? + + init() { + Self.singleton = self + renderRegisterEffects(ctrl) + renderRegisterShoulds(ctrl) + } + + func setup() { + renderSet(F.didSetup, true) + } +} diff --git a/components/render/renderConst.swift b/components/render/renderConst.swift new file mode 100644 index 0000000..688e4f5 --- /dev/null +++ b/components/render/renderConst.swift @@ -0,0 +1,12 @@ +import AppKit + +let RENDER_OPENGL_ATTRS: [NSOpenGLPixelFormatAttribute] = [ + UInt32(NSOpenGLPFAOpenGLProfile), UInt32(NSOpenGLProfileVersion3_2Core), + UInt32(NSOpenGLPFAColorSize), 24, + UInt32(NSOpenGLPFAAlphaSize), 8, + UInt32(NSOpenGLPFADepthSize), 24, + UInt32(NSOpenGLPFADoubleBuffer), + UInt32(NSOpenGLPFAAccelerated), + 0 +] + diff --git a/components/render/renderVM.swift b/components/render/renderVM.swift new file mode 100644 index 0000000..4fdef75 --- /dev/null +++ b/components/render/renderVM.swift @@ -0,0 +1,12 @@ +import Combine +import Foundation + +final class RenderVM: ObservableObject { + var timer: Timer? + + static private(set) weak var shared: RenderVM? + + init() { + Self.shared = self + } +} diff --git a/dialect.yml b/dialect.yml index fbf003b..8bc568f 100644 --- a/dialect.yml +++ b/dialect.yml @@ -4,10 +4,31 @@ output: game/ver-mac/src/dialect.swift: type: swift +RenderContext: + type: context + fields: + didLaunch: Bool + didSetup: Bool + isReadyToRender: Bool + pixelHeight: Int + pixelWidth: Int + +RenderComponent: + type: component + oneliners: + - isReadyToRender: print("ИГР isRTR: '\(c.isReadyToRender)'") + shoulds: + didLaunch: + 1. Only once during the first setup: + if: + c.recentField == F.didSetup && + c.didLaunch == false + then: + c.didLaunch = true + RootComponent: type: component oneliners: - - countText: print(c.countText) - countText: RootVM.shared!.countText = c.countText shoulds: count: diff --git a/game/ver-mac/src/AppDelegate.swift b/game/ver-mac/src/AppDelegate.swift index 9d214e6..502b8cf 100644 --- a/game/ver-mac/src/AppDelegate.swift +++ b/game/ver-mac/src/AppDelegate.swift @@ -4,6 +4,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate { + let render = RenderComponent() + let renderVM = RenderVM() let root = RootComponent() let rootVM = RootVM() @@ -14,6 +16,8 @@ final class AppDelegate: otherSetupConsoleLogging(root.ctrl, "Root") root.setup() + otherSetupConsoleLogging(render.ctrl, "Render") + render.setup() } func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { diff --git a/game/ver-mac/src/dialect.swift b/game/ver-mac/src/dialect.swift index 4b69471..a3316be 100644 --- a/game/ver-mac/src/dialect.swift +++ b/game/ver-mac/src/dialect.swift @@ -124,8 +124,63 @@ public struct F { public static let didClickIncrement = "didClickIncrement" public static let didLaunch = "didLaunch" public static let didSetup = "didSetup" + public static let isReadyToRender = "isReadyToRender" + public static let pixelHeight = "pixelHeight" + public static let pixelWidth = "pixelWidth" } +public struct RenderContext: DialectContext { + public var didLaunch = Bool() + public var didSetup = Bool() + public var isReadyToRender = Bool() + public var pixelHeight = Int() + public var pixelWidth = Int() + + public var recentField = "" + + public func field(_ name: String) -> T { + if (name == "didLaunch") { + return didLaunch as! T + } + else if (name == "didSetup") { + return didSetup as! T + } + else if (name == "isReadyToRender") { + return isReadyToRender as! T + } + else if (name == "pixelHeight") { + return pixelHeight as! T + } + else if (name == "pixelWidth") { + return pixelWidth as! T + } + + return "unknown-field-name" as! T + } + + public mutating func setField( + _ name: String, + _ value: Any + ) { + if (name == "didLaunch") { + didLaunch = value as! Bool + } + else if (name == "didSetup") { + didSetup = value as! Bool + } + else if (name == "isReadyToRender") { + isReadyToRender = value as! Bool + } + else if (name == "pixelHeight") { + pixelHeight = value as! Int + } + else if (name == "pixelWidth") { + pixelWidth = value as! Int + } + + } +} + public struct RootContext: DialectContext { public var count = Int() public var countText = String() @@ -178,6 +233,13 @@ public struct RootContext: DialectContext { } } +public func renderSet( + _ key: String, + _ value: Any +) { + RenderComponent.singleton!.ctrl.set(key, value) +} + public func rootSet( _ key: String, _ value: Any @@ -185,6 +247,24 @@ public func rootSet( RootComponent.singleton!.ctrl.set(key, value) } +func renderShouldResetDidLaunch(_ c: RenderContext) -> RenderContext { + var c = c + + /* 1. Only once during the first setup */ + if + c.recentField == F.didSetup && + c.didLaunch == false + { + c.didLaunch = true + c.recentField = F.didLaunch + return c + } + + + c.recentField = DIALECT_CONTEXT_RECENT_FIELD_NONE + return c +} + func rootShouldResetCount(_ c: RootContext) -> RootContext { var c = c @@ -256,6 +336,15 @@ func rootShouldResetDidLaunch(_ c: RootContext) -> RootContext { return c } +func renderRegisterShoulds(_ ctrl: DialectController) { + [ + renderShouldResetDidLaunch, + + ].forEach { f in + ctrl.registerFunction { c in f(c as! RenderContext) } + } +} + func rootRegisterShoulds(_ ctrl: DialectController) { [ rootShouldResetCount, @@ -267,9 +356,15 @@ func rootRegisterShoulds(_ ctrl: DialectController) { } } +func renderRegisterEffects(_ ctrl: DialectController) { + let _: RenderContext? = registerOneliners(ctrl, [ + F.isReadyToRender, { (c: RenderContext) in print("ИГР isRTR: '\(c.isReadyToRender)'") }, + + ]) +} + func rootRegisterEffects(_ ctrl: DialectController) { let _: RootContext? = registerOneliners(ctrl, [ - F.countText, { (c: RootContext) in print(c.countText) }, F.countText, { (c: RootContext) in RootVM.shared!.countText = c.countText }, ]) diff --git a/game/ver-mac/src/render/render.swift b/game/ver-mac/src/render/render.swift new file mode 120000 index 0000000..5e4b117 --- /dev/null +++ b/game/ver-mac/src/render/render.swift @@ -0,0 +1 @@ +../../../../components/render/render.swift \ No newline at end of file diff --git a/game/ver-mac/src/render/renderConst.swift b/game/ver-mac/src/render/renderConst.swift new file mode 120000 index 0000000..d953714 --- /dev/null +++ b/game/ver-mac/src/render/renderConst.swift @@ -0,0 +1 @@ +../../../../components/render/renderConst.swift \ No newline at end of file diff --git a/game/ver-mac/src/render/renderVM.swift b/game/ver-mac/src/render/renderVM.swift new file mode 120000 index 0000000..dde0ace --- /dev/null +++ b/game/ver-mac/src/render/renderVM.swift @@ -0,0 +1 @@ +../../../../components/render/renderVM.swift \ No newline at end of file