main.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // (SDL is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.)
  4. // (GL3W is a helper library to access OpenGL functions since there is no standard header to access modern OpenGL functions easily. Alternatives are GLEW, Glad, etc.)
  5. #include <imgui.h>
  6. #include "imgui_impl_sdl_gl3.h"
  7. #include <stdio.h>
  8. #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.
  9. #include <SDL.h>
  10. int main(int, char**)
  11. {
  12. // Setup SDL
  13. if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0)
  14. {
  15. printf("Error: %s\n", SDL_GetError());
  16. return -1;
  17. }
  18. // Setup window
  19. SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
  20. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  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, 3);
  25. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
  26. SDL_DisplayMode current;
  27. SDL_GetCurrentDisplayMode(0, &current);
  28. SDL_Window *window = SDL_CreateWindow("ImGui SDL2+OpenGL3 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
  29. SDL_GLContext glcontext = SDL_GL_CreateContext(window);
  30. gl3wInit();
  31. // Setup ImGui binding
  32. ImGui_ImplSdlGL3_Init(window);
  33. // Load Fonts
  34. // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
  35. // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
  36. // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
  37. // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
  38. // - Read 'extra_fonts/README.txt' for more instructions and details.
  39. // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
  40. //ImGuiIO& io = ImGui::GetIO();
  41. //io.Fonts->AddFontDefault();
  42. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Roboto-Medium.ttf", 16.0f);
  43. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Cousine-Regular.ttf", 15.0f);
  44. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 16.0f);
  45. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyTiny.ttf", 10.0f);
  46. //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
  47. //IM_ASSERT(font != NULL);
  48. bool show_test_window = true;
  49. bool show_another_window = false;
  50. ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
  51. // Main loop
  52. bool done = false;
  53. while (!done)
  54. {
  55. SDL_Event event;
  56. while (SDL_PollEvent(&event))
  57. {
  58. ImGui_ImplSdlGL3_ProcessEvent(&event);
  59. if (event.type == SDL_QUIT)
  60. done = true;
  61. }
  62. ImGui_ImplSdlGL3_NewFrame(window);
  63. // 1. Show a simple window
  64. // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
  65. {
  66. static float f = 0.0f;
  67. ImGui::Text("Hello, world!");
  68. ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
  69. ImGui::ColorEdit3("clear color", (float*)&clear_color);
  70. if (ImGui::Button("Test Window")) show_test_window ^= 1;
  71. if (ImGui::Button("Another Window")) show_another_window ^= 1;
  72. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  73. }
  74. // 2. Show another simple window, this time using an explicit Begin/End pair
  75. if (show_another_window)
  76. {
  77. ImGui::Begin("Another Window", &show_another_window);
  78. ImGui::Text("Hello from another window!");
  79. ImGui::End();
  80. }
  81. // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
  82. if (show_test_window)
  83. {
  84. ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiCond_FirstUseEver);
  85. ImGui::ShowTestWindow(&show_test_window);
  86. }
  87. // Rendering
  88. glViewport(0, 0, (int)ImGui::GetIO().DisplaySize.x, (int)ImGui::GetIO().DisplaySize.y);
  89. glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
  90. glClear(GL_COLOR_BUFFER_BIT);
  91. ImGui::Render();
  92. SDL_GL_SwapWindow(window);
  93. }
  94. // Cleanup
  95. ImGui_ImplSdlGL3_Shutdown();
  96. SDL_GL_DeleteContext(glcontext);
  97. SDL_DestroyWindow(window);
  98. SDL_Quit();
  99. return 0;
  100. }