1+ /*
2+ * Copyright 2025 Lambda
3+ *
4+ * This program is free software: you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation, either version 3 of the License, or
7+ * (at your option) any later version.
8+ *
9+ * This program is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public License
15+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16+ */
17+
18+ package com.lambda.module.hud
19+
20+ import com.lambda.event.events.RenderEvent
21+ import com.lambda.event.listener.SafeListener.Companion.listen
22+ import com.lambda.gui.dsl.ImGuiBuilder
23+ import com.lambda.module.HudModule
24+ import com.lambda.module.tag.ModuleTag
25+
26+ object Fps : HudModule(
27+ name = " Fps" ,
28+ description = " Displays your games frames per second" ,
29+ tag = ModuleTag .HUD
30+ ) {
31+ val updateDelay by setting(" Update Delay" , 50 , 0 .. 1000 , 1 ," Time between updating the fps value" )
32+
33+ var lastUpdated = System .currentTimeMillis()
34+ var lastFrameTime = System .nanoTime()
35+ var fps = 0
36+
37+ init {
38+ listen<RenderEvent .Render > {
39+ val currentTimeNano = System .nanoTime()
40+
41+ val currentTypeMilli = System .currentTimeMillis()
42+ if (currentTypeMilli - lastUpdated >= updateDelay) {
43+ lastUpdated = currentTypeMilli
44+ val elapsedNs = currentTimeNano - lastFrameTime
45+ fps = if (elapsedNs > 0 ) (1000000000 / elapsedNs).toInt()
46+ else 0
47+ }
48+
49+ lastFrameTime = currentTimeNano
50+ }
51+ }
52+
53+ override fun ImGuiBuilder.buildLayout () {
54+ text(" Fps: $fps " )
55+ }
56+ }
0 commit comments