voxelotl-engine/Sources/Voxelotl/Common/FPSCalculator.swift

21 lines
526 B
Swift
Raw Normal View History

2024-08-05 06:44:32 +00:00
import Foundation
public struct FPSCalculator {
2024-08-12 22:38:21 +00:00
private var _accumulator = Duration.zero
2024-08-05 06:44:32 +00:00
private var _framesCount = 0
2024-08-12 22:38:21 +00:00
public mutating func frame(deltaTime: Duration, result: (_ fps: Int) -> Void) {
self._framesCount += 1
self._accumulator += deltaTime
2024-08-05 06:44:32 +00:00
2024-08-12 22:38:21 +00:00
if self._accumulator >= Duration.seconds(1) {
result(self._framesCount)
2024-08-05 06:44:32 +00:00
2024-08-12 22:38:21 +00:00
self._framesCount = 0
self._accumulator = .init(
secondsComponent: 0,
attosecondsComponent: self._accumulator.components.attoseconds)
2024-08-05 06:44:32 +00:00
}
}
}