30
30
31
31
@ SuppressWarnings ("MagicNumber" )
32
32
public final class ImGuiGlfwExample {
33
- private long windowPtr ; // current GLFW window pointer
33
+ private long windowPtr ; // pointer to the current GLFW window
34
34
35
- // To get window properties
35
+ // For application window properties
36
36
private final int [] winWidth = new int [1 ];
37
37
private final int [] winHeight = new int [1 ];
38
38
private final int [] fbWidth = new int [1 ];
@@ -49,7 +49,7 @@ public final class ImGuiGlfwExample {
49
49
private final ImGuiImplGl3 imGuiGl3 = new ImGuiImplGl3 ();
50
50
private String glslVersion = null ; // We can initialize our renderer with different versions of the GLSL
51
51
52
- // Ui to render
52
+ // User UI to render
53
53
private final ExampleUi exampleUi = new ExampleUi ();
54
54
55
55
public void run () throws Exception {
@@ -61,7 +61,7 @@ public void run() throws Exception {
61
61
destroyGlfw ();
62
62
}
63
63
64
- // Initialize GLFW + create OpenGL context.
64
+ // Initialize GLFW + create an OpenGL context.
65
65
// All code is mostly a copy-paste from the official LWJGL3 "Get Started": https://www.lwjgl.org/guide
66
66
private void initGlfw () {
67
67
// Setup an error callback. The default implementation
@@ -79,7 +79,6 @@ private void initGlfw() {
79
79
80
80
decideGlGlslVersions ();
81
81
82
- // Create the window
83
82
windowPtr = glfwCreateWindow (1280 , 768 , "Dear ImGui + GLFW + LWJGL Example" , NULL , NULL );
84
83
85
84
if (windowPtr == NULL ) {
@@ -135,6 +134,7 @@ private void initImGui() {
135
134
// This line is critical for Dear ImGui to work.
136
135
ImGui .createContext ();
137
136
137
+ // ------------------------------------------------------------
138
138
// Initialize ImGuiIO config
139
139
final ImGuiIO io = ImGui .getIO ();
140
140
@@ -143,6 +143,7 @@ private void initImGui() {
143
143
io .setBackendFlags (ImGuiBackendFlags .HasMouseCursors ); // Mouse cursors to display while resizing windows etc.
144
144
io .setBackendPlatformName ("imgui_java_impl_glfw" );
145
145
146
+ // ------------------------------------------------------------
146
147
// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
147
148
final int [] keyMap = new int [ImGuiKey .COUNT ];
148
149
keyMap [ImGuiKey .Tab ] = GLFW_KEY_TAB ;
@@ -169,6 +170,7 @@ private void initImGui() {
169
170
keyMap [ImGuiKey .Z ] = GLFW_KEY_Z ;
170
171
io .setKeyMap (keyMap );
171
172
173
+ // ------------------------------------------------------------
172
174
// Mouse cursors mapping
173
175
mouseCursors [ImGuiMouseCursor .Arrow ] = glfwCreateStandardCursor (GLFW_ARROW_CURSOR );
174
176
mouseCursors [ImGuiMouseCursor .TextInput ] = glfwCreateStandardCursor (GLFW_IBEAM_CURSOR );
@@ -181,7 +183,7 @@ private void initImGui() {
181
183
mouseCursors [ImGuiMouseCursor .NotAllowed ] = glfwCreateStandardCursor (GLFW_ARROW_CURSOR );
182
184
183
185
// ------------------------------------------------------------
184
- // Here goes GLFW callbacks to update user input in Dear ImGui
186
+ // GLFW callbacks to handle user input
185
187
186
188
glfwSetKeyCallback (windowPtr , (w , key , scancode , action , mods ) -> {
187
189
if (action == GLFW_PRESS ) {
@@ -239,44 +241,38 @@ public String get() {
239
241
240
242
// ------------------------------------------------------------
241
243
// Fonts configuration
242
-
243
- // -------------------
244
- // Fonts merge example
244
+ // Read: https://raw.githubusercontent.com/ocornut/imgui/master/docs/FONTS.txt
245
245
246
246
final ImFontAtlas fontAtlas = io .getFonts ();
247
+ final ImFontConfig fontConfig = new ImFontConfig (); // Natively allocated object, should be explicitly destroyed
247
248
248
- // First of all we add a default font, which is 'ProggyClean.ttf, 13px'
249
+ // Add a default font, which is 'ProggyClean.ttf, 13px'
249
250
fontAtlas .addFontDefault ();
250
251
251
- final ImFontConfig fontConfig = new ImFontConfig (); // Keep in mind that creation of the ImFontConfig will allocate the native memory
252
- fontConfig .setMergeMode (true ); // All fonts added while this mode is turned on will be merged with the previously added font
252
+ // Fonts merge example
253
+ fontConfig .setMergeMode (true ); // When enabled, all fonts added with this config would be merged with the previously added font
253
254
fontConfig .setPixelSnapH (true );
254
- fontConfig .setGlyphRanges (fontAtlas .getGlyphRangesCyrillic ()); // Additional glyphs could be added like this or in "addFontFrom*()" methods
255
255
256
- // We merge font loaded from resources with the default one. Thus we will get an absent cyrillic glyphs
257
- fontAtlas .addFontFromMemoryTTF (loadFromResources ("basis33.ttf" ), 16 , fontConfig );
256
+ fontAtlas .addFontFromMemoryTTF (loadFromResources ("basis33.ttf" ), 16 , fontConfig , fontAtlas .getGlyphRangesCyrillic ());
258
257
259
- // Disable merge mode and add all other fonts normally
260
258
fontConfig .setMergeMode (false );
261
259
fontConfig .setPixelSnapH (false );
262
260
263
- // ------------------------------
264
261
// Fonts from file/memory example
262
+ // We can add new fonts from the file system
263
+ fontAtlas .addFontFromFileTTF ("src/test/resources/Rubik-Regular.ttf" , 13 , fontConfig );
264
+ fontAtlas .addFontFromFileTTF ("src/test/resources/Rubik-Regular.ttf" , 16 , fontConfig );
265
265
266
- // fontConfig.setRasterizerMultiply(1.2f); // This will make fonts a bit more readable
267
-
268
- // We can add new fonts directly from file
269
- fontAtlas .addFontFromFileTTF ("src/test/resources/DroidSans.ttf" , 13 , fontConfig );
270
- fontAtlas .addFontFromFileTTF ("src/test/resources/DroidSans.ttf" , 14 , fontConfig );
271
-
272
- // Or directly from memory
266
+ // Or directly from the memory
273
267
fontConfig .setName ("Roboto-Regular.ttf, 13px" ); // This name will be displayed in Style Editor
274
268
fontAtlas .addFontFromMemoryTTF (loadFromResources ("Roboto-Regular.ttf" ), 13 , fontConfig );
275
- fontConfig .setName ("Roboto-Regular.ttf, 14px " ); // We can apply a new config value every time we add a new font
276
- fontAtlas .addFontFromMemoryTTF (loadFromResources ("Roboto-Regular.ttf" ), 14 , fontConfig );
269
+ fontConfig .setName ("Roboto-Regular.ttf, 16px " ); // We can apply a new config value every time we add a new font
270
+ fontAtlas .addFontFromMemoryTTF (loadFromResources ("Roboto-Regular.ttf" ), 16 , fontConfig );
277
271
278
272
fontConfig .destroy (); // After all fonts were added we don't need this config more
279
273
274
+ // ------------------------------------------------------------
275
+ // Use freetype instead of stb_truetype to build a fonts texture
280
276
ImGuiFreeType .buildFontAtlas (fontAtlas , ImGuiFreeType .RasterizerFlags .LightHinting );
281
277
282
278
// Method initializes LWJGL3 renderer.
@@ -286,7 +282,7 @@ public String get() {
286
282
}
287
283
288
284
// Main application loop
289
- private void loop () throws Exception {
285
+ private void loop () {
290
286
double time = 0 ; // to track our frame delta value
291
287
292
288
// Run the rendering loop until the user has attempted to close the window
@@ -298,7 +294,7 @@ private void loop() throws Exception {
298
294
299
295
startFrame ((float ) deltaTime );
300
296
301
- // Any Dear ImGui code SHOULD go between NewFrame ()/Render () methods
297
+ // Any Dear ImGui code SHOULD go between ImGui.newFrame ()/ImGui.render () methods
302
298
ImGui .newFrame ();
303
299
exampleUi .render ();
304
300
ImGui .render ();
@@ -312,32 +308,30 @@ private void startFrame(final float deltaTime) {
312
308
glClearColor (exampleUi .backgroundColor [0 ], exampleUi .backgroundColor [1 ], exampleUi .backgroundColor [2 ], 0.0f );
313
309
glClear (GL_COLOR_BUFFER_BIT );
314
310
315
- // Get window size properties and mouse position
311
+ // Get window properties and mouse position
316
312
glfwGetWindowSize (windowPtr , winWidth , winHeight );
317
313
glfwGetFramebufferSize (windowPtr , fbWidth , fbHeight );
318
314
glfwGetCursorPos (windowPtr , mousePosX , mousePosY );
319
315
320
- // We SHOULD call those methods to update ImGui state for current frame
316
+ // We SHOULD call those methods to update Dear ImGui state for the current frame
321
317
final ImGuiIO io = ImGui .getIO ();
322
318
io .setDisplaySize (winWidth [0 ], winHeight [0 ]);
323
319
io .setDisplayFramebufferScale ((float ) fbWidth [0 ] / winWidth [0 ], (float ) fbHeight [0 ] / winHeight [0 ]);
324
320
io .setMousePos ((float ) mousePosX [0 ], (float ) mousePosY [0 ]);
325
321
io .setDeltaTime (deltaTime );
326
322
327
- // Update mouse cursor
323
+ // Update the mouse cursor
328
324
final int imguiCursor = ImGui .getMouseCursor ();
329
325
glfwSetCursor (windowPtr , mouseCursors [imguiCursor ]);
330
326
glfwSetInputMode (windowPtr , GLFW_CURSOR , GLFW_CURSOR_NORMAL );
331
327
}
332
328
333
329
private void endFrame () {
334
- // After Dear ImGui prepared a draw data, we use it in LWJGL3 renderer.
330
+ // After Dear ImGui prepared a draw data, we use it in the LWJGL3 renderer.
335
331
// At that moment ImGui will be rendered to the current OpenGL context.
336
332
imGuiGl3 .render (ImGui .getDrawData ());
337
333
338
- glfwSwapBuffers (windowPtr ); // swap the color buffers
339
-
340
- // Poll for window events. The key callback above will only be invoked during this call.
334
+ glfwSwapBuffers (windowPtr );
341
335
glfwPollEvents ();
342
336
}
343
337
0 commit comments