massive refactor

This commit is contained in:
JISAUAY committed 2024-09-24 17:00:18 -05:00
1 parent 26c0f433ae
commit e71a1a9b0b
14 files changed
+442 -275

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
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 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();
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;
}
}