Update example
This commit is contained in:
1 parent
7f152431fa
commit
d5b76bc2c8
4 files changed
+30
-36
No files matched your search
@@ -16,7 +16,7 @@ public final class ImGuiFreeType {
|
|||||||
nBuildFontAtlas(atlas.ptr, 0);
|
nBuildFontAtlas(atlas.ptr, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void buildFontAtlas(final ImFontAtlas atlas, int extraFlags) {
|
public static void buildFontAtlas(final ImFontAtlas atlas, final int extraFlags) {
|
||||||
nBuildFontAtlas(atlas.ptr, extraFlags);
|
nBuildFontAtlas(atlas.ptr, extraFlags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,9 +30,9 @@ import static org.lwjgl.system.MemoryUtil.NULL;
|
|||||||
|
|
||||||
@SuppressWarnings("MagicNumber")
|
@SuppressWarnings("MagicNumber")
|
||||||
public final class ImGuiGlfwExample {
|
public final class ImGuiGlfwExample {
|
||||||
private long windowPtr; // current GLFW window pointer
|
private long windowPtr; // pointer to the current GLFW window
|
||||||
|
|
||||||
// To get window properties
|
// For application window properties
|
||||||
private final int[] winWidth = new int[1];
|
private final int[] winWidth = new int[1];
|
||||||
private final int[] winHeight = new int[1];
|
private final int[] winHeight = new int[1];
|
||||||
private final int[] fbWidth = new int[1];
|
private final int[] fbWidth = new int[1];
|
||||||
@@ -49,7 +49,7 @@ public final class ImGuiGlfwExample {
|
|||||||
private final ImGuiImplGl3 imGuiGl3 = new ImGuiImplGl3();
|
private final ImGuiImplGl3 imGuiGl3 = new ImGuiImplGl3();
|
||||||
private String glslVersion = null; // We can initialize our renderer with different versions of the GLSL
|
private String glslVersion = null; // We can initialize our renderer with different versions of the GLSL
|
||||||
|
|
||||||
// Ui to render
|
// User UI to render
|
||||||
private final ExampleUi exampleUi = new ExampleUi();
|
private final ExampleUi exampleUi = new ExampleUi();
|
||||||
|
|
||||||
public void run() throws Exception {
|
public void run() throws Exception {
|
||||||
@@ -61,7 +61,7 @@ public final class ImGuiGlfwExample {
|
|||||||
destroyGlfw();
|
destroyGlfw();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize GLFW + create OpenGL context.
|
// Initialize GLFW + create an OpenGL context.
|
||||||
// All code is mostly a copy-paste from the official LWJGL3 "Get Started": https://www.lwjgl.org/guide
|
// All code is mostly a copy-paste from the official LWJGL3 "Get Started": https://www.lwjgl.org/guide
|
||||||
private void initGlfw() {
|
private void initGlfw() {
|
||||||
// Setup an error callback. The default implementation
|
// Setup an error callback. The default implementation
|
||||||
@@ -79,7 +79,6 @@ public final class ImGuiGlfwExample {
|
|||||||
|
|
||||||
decideGlGlslVersions();
|
decideGlGlslVersions();
|
||||||
|
|
||||||
// Create the window
|
|
||||||
windowPtr = glfwCreateWindow(1280, 768, "Dear ImGui + GLFW + LWJGL Example", NULL, NULL);
|
windowPtr = glfwCreateWindow(1280, 768, "Dear ImGui + GLFW + LWJGL Example", NULL, NULL);
|
||||||
|
|
||||||
if (windowPtr == NULL) {
|
if (windowPtr == NULL) {
|
||||||
@@ -135,6 +134,7 @@ public final class ImGuiGlfwExample {
|
|||||||
// This line is critical for Dear ImGui to work.
|
// This line is critical for Dear ImGui to work.
|
||||||
ImGui.createContext();
|
ImGui.createContext();
|
||||||
|
|
||||||
|
// ------------------------------------------------------------
|
||||||
// Initialize ImGuiIO config
|
// Initialize ImGuiIO config
|
||||||
final ImGuiIO io = ImGui.getIO();
|
final ImGuiIO io = ImGui.getIO();
|
||||||
|
|
||||||
@@ -143,6 +143,7 @@ public final class ImGuiGlfwExample {
|
|||||||
io.setBackendFlags(ImGuiBackendFlags.HasMouseCursors); // Mouse cursors to display while resizing windows etc.
|
io.setBackendFlags(ImGuiBackendFlags.HasMouseCursors); // Mouse cursors to display while resizing windows etc.
|
||||||
io.setBackendPlatformName("imgui_java_impl_glfw");
|
io.setBackendPlatformName("imgui_java_impl_glfw");
|
||||||
|
|
||||||
|
// ------------------------------------------------------------
|
||||||
// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
|
// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
|
||||||
final int[] keyMap = new int[ImGuiKey.COUNT];
|
final int[] keyMap = new int[ImGuiKey.COUNT];
|
||||||
keyMap[ImGuiKey.Tab] = GLFW_KEY_TAB;
|
keyMap[ImGuiKey.Tab] = GLFW_KEY_TAB;
|
||||||
@@ -169,6 +170,7 @@ public final class ImGuiGlfwExample {
|
|||||||
keyMap[ImGuiKey.Z] = GLFW_KEY_Z;
|
keyMap[ImGuiKey.Z] = GLFW_KEY_Z;
|
||||||
io.setKeyMap(keyMap);
|
io.setKeyMap(keyMap);
|
||||||
|
|
||||||
|
// ------------------------------------------------------------
|
||||||
// Mouse cursors mapping
|
// Mouse cursors mapping
|
||||||
mouseCursors[ImGuiMouseCursor.Arrow] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
|
mouseCursors[ImGuiMouseCursor.Arrow] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
|
||||||
mouseCursors[ImGuiMouseCursor.TextInput] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR);
|
mouseCursors[ImGuiMouseCursor.TextInput] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR);
|
||||||
@@ -181,7 +183,7 @@ public final class ImGuiGlfwExample {
|
|||||||
mouseCursors[ImGuiMouseCursor.NotAllowed] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
|
mouseCursors[ImGuiMouseCursor.NotAllowed] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
|
||||||
|
|
||||||
// ------------------------------------------------------------
|
// ------------------------------------------------------------
|
||||||
// Here goes GLFW callbacks to update user input in Dear ImGui
|
// GLFW callbacks to handle user input
|
||||||
|
|
||||||
glfwSetKeyCallback(windowPtr, (w, key, scancode, action, mods) -> {
|
glfwSetKeyCallback(windowPtr, (w, key, scancode, action, mods) -> {
|
||||||
if (action == GLFW_PRESS) {
|
if (action == GLFW_PRESS) {
|
||||||
@@ -239,44 +241,38 @@ public final class ImGuiGlfwExample {
|
|||||||
|
|
||||||
// ------------------------------------------------------------
|
// ------------------------------------------------------------
|
||||||
// Fonts configuration
|
// Fonts configuration
|
||||||
|
// Read: https://raw.githubusercontent.com/ocornut/imgui/master/docs/FONTS.txt
|
||||||
// -------------------
|
|
||||||
// Fonts merge example
|
|
||||||
|
|
||||||
final ImFontAtlas fontAtlas = io.getFonts();
|
final ImFontAtlas fontAtlas = io.getFonts();
|
||||||
|
final ImFontConfig fontConfig = new ImFontConfig(); // Natively allocated object, should be explicitly destroyed
|
||||||
|
|
||||||
// First of all we add a default font, which is 'ProggyClean.ttf, 13px'
|
// Add a default font, which is 'ProggyClean.ttf, 13px'
|
||||||
fontAtlas.addFontDefault();
|
fontAtlas.addFontDefault();
|
||||||
|
|
||||||
final ImFontConfig fontConfig = new ImFontConfig(); // Keep in mind that creation of the ImFontConfig will allocate the native memory
|
// Fonts merge example
|
||||||
fontConfig.setMergeMode(true); // All fonts added while this mode is turned on will be merged with the previously added font
|
fontConfig.setMergeMode(true); // When enabled, all fonts added with this config would be merged with the previously added font
|
||||||
fontConfig.setPixelSnapH(true);
|
fontConfig.setPixelSnapH(true);
|
||||||
fontConfig.setGlyphRanges(fontAtlas.getGlyphRangesCyrillic()); // Additional glyphs could be added like this or in "addFontFrom*()" methods
|
|
||||||
|
|
||||||
// We merge font loaded from resources with the default one. Thus we will get an absent cyrillic glyphs
|
fontAtlas.addFontFromMemoryTTF(loadFromResources("basis33.ttf"), 16, fontConfig, fontAtlas.getGlyphRangesCyrillic());
|
||||||
fontAtlas.addFontFromMemoryTTF(loadFromResources("basis33.ttf"), 16, fontConfig);
|
|
||||||
|
|
||||||
// Disable merge mode and add all other fonts normally
|
|
||||||
fontConfig.setMergeMode(false);
|
fontConfig.setMergeMode(false);
|
||||||
fontConfig.setPixelSnapH(false);
|
fontConfig.setPixelSnapH(false);
|
||||||
|
|
||||||
// ------------------------------
|
|
||||||
// Fonts from file/memory example
|
// Fonts from file/memory example
|
||||||
|
// We can add new fonts from the file system
|
||||||
|
fontAtlas.addFontFromFileTTF("src/test/resources/Rubik-Regular.ttf", 13, fontConfig);
|
||||||
|
fontAtlas.addFontFromFileTTF("src/test/resources/Rubik-Regular.ttf", 16, fontConfig);
|
||||||
|
|
||||||
// fontConfig.setRasterizerMultiply(1.2f); // This will make fonts a bit more readable
|
// Or directly from the memory
|
||||||
|
|
||||||
// We can add new fonts directly from file
|
|
||||||
fontAtlas.addFontFromFileTTF("src/test/resources/DroidSans.ttf", 13, fontConfig);
|
|
||||||
fontAtlas.addFontFromFileTTF("src/test/resources/DroidSans.ttf", 14, fontConfig);
|
|
||||||
|
|
||||||
// Or directly from memory
|
|
||||||
fontConfig.setName("Roboto-Regular.ttf, 13px"); // This name will be displayed in Style Editor
|
fontConfig.setName("Roboto-Regular.ttf, 13px"); // This name will be displayed in Style Editor
|
||||||
fontAtlas.addFontFromMemoryTTF(loadFromResources("Roboto-Regular.ttf"), 13, fontConfig);
|
fontAtlas.addFontFromMemoryTTF(loadFromResources("Roboto-Regular.ttf"), 13, fontConfig);
|
||||||
fontConfig.setName("Roboto-Regular.ttf, 14px"); // We can apply a new config value every time we add a new font
|
fontConfig.setName("Roboto-Regular.ttf, 16px"); // We can apply a new config value every time we add a new font
|
||||||
fontAtlas.addFontFromMemoryTTF(loadFromResources("Roboto-Regular.ttf"), 14, fontConfig);
|
fontAtlas.addFontFromMemoryTTF(loadFromResources("Roboto-Regular.ttf"), 16, fontConfig);
|
||||||
|
|
||||||
fontConfig.destroy(); // After all fonts were added we don't need this config more
|
fontConfig.destroy(); // After all fonts were added we don't need this config more
|
||||||
|
|
||||||
|
// ------------------------------------------------------------
|
||||||
|
// Use freetype instead of stb_truetype to build a fonts texture
|
||||||
ImGuiFreeType.buildFontAtlas(fontAtlas, ImGuiFreeType.RasterizerFlags.LightHinting);
|
ImGuiFreeType.buildFontAtlas(fontAtlas, ImGuiFreeType.RasterizerFlags.LightHinting);
|
||||||
|
|
||||||
// Method initializes LWJGL3 renderer.
|
// Method initializes LWJGL3 renderer.
|
||||||
@@ -286,7 +282,7 @@ public final class ImGuiGlfwExample {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Main application loop
|
// Main application loop
|
||||||
private void loop() throws Exception {
|
private void loop() {
|
||||||
double time = 0; // to track our frame delta value
|
double time = 0; // to track our frame delta value
|
||||||
|
|
||||||
// Run the rendering loop until the user has attempted to close the window
|
// Run the rendering loop until the user has attempted to close the window
|
||||||
@@ -298,7 +294,7 @@ public final class ImGuiGlfwExample {
|
|||||||
|
|
||||||
startFrame((float) deltaTime);
|
startFrame((float) deltaTime);
|
||||||
|
|
||||||
// Any Dear ImGui code SHOULD go between NewFrame()/Render() methods
|
// Any Dear ImGui code SHOULD go between ImGui.newFrame()/ImGui.render() methods
|
||||||
ImGui.newFrame();
|
ImGui.newFrame();
|
||||||
exampleUi.render();
|
exampleUi.render();
|
||||||
ImGui.render();
|
ImGui.render();
|
||||||
@@ -312,32 +308,30 @@ public final class ImGuiGlfwExample {
|
|||||||
glClearColor(exampleUi.backgroundColor[0], exampleUi.backgroundColor[1], exampleUi.backgroundColor[2], 0.0f);
|
glClearColor(exampleUi.backgroundColor[0], exampleUi.backgroundColor[1], exampleUi.backgroundColor[2], 0.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
// Get window size properties and mouse position
|
// Get window properties and mouse position
|
||||||
glfwGetWindowSize(windowPtr, winWidth, winHeight);
|
glfwGetWindowSize(windowPtr, winWidth, winHeight);
|
||||||
glfwGetFramebufferSize(windowPtr, fbWidth, fbHeight);
|
glfwGetFramebufferSize(windowPtr, fbWidth, fbHeight);
|
||||||
glfwGetCursorPos(windowPtr, mousePosX, mousePosY);
|
glfwGetCursorPos(windowPtr, mousePosX, mousePosY);
|
||||||
|
|
||||||
// We SHOULD call those methods to update ImGui state for current frame
|
// We SHOULD call those methods to update Dear ImGui state for the current frame
|
||||||
final ImGuiIO io = ImGui.getIO();
|
final ImGuiIO io = ImGui.getIO();
|
||||||
io.setDisplaySize(winWidth[0], winHeight[0]);
|
io.setDisplaySize(winWidth[0], winHeight[0]);
|
||||||
io.setDisplayFramebufferScale((float) fbWidth[0] / winWidth[0], (float) fbHeight[0] / winHeight[0]);
|
io.setDisplayFramebufferScale((float) fbWidth[0] / winWidth[0], (float) fbHeight[0] / winHeight[0]);
|
||||||
io.setMousePos((float) mousePosX[0], (float) mousePosY[0]);
|
io.setMousePos((float) mousePosX[0], (float) mousePosY[0]);
|
||||||
io.setDeltaTime(deltaTime);
|
io.setDeltaTime(deltaTime);
|
||||||
|
|
||||||
// Update mouse cursor
|
// Update the mouse cursor
|
||||||
final int imguiCursor = ImGui.getMouseCursor();
|
final int imguiCursor = ImGui.getMouseCursor();
|
||||||
glfwSetCursor(windowPtr, mouseCursors[imguiCursor]);
|
glfwSetCursor(windowPtr, mouseCursors[imguiCursor]);
|
||||||
glfwSetInputMode(windowPtr, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
glfwSetInputMode(windowPtr, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void endFrame() {
|
private void endFrame() {
|
||||||
// After Dear ImGui prepared a draw data, we use it in LWJGL3 renderer.
|
// After Dear ImGui prepared a draw data, we use it in the LWJGL3 renderer.
|
||||||
// At that moment ImGui will be rendered to the current OpenGL context.
|
// At that moment ImGui will be rendered to the current OpenGL context.
|
||||||
imGuiGl3.render(ImGui.getDrawData());
|
imGuiGl3.render(ImGui.getDrawData());
|
||||||
|
|
||||||
glfwSwapBuffers(windowPtr); // swap the color buffers
|
glfwSwapBuffers(windowPtr);
|
||||||
|
|
||||||
// Poll for window events. The key callback above will only be invoked during this call.
|
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Reference in new issue
Block a user