main.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. return 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. // (there is a default font, this is only if you want to change it. see extra_fonts/README.txt for more details)
  22. //ImGuiIO& io = ImGui::GetIO();
  23. //io.Fonts->AddFontDefault();
  24. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Cousine-Regular.ttf", 15.0f);
  25. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 16.0f);
  26. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyClean.ttf", 13.0f);
  27. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyTiny.ttf", 10.0f);
  28. //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
  29. bool show_test_window = true;
  30. bool show_another_window = false;
  31. ImVec4 clear_color = ImColor(114, 144, 154);
  32. // Main loop
  33. while (!glfwWindowShouldClose(window))
  34. {
  35. glfwPollEvents();
  36. ImGui_ImplGlfw_NewFrame();
  37. // 1. Show a simple window
  38. // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
  39. {
  40. static float f = 0.0f;
  41. ImGui::Text("Hello, world!");
  42. ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
  43. ImGui::ColorEdit3("clear color", (float*)&clear_color);
  44. if (ImGui::Button("Test Window")) show_test_window ^= 1;
  45. if (ImGui::Button("Another Window")) show_another_window ^= 1;
  46. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  47. }
  48. // 2. Show another simple window, this time using an explicit Begin/End pair
  49. if (show_another_window)
  50. {
  51. ImGui::SetNextWindowSize(ImVec2(200,100), ImGuiSetCond_FirstUseEver);
  52. ImGui::Begin("Another Window", &show_another_window);
  53. ImGui::Text("Hello");
  54. ImGui::End();
  55. }
  56. // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
  57. if (show_test_window)
  58. {
  59. ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCond_FirstUseEver);
  60. ImGui::ShowTestWindow(&show_test_window);
  61. }
  62. // Rendering
  63. int display_w, display_h;
  64. glfwGetFramebufferSize(window, &display_w, &display_h);
  65. glViewport(0, 0, display_w, display_h);
  66. glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
  67. glClear(GL_COLOR_BUFFER_BIT);
  68. ImGui::Render();
  69. glfwSwapBuffers(window);
  70. }
  71. // Cleanup
  72. ImGui_ImplGlfw_Shutdown();
  73. glfwTerminate();
  74. return 0;
  75. }