-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
158 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import AppKit | ||
|
||
class DisplayLink { | ||
let timer: CVDisplayLink | ||
let source: DispatchSourceUserDataAdd | ||
|
||
var callback : Optional<() -> ()> = nil | ||
|
||
var running : Bool { CVDisplayLinkIsRunning(timer) } | ||
|
||
init?(onQueue queue: DispatchQueue = DispatchQueue.main) { | ||
source = DispatchSource.makeUserDataAddSource(queue: queue) | ||
var timerRef : CVDisplayLink? = nil | ||
var successLink = CVDisplayLinkCreateWithActiveCGDisplays(&timerRef) | ||
|
||
if let timer = timerRef { | ||
successLink = CVDisplayLinkSetOutputCallback(timer, { | ||
(timer : CVDisplayLink, currentTime : UnsafePointer<CVTimeStamp>, outputTime : UnsafePointer<CVTimeStamp>, _ : CVOptionFlags, _ : UnsafeMutablePointer<CVOptionFlags>, sourceUnsafeRaw : UnsafeMutableRawPointer?) -> CVReturn in | ||
|
||
if let sourceUnsafeRaw = sourceUnsafeRaw { | ||
let sourceUnmanaged = Unmanaged<DispatchSourceUserDataAdd>.fromOpaque(sourceUnsafeRaw) | ||
sourceUnmanaged.takeUnretainedValue().add(data: 1) | ||
} | ||
|
||
return kCVReturnSuccess | ||
|
||
}, Unmanaged.passUnretained(source).toOpaque()) | ||
|
||
guard successLink == kCVReturnSuccess else { | ||
NSLog("Failed to create timer with active display") | ||
return nil | ||
} | ||
|
||
successLink = CVDisplayLinkSetCurrentCGDisplay(timer, CGMainDisplayID()) | ||
|
||
guard successLink == kCVReturnSuccess else { | ||
NSLog("Failed to connect to display") | ||
return nil | ||
} | ||
|
||
self.timer = timer | ||
} else { | ||
NSLog("Failed to create timer with active display") | ||
return nil | ||
} | ||
|
||
source.setEventHandler { | ||
[weak self] in self?.callback?() | ||
} | ||
} | ||
|
||
func start() { | ||
guard !running else { return } | ||
|
||
CVDisplayLinkStart(timer) | ||
source.resume() | ||
} | ||
|
||
func cancel() { | ||
guard running else { return } | ||
|
||
CVDisplayLinkStop(timer) | ||
source.cancel() | ||
} | ||
|
||
deinit { | ||
if running { | ||
cancel() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import Cocoa | ||
|
||
struct Settings { | ||
static let screenSize = NSScreen.main?.frame.size ?? .zero | ||
private static let screenSize = NSScreen.main?.frame.size ?? .zero | ||
static let snowflakeSizeRange: ClosedRange<CGFloat> = 2...10 | ||
static let maxSnowflakes = Int((screenSize.height * screenSize.width) * 0.0001) | ||
static let snowflakeSpeedRange: ClosedRange<CGFloat> = 1...5 | ||
static let windStrength: CGFloat = 0.5 | ||
static let maxSnowflakes = 1000 | ||
static let snowflakeSpeedRange: ClosedRange<CGFloat> = 1...3 | ||
static let windStrength: CGFloat = 0.3 | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <metal_stdlib> | ||
using namespace metal; | ||
|
||
struct Snowflake { | ||
float2 position; // Позиция снежинки | ||
float size; // Размер снежинки | ||
}; | ||
|
||
kernel void snowflakeVertexShader(const device Snowflake* snowflakes [[ buffer(0) ]], | ||
uint id [[ thread_position_in_grid ]], | ||
device float4* outPosition [[ buffer(1) ]]) { | ||
if (id < 100) { // Измените 100 на количество снежинок | ||
float2 pos = snowflakes[id].position; | ||
outPosition[id] = float4(pos.x, pos.y, 0.0, 1.0); | ||
} | ||
} | ||
|
||
fragment float4 snowflakeFragmentShader(float4 inPosition [[ position ]]) { | ||
return float4(1.0, 1.0, 1.0, 1.0); // Белый цвет снежинки | ||
} |