main.cpp 2.9 KB

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