-
-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathBGFXWindow.java
More file actions
269 lines (231 loc) · 7.84 KB
/
Copy pathBGFXWindow.java
File metadata and controls
269 lines (231 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package imgui.app;
import imgui.ImGui;
import imgui.bgfx.ImGuiImplBGFX;
import imgui.flag.ImGuiConfigFlags;
import imgui.glfw.ImGuiImplGlfw;
import org.lwjgl.BufferUtils;
import org.lwjgl.bgfx.BGFXInit;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.*;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.system.Platform;
import java.nio.IntBuffer;
import java.util.Objects;
import static org.lwjgl.bgfx.BGFX.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.system.Configuration.GLFW_LIBRARY_NAME;
import static org.lwjgl.system.MemoryStack.stackPush;
public abstract class BGFXWindow extends Application {
private final ImGuiImplBGFX imGuiBGFX = new ImGuiImplBGFX();
private final ImGuiImplGlfw imGuiGlfw = new ImGuiImplGlfw();
/**
* Pointer to the native GLFW window.
*/
protected long handle;
/**
* Background color of the window.
*/
protected final Color colorBg = new Color(.5f, .5f, .5f, 1);
/**
* Method to initialize application.
*
* @param config configuration object with basic window information
*/
protected void init(final Configuration config) {
initWindow(config);
initImGui(config);
imGuiGlfw.init(handle, true);
imGuiBGFX.init();
}
/**
* Method to dispose all used application resources and destroy its window.
*/
protected void dispose() {
imGuiBGFX.dispose();
imGuiGlfw.dispose();
disposeImGui();
disposeWindow();
}
/**
* Method to create and initialize GLFW window.
*
* @param config configuration object with basic window information
*/
protected void initWindow(final Configuration config) {
if (Platform.get() == Platform.MACOSX) {
GLFW_LIBRARY_NAME.set("glfw_async");
}
GLFWErrorCallback.createPrint(System.err).set();
if (!GLFW.glfwInit()) {
throw new IllegalStateException("Unable to initialize GLFW");
}
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
handle = glfwCreateWindow(config.getWidth(), config.getHeight(), config.getTitle(), MemoryUtil.NULL, MemoryUtil.NULL);
if (handle == MemoryUtil.NULL) {
throw new RuntimeException("Failed to create the GLFW window");
}
try (MemoryStack stack = stackPush()) {
final IntBuffer pWidth = stack.mallocInt(1); // int*
final IntBuffer pHeight = stack.mallocInt(1); // int*
GLFW.glfwGetWindowSize(handle, pWidth, pHeight);
final GLFWVidMode vidmode = Objects.requireNonNull(GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor()));
GLFW.glfwSetWindowPos(handle, (vidmode.width() - pWidth.get(0)) / 2, (vidmode.height() - pHeight.get(0)) / 2);
}
try (MemoryStack stack = stackPush()) {
BGFXInit init = BGFXInit.malloc(stack);
bgfx_init_ctor(init);
init.resolution(it -> it
.width(config.getWidth())
.height(config.getHeight())
.reset(BGFX_RESET_VSYNC));
switch (Platform.get()) {
case LINUX:
init.platformData()
.ndt(GLFWNativeX11.glfwGetX11Display())
.nwh(GLFWNativeX11.glfwGetX11Window(handle));
break;
case MACOSX:
init.platformData()
.nwh(GLFWNativeCocoa.glfwGetCocoaWindow(handle));
break;
case WINDOWS:
init.platformData()
.nwh(GLFWNativeWin32.glfwGetWin32Window(handle));
break;
}
if (!bgfx_init(init)) {
throw new RuntimeException("Error initializing bgfx renderer");
}
}
if (config.isFullScreen()) {
GLFW.glfwMaximizeWindow(handle);
} else {
GLFW.glfwShowWindow(handle);
}
clearBuffer();
renderBuffer();
GLFW.glfwSetWindowSizeCallback(handle, new GLFWWindowSizeCallback() {
@Override
public void invoke(final long window, final int width, final int height) {
bgfx_reset(width, height, BGFX_RESET_NONE, BGFX_TEXTURE_FORMAT_COUNT);
runFrame();
}
});
}
/**
* Method to initialize Dear ImGui context. Could be overridden to do custom Dear ImGui setup before application start.
*
* @param config configuration object with basic window information
*/
protected void initImGui(final Configuration config) {
ImGui.createContext();
}
/**
* Method called every frame, before calling {@link #process()} method.
*/
protected void preProcess() {
}
/**
* Method called every frame, after calling {@link #process()} method.
*/
protected void postProcess() {
}
/**
* Main application loop.
*/
protected void run() {
while (!GLFW.glfwWindowShouldClose(handle)) {
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() {
int color = Math.round(colorBg.getRed() * 255);
color = (color << 8) + Math.round(colorBg.getGreen() * 255);
color = (color << 8) + Math.round(colorBg.getBlue() * 255);
color = (color << 8) + Math.round(colorBg.getAlpha() * 255);
bgfx_set_view_clear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, color, 1.0f, 0);
IntBuffer w = BufferUtils.createIntBuffer(1);
IntBuffer h = BufferUtils.createIntBuffer(1);
glfwGetWindowSize(getHandle(), w, h);
int width = w.get(0);
int height = h.get(0);
bgfx_set_view_rect(0, 0, 0, width, height);
}
/**
* Method called at the beginning of the main cycle.
* It clears OpenGL buffer and starts an ImGui frame.
*/
protected void startFrame() {
clearBuffer();
imGuiGlfw.newFrame();
ImGui.newFrame();
}
/**
* Method called in the end of the main cycle.
* It renders ImGui and swaps GLFW buffers to show an updated frame.
*/
protected void endFrame() {
ImGui.render();
imGuiBGFX.renderDrawData(ImGui.getDrawData());
if (ImGui.getIO().hasConfigFlags(ImGuiConfigFlags.ViewportsEnable)) {
final long backupWindowPtr = GLFW.glfwGetCurrentContext();
ImGui.updatePlatformWindows();
ImGui.renderPlatformWindowsDefault();
GLFW.glfwMakeContextCurrent(backupWindowPtr);
}
renderBuffer();
}
/**
* Method to render the OpenGL buffer and poll window events.
*/
private void renderBuffer() {
bgfx_frame(false);
GLFW.glfwPollEvents();
}
/**
* Method to destroy Dear ImGui context.
*/
protected void disposeImGui() {
ImGui.destroyContext();
}
/**
* Method to destroy GLFW window.
*/
protected void disposeWindow() {
bgfx_shutdown();
Callbacks.glfwFreeCallbacks(handle);
GLFW.glfwDestroyWindow(handle);
GLFW.glfwTerminate();
Objects.requireNonNull(GLFW.glfwSetErrorCallback(null)).free();
}
/**
* @return pointer to the native GLFW window
*/
public final long getHandle() {
return handle;
}
/**
* @return {@link Color} instance, which represents background color for the window
*/
public final Color getColorBg() {
return colorBg;
}
}