Add engine manager class to handle non window stuff.
This commit is contained in:
parent
5c9ef20e6b
commit
9b1864191d
3 changed files with 132 additions and 5 deletions
112
src/main/java/com/okseby/core/EngineManager.java
Normal file
112
src/main/java/com/okseby/core/EngineManager.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,17 +1,27 @@
|
||||||
package com.okseby.core;
|
package com.okseby.core;
|
||||||
|
|
||||||
|
import com.okseby.core.utils.Constants;
|
||||||
import org.lwjgl.Version;
|
import org.lwjgl.Version;
|
||||||
|
|
||||||
public class Launcher {
|
public class Launcher {
|
||||||
|
|
||||||
|
private static WindowManager window;
|
||||||
|
private static EngineManager engine;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("LWJGL Version: " + Version.getVersion());
|
System.out.println("LWJGL Version: " + Version.getVersion());
|
||||||
|
|
||||||
WindowManager window = new WindowManager("Warabi Engine", 1600, 900, false);
|
window = new WindowManager(Constants.title, 1600, 900, false);
|
||||||
window.init();
|
engine = new EngineManager();
|
||||||
|
|
||||||
while (!window.windowShouldClose())
|
try {
|
||||||
window.update();
|
engine.start();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
window.cleanup();
|
public static WindowManager getWindow() {
|
||||||
|
return window;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
5
src/main/java/com/okseby/core/utils/Constants.java
Normal file
5
src/main/java/com/okseby/core/utils/Constants.java
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package com.okseby.core.utils;
|
||||||
|
|
||||||
|
public class Constants {
|
||||||
|
public static final String title = "Warabi Engine";
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue