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. gl3wInit();
  27. // Setup ImGui binding
  28. ImGui_ImplGlfwGL3_Init(window, true);
  29. // Load Fonts
  30. // (there is a default font, this is only if you want to change it. see extra_fonts/README.txt for more details)
  31. //ImGuiIO& io = ImGui::GetIO();
  32. //io.Fonts->AddFontDefault();
  33. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Cousine-Regular.ttf", 15.0f);
  34. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 16.0f);
  35. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyClean.ttf", 13.0f);
  36. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyTiny.ttf", 10.0f);
  37. //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
  38. bool show_test_window = true;
  39. bool show_another_window = false;
  40. ImVec4 clear_color = ImColor(114, 144, 154);
  41. // Main loop
  42. while (!glfwWindowShouldClose(window))
  43. {
  44. glfwPollEvents();
  45. ImGui_ImplGlfwGL3_NewFrame();
  46. // 1. Show a simple window
  47. // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
  48. {
  49. static float f = 0.0f;
  50. ImGui::Text("Hello, world!");
  51. ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
  52. ImGui::ColorEdit3("clear color", (float*)&clear_color);
  53. if (ImGui::Button("Test Window")) show_test_window ^= 1;
  54. if (ImGui::Button("Another Window")) show_another_window ^= 1;
  55. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  56. }
  57. // 2. Show another simple window, this time using an explicit Begin/End pair
  58. if (show_another_window)
  59. {
  60. ImGui::SetNextWindowSize(ImVec2(200,100), ImGuiSetCond_FirstUseEver);
  61. ImGui::Begin("Another Window", &show_another_window);
  62. ImGui::Text("Hello");
  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), ImGuiSetCond_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. }