|
| 1 | +// ImGui - standalone example application for GLFW + OpenGL 3, using programmable pipeline |
| 2 | +// If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp. |
| 3 | +// (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.) |
| 4 | +// (GL3W is a helper library to access OpenGL functions since there is no standard header to access modern OpenGL functions easily. Alternatives are GLEW, Glad, etc.) |
| 5 | + |
| 6 | +#include "imgui.h" |
| 7 | +#include "imgui_impl_gtk3.h" |
| 8 | +#include "imgui_impl_opengl3.h" |
| 9 | +#include <stdio.h> |
| 10 | + |
| 11 | +// About OpenGL function loaders: modern OpenGL doesn't have a standard header file and requires individual function pointers to be loaded manually. |
| 12 | +// Helper libraries are often used for this purpose! Here we are supporting a few common ones: gl3w, glew, glad. |
| 13 | +// You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own. |
| 14 | +#if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) |
| 15 | +#include <GL/gl3w.h> // Initialize with gl3wInit() |
| 16 | +#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) |
| 17 | +#include <GL/glew.h> // Initialize with glewInit() |
| 18 | +#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) |
| 19 | +#include <glad/glad.h> // Initialize with gladLoadGL() |
| 20 | +#else |
| 21 | +#include IMGUI_IMPL_OPENGL_LOADER_CUSTOM |
| 22 | +#endif |
| 23 | + |
| 24 | +#include <gtk/gtk.h> |
| 25 | + |
| 26 | +static gboolean render_area(GtkGLArea* area, GdkGLContext* context) |
| 27 | +{ |
| 28 | + static bool show_demo_window = true; |
| 29 | + static bool show_another_window = false; |
| 30 | + static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
| 31 | + |
| 32 | + // Start the Dear ImGui frame |
| 33 | + ImGui_ImplOpenGL3_NewFrame(); |
| 34 | + ImGui_ImplGtk3_NewFrame(); |
| 35 | + ImGui::NewFrame(); |
| 36 | + |
| 37 | + // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). |
| 38 | + if (show_demo_window) |
| 39 | + ImGui::ShowDemoWindow(&show_demo_window); |
| 40 | + |
| 41 | + // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window. |
| 42 | + { |
| 43 | + static float f = 0.0f; |
| 44 | + static int counter = 0; |
| 45 | + |
| 46 | + ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. |
| 47 | + |
| 48 | + ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) |
| 49 | + ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state |
| 50 | + ImGui::Checkbox("Another Window", &show_another_window); |
| 51 | + |
| 52 | + ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f |
| 53 | + ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color |
| 54 | + |
| 55 | + if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) |
| 56 | + counter++; |
| 57 | + ImGui::SameLine(); |
| 58 | + ImGui::Text("counter = %d", counter); |
| 59 | + |
| 60 | + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); |
| 61 | + ImGui::End(); |
| 62 | + } |
| 63 | + |
| 64 | + // 3. Show another simple window. |
| 65 | + if (show_another_window) |
| 66 | + { |
| 67 | + ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked) |
| 68 | + ImGui::Text("Hello from another window!"); |
| 69 | + if (ImGui::Button("Close Me")) |
| 70 | + show_another_window = false; |
| 71 | + ImGui::End(); |
| 72 | + } |
| 73 | + |
| 74 | + // Rendering |
| 75 | + ImGui::Render(); |
| 76 | + glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w); |
| 77 | + glClear(GL_COLOR_BUFFER_BIT); |
| 78 | + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); |
| 79 | + |
| 80 | + // Queue another rendering request. |
| 81 | + gtk_gl_area_queue_render(area); |
| 82 | + |
| 83 | + return TRUE; |
| 84 | +} |
| 85 | + |
| 86 | +static void realize_area(GtkGLArea* area) |
| 87 | +{ |
| 88 | + // Setup Dear ImGui binding |
| 89 | + IMGUI_CHECKVERSION(); |
| 90 | + ImGui::CreateContext(); |
| 91 | + ImGuiIO& io = ImGui::GetIO(); (void)io; |
| 92 | + //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls |
| 93 | + //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls |
| 94 | + |
| 95 | + ImGui_ImplGtk3_Init(GTK_WIDGET(area), true); |
| 96 | + ImGui_ImplOpenGL3_Init("#version 130"); |
| 97 | + |
| 98 | + // Setup style |
| 99 | + ImGui::StyleColorsDark(); |
| 100 | + //ImGui::StyleColorsClassic(); |
| 101 | + |
| 102 | + // Load Fonts |
| 103 | + // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them. |
| 104 | + // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. |
| 105 | + // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit). |
| 106 | + // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call. |
| 107 | + // - Read 'misc/fonts/README.txt' for more instructions and details. |
| 108 | + // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! |
| 109 | + //io.Fonts->AddFontDefault(); |
| 110 | + //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f); |
| 111 | + //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f); |
| 112 | + //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f); |
| 113 | + //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f); |
| 114 | + //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese()); |
| 115 | + //IM_ASSERT(font != NULL); |
| 116 | +} |
| 117 | + |
| 118 | +static void unrealize_area(GtkGLArea* area) |
| 119 | +{ |
| 120 | + gtk_gl_area_make_current(area); |
| 121 | + |
| 122 | + // Cleanup |
| 123 | + ImGui_ImplOpenGL3_Shutdown(); |
| 124 | + ImGui_ImplGtk3_Shutdown(); |
| 125 | + ImGui::DestroyContext(); |
| 126 | +} |
| 127 | + |
| 128 | +int main(int argc, char** argv) |
| 129 | +{ |
| 130 | + gtk_init(&argc, &argv); |
| 131 | + |
| 132 | + // Create window with graphics context |
| 133 | + GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 134 | + gtk_window_set_title(GTK_WINDOW(window), "Dear ImGui GTK+3 & OpenGL3 example"); |
| 135 | + g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL); |
| 136 | + gtk_window_resize(GTK_WINDOW(window), 1280, 720); |
| 137 | + |
| 138 | + GtkWidget* gl_area = gtk_gl_area_new(); |
| 139 | + gtk_gl_area_set_auto_render(GTK_GL_AREA(gl_area), TRUE); |
| 140 | + g_signal_connect(gl_area, "realize", G_CALLBACK(realize_area), NULL); |
| 141 | + g_signal_connect(gl_area, "unrealize", G_CALLBACK(unrealize_area), NULL); |
| 142 | + g_signal_connect(gl_area, "render", G_CALLBACK(render_area), NULL); |
| 143 | + gtk_container_add(GTK_CONTAINER(window), gl_area); |
| 144 | + |
| 145 | + gtk_widget_show_all(window); |
| 146 | + |
| 147 | + gtk_gl_area_make_current(GTK_GL_AREA(gl_area)); |
| 148 | + |
| 149 | + // Initialize OpenGL loader |
| 150 | +#if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) |
| 151 | + bool err = gl3wInit() != 0; |
| 152 | +#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) |
| 153 | + bool err = glewInit() != GLEW_OK; |
| 154 | +#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) |
| 155 | + bool err = gladLoadGL() != 0; |
| 156 | +#endif |
| 157 | + if (err) |
| 158 | + { |
| 159 | + fprintf(stderr, "Failed to initialize OpenGL loader!\n"); |
| 160 | + return 1; |
| 161 | + } |
| 162 | + |
| 163 | + // Main loop |
| 164 | + gtk_main(); |
| 165 | + |
| 166 | + return 0; |
| 167 | +} |
0 commit comments