source: opengl-game/imgui_example.cpp@ c1ca5b5

feature/imgui-sdl points-test
Last change on this file since c1ca5b5 was c1ca5b5, checked in by Dmitry Portnoy <dmp1488@…>, 7 years ago

Show the example ImGui gui in the OpenGL game.

  • Property mode set to 100644
File size: 6.1 KB
Line 
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/imgui.h"
6#include "imgui_impl_glfw_gl3.h"
7#include <stdio.h>
8
9#include <GL/glew.h>
10#include <GLFW/glfw3.h>
11
12static void glfw_error_callback(int error, const char* description)
13{
14 fprintf(stderr, "Error %d: %s\n", error, description);
15}
16
17int mainimgui(int, char**)
18{
19 // Setup window
20 glfwSetErrorCallback(glfw_error_callback);
21 if (!glfwInit())
22 return 1;
23 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
24 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
25 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
26#if __APPLE__
27 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
28#endif
29 GLFWwindow* window = glfwCreateWindow(1280, 720, "ImGui GLFW+OpenGL3 example", NULL, NULL);
30 glfwMakeContextCurrent(window);
31 glfwSwapInterval(1); // Enable vsync
32
33 glewExperimental = GL_TRUE;
34 glewInit();
35
36 // Setup Dear ImGui binding
37 IMGUI_CHECKVERSION();
38 ImGui::CreateContext();
39 ImGuiIO& io = ImGui::GetIO(); (void)io;
40 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
41 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
42 ImGui_ImplGlfwGL3_Init(window, true);
43
44 // Setup style
45 ImGui::StyleColorsDark();
46 //ImGui::StyleColorsClassic();
47
48 // Load Fonts
49 // - 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.
50 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
51 // - 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).
52 // - 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.
53 // - Read 'misc/fonts/README.txt' for more instructions and details.
54 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
55 //io.Fonts->AddFontDefault();
56 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
57 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
58 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
59 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
60 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
61 //IM_ASSERT(font != NULL);
62
63 bool show_demo_window = true;
64 bool show_another_window = false;
65 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
66
67 // Main loop
68 while (!glfwWindowShouldClose(window))
69 {
70 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
71 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
72 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
73 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
74 glfwPollEvents();
75 ImGui_ImplGlfwGL3_NewFrame();
76
77 // 1. Show a simple window.
78 // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug".
79 {
80 static float f = 0.0f;
81 static int counter = 0;
82 ImGui::Text("Hello, world!"); // Display some text (you can use a format string too)
83 ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
84 ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
85
86 ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our windows open/close state
87 ImGui::Checkbox("Another Window", &show_another_window);
88
89 if (ImGui::Button("Button")) // Buttons return true when clicked (NB: most widgets return true when edited/activated)
90 counter++;
91 ImGui::SameLine();
92 ImGui::Text("counter = %d", counter);
93
94 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
95 }
96
97 // 2. Show another simple window. In most cases you will use an explicit Begin/End pair to name your windows.
98 if (show_another_window)
99 {
100 ImGui::Begin("Another Window", &show_another_window);
101 ImGui::Text("Hello from another window!");
102 if (ImGui::Button("Close Me"))
103 show_another_window = false;
104 ImGui::End();
105 }
106
107 // 3. Show the ImGui demo window. Most of the sample code is in ImGui::ShowDemoWindow(). Read its code to learn more about Dear ImGui!
108 if (show_demo_window)
109 {
110 ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiCond_FirstUseEver); // Normally user code doesn't need/want to call this because positions are saved in .ini file anyway. Here we just want to make the demo initial state a bit more friendly!
111 ImGui::ShowDemoWindow(&show_demo_window);
112 }
113
114 // Rendering
115 int display_w, display_h;
116 glfwGetFramebufferSize(window, &display_w, &display_h);
117 glViewport(0, 0, display_w, display_h);
118 glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
119 glClear(GL_COLOR_BUFFER_BIT);
120 ImGui::Render();
121 ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
122 glfwSwapBuffers(window);
123 }
124
125 // Cleanup
126 ImGui_ImplGlfwGL3_Shutdown();
127 ImGui::DestroyContext();
128
129 glfwDestroyWindow(window);
130 glfwTerminate();
131
132 return 0;
133}
Note: See TracBrowser for help on using the repository browser.