Add engine manager class to handle non window stuff.

This commit is contained in:
Sebastian Cabrera 2023-05-26 17:40:59 -04:00
parent 5c9ef20e6b
commit 9b1864191d
3 changed files with 132 additions and 5 deletions

View file

@ -0,0 +1,112 @@
package com.okseby.core;
import com.okseby.core.utils.Constants;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
public class EngineManager {
public static final long NANOSECOND = 1000000000;
public static final float FRAMERATE = 1000;
private static int fps;
private static float frametime = 1.0f / FRAMERATE;
private boolean isRunning;
private WindowManager window;
private GLFWErrorCallback errorCallback;
private void init() throws Exception {
GLFW.glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
window = Launcher.getWindow();
window.init();
}
public void start() throws Exception {
init();
if (isRunning)
return;
run();
}
public void run() {
this.isRunning = true;
int frames = 0;
long frameCounter = 0;
long lastTime = System.nanoTime();
double unprocessedTime = 0;
while (isRunning) {
boolean render = false;
long startTime = System.nanoTime();
long passedTime = startTime - lastTime;
lastTime = startTime;
unprocessedTime += passedTime / (double) NANOSECOND;
frameCounter += passedTime;
input();
while (unprocessedTime > frametime) {
render = true;
unprocessedTime -= frametime;
if (window.windowShouldClose())
stop();
if (frameCounter >= NANOSECOND) {
setFps(frames);
window.setTitle(Constants.title + " - FPS: " + getFps());
frames = 0;
frameCounter = 0;
}
}
if (render) {
update();
render();
frames++;
}
}
cleanup();
}
private void stop() {
if (!isRunning)
return;
isRunning = false;
}
private void input() {
}
private void render() {
window.update();
}
private void update() {
}
private void cleanup() {
window.cleanup();
errorCallback.free();
GLFW.glfwTerminate();
}
public static int getFps() {
return fps;
}
public static void setFps(int fps) {
EngineManager.fps = fps;
}
}

View file

@ -1,17 +1,27 @@
package com.okseby.core;
import com.okseby.core.utils.Constants;
import org.lwjgl.Version;
public class Launcher {
private static WindowManager window;
private static EngineManager engine;
public static void main(String[] args) {
System.out.println("LWJGL Version: " + Version.getVersion());
WindowManager window = new WindowManager("Warabi Engine", 1600, 900, false);
window.init();
window = new WindowManager(Constants.title, 1600, 900, false);
engine = new EngineManager();
while (!window.windowShouldClose())
window.update();
window.cleanup();
try {
engine.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public static WindowManager getWindow() {
return window;
}
}

View file

@ -0,0 +1,5 @@
package com.okseby.core.utils;
public class Constants {
public static final String title = "Warabi Engine";
}