Skip to content

Commit 63346a4

Browse files
committed
Add reset method to Timer class to reset all time and frame tracking
values
1 parent 1ac7fd7 commit 63346a4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/main/java/engine/Timer.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,34 @@ public void update() {
105105
updateFPS();
106106
}
107107

108+
/**
109+
* Resets the {@code Timer} to its initial state, clearing all accumulated
110+
* time and frame count values. This includes resetting the following:
111+
* <ul>
112+
* <li>The start time to the current system time.</li>
113+
* <li>The last time recorded to the current system time.</li>
114+
* <li>The total elapsed time to zero.</li>
115+
* <li>The frame count to zero.</li>
116+
* <li>The frames per second (FPS) to zero.</li>
117+
* <li>The millisecond counter to zero.</li>
118+
* <li>The last frame count for FPS calculation to zero.</li>
119+
* </ul>
120+
* <p>
121+
* This method can be used when you need to restart the timer, such as for
122+
* restarting the game / application or resetting the simulation state.
123+
* </p>
124+
*/
125+
public void reset() {
126+
this.startTime = System.nanoTime();
127+
this.lastTime = startTime;
128+
this.time = 0;
129+
this.totalTime = 0;
130+
this.frameCount = 0;
131+
this.fps = 0;
132+
this.millisecondCounter = 0;
133+
this.lastFrameCount = 0;
134+
}
135+
108136
/**
109137
* Returns the total elapsed time in seconds, scaled by the current time
110138
* scale.

0 commit comments

Comments
 (0)