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