main.cpp 3.5 KB

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