Created
November 15, 2023 13:49
-
-
Save elkraneo/7732c74b0fca1fdcbeab646ab238e546 to your computer and use it in GitHub Desktop.
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
let sceneUpdateSubscription = content.subscribe(to: SceneEvents.Update.self) { event in | |
let deltaTime = event.deltaTime | |
// Accumulate delta time | |
self.accumulatedTime += deltaTime | |
self.frameCount += 1 | |
// Calculate FPS every n frames | |
if self.frameCount >= 10 { // Calculate FPS every 10 frames (1 second at 10 FPS) | |
if self.accumulatedTime > 0 { // Ensure accumulated time is not zero | |
let fps = Double(self.frameCount) / self.accumulatedTime | |
print("FPS: \(fps)") | |
Task { | |
await viewStore.send(.didUpdate(fps)).finish() | |
} | |
} else { | |
print("FPS: Infinity") // Handle the case when accumulated time is zero | |
} | |
self.frameCount = 0 | |
self.accumulatedTime = 0 | |
} | |
} | |
subscriptions.append(sceneUpdateSubscription) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment