Skip to content

Commit d5b76bc

Browse files
committed
Update example
1 parent 7f15243 commit d5b76bc

File tree

4 files changed

+30
-36
lines changed

4 files changed

+30
-36
lines changed

imgui-binding/src/main/java/imgui/ImGuiFreeType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static void buildFontAtlas(final ImFontAtlas atlas) {
1616
nBuildFontAtlas(atlas.ptr, 0);
1717
}
1818

19-
public static void buildFontAtlas(final ImFontAtlas atlas, int extraFlags) {
19+
public static void buildFontAtlas(final ImFontAtlas atlas, final int extraFlags) {
2020
nBuildFontAtlas(atlas.ptr, extraFlags);
2121
}
2222

imgui-lwjgl3/src/test/java/ImGuiGlfwExample.java

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030

3131
@SuppressWarnings("MagicNumber")
3232
public final class ImGuiGlfwExample {
33-
private long windowPtr; // current GLFW window pointer
33+
private long windowPtr; // pointer to the current GLFW window
3434

35-
// To get window properties
35+
// For application window properties
3636
private final int[] winWidth = new int[1];
3737
private final int[] winHeight = new int[1];
3838
private final int[] fbWidth = new int[1];
@@ -49,7 +49,7 @@ public final class ImGuiGlfwExample {
4949
private final ImGuiImplGl3 imGuiGl3 = new ImGuiImplGl3();
5050
private String glslVersion = null; // We can initialize our renderer with different versions of the GLSL
5151

52-
// Ui to render
52+
// User UI to render
5353
private final ExampleUi exampleUi = new ExampleUi();
5454

5555
public void run() throws Exception {
@@ -61,7 +61,7 @@ public void run() throws Exception {
6161
destroyGlfw();
6262
}
6363

64-
// Initialize GLFW + create OpenGL context.
64+
// Initialize GLFW + create an OpenGL context.
6565
// All code is mostly a copy-paste from the official LWJGL3 "Get Started": https://www.lwjgl.org/guide
6666
private void initGlfw() {
6767
// Setup an error callback. The default implementation
@@ -79,7 +79,6 @@ private void initGlfw() {
7979

8080
decideGlGlslVersions();
8181

82-
// Create the window
8382
windowPtr = glfwCreateWindow(1280, 768, "Dear ImGui + GLFW + LWJGL Example", NULL, NULL);
8483

8584
if (windowPtr == NULL) {
@@ -135,6 +134,7 @@ private void initImGui() {
135134
// This line is critical for Dear ImGui to work.
136135
ImGui.createContext();
137136

137+
// ------------------------------------------------------------
138138
// Initialize ImGuiIO config
139139
final ImGuiIO io = ImGui.getIO();
140140

@@ -143,6 +143,7 @@ private void initImGui() {
143143
io.setBackendFlags(ImGuiBackendFlags.HasMouseCursors); // Mouse cursors to display while resizing windows etc.
144144
io.setBackendPlatformName("imgui_java_impl_glfw");
145145

146+
// ------------------------------------------------------------
146147
// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
147148
final int[] keyMap = new int[ImGuiKey.COUNT];
148149
keyMap[ImGuiKey.Tab] = GLFW_KEY_TAB;
@@ -169,6 +170,7 @@ private void initImGui() {
169170
keyMap[ImGuiKey.Z] = GLFW_KEY_Z;
170171
io.setKeyMap(keyMap);
171172

173+
// ------------------------------------------------------------
172174
// Mouse cursors mapping
173175
mouseCursors[ImGuiMouseCursor.Arrow] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
174176
mouseCursors[ImGuiMouseCursor.TextInput] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR);
@@ -181,7 +183,7 @@ private void initImGui() {
181183
mouseCursors[ImGuiMouseCursor.NotAllowed] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
182184

183185
// ------------------------------------------------------------
184-
// Here goes GLFW callbacks to update user input in Dear ImGui
186+
// GLFW callbacks to handle user input
185187

186188
glfwSetKeyCallback(windowPtr, (w, key, scancode, action, mods) -> {
187189
if (action == GLFW_PRESS) {
@@ -239,44 +241,38 @@ public String get() {
239241

240242
// ------------------------------------------------------------
241243
// Fonts configuration
242-
243-
// -------------------
244-
// Fonts merge example
244+
// Read: https://raw.githubusercontent.com/ocornut/imgui/master/docs/FONTS.txt
245245

246246
final ImFontAtlas fontAtlas = io.getFonts();
247+
final ImFontConfig fontConfig = new ImFontConfig(); // Natively allocated object, should be explicitly destroyed
247248

248-
// First of all we add a default font, which is 'ProggyClean.ttf, 13px'
249+
// Add a default font, which is 'ProggyClean.ttf, 13px'
249250
fontAtlas.addFontDefault();
250251

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
253254
fontConfig.setPixelSnapH(true);
254-
fontConfig.setGlyphRanges(fontAtlas.getGlyphRangesCyrillic()); // Additional glyphs could be added like this or in "addFontFrom*()" methods
255255

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());
258257

259-
// Disable merge mode and add all other fonts normally
260258
fontConfig.setMergeMode(false);
261259
fontConfig.setPixelSnapH(false);
262260

263-
// ------------------------------
264261
// 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);
265265

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
273267
fontConfig.setName("Roboto-Regular.ttf, 13px"); // This name will be displayed in Style Editor
274268
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);
277271

278272
fontConfig.destroy(); // After all fonts were added we don't need this config more
279273

274+
// ------------------------------------------------------------
275+
// Use freetype instead of stb_truetype to build a fonts texture
280276
ImGuiFreeType.buildFontAtlas(fontAtlas, ImGuiFreeType.RasterizerFlags.LightHinting);
281277

282278
// Method initializes LWJGL3 renderer.
@@ -286,7 +282,7 @@ public String get() {
286282
}
287283

288284
// Main application loop
289-
private void loop() throws Exception {
285+
private void loop() {
290286
double time = 0; // to track our frame delta value
291287

292288
// Run the rendering loop until the user has attempted to close the window
@@ -298,7 +294,7 @@ private void loop() throws Exception {
298294

299295
startFrame((float) deltaTime);
300296

301-
// Any Dear ImGui code SHOULD go between NewFrame()/Render() methods
297+
// Any Dear ImGui code SHOULD go between ImGui.newFrame()/ImGui.render() methods
302298
ImGui.newFrame();
303299
exampleUi.render();
304300
ImGui.render();
@@ -312,32 +308,30 @@ private void startFrame(final float deltaTime) {
312308
glClearColor(exampleUi.backgroundColor[0], exampleUi.backgroundColor[1], exampleUi.backgroundColor[2], 0.0f);
313309
glClear(GL_COLOR_BUFFER_BIT);
314310

315-
// Get window size properties and mouse position
311+
// Get window properties and mouse position
316312
glfwGetWindowSize(windowPtr, winWidth, winHeight);
317313
glfwGetFramebufferSize(windowPtr, fbWidth, fbHeight);
318314
glfwGetCursorPos(windowPtr, mousePosX, mousePosY);
319315

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
321317
final ImGuiIO io = ImGui.getIO();
322318
io.setDisplaySize(winWidth[0], winHeight[0]);
323319
io.setDisplayFramebufferScale((float) fbWidth[0] / winWidth[0], (float) fbHeight[0] / winHeight[0]);
324320
io.setMousePos((float) mousePosX[0], (float) mousePosY[0]);
325321
io.setDeltaTime(deltaTime);
326322

327-
// Update mouse cursor
323+
// Update the mouse cursor
328324
final int imguiCursor = ImGui.getMouseCursor();
329325
glfwSetCursor(windowPtr, mouseCursors[imguiCursor]);
330326
glfwSetInputMode(windowPtr, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
331327
}
332328

333329
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.
335331
// At that moment ImGui will be rendered to the current OpenGL context.
336332
imGuiGl3.render(ImGui.getDrawData());
337333

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);
341335
glfwPollEvents();
342336
}
343337

-186 KB
Binary file not shown.
130 KB
Binary file not shown.

0 commit comments

Comments
 (0)