Added Time class + tests
This commit is contained in:
@@ -5,6 +5,7 @@ import static org.lwjgl.glfw.GLFW.*;
|
||||
import static org.lwjgl.opengl.GL11.*;
|
||||
import static org.lwjgl.system.MemoryUtil.*;
|
||||
|
||||
import org.hirw.game.util.Time;
|
||||
import org.lwjgl.Version;
|
||||
import org.lwjgl.glfw.*;
|
||||
import org.lwjgl.opengl.*;
|
||||
@@ -82,11 +83,8 @@ public class Window {
|
||||
|
||||
private void loop() {
|
||||
while (!glfwWindowShouldClose(glfwWindow)) {
|
||||
|
||||
if (Mouse.isPressed(Mouse.Buttons.LEFT)) {
|
||||
System.out.println("LEFT CLICKED");
|
||||
}
|
||||
|
||||
Time.update();
|
||||
System.out.println(Time.deltaTime());
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glfwSwapBuffers(glfwWindow);
|
||||
glfwPollEvents();
|
||||
|
||||
31
src/main/java/org/hirw/game/util/Time.java
Normal file
31
src/main/java/org/hirw/game/util/Time.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package org.hirw.game.util;
|
||||
|
||||
public final class Time {
|
||||
private static long lastTime = NanoTimer.system().nanoTime();
|
||||
private static long currentTime = NanoTimer.system().nanoTime();
|
||||
private static NanoTimer nanoTimer = NanoTimer.system();
|
||||
|
||||
public interface NanoTimer {
|
||||
long nanoTime();
|
||||
|
||||
static NanoTimer system() {
|
||||
return System::nanoTime;
|
||||
}
|
||||
}
|
||||
|
||||
static void setNanoTimer(NanoTimer timer) {
|
||||
nanoTimer = timer;
|
||||
lastTime = nanoTimer.nanoTime();
|
||||
currentTime = nanoTimer.nanoTime();
|
||||
}
|
||||
|
||||
public static void update() {
|
||||
lastTime = currentTime;
|
||||
currentTime = nanoTimer.nanoTime();
|
||||
}
|
||||
|
||||
public static float deltaTime() {
|
||||
final float ONE_SECOND = 1_000_000_000f;
|
||||
return (currentTime - lastTime) / ONE_SECOND;
|
||||
}
|
||||
}
|
||||
64
src/test/java/org/hirw/game/util/TimeTest.java
Normal file
64
src/test/java/org/hirw/game/util/TimeTest.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package org.hirw.game.util;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
class TimeTest {
|
||||
final float ONE_SECOND = 1_000_000_000f;
|
||||
final long ONE_MILLISECOND = 1_000_000L;
|
||||
final float MOE = 0.00000001f; // Margin of error
|
||||
|
||||
private Time.NanoTimer nanoTimer;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
nanoTimer = Mockito.mock(Time.NanoTimer.class);
|
||||
when(nanoTimer.nanoTime()).thenReturn(0L);
|
||||
Time.setNanoTimer(nanoTimer);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeltaTimeInitialState() {
|
||||
float deltaTime = Time.deltaTime();
|
||||
assertEquals(deltaTime, 0f, MOE, "should be 0");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeltaTimeAfterUpdate() {
|
||||
when(nanoTimer.nanoTime()).thenReturn(ONE_MILLISECOND);
|
||||
|
||||
Time.update();
|
||||
float deltaTime = Time.deltaTime();
|
||||
|
||||
assertEquals(deltaTime, ONE_MILLISECOND / ONE_SECOND, MOE, "should reflect time difference");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMultipleUpdates() {
|
||||
when(nanoTimer.nanoTime()).thenReturn(ONE_MILLISECOND).thenReturn(ONE_MILLISECOND * 4);
|
||||
|
||||
Time.update();
|
||||
float delta1 = Time.deltaTime();
|
||||
|
||||
Time.update();
|
||||
float delta2 = Time.deltaTime();
|
||||
|
||||
assertEquals(delta1, ONE_MILLISECOND / ONE_SECOND, MOE, "should be 1/1000th second");
|
||||
assertEquals(delta2, (ONE_MILLISECOND * 3) / ONE_SECOND, MOE, "should be 3/1000th second");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testConsistentStateAcrossCalls() {
|
||||
when(nanoTimer.nanoTime()).thenReturn(ONE_MILLISECOND);
|
||||
|
||||
Time.update();
|
||||
float delta1 = Time.deltaTime();
|
||||
float delta2 = Time.deltaTime();
|
||||
|
||||
assertEquals(delta1, delta2, MOE, "should be equal delta time");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user