Photon Editor Library 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
imgui_main.h
Go to the documentation of this file.
1#pragma once
2
3/******************************************************************************
4 Note:
5
6 Code that follows this comment section is copied from imgui's "./examples/"
7 directory, with `main()` function renamed to `imgui_main()` to avoid name
8 collision with the actual `main()` in global namespace.
9
10******************************************************************************/
11
12// Dear ImGui: standalone example application for GLFW + OpenGL 3, using programmable pipeline
13// (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan/Metal graphics context creation, etc.)
14// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
15// Read online: https://github.com/ocornut/imgui/tree/master/docs
16
17#include "imgui.h"
18#include "imgui_impl_glfw.h"
19#include "imgui_impl_opengl3.h"
20#include <stdio.h>
21#if defined(IMGUI_IMPL_OPENGL_ES2)
22#include <GLES2/gl2.h>
23#endif
24#include <GLFW/glfw3.h> // Will drag system OpenGL headers
25
26// [Win32] Our example includes a copy of glfw3.lib pre-compiled with VS2010 to maximize ease of testing and compatibility with old VS compilers.
27// To link with VS2010-era libraries, VS2015+ requires linking with legacy_stdio_definitions.lib, which we do using this pragma.
28// Your own project should not be affected, as you are likely to link with a newer binary of GLFW that is adequate for your version of Visual Studio.
29#if defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS)
30#pragma comment(lib, "legacy_stdio_definitions")
31#endif
32
33static void glfw_error_callback(int error, const char* description)
34{
35 fprintf(stderr, "Glfw Error %d: %s\n", error, description);
36}
37
38int imgui_main(int argc, char* argv[])
39{
40 // Setup window
41 glfwSetErrorCallback(glfw_error_callback);
42 if (!glfwInit())
43 return 1;
44
45 // Decide GL+GLSL versions
46#if defined(IMGUI_IMPL_OPENGL_ES2)
47 // GL ES 2.0 + GLSL 100
48 const char* glsl_version = "#version 100";
49 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
50 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
51 glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
52#elif defined(__APPLE__)
53 // GL 3.2 + GLSL 150
54 const char* glsl_version = "#version 150";
55 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
56 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
57 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
58 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
59#else
60 // GL 3.0 + GLSL 130
61 const char* glsl_version = "#version 130";
62 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
63 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
64 //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
65 //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
66#endif
67
68 // Create window with graphics context
69 GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+OpenGL3 example", NULL, NULL);
70 if (window == NULL)
71 return 1;
72 glfwMakeContextCurrent(window);
73 glfwSwapInterval(1); // Enable vsync
74
75 // Setup Dear ImGui context
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 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
82 io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
83 //io.ConfigViewportsNoAutoMerge = true;
84 //io.ConfigViewportsNoTaskBarIcon = true;
85
86 // Setup Dear ImGui style
87 ImGui::StyleColorsDark();
88 //ImGui::StyleColorsLight();
89
90 // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
91 ImGuiStyle& style = ImGui::GetStyle();
92 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
93 {
94 style.WindowRounding = 0.0f;
95 style.Colors[ImGuiCol_WindowBg].w = 1.0f;
96 }
97
98 // Setup Platform/Renderer backends
99 ImGui_ImplGlfw_InitForOpenGL(window, true);
100 ImGui_ImplOpenGL3_Init(glsl_version);
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 'docs/FONTS.md' 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 // Our state
118 bool show_demo_window = true;
119 bool show_another_window = false;
120 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
121
122 // Main loop
123 while (!glfwWindowShouldClose(window))
124 {
125 // Poll and handle events (inputs, window resize, etc.)
126 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
127 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
128 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
129 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
130 glfwPollEvents();
131
132 // Start the Dear ImGui frame
133 ImGui_ImplOpenGL3_NewFrame();
134 ImGui_ImplGlfw_NewFrame();
135 ImGui::NewFrame();
136
137 // 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!).
138 if (show_demo_window)
139 ImGui::ShowDemoWindow(&show_demo_window);
140
141 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
142 {
143 static float f = 0.0f;
144 static int counter = 0;
145
146 ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
147
148 ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
149 ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
150 ImGui::Checkbox("Another Window", &show_another_window);
151
152 ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
153 ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
154
155 if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
156 counter++;
157 ImGui::SameLine();
158 ImGui::Text("counter = %d", counter);
159
160 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
161 ImGui::End();
162 }
163
164 // 3. Show another simple window.
165 if (show_another_window)
166 {
167 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)
168 ImGui::Text("Hello from another window!");
169 if (ImGui::Button("Close Me"))
170 show_another_window = false;
171 ImGui::End();
172 }
173
174 // Rendering
175 ImGui::Render();
176 int display_w, display_h;
177 glfwGetFramebufferSize(window, &display_w, &display_h);
178 glViewport(0, 0, display_w, display_h);
179 glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
180 glClear(GL_COLOR_BUFFER_BIT);
181 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
182
183 // Update and Render additional Platform Windows
184 // (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere.
185 // For this specific demo app we could also call glfwMakeContextCurrent(window) directly)
186 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
187 {
188 GLFWwindow* backup_current_context = glfwGetCurrentContext();
189 ImGui::UpdatePlatformWindows();
190 ImGui::RenderPlatformWindowsDefault();
191 glfwMakeContextCurrent(backup_current_context);
192 }
193
194 glfwSwapBuffers(window);
195 }
196
197 // Cleanup
198 ImGui_ImplOpenGL3_Shutdown();
199 ImGui_ImplGlfw_Shutdown();
200 ImGui::DestroyContext();
201
202 glfwDestroyWindow(window);
203 glfwTerminate();
204
205 return 0;
206}
int imgui_main(int argc, char *argv[])
Definition imgui_main.h:38