main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // ImGui - standalone example application for OpenGL 2, using fixed pipeline
  2. #ifdef _MSC_VER
  3. #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen
  4. #endif
  5. #ifdef __clang__
  6. #pragma clang diagnostic ignored "-Wunused-function" // warning: unused function
  7. #endif
  8. #include "../../imgui.h"
  9. #include <stdio.h>
  10. // GLFW
  11. #include <GLFW/glfw3.h>
  12. #ifdef _MSC_VER
  13. #undef APIENTRY
  14. #define GLFW_EXPOSE_NATIVE_WIN32
  15. #define GLFW_EXPOSE_NATIVE_WGL
  16. #include <GLFW/glfw3native.h>
  17. #endif
  18. static GLFWwindow* window;
  19. static bool mousePressed[2] = { false, false };
  20. #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
  21. // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
  22. // If text or lines are blurry when integrating ImGui in your engine:
  23. // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
  24. static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count)
  25. {
  26. if (cmd_lists_count == 0)
  27. return;
  28. // We are using the OpenGL fixed pipeline to make the example code simpler to read!
  29. // A probable faster way to render would be to collate all vertices from all cmd_lists into a single vertex buffer.
  30. // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers.
  31. glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
  32. glEnable(GL_BLEND);
  33. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  34. glDisable(GL_CULL_FACE);
  35. glDisable(GL_DEPTH_TEST);
  36. glEnable(GL_SCISSOR_TEST);
  37. glEnableClientState(GL_VERTEX_ARRAY);
  38. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  39. glEnableClientState(GL_COLOR_ARRAY);
  40. glEnable(GL_TEXTURE_2D);
  41. // Setup orthographic projection matrix
  42. const float width = ImGui::GetIO().DisplaySize.x;
  43. const float height = ImGui::GetIO().DisplaySize.y;
  44. glMatrixMode(GL_PROJECTION);
  45. glPushMatrix();
  46. glLoadIdentity();
  47. glOrtho(0.0f, width, height, 0.0f, -1.0f, +1.0f);
  48. glMatrixMode(GL_MODELVIEW);
  49. glPushMatrix();
  50. glLoadIdentity();
  51. // Render command lists
  52. for (int n = 0; n < cmd_lists_count; n++)
  53. {
  54. const ImDrawList* cmd_list = cmd_lists[n];
  55. const unsigned char* vtx_buffer = (const unsigned char*)&cmd_list->vtx_buffer.front();
  56. glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, pos)));
  57. glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, uv)));
  58. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, col)));
  59. int vtx_offset = 0;
  60. for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++)
  61. {
  62. const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i];
  63. glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->texture_id);
  64. glScissor((int)pcmd->clip_rect.x, (int)(height - pcmd->clip_rect.w), (int)(pcmd->clip_rect.z - pcmd->clip_rect.x), (int)(pcmd->clip_rect.w - pcmd->clip_rect.y));
  65. glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count);
  66. vtx_offset += pcmd->vtx_count;
  67. }
  68. }
  69. // Restore modified state
  70. glDisableClientState(GL_COLOR_ARRAY);
  71. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  72. glDisableClientState(GL_VERTEX_ARRAY);
  73. glMatrixMode(GL_MODELVIEW);
  74. glPopMatrix();
  75. glMatrixMode(GL_PROJECTION);
  76. glPopMatrix();
  77. glPopAttrib();
  78. }
  79. // NB: ImGui already provide OS clipboard support for Windows so this isn't needed if you are using Windows only.
  80. static const char* ImImpl_GetClipboardTextFn()
  81. {
  82. return glfwGetClipboardString(window);
  83. }
  84. static void ImImpl_SetClipboardTextFn(const char* text)
  85. {
  86. glfwSetClipboardString(window, text);
  87. }
  88. // GLFW callbacks
  89. static void glfw_error_callback(int error, const char* description)
  90. {
  91. fputs(description, stderr);
  92. }
  93. static void glfw_mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
  94. {
  95. if (action == GLFW_PRESS && button >= 0 && button < 2)
  96. mousePressed[button] = true;
  97. }
  98. static void glfw_scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
  99. {
  100. ImGuiIO& io = ImGui::GetIO();
  101. io.MouseWheel += (float)yoffset; // Use fractional mouse wheel, 1.0 unit 5 lines.
  102. }
  103. static void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  104. {
  105. ImGuiIO& io = ImGui::GetIO();
  106. if (action == GLFW_PRESS)
  107. io.KeysDown[key] = true;
  108. if (action == GLFW_RELEASE)
  109. io.KeysDown[key] = false;
  110. io.KeyCtrl = (mods & GLFW_MOD_CONTROL) != 0;
  111. io.KeyShift = (mods & GLFW_MOD_SHIFT) != 0;
  112. }
  113. static void glfw_char_callback(GLFWwindow* window, unsigned int c)
  114. {
  115. if (c > 0 && c < 0x10000)
  116. ImGui::GetIO().AddInputCharacter((unsigned short)c);
  117. }
  118. void InitGL()
  119. {
  120. glfwSetErrorCallback(glfw_error_callback);
  121. if (!glfwInit())
  122. exit(1);
  123. window = glfwCreateWindow(1280, 720, "ImGui OpenGL2 example", NULL, NULL);
  124. glfwMakeContextCurrent(window);
  125. glfwSetKeyCallback(window, glfw_key_callback);
  126. glfwSetMouseButtonCallback(window, glfw_mouse_button_callback);
  127. glfwSetScrollCallback(window, glfw_scroll_callback);
  128. glfwSetCharCallback(window, glfw_char_callback);
  129. }
  130. void LoadFontsTexture()
  131. {
  132. ImGuiIO& io = ImGui::GetIO();
  133. //ImFont* my_font1 = io.Fonts->AddFontDefault();
  134. //ImFont* my_font2 = io.Fonts->AddFontFromFileTTF("extra_fonts/Karla-Regular.ttf", 15.0f);
  135. //ImFont* my_font3 = io.Fonts->AddFontFromFileTTF("extra_fonts/ProggyClean.ttf", 13.0f); my_font3->DisplayOffset.y += 1;
  136. //ImFont* my_font4 = io.Fonts->AddFontFromFileTTF("extra_fonts/ProggyTiny.ttf", 10.0f); my_font4->DisplayOffset.y += 1;
  137. //ImFont* my_font5 = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 20.0f, io.Fonts->GetGlyphRangesJapanese());
  138. unsigned char* pixels;
  139. int width, height;
  140. io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height);
  141. GLuint tex_id;
  142. glGenTextures(1, &tex_id);
  143. glBindTexture(GL_TEXTURE_2D, tex_id);
  144. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  145. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  146. glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels);
  147. // Store our identifier
  148. io.Fonts->TexID = (void *)(intptr_t)tex_id;
  149. }
  150. void InitImGui()
  151. {
  152. ImGuiIO& io = ImGui::GetIO();
  153. io.DeltaTime = 1.0f/60.0f; // Time elapsed since last frame, in seconds (in this sample app we'll override this every frame because our time step is variable)
  154. io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
  155. io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
  156. io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
  157. io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
  158. io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN;
  159. io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME;
  160. io.KeyMap[ImGuiKey_End] = GLFW_KEY_END;
  161. io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE;
  162. io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE;
  163. io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER;
  164. io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE;
  165. io.KeyMap[ImGuiKey_A] = GLFW_KEY_A;
  166. io.KeyMap[ImGuiKey_C] = GLFW_KEY_C;
  167. io.KeyMap[ImGuiKey_V] = GLFW_KEY_V;
  168. io.KeyMap[ImGuiKey_X] = GLFW_KEY_X;
  169. io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y;
  170. io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z;
  171. io.RenderDrawListsFn = ImImpl_RenderDrawLists;
  172. io.SetClipboardTextFn = ImImpl_SetClipboardTextFn;
  173. io.GetClipboardTextFn = ImImpl_GetClipboardTextFn;
  174. #ifdef _MSC_VER
  175. io.ImeWindowHandle = glfwGetWin32Window(window);
  176. #endif
  177. LoadFontsTexture();
  178. }
  179. void UpdateImGui()
  180. {
  181. ImGuiIO& io = ImGui::GetIO();
  182. // Setup resolution (every frame to accommodate for window resizing)
  183. int w, h;
  184. int display_w, display_h;
  185. glfwGetWindowSize(window, &w, &h);
  186. glfwGetFramebufferSize(window, &display_w, &display_h);
  187. io.DisplaySize = ImVec2((float)display_w, (float)display_h); // Display size, in pixels. For clamping windows positions.
  188. // Setup time step
  189. static double time = 0.0f;
  190. const double current_time = glfwGetTime();
  191. io.DeltaTime = (float)(current_time - time);
  192. time = current_time;
  193. // Setup inputs
  194. // (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
  195. double mouse_x, mouse_y;
  196. glfwGetCursorPos(window, &mouse_x, &mouse_y);
  197. mouse_x *= (float)display_w / w; // Convert mouse coordinates to pixels
  198. mouse_y *= (float)display_h / h;
  199. io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
  200. io.MouseDown[0] = mousePressed[0] || glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
  201. io.MouseDown[1] = mousePressed[1] || glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) != 0;
  202. // Start the frame
  203. ImGui::NewFrame();
  204. }
  205. // Application code
  206. int main(int argc, char** argv)
  207. {
  208. InitGL();
  209. InitImGui();
  210. bool show_test_window = true;
  211. bool show_another_window = false;
  212. ImVec4 clear_col = ImColor(114, 144, 154);
  213. while (!glfwWindowShouldClose(window))
  214. {
  215. ImGuiIO& io = ImGui::GetIO();
  216. mousePressed[0] = mousePressed[1] = false;
  217. glfwPollEvents();
  218. UpdateImGui();
  219. // 1. Show a simple window
  220. // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
  221. {
  222. static float f;
  223. ImGui::Text("Hello, world!");
  224. ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
  225. ImGui::ColorEdit3("clear color", (float*)&clear_col);
  226. if (ImGui::Button("Test Window")) show_test_window ^= 1;
  227. if (ImGui::Button("Another Window")) show_another_window ^= 1;
  228. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  229. }
  230. // 2. Show another simple window, this time using an explicit Begin/End pair
  231. if (show_another_window)
  232. {
  233. ImGui::Begin("Another Window", &show_another_window, ImVec2(200,100));
  234. ImGui::Text("Hello");
  235. ImGui::End();
  236. }
  237. // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
  238. if (show_test_window)
  239. {
  240. ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCond_FirstUseEver);
  241. ImGui::ShowTestWindow(&show_test_window);
  242. }
  243. // Rendering
  244. glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
  245. glClearColor(clear_col.x, clear_col.y, clear_col.z, clear_col.w);
  246. glClear(GL_COLOR_BUFFER_BIT);
  247. ImGui::Render();
  248. glfwSwapBuffers(window);
  249. }
  250. // Cleanup
  251. ImGui::Shutdown();
  252. glfwTerminate();
  253. return 0;
  254. }