main.cpp 3.7 KB

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