main.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // ImGui - standalone example application for SDL2 + OpenGL
  2. #include <imgui.h>
  3. #include "imgui_impl_sdl.h"
  4. #include <stdio.h>
  5. #include <SDL.h>
  6. #include <SDL_OpenGL.h>
  7. int SDL_main(int, char**)
  8. {
  9. // Setup SDL
  10. if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
  11. return -1;
  12. // Setup window
  13. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  14. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  15. SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  16. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  17. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
  18. SDL_DisplayMode current;
  19. SDL_GetCurrentDisplayMode(0, &current);
  20. SDL_Window *window = SDL_CreateWindow("ImGui SDL2+OpenGL example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
  21. SDL_GLContext glcontext = SDL_GL_CreateContext(window);
  22. // Setup ImGui binding
  23. ImGui_ImplSdl_Init(window);
  24. //ImGuiIO& io = ImGui::GetIO();
  25. //ImFont* my_font0 = io.Fonts->AddFontDefault();
  26. //ImFont* my_font1 = io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 16.0f);
  27. //ImFont* my_font2 = io.Fonts->AddFontFromFileTTF("../../extra_fonts/Karla-Regular.ttf", 16.0f);
  28. //ImFont* my_font3 = io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyClean.ttf", 13.0f); my_font3->DisplayOffset.y += 1;
  29. //ImFont* my_font4 = io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyTiny.ttf", 10.0f); my_font4->DisplayOffset.y += 1;
  30. //ImFont* my_font5 = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, io.Fonts->GetGlyphRangesJapanese());
  31. bool show_test_window = true;
  32. bool show_another_window = false;
  33. ImVec4 clear_color = ImColor(114, 144, 154);
  34. // Main loop
  35. bool done = false;
  36. while (!done)
  37. {
  38. SDL_Event event;
  39. while (SDL_PollEvent(&event))
  40. {
  41. ImGui_ImplSdl_ProcessEvent(&event);
  42. if (event.type == SDL_QUIT)
  43. done = true;
  44. }
  45. ImGui_ImplSdl_NewFrame(window);
  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. glViewport(0, 0, (int)ImGui::GetIO().DisplaySize.x, (int)ImGui::GetIO().DisplaySize.y);
  73. glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
  74. glClear(GL_COLOR_BUFFER_BIT);
  75. ImGui::Render();
  76. SDL_GL_SwapWindow(window);
  77. }
  78. // Cleanup
  79. ImGui_ImplSdl_Shutdown();
  80. SDL_GL_DeleteContext(glcontext);
  81. SDL_DestroyWindow(window);
  82. SDL_Quit();
  83. return 0;
  84. }