Getting Started with ImGui: A Beginner’s Tutorial

ImGui (short for “Immediate Mode GUI”) is a library for creating graphical user interfaces (GUIs) in C++. It is designed to be easy to use and integrate into existing projects, and is particularly well-suited for creating in-game tools and debugging windows.

Here is a simple tutorial for getting started with ImGui:

First, you’ll need to download and install the ImGui library. You can find the latest version on the ImGui GitHub page (https://github.com/ocornut/imgui).

Next, you’ll need to include the ImGui headers in your project. You can do this by adding the following line to the top of your main source file:

#include "imgui.h"

In your main loop, you will need to call the ImGui functions to create and update your GUI elements. At a minimum, you’ll need to call the following three functions:

ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
ImGui::Render();

These functions handle input, update the state of the GUI, and render it to the screen.

To create a simple window, you can use the ImGui::Begin and ImGui::End functions like so:

ImGui::Begin("My Window");
ImGui::Text("Hello, world!");
ImGui::End();

You can also add buttons, checkboxes, and other elements to your GUI using the various ImGui functions. For example:

ImGui::Begin("My Window");
ImGui::Text("Hello, world!");
ImGui::Checkbox("Toggle something", &some_variable);
ImGui::Button("Press me");
ImGui::End();

This will add a checkbox and a button to the “My Window” window.

you’ll need to handle any input received from the GUI. You can do this using the ImGui::IsItemClicked() function and the various ImGui::Get* functions to retrieve the current state of the GUI elements.

The complete code would look something like this.

#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <GLFW/glfw3.h>

int main(int argc, char** argv) {
    // Initialize GLFW
    if (!glfwInit())
        return 1;

    // Create a GLFW window
    GLFWwindow* window = glfwCreateWindow(1280, 720, "ImGui Example", NULL, NULL);
    if (!window) {
        glfwTerminate();
        return 1;
    }
    glfwMakeContextCurrent(window);

    // Initialize ImGui
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO(); (void)io;
    ImGui::StyleColorsDark();
    ImGui_ImplGlfw_InitForOpenGL(window, true);
    ImGui_ImplOpenGL3_Init("#version 130");

    // Main loop
    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();

        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        ImGui::NewFrame();

        // Your ImGui code here
        ImGui::Begin("Hello, world!");
        ImGui::Text("This is a simple ImGui application.");
        ImGui::End();

        ImGui::Render();
        int display_w, display_h;
        glfwGetFramebufferSize(window, &display_w, &display_h);
        glViewport(0, 0, display_w, display_h);
        glClearColor(0.45f, 0.55f, 0.60f, 1.00f);
        glClear(GL_COLOR_BUFFER_BIT);
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

        glfwSwapBuffers(window);
    }

    // Cleanup
    ImGui_ImplOpenGL3_Shutdown();
    ImGui_ImplGlfw_Shutdown();
    ImGui::DestroyContext();

    glfwDestroyWindow(window);
    glfwTerminate();

    return 0;
}

There are many more features and functions available in the library, so be sure to check out the documentation on the ImGui GitHub page for more information.

Leave a Comment