main.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // ImGui - standalone example application for SDL2 + OpenGL
  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_sdl.h"
  5. #include <stdio.h>
  6. #include <SDL.h>
  7. #include <SDL_opengl.h>
  8. int main(int, char**)
  9. {
  10. // Setup SDL
  11. if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
  12. {
  13. printf("Error: %s\n", SDL_GetError());
  14. return -1;
  15. }
  16. // Setup window
  17. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  18. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  19. SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  20. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  21. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
  22. SDL_DisplayMode current;
  23. SDL_GetCurrentDisplayMode(0, &current);
  24. SDL_Window *window = SDL_CreateWindow("ImGui SDL2+OpenGL example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
  25. SDL_GLContext glcontext = SDL_GL_CreateContext(window);
  26. // Setup ImGui binding
  27. ImGui_ImplSdl_Init(window);
  28. // Load Fonts
  29. // (there is a default font, this is only if you want to change it. 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. bool show_test_window = true;
  38. bool show_another_window = false;
  39. ImVec4 clear_color = ImColor(114, 144, 154);
  40. // Main loop
  41. bool done = false;
  42. while (!done)
  43. {
  44. SDL_Event event;
  45. while (SDL_PollEvent(&event))
  46. {
  47. ImGui_ImplSdl_ProcessEvent(&event);
  48. if (event.type == SDL_QUIT)
  49. done = true;
  50. }
  51. ImGui_ImplSdl_NewFrame(window);
  52. // 1. Show a simple window
  53. // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
  54. {
  55. static float f = 0.0f;
  56. ImGui::Text("Hello, world!");
  57. ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
  58. ImGui::ColorEdit3("clear color", (float*)&clear_color);
  59. if (ImGui::Button("Test Window")) show_test_window ^= 1;
  60. if (ImGui::Button("Another Window")) show_another_window ^= 1;
  61. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  62. }
  63. // 2. Show another simple window, this time using an explicit Begin/End pair
  64. if (show_another_window)
  65. {
  66. ImGui::SetNextWindowSize(ImVec2(200,100), ImGuiSetCond_FirstUseEver);
  67. ImGui::Begin("Another Window", &show_another_window);
  68. ImGui::Text("Hello");
  69. ImGui::End();
  70. }
  71. // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
  72. if (show_test_window)
  73. {
  74. ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCond_FirstUseEver);
  75. ImGui::ShowTestWindow(&show_test_window);
  76. }
  77. // Rendering
  78. glViewport(0, 0, (int)ImGui::GetIO().DisplaySize.x, (int)ImGui::GetIO().DisplaySize.y);
  79. glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
  80. glClear(GL_COLOR_BUFFER_BIT);
  81. ImGui::Render();
  82. SDL_GL_SwapWindow(window);
  83. }
  84. // Cleanup
  85. ImGui_ImplSdl_Shutdown();
  86. SDL_GL_DeleteContext(glcontext);
  87. SDL_DestroyWindow(window);
  88. SDL_Quit();
  89. return 0;
  90. }