Skip to content

Commit d98dce9

Browse files
authored
Merge pull request #59 from tonitch/sdl_renderer
Adding sdl2 and sdl3 SDL_Renderer backend
2 parents c73edd8 + 7c5ccd3 commit d98dce9

11 files changed

+845
-3
lines changed

build.zig

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ pub const Backend = enum {
1111
sdl2_opengl3,
1212
osx_metal,
1313
sdl2,
14+
sdl2_renderer,
1415
sdl3,
15-
sdl3_gpu,
1616
sdl3_opengl3,
17+
sdl3_renderer,
18+
sdl3_gpu,
1719
};
1820

1921
pub fn build(b: *std.Build) void {
@@ -348,6 +350,18 @@ pub fn build(b: *std.Build) void {
348350
.flags = cflags,
349351
});
350352
},
353+
.sdl2_renderer => {
354+
if (b.lazyDependency("zsdl", .{})) |zsdl| {
355+
imgui.addIncludePath(zsdl.path("libs/sdl2/include"));
356+
}
357+
imgui.addCSourceFiles(.{
358+
.files = &.{
359+
"libs/imgui/backends/imgui_impl_sdl2.cpp",
360+
"libs/imgui/backends/imgui_impl_sdlrenderer2.cpp",
361+
},
362+
.flags = cflags,
363+
});
364+
},
351365
.sdl3_gpu => {
352366
if (b.lazyDependency("zsdl", .{})) |zsdl| {
353367
imgui.addIncludePath(zsdl.path("libs/sdl3/include"));
@@ -360,6 +374,18 @@ pub fn build(b: *std.Build) void {
360374
.flags = cflags,
361375
});
362376
},
377+
.sdl3_renderer => {
378+
if (b.lazyDependency("zsdl", .{})) |zsdl| {
379+
imgui.addIncludePath(zsdl.path("libs/sdl3/include"));
380+
}
381+
imgui.addCSourceFiles(.{
382+
.files = &.{
383+
"libs/imgui/backends/imgui_impl_sdl3.cpp",
384+
"libs/imgui/backends/imgui_impl_sdlrenderer3.cpp",
385+
},
386+
.flags = cflags,
387+
});
388+
},
363389
.sdl3_opengl3 => {
364390
if (b.lazyDependency("zsdl", .{})) |zsdl| {
365391
imgui.addIncludePath(zsdl.path("libs/sdl3/include/SDL3"));

libs/imgui/backends/imgui_impl_sdl2.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ static const Uint32 SDL_WINDOW_VULKAN = 0x10000000;
137137
// FIX(zig-gamedev):
138138
extern "C" {
139139

140-
bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context);
141140
bool ImGui_ImplSDL2_InitForOther(SDL_Window* window);
141+
bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context);
142+
bool ImGui_ImplSDL2_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer);
142143
void ImGui_ImplSDL2_Shutdown();
143144
void ImGui_ImplSDL2_NewFrame();
144145
bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event);
Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
// dear imgui: Renderer Backend for SDL_Renderer for SDL2
2+
// (Requires: SDL 2.0.17+)
3+
4+
// Note that SDL_Renderer is an _optional_ component of SDL2, which IMHO is now largely obsolete.
5+
// For a multi-platform app consider using other technologies:
6+
// - SDL3+SDL_GPU: SDL_GPU is SDL3 new graphics abstraction API. You will need to update to SDL3.
7+
// - SDL2+DirectX, SDL2+OpenGL, SDL2+Vulkan: combine SDL with dedicated renderers.
8+
// If your application wants to render any non trivial amount of graphics other than UI,
9+
// please be aware that SDL_Renderer currently offers a limited graphic API to the end-user
10+
// and it might be difficult to step out of those boundaries.
11+
12+
// Implemented features:
13+
// [X] Renderer: User texture binding. Use 'SDL_Texture*' as ImTextureID. Read the FAQ about ImTextureID!
14+
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
15+
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
16+
17+
// You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
18+
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
19+
// Learn about Dear ImGui:
20+
// - FAQ https://dearimgui.com/faq
21+
// - Getting Started https://dearimgui.com/getting-started
22+
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
23+
// - Introduction, links and more at the top of imgui.cpp
24+
25+
// CHANGELOG
26+
// 2025-01-18: Use endian-dependent RGBA32 texture format, to match SDL_Color.
27+
// 2024-10-09: Expose selected render state in ImGui_ImplSDLRenderer2_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks.
28+
// 2024-05-14: *BREAKING CHANGE* ImGui_ImplSDLRenderer3_RenderDrawData() requires SDL_Renderer* passed as parameter.
29+
// 2023-05-30: Renamed imgui_impl_sdlrenderer.h/.cpp to imgui_impl_sdlrenderer2.h/.cpp to accommodate for upcoming SDL3.
30+
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
31+
// 2021-12-21: Update SDL_RenderGeometryRaw() format to work with SDL 2.0.19.
32+
// 2021-12-03: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.
33+
// 2021-10-06: Backup and restore modified ClipRect/Viewport.
34+
// 2021-09-21: Initial version.
35+
36+
#include "imgui.h"
37+
#ifndef IMGUI_DISABLE
38+
#include "imgui_impl_sdlrenderer2.h"
39+
#include <stdint.h> // intptr_t
40+
41+
// Clang warnings with -Weverything
42+
#if defined(__clang__)
43+
#pragma clang diagnostic push
44+
#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
45+
#endif
46+
47+
// SDL
48+
#include <SDL.h>
49+
#if !SDL_VERSION_ATLEAST(2,0,17)
50+
#error This backend requires SDL 2.0.17+ because of SDL_RenderGeometry() function
51+
#endif
52+
53+
// FIX(zig-gamedev):
54+
extern "C" {
55+
56+
bool ImGui_ImplSDLRenderer2_Init(SDL_Renderer* renderer);
57+
void ImGui_ImplSDLRenderer2_Shutdown();
58+
void ImGui_ImplSDLRenderer2_NewFrame();
59+
void ImGui_ImplSDLRenderer2_RenderDrawData(ImDrawData* draw_data, SDL_Renderer* renderer);
60+
61+
}
62+
63+
// SDL_Renderer data
64+
struct ImGui_ImplSDLRenderer2_Data
65+
{
66+
SDL_Renderer* Renderer; // Main viewport's renderer
67+
SDL_Texture* FontTexture;
68+
ImGui_ImplSDLRenderer2_Data() { memset((void*)this, 0, sizeof(*this)); }
69+
};
70+
71+
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
72+
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
73+
static ImGui_ImplSDLRenderer2_Data* ImGui_ImplSDLRenderer2_GetBackendData()
74+
{
75+
return ImGui::GetCurrentContext() ? (ImGui_ImplSDLRenderer2_Data*)ImGui::GetIO().BackendRendererUserData : nullptr;
76+
}
77+
78+
// Functions
79+
bool ImGui_ImplSDLRenderer2_Init(SDL_Renderer* renderer)
80+
{
81+
ImGuiIO& io = ImGui::GetIO();
82+
IMGUI_CHECKVERSION();
83+
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
84+
IM_ASSERT(renderer != nullptr && "SDL_Renderer not initialized!");
85+
86+
// Setup backend capabilities flags
87+
ImGui_ImplSDLRenderer2_Data* bd = IM_NEW(ImGui_ImplSDLRenderer2_Data)();
88+
io.BackendRendererUserData = (void*)bd;
89+
io.BackendRendererName = "imgui_impl_sdlrenderer2";
90+
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
91+
92+
bd->Renderer = renderer;
93+
94+
return true;
95+
}
96+
97+
void ImGui_ImplSDLRenderer2_Shutdown()
98+
{
99+
ImGui_ImplSDLRenderer2_Data* bd = ImGui_ImplSDLRenderer2_GetBackendData();
100+
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
101+
ImGuiIO& io = ImGui::GetIO();
102+
103+
ImGui_ImplSDLRenderer2_DestroyDeviceObjects();
104+
105+
io.BackendRendererName = nullptr;
106+
io.BackendRendererUserData = nullptr;
107+
io.BackendFlags &= ~ImGuiBackendFlags_RendererHasVtxOffset;
108+
IM_DELETE(bd);
109+
}
110+
111+
static void ImGui_ImplSDLRenderer2_SetupRenderState(SDL_Renderer* renderer)
112+
{
113+
// Clear out any viewports and cliprect set by the user
114+
// FIXME: Technically speaking there are lots of other things we could backup/setup/restore during our render process.
115+
SDL_RenderSetViewport(renderer, nullptr);
116+
SDL_RenderSetClipRect(renderer, nullptr);
117+
}
118+
119+
void ImGui_ImplSDLRenderer2_NewFrame()
120+
{
121+
ImGui_ImplSDLRenderer2_Data* bd = ImGui_ImplSDLRenderer2_GetBackendData();
122+
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDLRenderer2_Init()?");
123+
124+
if (!bd->FontTexture)
125+
ImGui_ImplSDLRenderer2_CreateDeviceObjects();
126+
}
127+
128+
void ImGui_ImplSDLRenderer2_RenderDrawData(ImDrawData* draw_data, SDL_Renderer* renderer)
129+
{
130+
// If there's a scale factor set by the user, use that instead
131+
// If the user has specified a scale factor to SDL_Renderer already via SDL_RenderSetScale(), SDL will scale whatever we pass
132+
// to SDL_RenderGeometryRaw() by that scale factor. In that case we don't want to be also scaling it ourselves here.
133+
float rsx = 1.0f;
134+
float rsy = 1.0f;
135+
SDL_RenderGetScale(renderer, &rsx, &rsy);
136+
ImVec2 render_scale;
137+
render_scale.x = (rsx == 1.0f) ? draw_data->FramebufferScale.x : 1.0f;
138+
render_scale.y = (rsy == 1.0f) ? draw_data->FramebufferScale.y : 1.0f;
139+
140+
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
141+
int fb_width = (int)(draw_data->DisplaySize.x * render_scale.x);
142+
int fb_height = (int)(draw_data->DisplaySize.y * render_scale.y);
143+
if (fb_width == 0 || fb_height == 0)
144+
return;
145+
146+
// Backup SDL_Renderer state that will be modified to restore it afterwards
147+
struct BackupSDLRendererState
148+
{
149+
SDL_Rect Viewport;
150+
bool ClipEnabled;
151+
SDL_Rect ClipRect;
152+
};
153+
BackupSDLRendererState old = {};
154+
old.ClipEnabled = SDL_RenderIsClipEnabled(renderer) == SDL_TRUE;
155+
SDL_RenderGetViewport(renderer, &old.Viewport);
156+
SDL_RenderGetClipRect(renderer, &old.ClipRect);
157+
158+
// Setup desired state
159+
ImGui_ImplSDLRenderer2_SetupRenderState(renderer);
160+
161+
// Setup render state structure (for callbacks and custom texture bindings)
162+
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
163+
ImGui_ImplSDLRenderer2_RenderState render_state;
164+
render_state.Renderer = renderer;
165+
platform_io.Renderer_RenderState = &render_state;
166+
167+
// Will project scissor/clipping rectangles into framebuffer space
168+
ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
169+
ImVec2 clip_scale = render_scale;
170+
171+
// Render command lists
172+
for (int n = 0; n < draw_data->CmdListsCount; n++)
173+
{
174+
const ImDrawList* draw_list = draw_data->CmdLists[n];
175+
const ImDrawVert* vtx_buffer = draw_list->VtxBuffer.Data;
176+
const ImDrawIdx* idx_buffer = draw_list->IdxBuffer.Data;
177+
178+
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
179+
{
180+
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
181+
if (pcmd->UserCallback)
182+
{
183+
// User callback, registered via ImDrawList::AddCallback()
184+
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
185+
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
186+
ImGui_ImplSDLRenderer2_SetupRenderState(renderer);
187+
else
188+
pcmd->UserCallback(draw_list, pcmd);
189+
}
190+
else
191+
{
192+
// Project scissor/clipping rectangles into framebuffer space
193+
ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
194+
ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
195+
if (clip_min.x < 0.0f) { clip_min.x = 0.0f; }
196+
if (clip_min.y < 0.0f) { clip_min.y = 0.0f; }
197+
if (clip_max.x > (float)fb_width) { clip_max.x = (float)fb_width; }
198+
if (clip_max.y > (float)fb_height) { clip_max.y = (float)fb_height; }
199+
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
200+
continue;
201+
202+
SDL_Rect r = { (int)(clip_min.x), (int)(clip_min.y), (int)(clip_max.x - clip_min.x), (int)(clip_max.y - clip_min.y) };
203+
SDL_RenderSetClipRect(renderer, &r);
204+
205+
const float* xy = (const float*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, pos));
206+
const float* uv = (const float*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, uv));
207+
#if SDL_VERSION_ATLEAST(2,0,19)
208+
const SDL_Color* color = (const SDL_Color*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, col)); // SDL 2.0.19+
209+
#else
210+
const int* color = (const int*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, col)); // SDL 2.0.17 and 2.0.18
211+
#endif
212+
213+
// Bind texture, Draw
214+
SDL_Texture* tex = (SDL_Texture*)pcmd->GetTexID();
215+
SDL_RenderGeometryRaw(renderer, tex,
216+
xy, (int)sizeof(ImDrawVert),
217+
color, (int)sizeof(ImDrawVert),
218+
uv, (int)sizeof(ImDrawVert),
219+
draw_list->VtxBuffer.Size - pcmd->VtxOffset,
220+
idx_buffer + pcmd->IdxOffset, pcmd->ElemCount, sizeof(ImDrawIdx));
221+
}
222+
}
223+
}
224+
platform_io.Renderer_RenderState = nullptr;
225+
226+
// Restore modified SDL_Renderer state
227+
SDL_RenderSetViewport(renderer, &old.Viewport);
228+
SDL_RenderSetClipRect(renderer, old.ClipEnabled ? &old.ClipRect : nullptr);
229+
}
230+
231+
// Called by Init/NewFrame/Shutdown
232+
bool ImGui_ImplSDLRenderer2_CreateFontsTexture()
233+
{
234+
ImGuiIO& io = ImGui::GetIO();
235+
ImGui_ImplSDLRenderer2_Data* bd = ImGui_ImplSDLRenderer2_GetBackendData();
236+
237+
// Build texture atlas
238+
unsigned char* pixels;
239+
int width, height;
240+
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.
241+
242+
// Upload texture to graphics system
243+
// (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
244+
bd->FontTexture = SDL_CreateTexture(bd->Renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, width, height);
245+
if (bd->FontTexture == nullptr)
246+
{
247+
SDL_Log("error creating texture");
248+
return false;
249+
}
250+
SDL_UpdateTexture(bd->FontTexture, nullptr, pixels, 4 * width);
251+
SDL_SetTextureBlendMode(bd->FontTexture, SDL_BLENDMODE_BLEND);
252+
SDL_SetTextureScaleMode(bd->FontTexture, SDL_ScaleModeLinear);
253+
254+
// Store our identifier
255+
io.Fonts->SetTexID((ImTextureID)(intptr_t)bd->FontTexture);
256+
257+
return true;
258+
}
259+
260+
void ImGui_ImplSDLRenderer2_DestroyFontsTexture()
261+
{
262+
ImGuiIO& io = ImGui::GetIO();
263+
ImGui_ImplSDLRenderer2_Data* bd = ImGui_ImplSDLRenderer2_GetBackendData();
264+
if (bd->FontTexture)
265+
{
266+
io.Fonts->SetTexID(0);
267+
SDL_DestroyTexture(bd->FontTexture);
268+
bd->FontTexture = nullptr;
269+
}
270+
}
271+
272+
bool ImGui_ImplSDLRenderer2_CreateDeviceObjects()
273+
{
274+
return ImGui_ImplSDLRenderer2_CreateFontsTexture();
275+
}
276+
277+
void ImGui_ImplSDLRenderer2_DestroyDeviceObjects()
278+
{
279+
ImGui_ImplSDLRenderer2_DestroyFontsTexture();
280+
}
281+
282+
//-----------------------------------------------------------------------------
283+
284+
#if defined(__clang__)
285+
#pragma clang diagnostic pop
286+
#endif
287+
288+
#endif // #ifndef IMGUI_DISABLE

0 commit comments

Comments
 (0)