main.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // ImGui - standalone example application for SDL2
  2. #include <imgui.h>
  3. #include "imgui_impl_sdl.h"
  4. #include <stdio.h>
  5. #ifdef WIN32
  6. #include <Windows.h>
  7. #include <gl/GL.h>
  8. #include <gl/GLU.h>
  9. #endif
  10. #ifdef MACOSX
  11. #include <OpenGL/gl.h>
  12. #endif
  13. #include <SDL.h>
  14. #include <SDL_OpenGL.h>
  15. int SDL_main(int /*argc*/, char* /*argv*/[])
  16. {
  17. // Setup SDL
  18. if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
  19. return -1;
  20. // Init OpenGL
  21. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  22. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  23. SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  24. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  25. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
  26. // SDL window
  27. SDL_DisplayMode current;
  28. SDL_GetCurrentDisplayMode(0, &current);
  29. SDL_Window *window = SDL_CreateWindow("ImGui SDL2+OpenGL example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
  30. // Create an OpenGL context associated with the window.
  31. SDL_GLContext glcontext = SDL_GL_CreateContext(window);
  32. // Setup ImGui binding
  33. ImGui_ImplSdl_Init(window);
  34. //ImGuiIO& io = ImGui::GetIO();
  35. //ImFont* my_font0 = io.Fonts->AddFontDefault();
  36. //ImFont* my_font1 = io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 16.0f);
  37. //ImFont* my_font2 = io.Fonts->AddFontFromFileTTF("../../extra_fonts/Karla-Regular.ttf", 16.0f);
  38. //ImFont* my_font3 = io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyClean.ttf", 13.0f); my_font3->DisplayOffset.y += 1;
  39. //ImFont* my_font4 = io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyTiny.ttf", 10.0f); my_font4->DisplayOffset.y += 1;
  40. //ImFont* my_font5 = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, io.Fonts->GetGlyphRangesJapanese());
  41. bool show_test_window = true;
  42. bool show_another_window = false;
  43. ImVec4 clear_color = ImColor(114, 144, 154);
  44. // Main loop
  45. bool done = false;
  46. while (!done)
  47. {
  48. done = ImGui_ImplSdl_NewFrame(window);
  49. // 1. Show a simple window
  50. // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
  51. {
  52. static float f = 0.0f;
  53. ImGui::Text("Hello, world!");
  54. ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
  55. ImGui::ColorEdit3("clear color", (float*)&clear_color);
  56. if (ImGui::Button("Test Window")) show_test_window ^= 1;
  57. if (ImGui::Button("Another Window")) show_another_window ^= 1;
  58. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  59. }
  60. // 2. Show another simple window, this time using an explicit Begin/End pair
  61. if (show_another_window)
  62. {
  63. ImGui::SetNextWindowSize(ImVec2(200,100), ImGuiSetCond_FirstUseEver);
  64. ImGui::Begin("Another Window", &show_another_window);
  65. ImGui::Text("Hello");
  66. ImGui::End();
  67. }
  68. // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
  69. if (show_test_window)
  70. {
  71. ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCond_FirstUseEver);
  72. ImGui::ShowTestWindow(&show_test_window);
  73. }
  74. // Rendering
  75. glViewport(0, 0, (int)ImGui::GetIO().DisplaySize.x, (int)ImGui::GetIO().DisplaySize.y);
  76. glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
  77. glClear(GL_COLOR_BUFFER_BIT);
  78. ImGui::Render();
  79. SDL_GL_SwapWindow(window);
  80. }
  81. // Cleanup
  82. ImGui_ImplSdl_Shutdown();
  83. SDL_GL_DeleteContext(glcontext);
  84. SDL_DestroyWindow(window);
  85. SDL_Quit();
  86. return 0;
  87. }