Skip to content

Commit 34e282e

Browse files
authored
[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.
1 parent cbdf36f commit 34e282e

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

imgui-app/src/main/java/imgui/app/Window.java

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.lwjgl.glfw.GLFW;
99
import org.lwjgl.glfw.GLFWErrorCallback;
1010
import org.lwjgl.glfw.GLFWVidMode;
11+
import org.lwjgl.glfw.GLFWWindowSizeCallback;
1112
import org.lwjgl.opengl.GL;
1213
import org.lwjgl.opengl.GL32;
1314
import org.lwjgl.system.MemoryStack;
@@ -22,6 +23,7 @@
2223
* When extended, life-cycle methods should be called manually.
2324
*/
2425
public abstract class Window {
26+
2527
private final ImGuiImplGlfw imGuiGlfw = new ImGuiImplGlfw();
2628
private final ImGuiImplGl3 imGuiGl3 = new ImGuiImplGl3();
2729

@@ -90,14 +92,26 @@ protected void initWindow(final Configuration config) {
9092
}
9193

9294
GLFW.glfwMakeContextCurrent(handle);
95+
96+
GL.createCapabilities();
97+
9398
GLFW.glfwSwapInterval(GLFW.GLFW_TRUE);
94-
GLFW.glfwShowWindow(handle);
9599

96100
if (config.isFullScreen()) {
97101
GLFW.glfwMaximizeWindow(handle);
102+
} else {
103+
GLFW.glfwShowWindow(handle);
98104
}
99105

100-
GL.createCapabilities();
106+
clearBuffer();
107+
renderBuffer();
108+
109+
GLFW.glfwSetWindowSizeCallback(handle, new GLFWWindowSizeCallback() {
110+
@Override
111+
public void invoke(final long window, final int width, final int height) {
112+
runFrame();
113+
}
114+
});
101115
}
102116

103117
private void decideGlGlslVersions() {
@@ -141,26 +155,40 @@ protected void postProcess() {
141155
*/
142156
protected void run() {
143157
while (!GLFW.glfwWindowShouldClose(handle)) {
144-
startFrame();
145-
preProcess();
146-
process();
147-
postProcess();
148-
endFrame();
158+
runFrame();
149159
}
150160
}
151161

162+
/**
163+
* Method used to run the next frame.
164+
*/
165+
protected void runFrame() {
166+
startFrame();
167+
preProcess();
168+
process();
169+
postProcess();
170+
endFrame();
171+
}
172+
152173
/**
153174
* Method to be overridden by user to provide main application logic.
154175
*/
155176
public abstract void process();
156177

178+
/**
179+
* Method used to clear the OpenGL buffer.
180+
*/
181+
private void clearBuffer() {
182+
GL32.glClearColor(colorBg.getRed(), colorBg.getGreen(), colorBg.getBlue(), colorBg.getAlpha());
183+
GL32.glClear(GL32.GL_COLOR_BUFFER_BIT | GL32.GL_DEPTH_BUFFER_BIT);
184+
}
185+
157186
/**
158187
* Method called at the beginning of the main cycle.
159188
* It clears OpenGL buffer and starts an ImGui frame.
160189
*/
161190
protected void startFrame() {
162-
GL32.glClearColor(colorBg.getRed(), colorBg.getGreen(), colorBg.getBlue(), colorBg.getAlpha());
163-
GL32.glClear(GL32.GL_COLOR_BUFFER_BIT | GL32.GL_DEPTH_BUFFER_BIT);
191+
clearBuffer();
164192
imGuiGlfw.newFrame();
165193
ImGui.newFrame();
166194
}
@@ -180,6 +208,13 @@ protected void endFrame() {
180208
GLFW.glfwMakeContextCurrent(backupWindowPtr);
181209
}
182210

211+
renderBuffer();
212+
}
213+
214+
/**
215+
* Method to render the OpenGL buffer and poll window events.
216+
*/
217+
private void renderBuffer() {
183218
GLFW.glfwSwapBuffers(handle);
184219
GLFW.glfwPollEvents();
185220
}

0 commit comments

Comments
 (0)