Implement RenderManager & Fix escape key callback.

This commit is contained in:
Sebastian Cabrera 2023-05-26 22:36:34 -04:00
parent 9b1864191d
commit 71284a9dae
6 changed files with 120 additions and 5 deletions

View file

@ -15,11 +15,15 @@ public class EngineManager {
private WindowManager window; private WindowManager window;
private GLFWErrorCallback errorCallback; private GLFWErrorCallback errorCallback;
private ILogic gameLogic;
private void init() throws Exception { private void init() throws Exception {
GLFW.glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err)); GLFW.glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
window = Launcher.getWindow(); window = Launcher.getWindow();
gameLogic = Launcher.getGame();
window.init(); window.init();
gameLogic.init();
} }
public void start() throws Exception { public void start() throws Exception {
@ -84,19 +88,22 @@ public class EngineManager {
} }
private void input() { private void input() {
gameLogic.input();
} }
private void render() { private void render() {
gameLogic.render();
window.update(); window.update();
} }
private void update() { private void update() {
gameLogic.update();
} }
private void cleanup() { private void cleanup() {
window.cleanup(); window.cleanup();
gameLogic.cleanup();
errorCallback.free(); errorCallback.free();
GLFW.glfwTerminate(); GLFW.glfwTerminate();

View file

@ -0,0 +1,13 @@
package com.okseby.core;
public interface ILogic {
void init() throws Exception;
void input();
void update();
void render();
void cleanup();
}

View file

@ -1,18 +1,21 @@
package com.okseby.core; package com.okseby.core;
import com.okseby.core.test.TestGame;
import com.okseby.core.utils.Constants; 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 WindowManager window;
private static EngineManager engine; private static TestGame game;
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());
window = new WindowManager(Constants.title, 1600, 900, false); window = new WindowManager(Constants.title, 1600, 900, false);
engine = new EngineManager(); game = new TestGame();
EngineManager engine = new EngineManager();
try { try {
engine.start(); engine.start();
@ -24,4 +27,8 @@ public class Launcher {
public static WindowManager getWindow() { public static WindowManager getWindow() {
return window; return window;
} }
public static TestGame getGame() {
return game;
}
} }

View file

@ -0,0 +1,27 @@
package com.okseby.core;
import org.lwjgl.opengl.GL11;
public class RenderManager {
private final WindowManager window;
public RenderManager() {
window = Launcher.getWindow();
}
public void init() throws Exception {
}
public void render() {
}
public void clear() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
}
public void cleanup() {
}
}

View file

@ -65,7 +65,7 @@ public class WindowManager {
}); });
GLFW.glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> { GLFW.glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
if (key == GLFW.GLFW_KEY_ESCAPE || action == GLFW.GLFW_RELEASE) if (key == GLFW.GLFW_KEY_ESCAPE && action == GLFW.GLFW_RELEASE)
GLFW.glfwSetWindowShouldClose(window, true); GLFW.glfwSetWindowShouldClose(window, true);
}); });

View file

@ -0,0 +1,61 @@
package com.okseby.core.test;
import com.okseby.core.ILogic;
import com.okseby.core.Launcher;
import com.okseby.core.RenderManager;
import com.okseby.core.WindowManager;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL11;
public class TestGame implements ILogic {
private int direction = 0;
private float color = 0.0f;
private final RenderManager renderer;
private final WindowManager window;
public TestGame() {
renderer = new RenderManager();
window = Launcher.getWindow();
}
@Override
public void init() throws Exception {
renderer.init();
}
@Override
public void input() {
if (window.isKeyPressed(GLFW.GLFW_KEY_UP))
direction = 1;
else if (window.isKeyPressed(GLFW.GLFW_KEY_DOWN))
direction = -1;
else
direction = 0;
}
@Override
public void update() {
color += direction * 0.01f;
if (color > 1)
color = 1.0f;
else if (color <= 0)
color = 0.0f;
}
@Override
public void render() {
if (window.isResizeable()) {
GL11.glViewport(0, 0, window.getWidth(), window.getHeight());
window.setResizeable(true);
}
window.setClearColor(color, color, color, 0.0f);
renderer.clear();
}
@Override
public void cleanup() {
renderer.cleanup();
}
}