main.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // ImGui - standalone example application for Glfw + OpenGL 2, using fixed pipeline
  2. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  3. #include <imgui.h>
  4. #include "imgui_impl_glfw.h"
  5. #include <stdio.h>
  6. #include <GLFW/glfw3.h>
  7. static void error_callback(int error, const char* description)
  8. {
  9. fprintf(stderr, "Error %d: %s\n", error, description);
  10. }
  11. int main(int, char**)
  12. {
  13. // Setup window
  14. glfwSetErrorCallback(error_callback);
  15. if (!glfwInit())
  16. return 1;
  17. GLFWwindow* window = glfwCreateWindow(1280, 720, "ImGui OpenGL2 example", NULL, NULL);
  18. glfwMakeContextCurrent(window);
  19. glfwSwapInterval(1); // Enable vsync
  20. // Setup ImGui binding
  21. ImGui_ImplGlfwGL2_Init(window, true);
  22. // Load Fonts
  23. // (there is a default font, this is only if you want to change it. see extra_fonts/README.txt for more details)
  24. //ImGuiIO& io = ImGui::GetIO();
  25. //io.Fonts->AddFontDefault();
  26. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Cousine-Regular.ttf", 15.0f);
  27. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 16.0f);
  28. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyClean.ttf", 13.0f);
  29. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyTiny.ttf", 10.0f);
  30. //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
  31. bool show_test_window = true;
  32. bool show_another_window = false;
  33. ImVec4 clear_color = ImColor(114, 144, 154);
  34. // Main loop
  35. while (!glfwWindowShouldClose(window))
  36. {
  37. glfwPollEvents();
  38. ImGui_ImplGlfwGL2_NewFrame();
  39. // 1. Show a simple window
  40. // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
  41. {
  42. static float f = 0.0f;
  43. ImGui::Text("Hello, world!");
  44. ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
  45. ImGui::ColorEdit3("clear color", (float*)&clear_color);
  46. if (ImGui::Button("Test Window")) show_test_window ^= 1;
  47. if (ImGui::Button("Another Window")) show_another_window ^= 1;
  48. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  49. }
  50. // 2. Show another simple window, this time using an explicit Begin/End pair
  51. if (show_another_window)
  52. {
  53. ImGui::Begin("Another Window", &show_another_window);
  54. ImGui::Text("Hello from another window!");
  55. ImGui::End();
  56. }
  57. // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
  58. if (show_test_window)
  59. {
  60. ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiCond_FirstUseEver);
  61. ImGui::ShowTestWindow(&show_test_window);
  62. }
  63. // Rendering
  64. int display_w, display_h;
  65. glfwGetFramebufferSize(window, &display_w, &display_h);
  66. glViewport(0, 0, display_w, display_h);
  67. glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
  68. glClear(GL_COLOR_BUFFER_BIT);
  69. //glUseProgram(0); // You may want this if using this code in an OpenGL 3+ context where shaders may be bound
  70. ImGui::Render();
  71. glfwSwapBuffers(window);
  72. }
  73. // Cleanup
  74. ImGui_ImplGlfwGL2_Shutdown();
  75. glfwTerminate();
  76. return 0;
  77. }