imgui_impl_sdlrenderer.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // dear imgui: Renderer Backend for SDL_Renderer, with Platform Backend SDL
  2. // (Requires: SDL 2.0.17+)
  3. // Implemented features:
  4. // You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
  5. // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
  6. // Read online: https://github.com/ocornut/imgui/tree/master/docs
  7. // CHANGELOG
  8. // 2021-16-03: Creation
  9. #include "imgui.h"
  10. #include "imgui_impl_sdlrenderer.h"
  11. #if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier
  12. #include <stddef.h> // intptr_t
  13. #else
  14. #include <stdint.h> // intptr_t
  15. #endif
  16. #include "SDL.h"
  17. #if SDL_MAJOR_VERSION < 2 || SDL_MINOR_VERSION < 0 || SDL_PATCHLEVEL < 17
  18. # error Requires: SDL 2.0.17+ because of SDL_RenderGeometry function
  19. #endif
  20. struct ImGui_ImplSDLRenderer_Data
  21. {
  22. SDL_Renderer *SDLRenderer;
  23. SDL_Texture *FontTexture;
  24. ImGui_ImplSDLRenderer_Data() { memset(this, 0, sizeof(*this)); }
  25. };
  26. // Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
  27. // It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
  28. static ImGui_ImplSDLRenderer_Data* ImGui_ImplSDLRenderer_GetBackendData()
  29. {
  30. return ImGui::GetCurrentContext() ? (ImGui_ImplSDLRenderer_Data*)ImGui::GetIO().BackendRendererUserData : NULL;
  31. }
  32. // Functions
  33. bool ImGui_ImplSDLRenderer_Init(SDL_Renderer *renderer)
  34. {
  35. ImGuiIO& io = ImGui::GetIO();
  36. IM_ASSERT(io.BackendRendererUserData == NULL && "Already initialized a renderer backend!");
  37. IM_ASSERT(renderer != NULL && "SDL_Renderer not initialized!");
  38. // Setup backend capabilities flags
  39. ImGui_ImplSDLRenderer_Data* bd = IM_NEW(ImGui_ImplSDLRenderer_Data)();
  40. io.BackendRendererUserData = (void*)bd;
  41. io.BackendRendererName = "imgui_impl_SDLRenderer";
  42. bd->SDLRenderer = renderer;
  43. return true;
  44. }
  45. void ImGui_ImplSDLRenderer_Shutdown()
  46. {
  47. ImGuiIO& io = ImGui::GetIO();
  48. ImGui_ImplSDLRenderer_Data* bd = ImGui_ImplSDLRenderer_GetBackendData();
  49. ImGui_ImplSDLRenderer_DestroyDeviceObjects();
  50. io.BackendRendererName = NULL;
  51. io.BackendRendererUserData = NULL;
  52. IM_DELETE(bd);
  53. }
  54. static void ImGui_ImplSDLRenderer_SetupRenderState()
  55. {
  56. ImGui_ImplSDLRenderer_Data *bd = ImGui_ImplSDLRenderer_GetBackendData();
  57. // Clear out any viewports and cliprects set by the user
  58. SDL_RenderSetViewport(bd->SDLRenderer, NULL);
  59. SDL_RenderSetClipRect(bd->SDLRenderer, NULL);
  60. }
  61. void ImGui_ImplSDLRenderer_NewFrame()
  62. {
  63. ImGui_ImplSDLRenderer_Data* bd = ImGui_ImplSDLRenderer_GetBackendData();
  64. IM_ASSERT(bd != NULL && "Did you call ImGui_ImplSDLRenderer_Init()?");
  65. if (!bd->FontTexture) {
  66. ImGui_ImplSDLRenderer_CreateDeviceObjects();
  67. }
  68. }
  69. void ImGui_ImplSDLRenderer_RenderDrawData(ImDrawData* draw_data)
  70. {
  71. ImGui_ImplSDLRenderer_Data* bd = ImGui_ImplSDLRenderer_GetBackendData();
  72. // If there's a scale factor set by the user, use that instead
  73. float rsX = 1.0f;
  74. float rsY = 1.0f;
  75. SDL_RenderGetScale(bd->SDLRenderer, &rsX, &rsY);
  76. ImVec2 renderScale;
  77. // If the user has specified a scale factor to SDL_Renderer already (via SDL_RenderSetScale()), SDL will scale whatever we pass
  78. // to SDL_RenderGeometryRaw() by that scale factor. In that case we don't want to be also scaling it ourselves here.
  79. renderScale.x = (rsX == 1.0f) ? draw_data->FramebufferScale.x : 1.0f;
  80. renderScale.y = (rsY == 1.0f) ? draw_data->FramebufferScale.y : 1.0f;
  81. // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
  82. int fb_width = (int)(draw_data->DisplaySize.x * renderScale.x);
  83. int fb_height = (int)(draw_data->DisplaySize.y * renderScale.y);
  84. if (fb_width == 0 || fb_height == 0)
  85. return;
  86. // Will project scissor/clipping rectangles into framebuffer space
  87. ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
  88. ImVec2 clip_scale = renderScale;
  89. ImGui_ImplSDLRenderer_SetupRenderState();
  90. for (int n = 0; n < draw_data->CmdListsCount; n++) {
  91. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  92. const ImDrawVert* vtx_buffer = cmd_list->VtxBuffer.Data;
  93. const ImDrawIdx* idx_buffer = cmd_list->IdxBuffer.Data;
  94. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
  95. {
  96. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  97. if (pcmd->UserCallback)
  98. {
  99. // User callback, registered via ImDrawList::AddCallback()
  100. // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
  101. if (pcmd->UserCallback == ImDrawCallback_ResetRenderState) {
  102. ImGui_ImplSDLRenderer_SetupRenderState();
  103. } else {
  104. pcmd->UserCallback(cmd_list, pcmd);
  105. }
  106. }
  107. else
  108. {
  109. // Project scissor/clipping rectangles into framebuffer space
  110. ImVec4 clip_rect;
  111. clip_rect.x = (pcmd->ClipRect.x - clip_off.x) * clip_scale.x;
  112. clip_rect.y = (pcmd->ClipRect.y - clip_off.y) * clip_scale.y;
  113. clip_rect.z = (pcmd->ClipRect.z - clip_off.x) * clip_scale.x;
  114. clip_rect.w = (pcmd->ClipRect.w - clip_off.y) * clip_scale.y;
  115. if (clip_rect.x < fb_width && clip_rect.y < fb_height && clip_rect.z >= 0.0f && clip_rect.w >= 0.0f)
  116. {
  117. SDL_Rect r;
  118. r.x = clip_rect.x;
  119. r.y = clip_rect.y;
  120. r.w = clip_rect.z - clip_rect.x;
  121. r.h = clip_rect.w - clip_rect.y;
  122. SDL_RenderSetClipRect(bd->SDLRenderer, &r);
  123. int xy_stride = sizeof(ImDrawVert);
  124. float *xy = (float *)((char*)vtx_buffer + IM_OFFSETOF(ImDrawVert, pos));
  125. int uv_stride = sizeof(ImDrawVert);
  126. float *uv = (float*)((char*)vtx_buffer + IM_OFFSETOF(ImDrawVert, uv));
  127. int col_stride = sizeof(ImDrawVert);
  128. int *color = (int*)((char*)vtx_buffer + IM_OFFSETOF(ImDrawVert, col));
  129. SDL_Texture *tex = (SDL_Texture*)pcmd->TextureId;
  130. SDL_RenderGeometryRaw(bd->SDLRenderer, tex,
  131. xy, xy_stride, color,
  132. col_stride,
  133. uv, uv_stride,
  134. cmd_list->VtxBuffer.Size,
  135. idx_buffer, pcmd->ElemCount, sizeof (ImDrawIdx));
  136. }
  137. }
  138. idx_buffer += pcmd->ElemCount;
  139. }
  140. }
  141. }
  142. // Called by Init/NewFrame/Shutdown
  143. bool ImGui_ImplSDLRenderer_CreateFontsTexture()
  144. {
  145. // Build texture atlas
  146. ImGuiIO& io = ImGui::GetIO();
  147. ImGui_ImplSDLRenderer_Data* bd = ImGui_ImplSDLRenderer_GetBackendData();
  148. unsigned char* pixels;
  149. int width, height;
  150. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bit (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory.
  151. bd->FontTexture = SDL_CreateTexture(bd->SDLRenderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, width, height);
  152. if (bd->FontTexture == NULL) {
  153. SDL_Log("error creating texture");
  154. return false;
  155. }
  156. SDL_UpdateTexture(bd->FontTexture, NULL, pixels, 4 * width);
  157. SDL_SetTextureBlendMode(bd->FontTexture, SDL_BLENDMODE_BLEND);
  158. // Store our identifier
  159. io.Fonts->SetTexID((ImTextureID)(intptr_t)bd->FontTexture);
  160. return true;
  161. }
  162. void ImGui_ImplSDLRenderer_DestroyFontsTexture()
  163. {
  164. ImGuiIO& io = ImGui::GetIO();
  165. ImGui_ImplSDLRenderer_Data* bd = ImGui_ImplSDLRenderer_GetBackendData();
  166. if (bd->FontTexture) {
  167. io.Fonts->SetTexID(0);
  168. SDL_DestroyTexture(bd->FontTexture);
  169. bd->FontTexture = NULL;
  170. }
  171. }
  172. bool ImGui_ImplSDLRenderer_CreateDeviceObjects()
  173. {
  174. return ImGui_ImplSDLRenderer_CreateFontsTexture();
  175. }
  176. void ImGui_ImplSDLRenderer_DestroyDeviceObjects()
  177. {
  178. ImGui_ImplSDLRenderer_DestroyFontsTexture();
  179. }