Skip to content

Commit d7915a2

Browse files
Examples: add Gtk3/OpenGL3 example
1 parent 1afd29d commit d7915a2

5 files changed

Lines changed: 515 additions & 0 deletions

File tree

examples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ example_glfw_opengl2/example_glfw_opengl2
3333
example_glfw_opengl3/example_glfw_opengl3
3434
example_sdl_opengl2/example_sdl_opengl2
3535
example_sdl_opengl3/example_sdl_opengl3
36+
example_gtk3_opengl3/example_gtk3_opengl3
3637

3738
## Dear ImGui Ini files
3839
imgui.ini
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#
2+
# You will need GTK+ 3.0 :
3+
# Linux:
4+
# apt-get install libgtk-3-dev
5+
#
6+
7+
#CXX = g++
8+
#CXX = clang++
9+
10+
EXE = example_gtk3_opengl3
11+
SOURCES = main.cpp
12+
SOURCES += ../imgui_impl_gtk3.cpp ../imgui_impl_opengl3.cpp
13+
SOURCES += ../../imgui.cpp ../../imgui_demo.cpp ../../imgui_draw.cpp ../../imgui_widgets.cpp
14+
SOURCES += ../libs/gl3w/GL/gl3w.c
15+
OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
16+
17+
UNAME_S := $(shell uname -s)
18+
19+
20+
ifeq ($(UNAME_S), Linux) #LINUX
21+
ECHO_MESSAGE = "Linux"
22+
LIBS = -lGL `pkg-config --libs gtk+-3.0 epoxy` -ldl
23+
24+
CXXFLAGS = -I../ -I../../ -I../libs/gl3w `pkg-config --cflags gtk+-3.0`
25+
CXXFLAGS += -Wall -Wformat -Wno-deprecated-declarations -Wno-parentheses
26+
CFLAGS = $(CXXFLAGS)
27+
endif
28+
29+
%.o:%.cpp
30+
$(CXX) $(CXXFLAGS) -c -o $@ $<
31+
32+
%.o:../%.cpp
33+
$(CXX) $(CXXFLAGS) -c -o $@ $<
34+
35+
%.o:../../%.cpp
36+
$(CXX) $(CXXFLAGS) -c -o $@ $<
37+
38+
%.o:../libs/gl3w/GL/%.c
39+
$(CC) $(CFLAGS) -c -o $@ $<
40+
41+
all: $(EXE)
42+
@echo Build complete for $(ECHO_MESSAGE)
43+
44+
$(EXE): $(OBJS)
45+
$(CXX) -o $@ $^ $(CXXFLAGS) $(LIBS)
46+
47+
clean:
48+
rm -f $(EXE) $(OBJS)
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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

Comments
 (0)