Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed window rendering on resize and white background on start up. #62

Merged
merged 3 commits into from
Jun 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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