diff options
author | Rudy Bousset <rbousset@z2r4p3.le-101.fr> | 2020-03-11 14:57:54 +0100 |
---|---|---|
committer | Rudy Bousset <rbousset@z2r4p3.le-101.fr> | 2020-03-11 14:57:54 +0100 |
commit | 650e83573987d004171f250e236b21b243477355 (patch) | |
tree | 50b4651c585574a802dd609b267a9ea07b1e78c0 /minilibx_beta/mlx_init.swift | |
parent | why macos (diff) | |
download | 42-cub3d-650e83573987d004171f250e236b21b243477355.tar.gz 42-cub3d-650e83573987d004171f250e236b21b243477355.tar.bz2 42-cub3d-650e83573987d004171f250e236b21b243477355.tar.xz 42-cub3d-650e83573987d004171f250e236b21b243477355.tar.zst 42-cub3d-650e83573987d004171f250e236b21b243477355.zip |
is not working
Diffstat (limited to 'minilibx_beta/mlx_init.swift')
-rw-r--r-- | minilibx_beta/mlx_init.swift | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/minilibx_beta/mlx_init.swift b/minilibx_beta/mlx_init.swift new file mode 100644 index 0000000..de6edf2 --- /dev/null +++ b/minilibx_beta/mlx_init.swift @@ -0,0 +1,100 @@ + +import Cocoa +import Metal +import mlx_window +import mlx_image + + +func _mlx_bridge<T : AnyObject>(obj : T) -> UnsafeMutableRawPointer? { + return UnsafeMutableRawPointer(Unmanaged.passUnretained(obj).toOpaque()) +} + +func _mlx_bridge<T : AnyObject>(ptr : UnsafeRawPointer) -> T { + return Unmanaged<T>.fromOpaque(ptr).takeUnretainedValue() +} + + + +public class MlxMain { + + public var winList = [MlxWin]() + public var imgList = [MlxImg]() + var myMlxApp:NSApplication? + public var device:MTLDevice! + var loopHook:UnsafeMutableRawPointer? + var loopParam:UnsafeMutableRawPointer + var loopHookTimer:CFRunLoopTimer? + public var inLoop = false + + public init(_ flag:Int = 0) + { + /// make app with top menubar + myMlxApp = NSApplication.shared + if (flag == 1) + { + NSApp.setActivationPolicy(NSApplication.ActivationPolicy.prohibited) /// for non clickable win, no top menu + } + else + { + NSApp.setActivationPolicy(NSApplication.ActivationPolicy.regular) + } + + device = MTLCreateSystemDefaultDevice()! + loopParam = UnsafeMutableRawPointer(&inLoop) /// dummy addr init + + /// Add observer anyway to flush pixels every loop. If loop_hook exists, call it. + var ocontext = CFRunLoopObserverContext(version:0, info:_mlx_bridge(obj:self), retain:nil, release:nil, copyDescription:nil) + let observer = CFRunLoopObserverCreate(kCFAllocatorDefault, CFRunLoopActivity.beforeWaiting.rawValue, true, 0, createOCallback(), &ocontext) + CFRunLoopAddObserver(CFRunLoopGetMain(), observer, CFRunLoopMode.commonModes) + + } + + public func addWinToList(_ win:MlxWin) + { winList.append(win) } + public func addImgToList(_ img:MlxImg) + { imgList.append(img) } + + + func doCallLoopHook() + { +/// if (loopHook != nil) +/// { + _ = (unsafeBitCast(loopHook!,to:(@convention(c)(UnsafeRawPointer)->Void).self))(loopParam) +/// } + } + + func createOCallback() -> CFRunLoopObserverCallBack + { + return { (cfRunloopObserver, cfrunloopactivity, info) -> Void in + let mlx:MlxMain = _mlx_bridge(ptr:info!) + mlx.winList.forEach { $0.flushImages() } +/// mlx.doCallLoopHook() + } + } + + func createTCallback() -> CFRunLoopTimerCallBack + { + return { (cfRunloopTimer, info) -> Void in + let mlx:MlxMain = _mlx_bridge(ptr:info!) + mlx.doCallLoopHook() + } + } + + public func addLoopHook(_ f:UnsafeMutableRawPointer?, _ p:UnsafeMutableRawPointer) + { + var tcontext = CFRunLoopTimerContext(version:0, info:_mlx_bridge(obj:self), retain:nil, release:nil, copyDescription:nil) + if (loopHook != nil) + { + CFRunLoopTimerInvalidate(loopHookTimer) + } + + loopHook = f + loopParam = p + if (loopHook != nil) + { + loopHookTimer = CFRunLoopTimerCreate(kCFAllocatorDefault, 0.0, 0.0001, 0, 0, createTCallback(), &tcontext) + CFRunLoopAddTimer(CFRunLoopGetMain(), loopHookTimer, CFRunLoopMode.commonModes) + } + } + +} |