diff --git a/physics-engine/src/main/java/com/project/physics/engine/core/FixedTimestepGame.java b/physics-engine/src/main/java/com/project/physics/engine/core/FixedTimestepGame.java index f741303..41e168e 100644 --- a/physics-engine/src/main/java/com/project/physics/engine/core/FixedTimestepGame.java +++ b/physics-engine/src/main/java/com/project/physics/engine/core/FixedTimestepGame.java @@ -23,6 +23,8 @@ */ package com.project.physics.engine.core; +import static org.lwjgl.opengl.GL11.glViewport; + /** * This class contains the implementation for a fixed timestep game loop. * @@ -43,6 +45,11 @@ public void gameLoop() { running = false; } + if (window.hasResized()) { + renderer.setProjection(window.getWidth(), window.getHeight()); + glViewport(0, 0, window.getWidth(), window.getHeight()); + } + /* Get delta time and update the accumulator */ delta = timer.getDelta(); accumulator += delta; diff --git a/physics-engine/src/main/java/com/project/physics/engine/core/VariableTimestepGame.java b/physics-engine/src/main/java/com/project/physics/engine/core/VariableTimestepGame.java index db500aa..7e930a5 100644 --- a/physics-engine/src/main/java/com/project/physics/engine/core/VariableTimestepGame.java +++ b/physics-engine/src/main/java/com/project/physics/engine/core/VariableTimestepGame.java @@ -23,6 +23,8 @@ */ package com.project.physics.engine.core; +import static org.lwjgl.opengl.GL11.glViewport; + /** * This class contains the implementation for a variable timestep game loop. * @@ -40,6 +42,11 @@ public void gameLoop() { running = false; } + if (window.hasResized()) { + renderer.setProjection(window.getWidth(), window.getHeight()); + glViewport(0, 0, window.getWidth(), window.getHeight()); + } + /* Get delta time */ delta = timer.getDelta(); diff --git a/physics-engine/src/main/java/com/project/physics/engine/graphic/window/DynamicRenderer.java b/physics-engine/src/main/java/com/project/physics/engine/graphic/window/DynamicRenderer.java index a2eaa2c..828467f 100644 --- a/physics-engine/src/main/java/com/project/physics/engine/graphic/window/DynamicRenderer.java +++ b/physics-engine/src/main/java/com/project/physics/engine/graphic/window/DynamicRenderer.java @@ -470,10 +470,14 @@ private void setupShaderProgram() { int uniView = program.getUniformLocation("view"); program.setUniform(uniView, view); + setProjection(width, height); + } + + public void setProjection(int width, int height) { /* Set projection matrix to an orthographic projection */ Matrix4f projection = Matrix4f.orthographic(0f, width, 0f, height, -1f, 1f); - int uniProjection = program.getUniformLocation("projection"); - program.setUniform(uniProjection, projection); + int uniformProjection = program.getUniformLocation("projection"); + program.setUniform(uniformProjection, projection); } /** diff --git a/physics-engine/src/main/java/com/project/physics/engine/graphic/window/Window.java b/physics-engine/src/main/java/com/project/physics/engine/graphic/window/Window.java index 55a8f8e..57e8f20 100644 --- a/physics-engine/src/main/java/com/project/physics/engine/graphic/window/Window.java +++ b/physics-engine/src/main/java/com/project/physics/engine/graphic/window/Window.java @@ -31,7 +31,6 @@ import static org.lwjgl.glfw.GLFW.GLFW_OPENGL_FORWARD_COMPAT; import static org.lwjgl.glfw.GLFW.GLFW_OPENGL_PROFILE; import static org.lwjgl.glfw.GLFW.GLFW_PRESS; -import static org.lwjgl.glfw.GLFW.GLFW_RESIZABLE; import static org.lwjgl.glfw.GLFW.GLFW_TRUE; import static org.lwjgl.glfw.GLFW.GLFW_VISIBLE; import static org.lwjgl.glfw.GLFW.glfwCreateWindow; @@ -44,6 +43,7 @@ import static org.lwjgl.glfw.GLFW.glfwSetKeyCallback; import static org.lwjgl.glfw.GLFW.glfwSetWindowPos; import static org.lwjgl.glfw.GLFW.glfwSetWindowShouldClose; +import static org.lwjgl.glfw.GLFW.glfwSetWindowSizeCallback; import static org.lwjgl.glfw.GLFW.glfwSetWindowTitle; import static org.lwjgl.glfw.GLFW.glfwSwapBuffers; import static org.lwjgl.glfw.GLFW.glfwSwapInterval; @@ -52,6 +52,7 @@ import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose; import org.lwjgl.glfw.GLFWKeyCallback; import org.lwjgl.glfw.GLFWVidMode; +import org.lwjgl.glfw.GLFWWindowSizeCallback; import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GLCapabilities; import static org.lwjgl.system.MemoryUtil.NULL; @@ -63,21 +64,14 @@ */ public class Window { - /** - * Stores the window handle. - */ private final long id; - - /** - * Key callback for the window. - */ private final GLFWKeyCallback keyCallback; - - /** - * Shows if vsync is enabled. - */ private boolean vsync; + private int width, height; + private boolean hasResized; + private GLFWWindowSizeCallback windowSizeCallback; + /** * Creates a GLFW window and its OpenGL context with the specified width, * height and title. @@ -88,8 +82,12 @@ public class Window { * @param vsync Set to true, if you want v-sync */ public Window(int width, int height, CharSequence title, boolean vsync) { + this.width = width; + this.height = height; this.vsync = vsync; + hasResized = false; + /* Creating a temporary window for getting the available OpenGL version */ glfwDefaultWindowHints(); glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); @@ -113,9 +111,9 @@ public Window(int width, int height, CharSequence title, boolean vsync) { glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); } else { throw new RuntimeException("Neither OpenGL 3.2 nor OpenGL 2.1 is " - + "supported, you may want to update your graphics driver."); + + "supported, you may want to update your graphics driver."); } - glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + // glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); /* Create window with specified OpenGL context */ id = glfwCreateWindow(width, height, title, NULL, NULL); @@ -127,9 +125,8 @@ public Window(int width, int height, CharSequence title, boolean vsync) { /* Center window on screen */ GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); glfwSetWindowPos(id, - (vidmode.width() - width) / 2, - (vidmode.height() - height) / 2 - ); + (vidmode.width() - width) / 2, + (vidmode.height() - height) / 2); /* Create OpenGL context */ glfwMakeContextCurrent(id); @@ -150,6 +147,24 @@ public void invoke(long window, int key, int scancode, int action, int mods) { } }; glfwSetKeyCallback(id, keyCallback); + + /* set resizing callbacks */ + setResizeCallback(id); + } + + private void setResizeCallback(long id) { + /* Set resize callback */ + windowSizeCallback = new GLFWWindowSizeCallback() { + + @Override + public void invoke(long window, int argWidth, int argHeight) { + width = argWidth; + height = argHeight; + hasResized = true; + } + + }; + glfwSetWindowSizeCallback(id, windowSizeCallback); } /** @@ -174,8 +189,10 @@ public void setTitle(CharSequence title) { * Updates the screen. */ public void update() { + hasResized = false; glfwSwapBuffers(id); glfwPollEvents(); + } /** @@ -184,6 +201,7 @@ public void update() { public void destroy() { glfwDestroyWindow(id); keyCallback.free(); + windowSizeCallback.free(); } /** @@ -209,4 +227,15 @@ public boolean isVSyncEnabled() { return this.vsync; } + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + + public boolean hasResized() { + return hasResized; + } }