source: opengl-game/imgui_example.cpp@ 4e0b82b

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

Add an ImGui example project

  • Property mode set to 100644
File size: 6.4 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// (GL3W is a helper library to access OpenGL functions since there is no standard header to access modern OpenGL functions easily. Alternatives are GLEW, Glad, etc.)
5
6#include "imgui.h"
7#include "imgui_impl_glfw_gl3.h"
8#include <stdio.h>
9#include "gl3w.h" // This example is using gl3w to access OpenGL functions (because it is small). You may use glew/glad/glLoadGen/etc. whatever already works for you.
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 main(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 gl3wInit();
33
34 // Setup Dear ImGui binding
35 IMGUI_CHECKVERSION();
36 ImGui::CreateContext();
37 ImGuiIO& io = ImGui::GetIO(); (void)io;
38 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
39 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
40 ImGui_ImplGlfwGL3_Init(window, true);
41
42 // Setup style
43 ImGui::StyleColorsDark();
44 //ImGui::StyleColorsClassic();
45
46 // Load Fonts
47 // - 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.
48 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
49 // - 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).
50 // - 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.
51 // - Read 'misc/fonts/README.txt' for more instructions and details.
52 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
53 //io.Fonts->AddFontDefault();
54 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
55 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
56 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
57 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
58 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
59 //IM_ASSERT(font != NULL);
60
61 bool show_demo_window = true;
62 bool show_another_window = false;
63 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
64
65 // Main loop
66 while (!glfwWindowShouldClose(window))
67 {
68 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
69 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
70 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
71 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
72 glfwPollEvents();
73 ImGui_ImplGlfwGL3_NewFrame();
74
75 // 1. Show a simple window.
76 // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug".
77 {
78 static float f = 0.0f;
79 static int counter = 0;
80 ImGui::Text("Hello, world!"); // Display some text (you can use a format string too)
81 ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
82 ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
83
84 ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our windows open/close state
85 ImGui::Checkbox("Another Window", &show_another_window);
86
87 if (ImGui::Button("Button")) // Buttons return true when clicked (NB: most widgets return true when edited/activated)
88 counter++;
89 ImGui::SameLine();
90 ImGui::Text("counter = %d", counter);
91
92 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
93 }
94
95 // 2. Show another simple window. In most cases you will use an explicit Begin/End pair to name your windows.
96 if (show_another_window)
97 {
98 ImGui::Begin("Another Window", &show_another_window);
99 ImGui::Text("Hello from another window!");
100 if (ImGui::Button("Close Me"))
101 show_another_window = false;
102 ImGui::End();
103 }
104
105 // 3. Show the ImGui demo window. Most of the sample code is in ImGui::ShowDemoWindow(). Read its code to learn more about Dear ImGui!
106 if (show_demo_window)
107 {
108 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!
109 ImGui::ShowDemoWindow(&show_demo_window);
110 }
111
112 // Rendering
113 int display_w, display_h;
114 glfwGetFramebufferSize(window, &display_w, &display_h);
115 glViewport(0, 0, display_w, display_h);
116 glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
117 glClear(GL_COLOR_BUFFER_BIT);
118 ImGui::Render();
119 ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
120 glfwSwapBuffers(window);
121 }
122
123 // Cleanup
124 ImGui_ImplGlfwGL3_Shutdown();
125 ImGui::DestroyContext();
126
127 glfwDestroyWindow(window);
128 glfwTerminate();
129
130 return 0;
131}
Note: See TracBrowser for help on using the repository browser.