|
@@ -1,12 +1,11 @@
|
|
-// dear imgui: Renderer Backend for SDL_Renderer
|
|
|
|
|
|
+// dear imgui: Renderer Backend for SDL_Renderer for SDL3
|
|
// (Requires: SDL 3.0.0+)
|
|
// (Requires: SDL 3.0.0+)
|
|
|
|
|
|
-// Important to understand: SDL_Renderer is an _optional_ component of SDL.
|
|
|
|
|
|
+// Note how SDL_Renderer is an _optional_ component of SDL3.
|
|
// For a multi-platform app consider using e.g. SDL+DirectX on Windows and SDL+OpenGL on Linux/OSX.
|
|
// For a multi-platform app consider using e.g. SDL+DirectX on Windows and SDL+OpenGL on Linux/OSX.
|
|
// If your application will want to render any non trivial amount of graphics other than UI,
|
|
// If your application will want to render any non trivial amount of graphics other than UI,
|
|
-// please be aware that SDL_Renderer offers a limited graphic API to the end-user and it might
|
|
|
|
-// be difficult to step out of those boundaries.
|
|
|
|
-// However, we understand it is a convenient choice to get an app started easily.
|
|
|
|
|
|
+// please be aware that SDL_Renderer currently offers a limited graphic API to the end-user and
|
|
|
|
+// it might be difficult to step out of those boundaries.
|
|
|
|
|
|
// Implemented features:
|
|
// Implemented features:
|
|
// [X] Renderer: User texture binding. Use 'SDL_Texture*' as ImTextureID. Read the FAQ about ImTextureID!
|
|
// [X] Renderer: User texture binding. Use 'SDL_Texture*' as ImTextureID. Read the FAQ about ImTextureID!
|
|
@@ -17,6 +16,7 @@
|
|
// Read online: https://github.com/ocornut/imgui/tree/master/docs
|
|
// Read online: https://github.com/ocornut/imgui/tree/master/docs
|
|
|
|
|
|
// CHANGELOG
|
|
// CHANGELOG
|
|
|
|
+// 2023-05-30: Initial version.
|
|
|
|
|
|
#include "imgui.h"
|
|
#include "imgui.h"
|
|
#include "imgui_impl_sdlrenderer3.h"
|
|
#include "imgui_impl_sdlrenderer3.h"
|
|
@@ -39,31 +39,31 @@
|
|
#endif
|
|
#endif
|
|
|
|
|
|
// SDL_Renderer data
|
|
// SDL_Renderer data
|
|
-struct ImGui_ImplSDLRenderer_Data
|
|
|
|
|
|
+struct ImGui_ImplSDLRenderer3_Data
|
|
{
|
|
{
|
|
SDL_Renderer* SDLRenderer;
|
|
SDL_Renderer* SDLRenderer;
|
|
SDL_Texture* FontTexture;
|
|
SDL_Texture* FontTexture;
|
|
- ImGui_ImplSDLRenderer_Data() { memset((void*)this, 0, sizeof(*this)); }
|
|
|
|
|
|
+ ImGui_ImplSDLRenderer3_Data() { memset((void*)this, 0, sizeof(*this)); }
|
|
};
|
|
};
|
|
|
|
|
|
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
|
|
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
|
|
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
|
|
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
|
|
-static ImGui_ImplSDLRenderer_Data* ImGui_ImplSDLRenderer_GetBackendData()
|
|
|
|
|
|
+static ImGui_ImplSDLRenderer3_Data* ImGui_ImplSDLRenderer3_GetBackendData()
|
|
{
|
|
{
|
|
- return ImGui::GetCurrentContext() ? (ImGui_ImplSDLRenderer_Data*)ImGui::GetIO().BackendRendererUserData : nullptr;
|
|
|
|
|
|
+ return ImGui::GetCurrentContext() ? (ImGui_ImplSDLRenderer3_Data*)ImGui::GetIO().BackendRendererUserData : nullptr;
|
|
}
|
|
}
|
|
|
|
|
|
// Functions
|
|
// Functions
|
|
-bool ImGui_ImplSDLRenderer_Init(SDL_Renderer* renderer)
|
|
|
|
|
|
+bool ImGui_ImplSDLRenderer3_Init(SDL_Renderer* renderer)
|
|
{
|
|
{
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
|
|
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
|
|
IM_ASSERT(renderer != nullptr && "SDL_Renderer not initialized!");
|
|
IM_ASSERT(renderer != nullptr && "SDL_Renderer not initialized!");
|
|
|
|
|
|
// Setup backend capabilities flags
|
|
// Setup backend capabilities flags
|
|
- ImGui_ImplSDLRenderer_Data* bd = IM_NEW(ImGui_ImplSDLRenderer_Data)();
|
|
|
|
|
|
+ ImGui_ImplSDLRenderer3_Data* bd = IM_NEW(ImGui_ImplSDLRenderer3_Data)();
|
|
io.BackendRendererUserData = (void*)bd;
|
|
io.BackendRendererUserData = (void*)bd;
|
|
- io.BackendRendererName = "imgui_impl_sdlrenderer";
|
|
|
|
|
|
+ io.BackendRendererName = "imgui_impl_sdlrenderer3";
|
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
|
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
|
|
|
|
|
|
bd->SDLRenderer = renderer;
|
|
bd->SDLRenderer = renderer;
|
|
@@ -71,22 +71,23 @@ bool ImGui_ImplSDLRenderer_Init(SDL_Renderer* renderer)
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
-void ImGui_ImplSDLRenderer_Shutdown()
|
|
|
|
|
|
+void ImGui_ImplSDLRenderer3_Shutdown()
|
|
{
|
|
{
|
|
- ImGui_ImplSDLRenderer_Data* bd = ImGui_ImplSDLRenderer_GetBackendData();
|
|
|
|
|
|
+ ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData();
|
|
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
|
|
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
|
|
- ImGui_ImplSDLRenderer_DestroyDeviceObjects();
|
|
|
|
|
|
+ ImGui_ImplSDLRenderer3_DestroyDeviceObjects();
|
|
|
|
|
|
io.BackendRendererName = nullptr;
|
|
io.BackendRendererName = nullptr;
|
|
io.BackendRendererUserData = nullptr;
|
|
io.BackendRendererUserData = nullptr;
|
|
|
|
+ io.BackendFlags &= ~ImGuiBackendFlags_RendererHasVtxOffset;
|
|
IM_DELETE(bd);
|
|
IM_DELETE(bd);
|
|
}
|
|
}
|
|
|
|
|
|
-static void ImGui_ImplSDLRenderer_SetupRenderState()
|
|
|
|
|
|
+static void ImGui_ImplSDLRenderer3_SetupRenderState()
|
|
{
|
|
{
|
|
- ImGui_ImplSDLRenderer_Data* bd = ImGui_ImplSDLRenderer_GetBackendData();
|
|
|
|
|
|
+ ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData();
|
|
|
|
|
|
// Clear out any viewports and cliprect set by the user
|
|
// Clear out any viewports and cliprect set by the user
|
|
// FIXME: Technically speaking there are lots of other things we could backup/setup/restore during our render process.
|
|
// FIXME: Technically speaking there are lots of other things we could backup/setup/restore during our render process.
|
|
@@ -94,18 +95,18 @@ static void ImGui_ImplSDLRenderer_SetupRenderState()
|
|
SDL_SetRenderClipRect(bd->SDLRenderer, nullptr);
|
|
SDL_SetRenderClipRect(bd->SDLRenderer, nullptr);
|
|
}
|
|
}
|
|
|
|
|
|
-void ImGui_ImplSDLRenderer_NewFrame()
|
|
|
|
|
|
+void ImGui_ImplSDLRenderer3_NewFrame()
|
|
{
|
|
{
|
|
- ImGui_ImplSDLRenderer_Data* bd = ImGui_ImplSDLRenderer_GetBackendData();
|
|
|
|
- IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplSDLRenderer_Init()?");
|
|
|
|
|
|
+ ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData();
|
|
|
|
+ IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplSDLRenderer3_Init()?");
|
|
|
|
|
|
if (!bd->FontTexture)
|
|
if (!bd->FontTexture)
|
|
- ImGui_ImplSDLRenderer_CreateDeviceObjects();
|
|
|
|
|
|
+ ImGui_ImplSDLRenderer3_CreateDeviceObjects();
|
|
}
|
|
}
|
|
|
|
|
|
-void ImGui_ImplSDLRenderer_RenderDrawData(ImDrawData* draw_data)
|
|
|
|
|
|
+void ImGui_ImplSDLRenderer3_RenderDrawData(ImDrawData* draw_data)
|
|
{
|
|
{
|
|
- ImGui_ImplSDLRenderer_Data* bd = ImGui_ImplSDLRenderer_GetBackendData();
|
|
|
|
|
|
+ ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData();
|
|
|
|
|
|
// If there's a scale factor set by the user, use that instead
|
|
// If there's a scale factor set by the user, use that instead
|
|
// If the user has specified a scale factor to SDL_Renderer already via SDL_RenderSetScale(), SDL will scale whatever we pass
|
|
// If the user has specified a scale factor to SDL_Renderer already via SDL_RenderSetScale(), SDL will scale whatever we pass
|
|
@@ -140,7 +141,7 @@ void ImGui_ImplSDLRenderer_RenderDrawData(ImDrawData* draw_data)
|
|
ImVec2 clip_scale = render_scale;
|
|
ImVec2 clip_scale = render_scale;
|
|
|
|
|
|
// Render command lists
|
|
// Render command lists
|
|
- ImGui_ImplSDLRenderer_SetupRenderState();
|
|
|
|
|
|
+ ImGui_ImplSDLRenderer3_SetupRenderState();
|
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
|
{
|
|
{
|
|
const ImDrawList* cmd_list = draw_data->CmdLists[n];
|
|
const ImDrawList* cmd_list = draw_data->CmdLists[n];
|
|
@@ -155,7 +156,7 @@ void ImGui_ImplSDLRenderer_RenderDrawData(ImDrawData* draw_data)
|
|
// User callback, registered via ImDrawList::AddCallback()
|
|
// User callback, registered via ImDrawList::AddCallback()
|
|
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
|
|
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
|
|
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
|
|
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
|
|
- ImGui_ImplSDLRenderer_SetupRenderState();
|
|
|
|
|
|
+ ImGui_ImplSDLRenderer3_SetupRenderState();
|
|
else
|
|
else
|
|
pcmd->UserCallback(cmd_list, pcmd);
|
|
pcmd->UserCallback(cmd_list, pcmd);
|
|
}
|
|
}
|
|
@@ -200,10 +201,10 @@ void ImGui_ImplSDLRenderer_RenderDrawData(ImDrawData* draw_data)
|
|
}
|
|
}
|
|
|
|
|
|
// Called by Init/NewFrame/Shutdown
|
|
// Called by Init/NewFrame/Shutdown
|
|
-bool ImGui_ImplSDLRenderer_CreateFontsTexture()
|
|
|
|
|
|
+bool ImGui_ImplSDLRenderer3_CreateFontsTexture()
|
|
{
|
|
{
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
- ImGui_ImplSDLRenderer_Data* bd = ImGui_ImplSDLRenderer_GetBackendData();
|
|
|
|
|
|
+ ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData();
|
|
|
|
|
|
// Build texture atlas
|
|
// Build texture atlas
|
|
unsigned char* pixels;
|
|
unsigned char* pixels;
|
|
@@ -228,10 +229,10 @@ bool ImGui_ImplSDLRenderer_CreateFontsTexture()
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
-void ImGui_ImplSDLRenderer_DestroyFontsTexture()
|
|
|
|
|
|
+void ImGui_ImplSDLRenderer3_DestroyFontsTexture()
|
|
{
|
|
{
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
- ImGui_ImplSDLRenderer_Data* bd = ImGui_ImplSDLRenderer_GetBackendData();
|
|
|
|
|
|
+ ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData();
|
|
if (bd->FontTexture)
|
|
if (bd->FontTexture)
|
|
{
|
|
{
|
|
io.Fonts->SetTexID(0);
|
|
io.Fonts->SetTexID(0);
|
|
@@ -240,14 +241,14 @@ void ImGui_ImplSDLRenderer_DestroyFontsTexture()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-bool ImGui_ImplSDLRenderer_CreateDeviceObjects()
|
|
|
|
|
|
+bool ImGui_ImplSDLRenderer3_CreateDeviceObjects()
|
|
{
|
|
{
|
|
- return ImGui_ImplSDLRenderer_CreateFontsTexture();
|
|
|
|
|
|
+ return ImGui_ImplSDLRenderer3_CreateFontsTexture();
|
|
}
|
|
}
|
|
|
|
|
|
-void ImGui_ImplSDLRenderer_DestroyDeviceObjects()
|
|
|
|
|
|
+void ImGui_ImplSDLRenderer3_DestroyDeviceObjects()
|
|
{
|
|
{
|
|
- ImGui_ImplSDLRenderer_DestroyFontsTexture();
|
|
|
|
|
|
+ ImGui_ImplSDLRenderer3_DestroyFontsTexture();
|
|
}
|
|
}
|
|
|
|
|
|
#if defined(__clang__)
|
|
#if defined(__clang__)
|