From 34e282e091224afffd7e530d98406e43a9b47703 Mon Sep 17 00:00:00 2001 From: Marco Lima <45668024+Marco012@users.noreply.github.com> Date: Thu, 24 Jun 2021 09:51:43 +0100 Subject: [PATCH] [API] Fixed window rendering on resize and white background on start up. (#62) * Fixed window rendering on resize and white background on start up. * Code formatting and clean up. * Fixed attribute modifier. --- imgui-app/src/main/java/imgui/app/Window.java | 53 +++++++++++++++---- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/imgui-app/src/main/java/imgui/app/Window.java b/imgui-app/src/main/java/imgui/app/Window.java index 166b2e6..b04f2bd 100644 --- a/imgui-app/src/main/java/imgui/app/Window.java +++ b/imgui-app/src/main/java/imgui/app/Window.java @@ -8,6 +8,7 @@ import org.lwjgl.glfw.Callbacks; import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.GLFWVidMode; +import org.lwjgl.glfw.GLFWWindowSizeCallback; import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GL32; import org.lwjgl.system.MemoryStack; @@ -22,6 +23,7 @@ import java.util.Objects; * When extended, life-cycle methods should be called manually. */ public abstract class Window { + private final ImGuiImplGlfw imGuiGlfw = new ImGuiImplGlfw(); private final ImGuiImplGl3 imGuiGl3 = new ImGuiImplGl3(); @@ -90,14 +92,26 @@ public abstract class Window { } GLFW.glfwMakeContextCurrent(handle); + + GL.createCapabilities(); + GLFW.glfwSwapInterval(GLFW.GLFW_TRUE); - GLFW.glfwShowWindow(handle); if (config.isFullScreen()) { GLFW.glfwMaximizeWindow(handle); + } else { + GLFW.glfwShowWindow(handle); } - GL.createCapabilities(); + clearBuffer(); + renderBuffer(); + + GLFW.glfwSetWindowSizeCallback(handle, new GLFWWindowSizeCallback() { + @Override + public void invoke(final long window, final int width, final int height) { + runFrame(); + } + }); } private void decideGlGlslVersions() { @@ -141,26 +155,40 @@ public abstract class Window { */ protected void run() { while (!GLFW.glfwWindowShouldClose(handle)) { - startFrame(); - preProcess(); - process(); - postProcess(); - endFrame(); + runFrame(); } } + /** + * Method used to run the next frame. + */ + protected void runFrame() { + startFrame(); + preProcess(); + process(); + postProcess(); + endFrame(); + } + /** * Method to be overridden by user to provide main application logic. */ public abstract void process(); + /** + * Method used to clear the OpenGL buffer. + */ + private void clearBuffer() { + GL32.glClearColor(colorBg.getRed(), colorBg.getGreen(), colorBg.getBlue(), colorBg.getAlpha()); + GL32.glClear(GL32.GL_COLOR_BUFFER_BIT | GL32.GL_DEPTH_BUFFER_BIT); + } + /** * Method called at the beginning of the main cycle. * It clears OpenGL buffer and starts an ImGui frame. */ protected void startFrame() { - GL32.glClearColor(colorBg.getRed(), colorBg.getGreen(), colorBg.getBlue(), colorBg.getAlpha()); - GL32.glClear(GL32.GL_COLOR_BUFFER_BIT | GL32.GL_DEPTH_BUFFER_BIT); + clearBuffer(); imGuiGlfw.newFrame(); ImGui.newFrame(); } @@ -180,6 +208,13 @@ public abstract class Window { GLFW.glfwMakeContextCurrent(backupWindowPtr); } + renderBuffer(); + } + + /** + * Method to render the OpenGL buffer and poll window events. + */ + private void renderBuffer() { GLFW.glfwSwapBuffers(handle); GLFW.glfwPollEvents(); }