Skip to content

Commit

Permalink
[API] Fixed window rendering on resize and white background on start …
Browse files Browse the repository at this point in the history
…up. (#62)

* Fixed window rendering on resize and white background on start up.

* Code formatting and clean up.

* Fixed attribute modifier.
  • Loading branch information
Marco012 committed Jun 24, 2021
1 parent cbdf36f commit 34e282e
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions imgui-app/src/main/java/imgui/app/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand All @@ -22,6 +23,7 @@
* 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();

Expand Down Expand Up @@ -90,14 +92,26 @@ protected void initWindow(final Configuration config) {
}

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() {
Expand Down Expand Up @@ -141,26 +155,40 @@ protected void postProcess() {
*/
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();
}
Expand All @@ -180,6 +208,13 @@ protected void endFrame() {
GLFW.glfwMakeContextCurrent(backupWindowPtr);
}

renderBuffer();
}

/**
* Method to render the OpenGL buffer and poll window events.
*/
private void renderBuffer() {
GLFW.glfwSwapBuffers(handle);
GLFW.glfwPollEvents();
}
Expand Down

0 comments on commit 34e282e

Please sign in to comment.