1 // ImGui - standalone example application for FreeGLUT + OpenGL2, using legacy fixed pipeline
2 // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
3 // (Using GLUT or FreeGLUT is not recommended unless you really miss the 90's)
4 
5 #include "imgui.h"
6 #include "../imgui_impl_freeglut.h"
7 #include "../imgui_impl_opengl2.h"
8 #include <GL/freeglut.h>
9 
10 #ifdef _MSC_VER
11 #pragma warning (disable: 4505) // unreferenced local function has been removed
12 #endif
13 
14 static bool show_demo_window = true;
15 static bool show_another_window = false;
16 static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
17 
my_display_code()18 void my_display_code()
19 {
20     // 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!).
21     if (show_demo_window)
22         ImGui::ShowDemoWindow(&show_demo_window);
23 
24     // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
25     {
26         static float f = 0.0f;
27         static int counter = 0;
28 
29         ImGui::Begin("Hello, world!");                          // Create a window called "Hello, world!" and append into it.
30 
31         ImGui::Text("This is some useful text.");               // Display some text (you can use a format strings too)
32         ImGui::Checkbox("Demo Window", &show_demo_window);      // Edit bools storing our window open/close state
33         ImGui::Checkbox("Another Window", &show_another_window);
34 
35         ImGui::SliderFloat("float", &f, 0.0f, 1.0f);            // Edit 1 float using a slider from 0.0f to 1.0f
36         ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
37 
38         if (ImGui::Button("Button"))                            // Buttons return true when clicked (most widgets return true when edited/activated)
39             counter++;
40         ImGui::SameLine();
41         ImGui::Text("counter = %d", counter);
42 
43         ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
44         ImGui::End();
45     }
46 
47     // 3. Show another simple window.
48     if (show_another_window)
49     {
50         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)
51         ImGui::Text("Hello from another window!");
52         if (ImGui::Button("Close Me"))
53             show_another_window = false;
54         ImGui::End();
55     }
56 }
57 
glut_display_func()58 void glut_display_func()
59 {
60     // Start the ImGui frame
61     ImGui_ImplOpenGL2_NewFrame();
62     ImGui_ImplFreeGLUT_NewFrame();
63 
64     my_display_code();
65 
66     // Rendering
67     ImGui::Render();
68     ImGuiIO& io = ImGui::GetIO();
69     glViewport(0, 0, (GLsizei)io.DisplaySize.x, (GLsizei)io.DisplaySize.y);
70     glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
71     glClear(GL_COLOR_BUFFER_BIT);
72     //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.
73     ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
74 
75     glutSwapBuffers();
76     glutPostRedisplay();
77 }
78 
79 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
80 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
81 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
82 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
83 
main(int argc,char ** argv)84 int main(int argc, char** argv)
85 {
86     // Create GLUT window
87     glutInit(&argc, argv);
88     glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
89     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_MULTISAMPLE);
90     glutInitWindowSize(1280, 720);
91     glutCreateWindow("Dear ImGui FreeGLUT+OpenGL2 Example");
92 
93     // Setup GLUT display function
94     // We will also call ImGui_ImplFreeGLUT_InstallFuncs() to get all the other functions installed for us,
95     // otherwise it is possible to install our own functions and call the imgui_impl_freeglut.h functions ourselves.
96     glutDisplayFunc(glut_display_func);
97 
98     // Setup ImGui binding
99     ImGui::CreateContext();
100     ImGuiIO& io = ImGui::GetIO(); (void)io;
101     //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;  // Enable Keyboard Controls
102 
103     ImGui_ImplFreeGLUT_Init();
104     ImGui_ImplFreeGLUT_InstallFuncs();
105     ImGui_ImplOpenGL2_Init();
106 
107     // Setup style
108     ImGui::StyleColorsDark();
109     //ImGui::StyleColorsClassic();
110 
111     // Load Fonts
112     // - 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.
113     // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
114     // - 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).
115     // - 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.
116     // - Read 'misc/fonts/README.txt' for more instructions and details.
117     // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
118     //io.Fonts->AddFontDefault();
119     //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
120     //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
121     //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
122     //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
123     //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
124     //IM_ASSERT(font != NULL);
125 
126     glutMainLoop();
127 
128     // Cleanup
129     ImGui_ImplOpenGL2_Shutdown();
130     ImGui_ImplFreeGLUT_Shutdown();
131     ImGui::DestroyContext();
132 
133     return 0;
134 }
135