7.5 KB、166行 生 Blame 履歴
1// Dear ImGui: standalone example application for GLUT/FreeGLUT + OpenGL2, using legacy fixed pipeline
2
3// Learn about Dear ImGui:
4// - FAQ https://dearimgui.com/faq
5// - Getting Started https://dearimgui.com/getting-started
6// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
7// - Introduction, links and more at the top of imgui.cpp
8
9// !!! GLUT/FreeGLUT IS OBSOLETE PREHISTORIC SOFTWARE. Using GLUT is not recommended unless you really miss the 90's. !!!
10// !!! If someone or something is teaching you GLUT today, you are being abused. Please show some resistance. !!!
11// !!! Nowadays, prefer using GLFW or SDL instead!
12
13// On Windows, you can install Freeglut using vcpkg:
14// git clone https://github.com/Microsoft/vcpkg
15// cd vcpkg
16// bootstrap - vcpkg.bat
17// vcpkg install freeglut --triplet=x86-windows ; for win32
18// vcpkg install freeglut --triplet=x64-windows ; for win64
19// vcpkg integrate install ; register include and libs in Visual Studio
20
21#include "imgui.h"
22#include "imgui_impl_glut.h"
23#include "imgui_impl_opengl2.h"
24#define GL_SILENCE_DEPRECATION
25#ifdef __APPLE__
26#include <GLUT/glut.h>
27#else
28#include <GL/freeglut.h>
29#endif
30
31#ifdef _MSC_VER
32#pragma warning (disable: 4505) // unreferenced local function has been removed
33#endif
34
35// Forward declarations of helper functions
36void MainLoopStep();
37
38// Our state
39static bool show_demo_window = true;
40static bool show_another_window = false;
41static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
42
43int main(int argc, char** argv)
44{
45 // Create GLUT window
46 glutInit(&argc, argv);
47#ifdef __FREEGLUT_EXT_H__
48 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
49#endif
50 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_MULTISAMPLE);
51 glutInitWindowSize(1280, 800);
52 glutCreateWindow("Dear ImGui GLUT+OpenGL2 Example");
53
54 // Setup GLUT display function
55 // We will also call ImGui_ImplGLUT_InstallFuncs() to get all the other functions installed for us,
56 // otherwise it is possible to install our own functions and call the imgui_impl_glut.h functions ourselves.
57 glutDisplayFunc(MainLoopStep);
58
59 // Setup Dear ImGui context
60 IMGUI_CHECKVERSION();
61 ImGui::CreateContext();
62 ImGuiIO& io = ImGui::GetIO(); (void)io;
63 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
64 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
65
66 // Setup Dear ImGui style
67 ImGui::StyleColorsDark();
68 //ImGui::StyleColorsLight();
69
70 // Setup Platform/Renderer backends
71 // FIXME: Consider reworking this example to install our own GLUT funcs + forward calls ImGui_ImplGLUT_XXX ones, instead of using ImGui_ImplGLUT_InstallFuncs().
72 ImGui_ImplGLUT_Init();
73 ImGui_ImplOpenGL2_Init();
74
75 // Install GLUT handlers (glutReshapeFunc(), glutMotionFunc(), glutPassiveMotionFunc(), glutMouseFunc(), glutKeyboardFunc() etc.)
76 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
77 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
78 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
79 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
80 ImGui_ImplGLUT_InstallFuncs();
81
82 // Load Fonts
83 // - If fonts are not explicitly loaded, Dear ImGui will select an embedded font: either AddFontDefaultVector() or AddFontDefaultBitmap().
84 // This selection is based on (style.FontSizeBase * style.FontScaleMain * style.FontScaleDpi) reaching a small threshold.
85 // - You can load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
86 // - If a file cannot be loaded, AddFont functions will return a nullptr. Please handle those errors in your code (e.g. use an assertion, display an error and quit).
87 // - Read 'docs/FONTS.md' for more instructions and details.
88 // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use FreeType for higher quality font rendering.
89 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
90 //style.FontSizeBase = 20.0f;
91 //io.Fonts->AddFontDefaultVector();
92 //io.Fonts->AddFontDefaultBitmap();
93 //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
94 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
95 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
96 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
97 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
98 //IM_ASSERT(font != nullptr);
99
100 // Main loop
101 glutMainLoop();
102
103 // Cleanup
104 ImGui_ImplOpenGL2_Shutdown();
105 ImGui_ImplGLUT_Shutdown();
106 ImGui::DestroyContext();
107
108 return 0;
109}
110
111void MainLoopStep()
112{
113 // Start the Dear ImGui frame
114 ImGui_ImplOpenGL2_NewFrame();
115 ImGui_ImplGLUT_NewFrame();
116 ImGui::NewFrame();
117 ImGuiIO& io = ImGui::GetIO();
118
119 // 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!).
120 if (show_demo_window)
121 ImGui::ShowDemoWindow(&show_demo_window);
122
123 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
124 {
125 static float f = 0.0f;
126 static int counter = 0;
127
128 ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
129
130 ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
131 ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
132 ImGui::Checkbox("Another Window", &show_another_window);
133
134 ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
135 ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
136
137 if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
138 counter++;
139 ImGui::SameLine();
140 ImGui::Text("counter = %d", counter);
141
142 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
143 ImGui::End();
144 }
145
146 // 3. Show another simple window.
147 if (show_another_window)
148 {
149 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)
150 ImGui::Text("Hello from another window!");
151 if (ImGui::Button("Close Me"))
152 show_another_window = false;
153 ImGui::End();
154 }
155
156 // Rendering
157 ImGui::Render();
158 glViewport(0, 0, (GLsizei)io.DisplaySize.x, (GLsizei)io.DisplaySize.y);
159 glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
160 glClear(GL_COLOR_BUFFER_BIT);
161 //glUseProgram(0); // You may want this if using this code in an OpenGL 3+ context where shaders may be bound, but prefer using the GL3+ code.
162 ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
163
164 glutSwapBuffers();
165 glutPostRedisplay();
166}