main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // ImGui - standalone example application for Glfw + OpenGL 3, using programmable pipeline
  2. #include <imgui.h>
  3. #include "imgui_impl_glfw_gl3.h"
  4. #include <stdio.h>
  5. #include <GL/gl3w.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. exit(1);
  17. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  18. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  19. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  20. #if __APPLE__
  21. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  22. #endif
  23. GLFWwindow* window = glfwCreateWindow(1280, 720, "ImGui OpenGL3 example", NULL, NULL);
  24. glfwMakeContextCurrent(window);
  25. gl3wInit();
  26. // Setup ImGui binding
  27. ImGui_ImplGlfwGL3_Init(window, true);
  28. // Load Fonts
  29. // (see extra_fonts/README.txt for more details)
  30. //ImGuiIO& io = ImGui::GetIO();
  31. //io.Fonts->AddFontDefault();
  32. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Cousine-Regular.ttf", 15.0f);
  33. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 16.0f);
  34. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyClean.ttf", 13.0f);
  35. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyTiny.ttf", 10.0f);
  36. //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
  37. // Merge glyphs from multiple fonts into one (e.g. combine default font with another with Chinese glyphs, or add icons)
  38. //static const ImWchar icons_ranges[] = { 0xf000, 0xf3ff, 0 }; // will not be copied by AddFont* so keep in scope.
  39. //ImFontConfig icons_config; icons_config.MergeMode = true; icons_config.PixelSnapH = true;
  40. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 18.0f);
  41. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/fontawesome-webfont.ttf", 18.0f, &icons_config, icons_ranges);
  42. bool show_test_window = true;
  43. bool show_another_window = false;
  44. ImVec4 clear_color = ImColor(114, 144, 154);
  45. // Main loop
  46. while (!glfwWindowShouldClose(window))
  47. {
  48. glfwPollEvents();
  49. ImGui_ImplGlfwGL3_NewFrame();
  50. // 1. Show a simple window
  51. // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
  52. {
  53. static float f = 0.0f;
  54. ImGui::Text("Hello, world!");
  55. ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
  56. ImGui::ColorEdit3("clear color", (float*)&clear_color);
  57. if (ImGui::Button("Test Window")) show_test_window ^= 1;
  58. if (ImGui::Button("Another Window")) show_another_window ^= 1;
  59. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  60. }
  61. // 2. Show another simple window, this time using an explicit Begin/End pair
  62. if (show_another_window)
  63. {
  64. ImGui::SetNextWindowSize(ImVec2(200,100), ImGuiSetCond_FirstUseEver);
  65. ImGui::Begin("Another Window", &show_another_window);
  66. ImGui::Text("Hello");
  67. ImGui::End();
  68. }
  69. // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
  70. if (show_test_window)
  71. {
  72. ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCond_FirstUseEver);
  73. ImGui::ShowTestWindow(&show_test_window);
  74. }
  75. // Rendering
  76. int display_w, display_h;
  77. glfwGetFramebufferSize(window, &display_w, &display_h);
  78. glViewport(0, 0, display_w, display_h);
  79. glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
  80. glClear(GL_COLOR_BUFFER_BIT);
  81. ImGui::Render();
  82. glfwSwapBuffers(window);
  83. }
  84. // Cleanup
  85. ImGui_ImplGlfwGL3_Shutdown();
  86. glfwTerminate();
  87. return 0;
  88. }