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
5 #include "imgui.h"
6 #include "imgui_impl_glfw.h"
7 #include "imgui_impl_opengl3.h"
8 #include <stdio.h>
9
10 // About OpenGL function loaders: modern OpenGL doesn't have a standard header file and requires individual function pointers to be loaded manually.
11 // Helper libraries are often used for this purpose! Here we are supporting a few common ones: gl3w, glew, glad.
12 // You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own.
13 #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
14 #include <GL/gl3w.h> // Initialize with gl3wInit()
15 #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
16 #include <GL/glew.h> // Initialize with glewInit()
17 #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
18 #include <glad/glad.h> // Initialize with gladLoadGL()
19 #else
20 #include IMGUI_IMPL_OPENGL_LOADER_CUSTOM
21 #endif
22
23 #include <GLFW/glfw3.h> // Include glfw3.h after our OpenGL definitions
24
glfw_error_callback(int error,const char * description)25 static void glfw_error_callback(int error, const char* description)
26 {
27 fprintf(stderr, "Glfw Error %d: %s\n", error, description);
28 }
29
main(int,char **)30 int main(int, char**)
31 {
32 // Setup window
33 glfwSetErrorCallback(glfw_error_callback);
34 if (!glfwInit())
35 return 1;
36
37 // Decide GL+GLSL versions
38 #if __APPLE__
39 // GL 3.2 + GLSL 150
40 const char* glsl_version = "#version 150";
41 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
42 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
43 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
44 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
45 #else
46 // GL 3.0 + GLSL 130
47 const char* glsl_version = "#version 130";
48 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
49 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
50 //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
51 //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
52 #endif
53
54 // Create window with graphics context
55 GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+OpenGL3 example", NULL, NULL);
56 if (window == NULL)
57 return 1;
58 glfwMakeContextCurrent(window);
59 glfwSwapInterval(1); // Enable vsync
60
61 // Initialize OpenGL loader
62 #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
63 bool err = gl3wInit() != 0;
64 #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
65 bool err = glewInit() != GLEW_OK;
66 #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
67 bool err = gladLoadGL() != 0;
68 #endif
69 if (err)
70 {
71 fprintf(stderr, "Failed to initialize OpenGL loader!\n");
72 return 1;
73 }
74
75 // Setup Dear ImGui binding
76 IMGUI_CHECKVERSION();
77 ImGui::CreateContext();
78 ImGuiIO& io = ImGui::GetIO(); (void)io;
79 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
80 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
81
82 ImGui_ImplGlfw_InitForOpenGL(window, true);
83 ImGui_ImplOpenGL3_Init(glsl_version);
84
85 // Setup style
86 ImGui::StyleColorsDark();
87 //ImGui::StyleColorsClassic();
88
89 // Load Fonts
90 // - 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.
91 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
92 // - 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).
93 // - 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.
94 // - Read 'misc/fonts/README.txt' for more instructions and details.
95 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
96 //io.Fonts->AddFontDefault();
97 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
98 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
99 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
100 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
101 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
102 //IM_ASSERT(font != NULL);
103
104 bool show_demo_window = true;
105 bool show_another_window = false;
106 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
107
108 // Main loop
109 while (!glfwWindowShouldClose(window))
110 {
111 // Poll and handle events (inputs, window resize, etc.)
112 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
113 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
114 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
115 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
116 glfwPollEvents();
117
118 // Start the Dear ImGui frame
119 ImGui_ImplOpenGL3_NewFrame();
120 ImGui_ImplGlfw_NewFrame();
121 ImGui::NewFrame();
122
123 // 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!).
124 if (show_demo_window)
125 ImGui::ShowDemoWindow(&show_demo_window);
126
127 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
128 {
129 static float f = 0.0f;
130 static int counter = 0;
131
132 ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
133
134 ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
135 ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
136 ImGui::Checkbox("Another Window", &show_another_window);
137
138 ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
139 ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
140
141 if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
142 counter++;
143 ImGui::SameLine();
144 ImGui::Text("counter = %d", counter);
145
146 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
147 ImGui::End();
148 }
149
150 // 3. Show another simple window.
151 if (show_another_window)
152 {
153 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)
154 ImGui::Text("Hello from another window!");
155 if (ImGui::Button("Close Me"))
156 show_another_window = false;
157 ImGui::End();
158 }
159
160 // Rendering
161 ImGui::Render();
162 int display_w, display_h;
163 glfwMakeContextCurrent(window);
164 glfwGetFramebufferSize(window, &display_w, &display_h);
165 glViewport(0, 0, display_w, display_h);
166 glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
167 glClear(GL_COLOR_BUFFER_BIT);
168 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
169
170 glfwMakeContextCurrent(window);
171 glfwSwapBuffers(window);
172 }
173
174 // Cleanup
175 ImGui_ImplOpenGL3_Shutdown();
176 ImGui_ImplGlfw_Shutdown();
177 ImGui::DestroyContext();
178
179 glfwDestroyWindow(window);
180 glfwTerminate();
181
182 return 0;
183 }
184