main.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_gl3.h"
  5. #include <stdio.h>
  6. #include <GL/gl3w.h> // This example is using gl3w to access OpenGL functions (because it is small). You may use glew/glad/glLoadGen/etc. whatever already works for you.
  7. #include <SDL.h>
  8. int main(int, char**)
  9. {
  10. // Setup SDL
  11. if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0)
  12. {
  13. printf("Error: %s\n", SDL_GetError());
  14. return -1;
  15. }
  16. // Setup window
  17. SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
  18. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  19. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  20. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  21. SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  22. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  23. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
  24. SDL_DisplayMode current;
  25. SDL_GetCurrentDisplayMode(0, &current);
  26. SDL_Window *window = SDL_CreateWindow("ImGui SDL2+OpenGL3 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
  27. SDL_GLContext glcontext = SDL_GL_CreateContext(window);
  28. gl3wInit();
  29. // Setup ImGui binding
  30. ImGui_ImplSdlGL3_Init(window);
  31. // Load Fonts
  32. // (there is a default font, this is only if you want to change it. see extra_fonts/README.txt for more details)
  33. //ImGuiIO& io = ImGui::GetIO();
  34. //io.Fonts->AddFontDefault();
  35. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Cousine-Regular.ttf", 15.0f);
  36. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 16.0f);
  37. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyClean.ttf", 13.0f);
  38. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyTiny.ttf", 10.0f);
  39. //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
  40. bool show_test_window = true;
  41. bool show_another_window = false;
  42. ImVec4 clear_color = ImColor(114, 144, 154);
  43. // Main loop
  44. bool done = false;
  45. while (!done)
  46. {
  47. SDL_Event event;
  48. while (SDL_PollEvent(&event))
  49. {
  50. ImGui_ImplSdlGL3_ProcessEvent(&event);
  51. if (event.type == SDL_QUIT)
  52. done = true;
  53. }
  54. ImGui_ImplSdlGL3_NewFrame(window);
  55. // 1. Show a simple window
  56. // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
  57. {
  58. static float f = 0.0f;
  59. ImGui::Text("Hello, world!");
  60. ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
  61. ImGui::ColorEdit3("clear color", (float*)&clear_color);
  62. if (ImGui::Button("Test Window")) show_test_window ^= 1;
  63. if (ImGui::Button("Another Window")) show_another_window ^= 1;
  64. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  65. }
  66. // 2. Show another simple window, this time using an explicit Begin/End pair
  67. if (show_another_window)
  68. {
  69. ImGui::Begin("Another Window", &show_another_window);
  70. ImGui::Text("Hello from another window!");
  71. ImGui::End();
  72. }
  73. // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
  74. if (show_test_window)
  75. {
  76. ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiCond_FirstUseEver);
  77. ImGui::ShowTestWindow(&show_test_window);
  78. }
  79. // Rendering
  80. glViewport(0, 0, (int)ImGui::GetIO().DisplaySize.x, (int)ImGui::GetIO().DisplaySize.y);
  81. glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
  82. glClear(GL_COLOR_BUFFER_BIT);
  83. ImGui::Render();
  84. SDL_GL_SwapWindow(window);
  85. }
  86. // Cleanup
  87. ImGui_ImplSdlGL3_Shutdown();
  88. SDL_GL_DeleteContext(glcontext);
  89. SDL_DestroyWindow(window);
  90. SDL_Quit();
  91. return 0;
  92. }