1 // ImGui - standalone example application for SDL2 + OpenGL
2 // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
3 // (SDL is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.)
4 
5 // **DO NOT USE THIS CODE IF YOUR CODE/ENGINE IS USING MODERN OPENGL (SHADERS, VBO, VAO, etc.)**
6 // **Prefer using the code in the example_sdl_opengl3/ folder**
7 // See imgui_impl_sdl.cpp for details.
8 
9 #include "imgui.h"
10 #include "imgui_impl_sdl.h"
11 #include "imgui_impl_opengl2.h"
12 #include <stdio.h>
13 #include <SDL.h>
14 #include <SDL_opengl.h>
15 
main(int,char **)16 int main(int, char**)
17 {
18     // Setup SDL
19     if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0)
20     {
21         printf("Error: %s\n", SDL_GetError());
22         return -1;
23     }
24 
25     // Setup window
26     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
27     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
28     SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
29     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
30     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
31     SDL_DisplayMode current;
32     SDL_GetCurrentDisplayMode(0, &current);
33     SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
34     SDL_GLContext gl_context = SDL_GL_CreateContext(window);
35     SDL_GL_SetSwapInterval(1); // Enable vsync
36 
37     // Setup Dear ImGui binding
38     IMGUI_CHECKVERSION();
39     ImGui::CreateContext();
40     ImGuiIO& io = ImGui::GetIO(); (void)io;
41     //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;  // Enable Keyboard Controls
42 
43     ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
44     ImGui_ImplOpenGL2_Init();
45 
46     // Setup style
47     ImGui::StyleColorsDark();
48     //ImGui::StyleColorsClassic();
49 
50     // Load Fonts
51     // - 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.
52     // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
53     // - 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).
54     // - 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.
55     // - Read 'misc/fonts/README.txt' for more instructions and details.
56     // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
57     //io.Fonts->AddFontDefault();
58     //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
59     //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
60     //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
61     //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
62     //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
63     //IM_ASSERT(font != NULL);
64 
65     bool show_demo_window = true;
66     bool show_another_window = false;
67     ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
68 
69     // Main loop
70     bool done = false;
71     while (!done)
72     {
73         // Poll and handle events (inputs, window resize, etc.)
74         // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
75         // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
76         // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
77         // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
78         SDL_Event event;
79         while (SDL_PollEvent(&event))
80         {
81             ImGui_ImplSDL2_ProcessEvent(&event);
82             if (event.type == SDL_QUIT)
83                 done = true;
84         }
85 
86         // Start the Dear ImGui frame
87         ImGui_ImplOpenGL2_NewFrame();
88         ImGui_ImplSDL2_NewFrame(window);
89         ImGui::NewFrame();
90 
91         // 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!).
92         if (show_demo_window)
93             ImGui::ShowDemoWindow(&show_demo_window);
94 
95         // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
96         {
97             static float f = 0.0f;
98             static int counter = 0;
99 
100             ImGui::Begin("Hello, world!");                          // Create a window called "Hello, world!" and append into it.
101 
102             ImGui::Text("This is some useful text.");               // Display some text (you can use a format strings too)
103             ImGui::Checkbox("Demo Window", &show_demo_window);      // Edit bools storing our window open/close state
104             ImGui::Checkbox("Another Window", &show_another_window);
105 
106             ImGui::SliderFloat("float", &f, 0.0f, 1.0f);            // Edit 1 float using a slider from 0.0f to 1.0f
107             ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
108 
109             if (ImGui::Button("Button"))                            // Buttons return true when clicked (most widgets return true when edited/activated)
110                 counter++;
111             ImGui::SameLine();
112             ImGui::Text("counter = %d", counter);
113 
114             ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
115             ImGui::End();
116         }
117 
118         // 3. Show another simple window.
119         if (show_another_window)
120         {
121             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)
122             ImGui::Text("Hello from another window!");
123             if (ImGui::Button("Close Me"))
124                 show_another_window = false;
125             ImGui::End();
126         }
127 
128         // Rendering
129         ImGui::Render();
130         glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
131         glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
132         glClear(GL_COLOR_BUFFER_BIT);
133         //glUseProgram(0); // You may want this if using this code in an OpenGL 3+ context where shaders may be bound
134         ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
135         SDL_GL_SwapWindow(window);
136     }
137 
138     // Cleanup
139     ImGui_ImplOpenGL2_Shutdown();
140     ImGui_ImplSDL2_Shutdown();
141     ImGui::DestroyContext();
142 
143     SDL_GL_DeleteContext(gl_context);
144     SDL_DestroyWindow(window);
145     SDL_Quit();
146 
147     return 0;
148 }
149