@@ -22,31 +22,40 @@ import com.lambda.event.listener.SafeListener.Companion.listen
2222import com.lambda.gui.dsl.ImGuiBuilder
2323import com.lambda.module.HudModule
2424import com.lambda.module.tag.ModuleTag
25+ import com.lambda.util.collections.LimitedDecayQueue
26+ import kotlin.time.Duration.Companion.seconds
2527
2628object FPS : HudModule(
2729 name = " FPS" ,
2830 description = " Displays your games frames per second" ,
2931 tag = ModuleTag .HUD
3032) {
33+ val average by setting(" Average" , true )
3134 val updateDelay by setting(" Update Delay" , 50 , 0 .. 1000 , 1 , " Time between updating the fps value" )
3235
36+ val frames = LimitedDecayQueue <Unit >(Int .MAX_VALUE , 1 .seconds.inWholeMilliseconds)
3337 var lastUpdated = System .currentTimeMillis()
3438 var lastFrameTime = System .nanoTime()
3539 var fps = 0
3640
3741 init {
3842 listen<RenderEvent .Render > {
39- val currentTimeNano = System .nanoTime()
43+ var currentFps = 0
44+ if (average) {
45+ frames.add(Unit )
46+ currentFps = frames.size
47+ } else {
48+ val currentTimeNano = System .nanoTime()
49+ val elapsedNs = currentTimeNano - lastFrameTime
50+ currentFps = if (elapsedNs > 0 ) (1000000000 / elapsedNs).toInt() else 0
51+ lastFrameTime = currentTimeNano
52+ }
4053
4154 val currentTypeMilli = System .currentTimeMillis()
4255 if (currentTypeMilli - lastUpdated >= updateDelay) {
56+ fps = currentFps
4357 lastUpdated = currentTypeMilli
44- val elapsedNs = currentTimeNano - lastFrameTime
45- fps = if (elapsedNs > 0 ) (1000000000 / elapsedNs).toInt()
46- else 0
4758 }
48-
49- lastFrameTime = currentTimeNano
5059 }
5160 }
5261
0 commit comments