dear-imgui-impl-sdlrenderer2 (empty) → 1.0.0
raw patch · 12 files changed
+856/−0 lines, 12 filesdep +basedep +dear-imgui-rawdep +sdl2
Dependencies added: base, dear-imgui-raw, sdl2, unliftio-core
Files
- CHANGELOG.md +13/−0
- LICENSE +26/−0
- README.md +6/−0
- cbits/DearImGuiWrappers.cpp +3/−0
- cbits/DearImGuiWrappers.h +16/−0
- cbits/dcimgui_impl_sdlrenderer2.cpp +138/−0
- cbits/dcimgui_impl_sdlrenderer2.h +86/−0
- cbits/imgui_impl_sdlrenderer2.cpp +317/−0
- cbits/imgui_impl_sdlrenderer2.h +57/−0
- dear-imgui-impl-sdlrenderer2.cabal +68/−0
- src/DearImGui/Impl/SDLRenderer2.hs +40/−0
- src/DearImGui/Raw/Impl/SDLRenderer2.hsc +86/−0
+ CHANGELOG.md view
@@ -0,0 +1,13 @@+# Changelog for `dear-imgui-impl-sdlrenderer2`++All notable changes to this package will be documented in this file.++The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),+and this project adheres to the+[Haskell Package Versioning Policy](https://pvp.haskell.org/).++## 1.0.0 - 2026-10-21++Initial release.++- Generated with dear_bindings `v0.26` from Dear ImGui `v1.93.0-docking`.
+ LICENSE view
@@ -0,0 +1,26 @@+Copyright 2026 IC Rainbow++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this+ list of conditions and the following disclaimer.++2. Redistributions in binary form must reproduce the above copyright notice,+ this list of conditions and the following disclaimer in the documentation+ and/or other materials provided with the distribution.++3. Neither the name of the copyright holder nor the names of its contributors+ may be used to endorse or promote products derived from this software+ without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,6 @@+# dear-imgui-impl-sdlrenderer2++Renderer backend for [Dear ImGui] on SDL2's `SDL_Renderer`.+Pair it with the SDL2 platform backend.++[Dear ImGui]: https://github.com/ocornut/imgui
+ cbits/DearImGuiWrappers.cpp view
@@ -0,0 +1,3 @@+// Generated by dear-bindings-ffi. Do not edit.+#include "DearImGuiWrappers.h"+
+ cbits/DearImGuiWrappers.h view
@@ -0,0 +1,16 @@+// Generated by dear-bindings-ffi. Do not edit.+#ifndef DEAR_IMGUI_WRAPPERS_H+#define DEAR_IMGUI_WRAPPERS_H++#include "dcimgui_impl_sdlrenderer2.h"++#ifdef __cplusplus+extern "C" {+#endif+++#ifdef __cplusplus+}+#endif++#endif
+ cbits/dcimgui_impl_sdlrenderer2.cpp view
@@ -0,0 +1,138 @@+// THIS FILE HAS BEEN AUTO-GENERATED BY THE 'DEAR BINDINGS' GENERATOR.+// **DO NOT EDIT DIRECTLY**+// https://github.com/dearimgui/dear_bindings++#include "imgui.h"+#include "imgui_impl_sdlrenderer2.h"++#include <stdio.h>++// Wrap this in a namespace to keep it separate from the C++ API+// This define prevents #defines in the header getting defined again (as they are already in the normal header above),+// and thus generating redefinition warnings+#define DEAR_BINDINGS_INTERNAL_GLUE_CODE+namespace cimgui+{+#include "dcimgui_impl_sdlrenderer2.h"+}+#undef DEAR_BINDINGS_INTERNAL_GLUE_CODE++// By-value struct conversions++static inline cimgui::ImVec2 ConvertFromCPP_ImVec2(const ::ImVec2& src)+{+ cimgui::ImVec2 dest;+ dest.x = src.x;+ dest.y = src.y;+ return dest;+}++static inline ::ImVec2 ConvertToCPP_ImVec2(const cimgui::ImVec2& src)+{+ ::ImVec2 dest;+ dest.x = src.x;+ dest.y = src.y;+ return dest;+}++static inline cimgui::ImVec4 ConvertFromCPP_ImVec4(const ::ImVec4& src)+{+ cimgui::ImVec4 dest;+ dest.x = src.x;+ dest.y = src.y;+ dest.z = src.z;+ dest.w = src.w;+ return dest;+}++static inline ::ImVec4 ConvertToCPP_ImVec4(const cimgui::ImVec4& src)+{+ ::ImVec4 dest;+ dest.x = src.x;+ dest.y = src.y;+ dest.z = src.z;+ dest.w = src.w;+ return dest;+}++static inline cimgui::ImTextureRef ConvertFromCPP_ImTextureRef(const ::ImTextureRef& src)+{+ cimgui::ImTextureRef dest;+ dest._TexData = reinterpret_cast<cimgui::ImTextureData*>(src._TexData);+ dest._TexID = src._TexID;+ return dest;+}++static inline ::ImTextureRef ConvertToCPP_ImTextureRef(const cimgui::ImTextureRef& src)+{+ ::ImTextureRef dest;+ dest._TexData = reinterpret_cast<::ImTextureData*>(src._TexData);+ dest._TexID = src._TexID;+ return dest;+}++static inline cimgui::ImColor ConvertFromCPP_ImColor(const ::ImColor& src)+{+ cimgui::ImColor dest;+ dest.Value.x = src.Value.x;+ dest.Value.y = src.Value.y;+ dest.Value.z = src.Value.z;+ dest.Value.w = src.Value.w;+ return dest;+}++static inline ::ImColor ConvertToCPP_ImColor(const cimgui::ImColor& src)+{+ ::ImColor dest;+ dest.Value.x = src.Value.x;+ dest.Value.y = src.Value.y;+ dest.Value.z = src.Value.z;+ dest.Value.w = src.Value.w;+ return dest;+}++// Function stubs++#ifndef IMGUI_DISABLE++CIMGUI_IMPL_API bool cimgui::cImGui_ImplSDLRenderer2_Init(cimgui::SDL_Renderer* renderer)+{+ return ::ImGui_ImplSDLRenderer2_Init(reinterpret_cast<::SDL_Renderer*>(renderer));+}++CIMGUI_IMPL_API void cimgui::cImGui_ImplSDLRenderer2_Shutdown(void)+{+ ::ImGui_ImplSDLRenderer2_Shutdown();+}++CIMGUI_IMPL_API void cimgui::cImGui_ImplSDLRenderer2_NewFrame(void)+{+ ::ImGui_ImplSDLRenderer2_NewFrame();+}++CIMGUI_IMPL_API void cimgui::cImGui_ImplSDLRenderer2_RenderDrawData(cimgui::ImDrawData* draw_data, cimgui::SDL_Renderer* renderer)+{+ ::ImGui_ImplSDLRenderer2_RenderDrawData(reinterpret_cast<::ImDrawData*>(draw_data), reinterpret_cast<::SDL_Renderer*>(renderer));+}++CIMGUI_IMPL_API void cimgui::cImGui_ImplSDLRenderer2_CreateDeviceObjects(void)+{+ ::ImGui_ImplSDLRenderer2_CreateDeviceObjects();+}++CIMGUI_IMPL_API void cimgui::cImGui_ImplSDLRenderer2_DestroyDeviceObjects(void)+{+ ::ImGui_ImplSDLRenderer2_DestroyDeviceObjects();+}++CIMGUI_IMPL_API void cimgui::cImGui_ImplSDLRenderer2_UpdateTexture(cimgui::ImTextureData* tex)+{+ ::ImGui_ImplSDLRenderer2_UpdateTexture(reinterpret_cast<::ImTextureData*>(tex));+}++CIMGUI_IMPL_API cimgui::ImGui_ImplSDLRenderer2_RenderState* cimgui::cImGui_ImplSDLRenderer2_GetRenderState(void)+{+ return reinterpret_cast<::cimgui::ImGui_ImplSDLRenderer2_RenderState*>(::ImGui_ImplSDLRenderer2_GetRenderState());+}++#endif // #ifndef IMGUI_DISABLE
+ cbits/dcimgui_impl_sdlrenderer2.h view
@@ -0,0 +1,86 @@+// THIS FILE HAS BEEN AUTO-GENERATED BY THE 'DEAR BINDINGS' GENERATOR.+// **DO NOT EDIT DIRECTLY**+// https://github.com/dearimgui/dear_bindings++// Dear Bindings version as a string+#ifndef DEAR_BINDINGS_VERSION+#define DEAR_BINDINGS_VERSION "0.26"+#endif++// Dear Bindings version as an integer+#ifndef DEAR_BINDINGS_VERSION_NUMBER+#define DEAR_BINDINGS_VERSION_NUMBER 26+#endif++// dear imgui: Renderer Backend for SDL_Renderer for SDL2+// Auto-generated forward declarations for C header+typedef struct ImGui_ImplSDLRenderer2_RenderState_t ImGui_ImplSDLRenderer2_RenderState;+// ImDrawIdx: vertex index. [Compile-time configurable type]+// - To use 16-bit indices + allow large meshes: backend need to set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset' and handle ImDrawCmd::VtxOffset (recommended).+// - To use 32-bit indices: override with '#define ImDrawIdx unsigned int' in your imconfig.h file.+#ifndef ImDrawIdx+typedef unsigned short ImDrawIdx; // Default: 16-bit (for maximum compatibility with renderer backends)+#endif // #ifndef ImDrawIdx+// (Requires: SDL 2.0.17+)++// Note that SDL_Renderer is an _optional_ component of SDL2, which IMHO is now largely obsolete.+// For a multi-platform app consider using other technologies:+// - SDL3+SDL_GPU: SDL_GPU is SDL3 new graphics abstraction API. You will need to update to SDL3.+// - SDL2+DirectX, SDL2+OpenGL, SDL2+Vulkan: combine SDL with dedicated renderers.+// If your application wants to render any non trivial amount of graphics other than UI,+// 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:+// [X] Renderer: User texture binding. Use 'SDL_Texture*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!+// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).+// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.+// Missing features or Issues:+// [ ] Renderer: Multi-viewport support (multiple windows).+// [ ] Renderer: Missing support for DrawCallback_SetSamplerLinear, DrawCallback_SetSamplerNearest callbacks: SDLRenderer2 does not support changing SDL_SCALE_MODE while rendering.++// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.+// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.+// Learn about Dear ImGui:+// - FAQ https://dearimgui.com/faq+// - Getting Started https://dearimgui.com/getting-started+// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).+// - Introduction, links and more at the top of imgui.cpp++#pragma once++#ifdef __cplusplus+extern "C"+{+#endif+#ifndef IMGUI_DISABLE+#include "dcimgui.h"+typedef struct SDL_Renderer SDL_Renderer;++typedef struct ImDrawData_t ImDrawData;+// Follow "Getting Started" link and check examples/ folder to learn about using backends!+CIMGUI_IMPL_API bool cImGui_ImplSDLRenderer2_Init(SDL_Renderer* renderer);+CIMGUI_IMPL_API void cImGui_ImplSDLRenderer2_Shutdown(void);+CIMGUI_IMPL_API void cImGui_ImplSDLRenderer2_NewFrame(void);+CIMGUI_IMPL_API void cImGui_ImplSDLRenderer2_RenderDrawData(ImDrawData* draw_data, SDL_Renderer* renderer);++// Called by Init/NewFrame/Shutdown+CIMGUI_IMPL_API void cImGui_ImplSDLRenderer2_CreateDeviceObjects(void);+CIMGUI_IMPL_API void cImGui_ImplSDLRenderer2_DestroyDeviceObjects(void);++// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = nullptr to handle this manually.+CIMGUI_IMPL_API void cImGui_ImplSDLRenderer2_UpdateTexture(ImTextureData* tex);++// [BETA] Selected render state data shared with callbacks.+// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplSDLRenderer2_RenderDrawData() call.+// (Please open an issue if you feel you need access to more data)+struct ImGui_ImplSDLRenderer2_RenderState_t+{+ SDL_Renderer* Renderer;+};+CIMGUI_IMPL_API ImGui_ImplSDLRenderer2_RenderState* cImGui_ImplSDLRenderer2_GetRenderState(void);+#endif// #ifndef IMGUI_DISABLE+#ifdef __cplusplus+} // End of extern "C" block+#endif
+ cbits/imgui_impl_sdlrenderer2.cpp view
@@ -0,0 +1,317 @@+// dear imgui: Renderer Backend for SDL_Renderer for SDL2+// (Requires: SDL 2.0.17+)++// Note that SDL_Renderer is an _optional_ component of SDL2, which IMHO is now largely obsolete.+// For a multi-platform app consider using other technologies:+// - SDL3+SDL_GPU: SDL_GPU is SDL3 new graphics abstraction API. You will need to update to SDL3.+// - SDL2+DirectX, SDL2+OpenGL, SDL2+Vulkan: combine SDL with dedicated renderers.+// If your application wants to render any non trivial amount of graphics other than UI,+// 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:+// [X] Renderer: User texture binding. Use 'SDL_Texture*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!+// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).+// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.+// Missing features or Issues:+// [ ] Renderer: Multi-viewport support (multiple windows).+// [ ] Renderer: Missing support for DrawCallback_SetSamplerLinear, DrawCallback_SetSamplerNearest callbacks: SDLRenderer2 does not support changing SDL_SCALE_MODE while rendering.++// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.+// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.+// Learn about Dear ImGui:+// - FAQ https://dearimgui.com/faq+// - Getting Started https://dearimgui.com/getting-started+// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).+// - Introduction, links and more at the top of imgui.cpp++// CHANGELOG+// (minor and older changes stripped away, please see git history for details)+// 2026-04-23: Added support for standard draw callbacks (in platform_io): DrawCallback_ResetRenderState (others cannot be supported). (#9378)+// 2026-03-12: Fixed invalid assert in ImGui_ImplSDLRenderer2_UpdateTexture() if ImTextureID_Invalid is defined to be != 0, which became the default since 2026-03-12. (#9295)+// 2025-09-18: Call platform_io.ClearRendererHandlers() on shutdown.+// 2025-06-11: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplSDLRenderer2_CreateFontsTexture() and ImGui_ImplSDLRenderer2_DestroyFontsTexture().+// 2025-01-18: Use endian-dependent RGBA32 texture format, to match SDL_Color.+// 2024-10-09: Expose selected render state in ImGui_ImplSDLRenderer2_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks.+// 2024-05-14: *BREAKING CHANGE* ImGui_ImplSDLRenderer3_RenderDrawData() requires SDL_Renderer* passed as parameter.+// 2023-05-30: Renamed imgui_impl_sdlrenderer.h/.cpp to imgui_impl_sdlrenderer2.h/.cpp to accommodate for upcoming SDL3.+// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.+// 2021-12-21: Update SDL_RenderGeometryRaw() format to work with SDL 2.0.19.+// 2021-12-03: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.+// 2021-10-06: Backup and restore modified ClipRect/Viewport.+// 2021-09-21: Initial version.++#include "imgui.h"+#ifndef IMGUI_DISABLE+#include "imgui_impl_sdlrenderer2.h"+#include <stdint.h> // intptr_t++// Clang warnings with -Weverything+#if defined(__clang__)+#pragma clang diagnostic push+#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness+#elif defined(__GNUC__)+#pragma GCC diagnostic ignored "-Wfloat-equal" // warning: comparing floating-point with '==' or '!=' is unsafe+#endif++// SDL+#include <SDL_render.h>+#include <SDL_version.h>+#if !SDL_VERSION_ATLEAST(2,0,17)+#error This backend requires SDL 2.0.17+ because of SDL_RenderGeometry() function+#endif++// SDL_Renderer data+struct ImGui_ImplSDLRenderer2_Data+{+ SDL_Renderer* Renderer; // Main viewport's renderer++ ImGui_ImplSDLRenderer2_Data() { memset((void*)this, 0, sizeof(*this)); }+};++// 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.+static ImGui_ImplSDLRenderer2_Data* ImGui_ImplSDLRenderer2_GetBackendData()+{+ return ImGui::GetCurrentContext() ? (ImGui_ImplSDLRenderer2_Data*)ImGui::GetIO().BackendRendererUserData : nullptr;+}++ImGui_ImplSDLRenderer2_RenderState* ImGui_ImplSDLRenderer2_GetRenderState()+{+ return (ImGui_ImplSDLRenderer2_RenderState*)ImGui::GetPlatformIO().Renderer_RenderState;+}++// Functions+static void ImGui_ImplSDLRenderer2_SetupRenderState(SDL_Renderer* renderer)+{+ // 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.+ SDL_RenderSetViewport(renderer, nullptr);+ SDL_RenderSetClipRect(renderer, nullptr);+}++void ImGui_ImplSDLRenderer2_NewFrame()+{+ ImGui_ImplSDLRenderer2_Data* bd = ImGui_ImplSDLRenderer2_GetBackendData();+ IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDLRenderer2_Init()?");+ IM_UNUSED(bd);+}++// Draw callbacks+static void ImGui_ImplSDLRenderer2_DrawCallback_ResetRenderState(const ImDrawList*, const ImDrawCmd*) {} // Intentionally empty. Used as an identifier for rendering loop to call its code. Simpler to implement this way.++void ImGui_ImplSDLRenderer2_RenderDrawData(ImDrawData* draw_data, SDL_Renderer* renderer)+{+ // 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+ // to SDL_RenderGeometryRaw() by that scale factor. In that case we don't want to be also scaling it ourselves here.+ float rsx = 1.0f;+ float rsy = 1.0f;+ SDL_RenderGetScale(renderer, &rsx, &rsy);+ ImVec2 render_scale;+ render_scale.x = (rsx == 1.0f) ? draw_data->FramebufferScale.x : 1.0f;+ render_scale.y = (rsy == 1.0f) ? draw_data->FramebufferScale.y : 1.0f;++ // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)+ int fb_width = (int)(draw_data->DisplaySize.x * render_scale.x);+ int fb_height = (int)(draw_data->DisplaySize.y * render_scale.y);+ if (fb_width == 0 || fb_height == 0)+ return;++ // Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.+ // (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).+ if (draw_data->Textures != nullptr)+ for (ImTextureData* tex : *draw_data->Textures)+ if (tex->Status != ImTextureStatus_OK)+ ImGui_ImplSDLRenderer2_UpdateTexture(tex);++ // Backup SDL_Renderer state that will be modified to restore it afterwards+ struct BackupSDLRendererState+ {+ SDL_Rect Viewport;+ bool ClipEnabled;+ SDL_Rect ClipRect;+ };+ BackupSDLRendererState old = {};+ old.ClipEnabled = SDL_RenderIsClipEnabled(renderer) == SDL_TRUE;+ SDL_RenderGetViewport(renderer, &old.Viewport);+ SDL_RenderGetClipRect(renderer, &old.ClipRect);++ // Setup desired state+ ImGui_ImplSDLRenderer2_SetupRenderState(renderer);++ // Setup render state structure (for callbacks and custom texture bindings)+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();+ ImGui_ImplSDLRenderer2_RenderState render_state;+ render_state.Renderer = renderer;+ platform_io.Renderer_RenderState = &render_state;++ // Will project scissor/clipping rectangles into framebuffer space+ ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports+ ImVec2 clip_scale = render_scale;++ // Render command lists+ for (const ImDrawList* draw_list : draw_data->CmdLists)+ {+ const ImDrawVert* vtx_buffer = draw_list->VtxBuffer.Data;+ const ImDrawIdx* idx_buffer = draw_list->IdxBuffer.Data;++ for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)+ {+ const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];+ if (pcmd->UserCallback)+ {+ // User callback, registered via ImDrawList::AddCallback()+ if (pcmd->UserCallback == ImGui_ImplSDLRenderer2_DrawCallback_ResetRenderState)+ ImGui_ImplSDLRenderer2_SetupRenderState(renderer);+ else+ pcmd->UserCallback(draw_list, pcmd);+ }+ else+ {+ // Project scissor/clipping rectangles into framebuffer space+ ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);+ ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);+ if (clip_min.x < 0.0f) { clip_min.x = 0.0f; }+ if (clip_min.y < 0.0f) { clip_min.y = 0.0f; }+ if (clip_max.x > (float)fb_width) { clip_max.x = (float)fb_width; }+ if (clip_max.y > (float)fb_height) { clip_max.y = (float)fb_height; }+ if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)+ continue;++ 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) };+ SDL_RenderSetClipRect(renderer, &r);++ const float* xy = (const float*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, pos));+ const float* uv = (const float*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, uv));+#if SDL_VERSION_ATLEAST(2,0,19)+ const SDL_Color* color = (const SDL_Color*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, col)); // SDL 2.0.19++#else+ const int* color = (const int*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, col)); // SDL 2.0.17 and 2.0.18+#endif++ // Bind texture, Draw+ SDL_Texture* tex = (SDL_Texture*)pcmd->GetTexID();+ SDL_RenderGeometryRaw(renderer, tex,+ xy, (int)sizeof(ImDrawVert),+ color, (int)sizeof(ImDrawVert),+ uv, (int)sizeof(ImDrawVert),+ draw_list->VtxBuffer.Size - pcmd->VtxOffset,+ idx_buffer + pcmd->IdxOffset, pcmd->ElemCount, sizeof(ImDrawIdx));+ }+ }+ }+ platform_io.Renderer_RenderState = nullptr;++ // Restore modified SDL_Renderer state+ SDL_RenderSetViewport(renderer, &old.Viewport);+ SDL_RenderSetClipRect(renderer, old.ClipEnabled ? &old.ClipRect : nullptr);+}++void ImGui_ImplSDLRenderer2_UpdateTexture(ImTextureData* tex)+{+ ImGui_ImplSDLRenderer2_Data* bd = ImGui_ImplSDLRenderer2_GetBackendData();++ if (tex->Status == ImTextureStatus_WantCreate)+ {+ // Create and upload new texture to graphics system+ //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);+ IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);+ IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);++ // Create texture (bilinear sampling is required)+ SDL_Texture* sdl_texture = SDL_CreateTexture(bd->Renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, tex->Width, tex->Height);+ IM_ASSERT(sdl_texture != nullptr && "Backend failed to create texture!");+ SDL_UpdateTexture(sdl_texture, nullptr, tex->GetPixels(), tex->GetPitch());+ SDL_SetTextureBlendMode(sdl_texture, SDL_BLENDMODE_BLEND);+ SDL_SetTextureScaleMode(sdl_texture, SDL_ScaleModeLinear);++ // Store identifiers+ tex->SetTexID((ImTextureID)(intptr_t)sdl_texture);+ tex->SetStatus(ImTextureStatus_OK);+ }+ else if (tex->Status == ImTextureStatus_WantUpdates)+ {+ // Update selected blocks. We only ever write to textures regions which have never been used before!+ // This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.+ SDL_Texture* sdl_texture = (SDL_Texture*)(intptr_t)tex->TexID;+ for (ImTextureRect& r : tex->Updates)+ {+ SDL_Rect sdl_r = { r.x, r.y, r.w, r.h };+ SDL_UpdateTexture(sdl_texture, &sdl_r, tex->GetPixelsAt(r.x, r.y), tex->GetPitch());+ }+ tex->SetStatus(ImTextureStatus_OK);+ }+ else if (tex->Status == ImTextureStatus_WantDestroy)+ {+ if (tex->TexID != ImTextureID_Invalid)+ if (SDL_Texture* sdl_texture = (SDL_Texture*)(intptr_t)tex->TexID)+ SDL_DestroyTexture(sdl_texture);++ // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)+ tex->SetTexID(ImTextureID_Invalid);+ tex->SetStatus(ImTextureStatus_Destroyed);+ }+}++void ImGui_ImplSDLRenderer2_CreateDeviceObjects()+{+}++void ImGui_ImplSDLRenderer2_DestroyDeviceObjects()+{+ // Destroy all textures+ for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)+ if (tex->RefCount == 1)+ {+ tex->SetStatus(ImTextureStatus_WantDestroy);+ ImGui_ImplSDLRenderer2_UpdateTexture(tex);+ }+}++bool ImGui_ImplSDLRenderer2_Init(SDL_Renderer* renderer)+{+ ImGuiIO& io = ImGui::GetIO();+ IMGUI_CHECKVERSION();+ IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");+ IM_ASSERT(renderer != nullptr && "SDL_Renderer not initialized!");++ // Setup backend capabilities flags+ ImGui_ImplSDLRenderer2_Data* bd = IM_NEW(ImGui_ImplSDLRenderer2_Data)();+ io.BackendRendererUserData = (void*)bd;+ io.BackendRendererName = "imgui_impl_sdlrenderer2";+ io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.+ io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.++ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();+ platform_io.DrawCallback_ResetRenderState = ImGui_ImplSDLRenderer2_DrawCallback_ResetRenderState;++ bd->Renderer = renderer;++ return true;+}++void ImGui_ImplSDLRenderer2_Shutdown()+{+ ImGui_ImplSDLRenderer2_Data* bd = ImGui_ImplSDLRenderer2_GetBackendData();+ IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");+ ImGuiIO& io = ImGui::GetIO();+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();++ ImGui_ImplSDLRenderer2_DestroyDeviceObjects();++ io.BackendRendererName = nullptr;+ io.BackendRendererUserData = nullptr;+ io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures);+ platform_io.ClearRendererHandlers();+ IM_DELETE(bd);+}++//-----------------------------------------------------------------------------++#if defined(__clang__)+#pragma clang diagnostic pop+#endif++#endif // #ifndef IMGUI_DISABLE
+ cbits/imgui_impl_sdlrenderer2.h view
@@ -0,0 +1,57 @@+// dear imgui: Renderer Backend for SDL_Renderer for SDL2+// (Requires: SDL 2.0.17+)++// Note that SDL_Renderer is an _optional_ component of SDL2, which IMHO is now largely obsolete.+// For a multi-platform app consider using other technologies:+// - SDL3+SDL_GPU: SDL_GPU is SDL3 new graphics abstraction API. You will need to update to SDL3.+// - SDL2+DirectX, SDL2+OpenGL, SDL2+Vulkan: combine SDL with dedicated renderers.+// If your application wants to render any non trivial amount of graphics other than UI,+// 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:+// [X] Renderer: User texture binding. Use 'SDL_Texture*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!+// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).+// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.+// Missing features or Issues:+// [ ] Renderer: Multi-viewport support (multiple windows).+// [ ] Renderer: Missing support for DrawCallback_SetSamplerLinear, DrawCallback_SetSamplerNearest callbacks: SDLRenderer2 does not support changing SDL_SCALE_MODE while rendering.++// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.+// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.+// Learn about Dear ImGui:+// - FAQ https://dearimgui.com/faq+// - Getting Started https://dearimgui.com/getting-started+// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).+// - Introduction, links and more at the top of imgui.cpp++#pragma once+#ifndef IMGUI_DISABLE+#include "imgui.h" // IMGUI_IMPL_API++struct SDL_Renderer;++// Follow "Getting Started" link and check examples/ folder to learn about using backends!+IMGUI_IMPL_API bool ImGui_ImplSDLRenderer2_Init(SDL_Renderer* renderer);+IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_Shutdown();+IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_NewFrame();+IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_RenderDrawData(ImDrawData* draw_data, SDL_Renderer* renderer);++// Called by Init/NewFrame/Shutdown+IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_CreateDeviceObjects();+IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_DestroyDeviceObjects();++// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = nullptr to handle this manually.+IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_UpdateTexture(ImTextureData* tex);++// [BETA] Selected render state data shared with callbacks.+// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplSDLRenderer2_RenderDrawData() call.+// (Please open an issue if you feel you need access to more data)+struct ImGui_ImplSDLRenderer2_RenderState+{+ SDL_Renderer* Renderer;+};+IMGUI_IMPL_API ImGui_ImplSDLRenderer2_RenderState* ImGui_ImplSDLRenderer2_GetRenderState();++#endif // #ifndef IMGUI_DISABLE
+ dear-imgui-impl-sdlrenderer2.cabal view
@@ -0,0 +1,68 @@+cabal-version: 2.2++-- This file has been generated from package.yaml by hpack version 0.39.6.+--+-- see: https://github.com/sol/hpack++name: dear-imgui-impl-sdlrenderer2+version: 1.0.0+synopsis: SDL2 SDL_Renderer renderer-backend bindings for dear-imgui-raw.+description: Generated with dear_bindings from the imgui_impl_sdlrenderer2 backend that ships with Dear ImGui.+category: GUI+maintainer: IC Rainbow <aenor.realm@gmail.com>+license: BSD-3-Clause+license-file: LICENSE+build-type: Simple+extra-source-files:+ cbits/dcimgui_impl_sdlrenderer2.h+ cbits/DearImGuiWrappers.h+ cbits/imgui_impl_sdlrenderer2.h+extra-doc-files:+ README.md+ CHANGELOG.md++flag sdl2+ description: Bridge module DearImGui.Impl.SDLRenderer2 over the sdl2 Renderer type.+ manual: True+ default: True++library+ exposed-modules:+ DearImGui.Raw.Impl.SDLRenderer2+ other-modules:+ Paths_dear_imgui_impl_sdlrenderer2+ autogen-modules:+ Paths_dear_imgui_impl_sdlrenderer2+ hs-source-dirs:+ src+ default-extensions:+ BlockArguments+ DataKinds+ DuplicateRecordFields+ LambdaCase+ NoFieldSelectors+ OverloadedRecordDot+ OverloadedStrings+ StrictData+ ghc-options: -Wall -optc-Wno-discarded-qualifiers -optc-Wno-incompatible-pointer-types+ cxx-options: -std=c++11 -fpermissive+ include-dirs:+ cbits+ cxx-sources:+ cbits/dcimgui_impl_sdlrenderer2.cpp+ cbits/DearImGuiWrappers.cpp+ cbits/imgui_impl_sdlrenderer2.cpp+ pkgconfig-depends:+ sdl2+ build-tool-depends:+ hsc2hs:hsc2hs+ build-depends:+ base >=4.16 && <5+ , dear-imgui-raw ==1.0.*+ default-language: GHC2021+ if flag(sdl2)+ exposed-modules:+ DearImGui.Impl.SDLRenderer2+ build-depends:+ sdl2+ , unliftio-core
+ src/DearImGui/Impl/SDLRenderer2.hs view
@@ -0,0 +1,40 @@+-- | "DearImGui.Raw.Impl.SDLRenderer2" over the @sdl2@ 'SDL.Renderer' handle.+module DearImGui.Impl.SDLRenderer2+ ( rendererPtr+ , init+ , withInit+ , renderDrawData+ , Raw.createDeviceObjects+ , Raw.destroyDeviceObjects+ , Raw.newFrame+ , Raw.shutdown+ , Raw.updateTexture+ ) where++import Prelude hiding (init)++import Control.Monad.IO.Unlift (MonadUnliftIO)+import Foreign.Marshal.Utils (toBool)+import Foreign.Ptr (Ptr, castPtr)+import SDL qualified+import SDL.Internal.Types (Renderer (..))++import DearImGui.Impl.Init (withInitOrFail)+import DearImGui.Raw.ImDrawData (ImDrawData)+import DearImGui.Raw.Impl.SDLRenderer2 (SDL_Renderer)+import DearImGui.Raw.Impl.SDLRenderer2 qualified as Raw++rendererPtr :: SDL.Renderer -> Ptr SDL_Renderer+rendererPtr (Renderer raw) = castPtr raw++init :: SDL.Renderer -> IO Bool+init renderer =+ toBool <$> Raw.init (rendererPtr renderer)++-- | 'init'-or-'InitFailed' before the body, 'Raw.shutdown' after it.+withInit :: (MonadUnliftIO m) => SDL.Renderer -> m a -> m a+withInit renderer = withInitOrFail "ImGui_ImplSDLRenderer2_Init" (init renderer) Raw.shutdown++renderDrawData :: Ptr ImDrawData -> SDL.Renderer -> IO ()+renderDrawData drawData renderer =+ Raw.renderDrawData drawData (rendererPtr renderer)
+ src/DearImGui/Raw/Impl/SDLRenderer2.hsc view
@@ -0,0 +1,86 @@+{-# LANGUAGE CApiFFI #-}+{-# LANGUAGE PatternSynonyms #-}++-- This file was generated by dear-bindings-ffi. Do not edit by hand.++module DearImGui.Raw.Impl.SDLRenderer2+ ( ImGui_ImplSDLRenderer2_RenderState (..)+ , SDL_Renderer+ , createDeviceObjects+ , destroyDeviceObjects+ , getRenderState+ , init+ , newFrame+ , renderDrawData+ , shutdown+ , updateTexture+ ) where++import DearImGui.Raw.ImDrawData (ImDrawData)+import DearImGui.Raw.ImTextureData (ImTextureData)+import Foreign.C.Types+import Foreign.Ptr (Ptr, plusPtr)+import Foreign.Storable (Storable (..))+import GHC.Records (HasField (..))+import Prelude hiding (init)++#include "dcimgui_impl_sdlrenderer2.h"++-- structs (opaque)++newtype {-# CTYPE "dcimgui_impl_sdlrenderer2.h" "ImGui_ImplSDLRenderer2_RenderState" #-} ImGui_ImplSDLRenderer2_RenderState = ImGui_ImplSDLRenderer2_RenderState+ { renderer :: Ptr SDL_Renderer+ }+ deriving (Eq, Show)+instance Storable ImGui_ImplSDLRenderer2_RenderState where+ sizeOf _ = #{size ImGui_ImplSDLRenderer2_RenderState}+ alignment _ = #{alignment ImGui_ImplSDLRenderer2_RenderState}+ peek p = ImGui_ImplSDLRenderer2_RenderState <$> #{peek ImGui_ImplSDLRenderer2_RenderState, Renderer} p+ poke p v = do+ #{poke ImGui_ImplSDLRenderer2_RenderState, Renderer} p v.renderer++instance HasField "renderer" (Ptr ImGui_ImplSDLRenderer2_RenderState) (Ptr (Ptr SDL_Renderer)) where+ getField = #{ptr ImGui_ImplSDLRenderer2_RenderState, Renderer}++data SDL_Renderer++-- functions++-- C: void ImGui_ImplSDLRenderer2_CreateDeviceObjects()+foreign import capi safe "dcimgui_impl_sdlrenderer2.h cImGui_ImplSDLRenderer2_CreateDeviceObjects"+ createDeviceObjects :: IO ()++-- C: void ImGui_ImplSDLRenderer2_DestroyDeviceObjects()+foreign import capi safe "dcimgui_impl_sdlrenderer2.h cImGui_ImplSDLRenderer2_DestroyDeviceObjects"+ destroyDeviceObjects :: IO ()++-- C: ImGui_ImplSDLRenderer2_RenderState* ImGui_ImplSDLRenderer2_GetRenderState()+foreign import capi safe "dcimgui_impl_sdlrenderer2.h cImGui_ImplSDLRenderer2_GetRenderState"+ getRenderState :: IO (Ptr ImGui_ImplSDLRenderer2_RenderState)++-- C: bool ImGui_ImplSDLRenderer2_Init(SDL_Renderer* renderer)+foreign import capi safe "dcimgui_impl_sdlrenderer2.h cImGui_ImplSDLRenderer2_Init"+ init+ :: (Ptr SDL_Renderer) -- ^ renderer+ -> IO CBool++-- C: void ImGui_ImplSDLRenderer2_NewFrame()+foreign import capi safe "dcimgui_impl_sdlrenderer2.h cImGui_ImplSDLRenderer2_NewFrame"+ newFrame :: IO ()++-- C: void ImGui_ImplSDLRenderer2_RenderDrawData(ImDrawData* draw_data, SDL_Renderer* renderer)+foreign import capi safe "dcimgui_impl_sdlrenderer2.h cImGui_ImplSDLRenderer2_RenderDrawData"+ renderDrawData+ :: (Ptr ImDrawData) -- ^ draw_data+ -> (Ptr SDL_Renderer) -- ^ renderer+ -> IO ()++-- C: void ImGui_ImplSDLRenderer2_Shutdown()+foreign import capi safe "dcimgui_impl_sdlrenderer2.h cImGui_ImplSDLRenderer2_Shutdown"+ shutdown :: IO ()++-- C: void ImGui_ImplSDLRenderer2_UpdateTexture(ImTextureData* tex)+foreign import capi safe "dcimgui_impl_sdlrenderer2.h cImGui_ImplSDLRenderer2_UpdateTexture"+ updateTexture+ :: (Ptr ImTextureData) -- ^ tex+ -> IO ()