mirror of
https://github.com/GayPizzaSpecifications/voxelotl-engine.git
synced 2025-08-02 13:00:53 +00:00
add fps counter
This commit is contained in:
parent
b08fc1c51b
commit
1fe9578b3a
@ -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
|
||||
|
@ -1,6 +1,7 @@
|
||||
add_executable(Voxelotl MACOSX_BUNDLE
|
||||
Assets.xcassets
|
||||
Renderer.swift
|
||||
FPSCalculator.swift
|
||||
Application.swift
|
||||
main.swift)
|
||||
|
||||
|
18
Sources/Voxelotl/FPSCalculator.swift
Normal file
18
Sources/Voxelotl/FPSCalculator.swift
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user