main.cpp 3.0 KB

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