95 lines
2.5 KiB
Java
95 lines
2.5 KiB
Java
package org.xfe.engine;
|
|
|
|
import org.joml.*;
|
|
|
|
import java.lang.Math;
|
|
|
|
public class Engine
|
|
{
|
|
public static final int TARGET_UPS = 30;
|
|
private final GameLogic gameLogic;
|
|
private final Render render;
|
|
private final Window window;
|
|
private final World world;
|
|
private int target_fps;
|
|
private int target_ups;
|
|
private boolean running;
|
|
|
|
public static final float Z_FAR = 1000.0f;
|
|
public static final float Z_NEAR = 0.1f;
|
|
public static final float FOV = (float) Math.toRadians(90.0f);
|
|
public static final boolean WIREFRAME_MODE = false;
|
|
public Engine(String window_title, Window.WindowOptions opts, GameLogic gameLogic) {
|
|
window = new Window(window_title, opts, () -> {
|
|
resize();
|
|
return null;
|
|
});
|
|
target_fps = opts.fps;
|
|
target_ups = opts.ups;
|
|
|
|
this.gameLogic = gameLogic;
|
|
this.render = new Render(window);
|
|
this.world = new World();
|
|
|
|
gameLogic.init(window, world, render);
|
|
|
|
this.running = true;
|
|
}
|
|
|
|
private void cleanup() {
|
|
this.gameLogic.cleanup();
|
|
this.render.cleanup();
|
|
this.world.cleanup();
|
|
this.window.cleanup();
|
|
}
|
|
|
|
private void resize() {
|
|
|
|
}
|
|
|
|
private void run() {
|
|
long initialTime = System.currentTimeMillis();
|
|
float timeU = 1000.0f / target_ups;
|
|
float timeR = target_fps > 0 ? 1000.0f / target_fps : 0;
|
|
float deltaUpdate = 0;
|
|
float deltaFps = 0;
|
|
|
|
long updateTime = initialTime;
|
|
while (running && !window.windowShouldClose()) {
|
|
window.pollEvents();
|
|
|
|
long now = System.currentTimeMillis();
|
|
deltaUpdate += (now - initialTime) / timeU;
|
|
deltaFps += (now - initialTime) / timeR;
|
|
|
|
if (target_fps <= 0 || deltaFps >= 1) {
|
|
gameLogic.input(window, world, now - initialTime);
|
|
}
|
|
|
|
if (deltaUpdate >= 1) {
|
|
long diffTimeMillis = now - updateTime;
|
|
gameLogic.update(window, world, diffTimeMillis);
|
|
updateTime = now;
|
|
deltaUpdate--;
|
|
}
|
|
|
|
if (target_fps <= 0 || deltaFps >= 1) {
|
|
render.render(window, world);
|
|
deltaFps--;
|
|
window.update();
|
|
}
|
|
initialTime = now;
|
|
}
|
|
|
|
cleanup();
|
|
}
|
|
|
|
public void start() {
|
|
running = true;
|
|
run();
|
|
}
|
|
|
|
public void stop() {
|
|
running = false;
|
|
}
|
|
} |