8
8
import org .lwjgl .glfw .GLFW ;
9
9
import org .lwjgl .glfw .GLFWErrorCallback ;
10
10
import org .lwjgl .glfw .GLFWVidMode ;
11
+ import org .lwjgl .glfw .GLFWWindowSizeCallback ;
11
12
import org .lwjgl .opengl .GL ;
12
13
import org .lwjgl .opengl .GL32 ;
13
14
import org .lwjgl .system .MemoryStack ;
22
23
* When extended, life-cycle methods should be called manually.
23
24
*/
24
25
public abstract class Window {
26
+
25
27
private final ImGuiImplGlfw imGuiGlfw = new ImGuiImplGlfw ();
26
28
private final ImGuiImplGl3 imGuiGl3 = new ImGuiImplGl3 ();
27
29
@@ -90,14 +92,26 @@ protected void initWindow(final Configuration config) {
90
92
}
91
93
92
94
GLFW .glfwMakeContextCurrent (handle );
95
+
96
+ GL .createCapabilities ();
97
+
93
98
GLFW .glfwSwapInterval (GLFW .GLFW_TRUE );
94
- GLFW .glfwShowWindow (handle );
95
99
96
100
if (config .isFullScreen ()) {
97
101
GLFW .glfwMaximizeWindow (handle );
102
+ } else {
103
+ GLFW .glfwShowWindow (handle );
98
104
}
99
105
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
+ });
101
115
}
102
116
103
117
private void decideGlGlslVersions () {
@@ -141,26 +155,40 @@ protected void postProcess() {
141
155
*/
142
156
protected void run () {
143
157
while (!GLFW .glfwWindowShouldClose (handle )) {
144
- startFrame ();
145
- preProcess ();
146
- process ();
147
- postProcess ();
148
- endFrame ();
158
+ runFrame ();
149
159
}
150
160
}
151
161
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
+
152
173
/**
153
174
* Method to be overridden by user to provide main application logic.
154
175
*/
155
176
public abstract void process ();
156
177
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
+
157
186
/**
158
187
* Method called at the beginning of the main cycle.
159
188
* It clears OpenGL buffer and starts an ImGui frame.
160
189
*/
161
190
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 ();
164
192
imGuiGlfw .newFrame ();
165
193
ImGui .newFrame ();
166
194
}
@@ -180,6 +208,13 @@ protected void endFrame() {
180
208
GLFW .glfwMakeContextCurrent (backupWindowPtr );
181
209
}
182
210
211
+ renderBuffer ();
212
+ }
213
+
214
+ /**
215
+ * Method to render the OpenGL buffer and poll window events.
216
+ */
217
+ private void renderBuffer () {
183
218
GLFW .glfwSwapBuffers (handle );
184
219
GLFW .glfwPollEvents ();
185
220
}
0 commit comments