add fps counter

This commit is contained in:
a dinosaur 2024-08-05 16:44:32 +10:00
parent b08fc1c51b
commit 1fe9578b3a
3 changed files with 27 additions and 1 deletions

View File

@ -9,6 +9,7 @@ public class Application {
private var view: SDL_MetalView? = nil
private var renderer: Renderer? = nil
private var lastCounter: UInt64 = 0
private var fpsCalculator = FPSCalculator()
private var stderr = FileHandle.standardError
@ -40,6 +41,7 @@ public class Application {
view = SDL_Metal_CreateView(window)
do {
let layer = unsafeBitCast(SDL_Metal_GetLayer(view), to: CAMetalLayer.self)
layer.displaySyncEnabled = cfg.vsyncMode == .off ? false : true
self.renderer = try Renderer(layer: layer)
} catch RendererError.initFailure(let message) {
print("Renderer init error: \(message)", to: &stderr)
@ -92,6 +94,10 @@ public class Application {
}
private func update(_ deltaTime: Double) -> ApplicationExecutionState {
fpsCalculator.frame(deltaTime: deltaTime) { fps in
print("FPS: \(fps)", to: &stderr)
}
do {
try renderer!.paint()
} catch RendererError.drawFailure(let message) {
@ -99,6 +105,7 @@ public class Application {
} catch {
print("Renderer draw error: unexpected error", to: &stderr)
}
return .running
}
@ -140,7 +147,7 @@ public struct ApplicationConfiguration {
static let highDPI = Flags(rawValue: 1 << 1)
}
public enum VSyncMode {
public enum VSyncMode: Equatable {
case off
case on(interval: UInt)
case adaptive

View File

@ -1,6 +1,7 @@
add_executable(Voxelotl MACOSX_BUNDLE
Assets.xcassets
Renderer.swift
FPSCalculator.swift
Application.swift
main.swift)

View File

@ -0,0 +1,18 @@
import Foundation
public struct FPSCalculator {
private var _accumulator = 0.0
private var _framesCount = 0
public mutating func frame(deltaTime: Double, result: (_ fps: Int) -> Void) {
_framesCount += 1
_accumulator += deltaTime
if (_accumulator >= 1.0) {
result(_framesCount)
_framesCount = 0
_accumulator = fmod(_accumulator, 1.0)
}
}
}