main.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // ImGui - standalone example application for Glfw + OpenGL 2, using fixed pipeline
  2. #include <imgui.h>
  3. #include "imgui_impl_glfw.h"
  4. #include <stdio.h>
  5. #include <GLFW/glfw3.h>
  6. static void error_callback(int error, const char* description)
  7. {
  8. fprintf(stderr, "Error %d: %s\n", error, description);
  9. }
  10. int main(int, char**)
  11. {
  12. // Setup window
  13. glfwSetErrorCallback(error_callback);
  14. if (!glfwInit())
  15. exit(1);
  16. GLFWwindow* window = glfwCreateWindow(1280, 720, "ImGui OpenGL2 example", NULL, NULL);
  17. glfwMakeContextCurrent(window);
  18. // Setup ImGui binding
  19. ImGui_ImplGlfw_Init(window, true);
  20. // Load Fonts
  21. // (see extra_fonts/README.txt for more details)
  22. //ImGuiIO& io = ImGui::GetIO();
  23. //io.Fonts->AddFontDefault();
  24. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 16.0f);
  25. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyClean.ttf", 13.0f);
  26. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyTiny.ttf", 10.0f);
  27. //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
  28. // Merge glyphs from multiple fonts into one (e.g. combine default font with another with Chinese glyphs, or add icons)
  29. //ImWchar icons_ranges[] = { 0xf000, 0xf3ff, 0 };
  30. //ImFontConfig icons_config; icons_config.MergeMode = true; icons_config.PixelSnapH = true;
  31. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 18.0f);
  32. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/fontawesome-webfont.ttf", 18.0f, &icons_config, icons_ranges);
  33. bool show_test_window = true;
  34. bool show_another_window = false;
  35. ImVec4 clear_color = ImColor(114, 144, 154);
  36. // Main loop
  37. while (!glfwWindowShouldClose(window))
  38. {
  39. glfwPollEvents();
  40. ImGui_ImplGlfw_NewFrame();
  41. // 1. Show a simple window
  42. // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
  43. {
  44. static float f = 0.0f;
  45. ImGui::Text("Hello, world!");
  46. ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
  47. ImGui::ColorEdit3("clear color", (float*)&clear_color);
  48. if (ImGui::Button("Test Window")) show_test_window ^= 1;
  49. if (ImGui::Button("Another Window")) show_another_window ^= 1;
  50. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  51. }
  52. // 2. Show another simple window, this time using an explicit Begin/End pair
  53. if (show_another_window)
  54. {
  55. ImGui::SetNextWindowSize(ImVec2(200,100), ImGuiSetCond_FirstUseEver);
  56. ImGui::Begin("Another Window", &show_another_window);
  57. ImGui::Text("Hello");
  58. ImGui::End();
  59. }
  60. // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
  61. if (show_test_window)
  62. {
  63. ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCond_FirstUseEver);
  64. ImGui::ShowTestWindow(&show_test_window);
  65. }
  66. // Rendering
  67. glViewport(0, 0, (int)ImGui::GetIO().DisplaySize.x, (int)ImGui::GetIO().DisplaySize.y);
  68. glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
  69. glClear(GL_COLOR_BUFFER_BIT);
  70. ImGui::Render();
  71. glfwSwapBuffers(window);
  72. }
  73. // Cleanup
  74. ImGui_ImplGlfw_Shutdown();
  75. glfwTerminate();
  76. return 0;
  77. }