From f2703c101567cc02d1b386ced499cd5a69d1b2f7 Mon Sep 17 00:00:00 2001 From: moonpower Date: Wed, 24 Dec 2025 23:17:37 +0300 Subject: [PATCH 01/10] Backends: Start adding sdl2 surface --- backends/imgui_impl_sdlsurface2.cpp | 225 +++++++++++++++++++++++++ backends/imgui_impl_sdlsurface2.h | 14 ++ examples/example_sdl2_surface/Makefile | 31 ++++ examples/example_sdl2_surface/main.cpp | 95 +++++++++++ 4 files changed, 365 insertions(+) create mode 100644 backends/imgui_impl_sdlsurface2.cpp create mode 100644 backends/imgui_impl_sdlsurface2.h create mode 100644 examples/example_sdl2_surface/Makefile create mode 100644 examples/example_sdl2_surface/main.cpp diff --git a/backends/imgui_impl_sdlsurface2.cpp b/backends/imgui_impl_sdlsurface2.cpp new file mode 100644 index 000000000..c6e27a651 --- /dev/null +++ b/backends/imgui_impl_sdlsurface2.cpp @@ -0,0 +1,225 @@ +// imgui_impl_sdlsurface2.cpp +// CPU-only SDL_Surface backend for Dear ImGui + +#include "imgui_impl_sdlsurface2.h" +#include "imgui.h" +#include +#include +#include + +static SDL_Surface* g_TargetSurface = nullptr; +static SDL_Surface* g_FontSurface = nullptr; + +static inline void PutPixel(SDL_Surface* surface, int x, int y, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + if (x < 0 || y < 0 || x >= surface->w || y >= surface->h) return; + Uint32 pixel = SDL_MapRGBA(surface->format, r, g, b, a); + Uint8* pixels = (Uint8*)surface->pixels; + int pitch = surface->pitch; + Uint32* dst = (Uint32*)(pixels + y * pitch + x * 4); + *dst = pixel; +} + +static inline float Edge(const ImVec2& a, const ImVec2& b, float x, float y) +{ + return (b.x - a.x) * (y - a.y) - (b.y - a.y) * (x - a.x); +} + +SDL_Surface* ImGui_ImplSDLSurface2_CreateFontAtlasSurface() +{ + ImGuiIO& io = ImGui::GetIO(); + unsigned char* pixels = nullptr; + int width = 0, height = 0; + io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); + if (!pixels || width <= 0 || height <= 0) return nullptr; + + SDL_Surface* surf = SDL_CreateRGBSurfaceWithFormat(0, width, height, 32, SDL_PIXELFORMAT_RGBA32); + if (!surf) return nullptr; + + SDL_LockSurface(surf); + memcpy(surf->pixels, pixels, width * height * 4); + SDL_UnlockSurface(surf); + return surf; +} + +bool ImGui_ImplSDLSurface2_Init(SDL_Surface* surface) +{ + if (!surface) return false; + g_TargetSurface = surface; + + g_FontSurface = ImGui_ImplSDLSurface2_CreateFontAtlasSurface(); + if (g_FontSurface) + { + ImGui::GetIO().Fonts->TexID = (ImTextureID)g_FontSurface; + } + return true; +} + +void ImGui_ImplSDLSurface2_Shutdown() +{ + ImGui::GetIO().Fonts->TexID = nullptr; + if (g_FontSurface) + { + SDL_FreeSurface(g_FontSurface); + g_FontSurface = nullptr; + } + g_TargetSurface = nullptr; +} + +void ImGui_ImplSDLSurface2_NewFrame() +{ + // noop +} + +void ImGui_ImplSDLSurface2_RenderDrawData(ImDrawData* draw_data) +{ + if (!draw_data || !g_TargetSurface) return; + + if (SDL_MUSTLOCK(g_TargetSurface)) SDL_LockSurface(g_TargetSurface); + + const ImVec2 display_pos = draw_data->DisplayPos; + const ImVec2 fb_scale = draw_data->FramebufferScale; + + for (int n = 0; n < draw_data->CmdListsCount; n++) + { + const ImDrawList* cmd_list = draw_data->CmdLists[n]; + const ImDrawVert* vtx_buffer = cmd_list->VtxBuffer.Data; + const ImDrawIdx* idx_buffer = cmd_list->IdxBuffer.Data; + + int idx_offset = 0; + + for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) + { + const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; + if (pcmd->ElemCount == 0) { idx_offset += 0; continue; } + + ImVec2 clip_min = ImVec2((pcmd->ClipRect.x - display_pos.x) * fb_scale.x, + (pcmd->ClipRect.y - display_pos.y) * fb_scale.y); + ImVec2 clip_max = ImVec2((pcmd->ClipRect.z - display_pos.x) * fb_scale.x, + (pcmd->ClipRect.w - display_pos.y) * fb_scale.y); + + int cx0 = (int)floor(clip_min.x); + int cy0 = (int)floor(clip_min.y); + int cx1 = (int)ceil(clip_max.x); + int cy1 = (int)ceil(clip_max.y); + + cx0 = std::max(cx0, 0); + cy0 = std::max(cy0, 0); + cx1 = std::min(cx1, g_TargetSurface->w); + cy1 = std::min(cy1, g_TargetSurface->h); + + if (cx0 >= cx1 || cy0 >= cy1) + { + idx_offset += pcmd->ElemCount; + continue; + } + + SDL_Rect srect; + srect.x = cx0; + srect.y = cy0; + srect.w = cx1 - cx0; + srect.h = cy1 - cy0; + SDL_SetClipRect(g_TargetSurface, &srect); + + SDL_Surface* tex = (SDL_Surface*)pcmd->GetTexID(); + + for (unsigned int i = 0; i + 2 < (unsigned int)pcmd->ElemCount; i += 3) + { + ImDrawIdx i0 = idx_buffer[idx_offset + i + 0]; + ImDrawIdx i1 = idx_buffer[idx_offset + i + 1]; + ImDrawIdx i2 = idx_buffer[idx_offset + i + 2]; + + const ImDrawVert& v0 = vtx_buffer[i0]; + const ImDrawVert& v1 = vtx_buffer[i1]; + const ImDrawVert& v2 = vtx_buffer[i2]; + + ImVec2 p0 = ImVec2((v0.pos.x - display_pos.x) * fb_scale.x, (v0.pos.y - display_pos.y) * fb_scale.y); + ImVec2 p1 = ImVec2((v1.pos.x - display_pos.x) * fb_scale.x, (v1.pos.y - display_pos.y) * fb_scale.y); + ImVec2 p2 = ImVec2((v2.pos.x - display_pos.x) * fb_scale.x, (v2.pos.y - display_pos.y) * fb_scale.y); + + int minx = (int)floor(std::min(std::min(p0.x, p1.x), p2.x)); + int miny = (int)floor(std::min(std::min(p0.y, p1.y), p2.y)); + int maxx = (int)ceil(std::max(std::max(p0.x, p1.x), p2.x)); + int maxy = (int)ceil(std::max(std::max(p0.y, p1.y), p2.y)); + + minx = std::max(minx, cx0); + miny = std::max(miny, cy0); + maxx = std::min(maxx, cx1 - 1); + maxy = std::min(maxy, cy1 - 1); + + if (minx > maxx || miny > maxy) continue; + + float area = Edge(p0, p1, p2.x, p2.y); + if (area == 0.0f) continue; + + auto unpack_col = [](ImU32 c, float out[4]) { + out[0] = (float)((c >> IM_COL32_R_SHIFT) & 0xFF) / 255.0f; + out[1] = (float)((c >> IM_COL32_G_SHIFT) & 0xFF) / 255.0f; + out[2] = (float)((c >> IM_COL32_B_SHIFT) & 0xFF) / 255.0f; + out[3] = (float)((c >> IM_COL32_A_SHIFT) & 0xFF) / 255.0f; + }; + + float c0[4], c1[4], c2[4]; + unpack_col(v0.col, c0); unpack_col(v1.col, c1); unpack_col(v2.col, c2); + + for (int y = miny; y <= maxy; y++) + { + for (int x = minx; x <= maxx; x++) + { + float px = (float)x + 0.5f; + float py = (float)y + 0.5f; + + float w0 = Edge(p1, p2, px, py) / area; + float w1 = Edge(p2, p0, px, py) / area; + float w2 = Edge(p0, p1, px, py) / area; + + if (w0 < 0.0f || w1 < 0.0f || w2 < 0.0f) continue; + + float r = w0 * c0[0] + w1 * c1[0] + w2 * c2[0]; + float g = w0 * c0[1] + w1 * c1[1] + w2 * c2[1]; + float b = w0 * c0[2] + w1 * c1[2] + w2 * c2[2]; + float a = w0 * c0[3] + w1 * c1[3] + w2 * c2[3]; + + Uint8 out_r = (Uint8)(std::min(1.0f, r) * 255.0f); + Uint8 out_g = (Uint8)(std::min(1.0f, g) * 255.0f); + Uint8 out_b = (Uint8)(std::min(1.0f, b) * 255.0f); + Uint8 out_a = (Uint8)(std::min(1.0f, a) * 255.0f); + + if (tex) + { + ImVec2 uv0 = v0.uv; ImVec2 uv1 = v1.uv; ImVec2 uv2 = v2.uv; + float u = w0 * uv0.x + w1 * uv1.x + w2 * uv2.x; + float v = w0 * uv0.y + w1 * uv1.y + w2 * uv2.y; + int tx = (int)(u * (tex->w - 1) + 0.5f); + int ty = (int)(v * (tex->h - 1) + 0.5f); + tx = std::clamp(tx, 0, tex->w - 1); + ty = std::clamp(ty, 0, tex->h - 1); + + Uint8* tpx = (Uint8*)tex->pixels + ty * tex->pitch + tx * 4; + Uint8 tr = tpx[0]; + Uint8 tg = tpx[1]; + Uint8 tb = tpx[2]; + Uint8 ta = tpx[3]; + + Uint8 final_r = (Uint8)((tr * out_r) / 255); + Uint8 final_g = (Uint8)((tg * out_g) / 255); + Uint8 final_b = (Uint8)((tb * out_b) / 255); + Uint8 final_a = (Uint8)((ta * out_a) / 255); + + PutPixel(g_TargetSurface, x, y, final_r, final_g, final_b, final_a); + } + else + { + PutPixel(g_TargetSurface, x, y, out_r, out_g, out_b, out_a); + } + } + } + } + + idx_offset += pcmd->ElemCount; + } + } + + SDL_SetClipRect(g_TargetSurface, nullptr); + if (SDL_MUSTLOCK(g_TargetSurface)) SDL_UnlockSurface(g_TargetSurface); +} diff --git a/backends/imgui_impl_sdlsurface2.h b/backends/imgui_impl_sdlsurface2.h new file mode 100644 index 000000000..32ff2ec08 --- /dev/null +++ b/backends/imgui_impl_sdlsurface2.h @@ -0,0 +1,14 @@ +// imgui_impl_sdlsurface2.h +// CPU-only SDL_Surface backend for Dear ImGui +#pragma once + +#include "imgui.h" +#include + +// Initialize with target SDL_Surface (32-bit RGBA) +IMGUI_API bool ImGui_ImplSDLSurface2_Init(SDL_Surface* surface); +IMGUI_API void ImGui_ImplSDLSurface2_Shutdown(); +IMGUI_API void ImGui_ImplSDLSurface2_NewFrame(); +IMGUI_API void ImGui_ImplSDLSurface2_RenderDrawData(ImDrawData* draw_data); + +IMGUI_API SDL_Surface* ImGui_ImplSDLSurface2_CreateFontAtlasSurface(); diff --git a/examples/example_sdl2_surface/Makefile b/examples/example_sdl2_surface/Makefile new file mode 100644 index 000000000..400112ea5 --- /dev/null +++ b/examples/example_sdl2_surface/Makefile @@ -0,0 +1,31 @@ +# Makefile for example_sdl2_surface (MinGW) + +CXX = g++ +CXXFLAGS = -std=c++17 -Wall -Wformat +INCLUDES = -I../.. -I../../backends -I/mingw64/include/SDL2 +LIBS = -lmingw32 -lSDL2main -lSDL2 -lgdi32 + +IMGUI_DIR = ../.. +SOURCES = main.cpp +SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp +SOURCES += $(IMGUI_DIR)/backends/imgui_impl_sdl2.cpp +SOURCES += $(IMGUI_DIR)/backends/imgui_impl_sdlsurface2.cpp + +OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES)))) + +%.o:%.cpp + $(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $< + +%.o:$(IMGUI_DIR)/%.cpp + $(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $< + +%.o:$(IMGUI_DIR)/backends/%.cpp + $(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $< + +all: example_sdl2_surface.exe + +example_sdl2_surface.exe: $(OBJS) + $(CXX) -o $@ $^ $(LIBS) + +clean: + del /Q *.o *.exe diff --git a/examples/example_sdl2_surface/main.cpp b/examples/example_sdl2_surface/main.cpp new file mode 100644 index 000000000..e2e935d23 --- /dev/null +++ b/examples/example_sdl2_surface/main.cpp @@ -0,0 +1,95 @@ +// main.cpp +// Example program using imgui_impl_sdlsurface2 backend + +#include "imgui.h" +#include "imgui_impl_sdl2.h" +#include "imgui_impl_sdlsurface2.h" +#include +#include + +int main(int, char**) +{ + if (SDL_Init(SDL_INIT_VIDEO) != 0) + { + printf("Error: %s\n", SDL_GetError()); + return -1; + } + + SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+Surface Example", + SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, + 1280, 720, SDL_WINDOW_SHOWN); + if (!window) + { + printf("Error creating window: %s\n", SDL_GetError()); + return -1; + } + + SDL_Surface* window_surface = SDL_GetWindowSurface(window); + if (!window_surface) + { + printf("Error getting window surface: %s\n", SDL_GetError()); + return -1; + } + + SDL_Surface* framebuffer = SDL_CreateRGBSurfaceWithFormat(0, 1280, 720, 32, SDL_PIXELFORMAT_RGBA32); + if (!framebuffer) + { + printf("Error creating framebuffer: %s\n", SDL_GetError()); + return -1; + } + + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); (void)io; + + ImGui::StyleColorsDark(); + + ImGui_ImplSDL2_InitForSDLRenderer(window, nullptr); + ImGui_ImplSDLSurface2_Init(framebuffer); + + bool show_demo_window = true; + ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); + + bool done = false; + while (!done) + { + SDL_Event event; + while (SDL_PollEvent(&event)) + { + ImGui_ImplSDL2_ProcessEvent(&event); + if (event.type == SDL_QUIT) + done = true; + if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) + done = true; + } + + ImGui_ImplSDLSurface2_NewFrame(); + ImGui_ImplSDL2_NewFrame(); + ImGui::NewFrame(); + + if (show_demo_window) + ImGui::ShowDemoWindow(&show_demo_window); + + ImGui::Render(); + + SDL_FillRect(framebuffer, nullptr, SDL_MapRGB(framebuffer->format, + (Uint8)(clear_color.x * 255), + (Uint8)(clear_color.y * 255), + (Uint8)(clear_color.z * 255))); + + ImGui_ImplSDLSurface2_RenderDrawData(ImGui::GetDrawData()); + + SDL_BlitSurface(framebuffer, nullptr, window_surface, nullptr); + SDL_UpdateWindowSurface(window); + } + + ImGui_ImplSDLSurface2_Shutdown(); + ImGui_ImplSDL2_Shutdown(); + ImGui::DestroyContext(); + + SDL_FreeSurface(framebuffer); + SDL_DestroyWindow(window); + SDL_Quit(); + + return 0; +} From b3008c0543566cbfa9fe87206ff138a94e0018b8 Mon Sep 17 00:00:00 2001 From: moonpower Date: Thu, 25 Dec 2025 00:03:50 +0300 Subject: [PATCH 02/10] Backends: Enhance pixel manipulation functions for SDL_Surface --- backends/imgui_impl_sdlsurface2.cpp | 131 ++++++++++++++++++++-------- 1 file changed, 93 insertions(+), 38 deletions(-) diff --git a/backends/imgui_impl_sdlsurface2.cpp b/backends/imgui_impl_sdlsurface2.cpp index c6e27a651..d432a2f2b 100644 --- a/backends/imgui_impl_sdlsurface2.cpp +++ b/backends/imgui_impl_sdlsurface2.cpp @@ -10,14 +10,70 @@ static SDL_Surface* g_TargetSurface = nullptr; static SDL_Surface* g_FontSurface = nullptr; -static inline void PutPixel(SDL_Surface* surface, int x, int y, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +static inline Uint32 GetPixel(SDL_Surface* s, int x, int y) { - if (x < 0 || y < 0 || x >= surface->w || y >= surface->h) return; - Uint32 pixel = SDL_MapRGBA(surface->format, r, g, b, a); - Uint8* pixels = (Uint8*)surface->pixels; - int pitch = surface->pitch; - Uint32* dst = (Uint32*)(pixels + y * pitch + x * 4); - *dst = pixel; + Uint8* p = (Uint8*)s->pixels + y * s->pitch + x * s->format->BytesPerPixel; + switch (s->format->BytesPerPixel) + { + case 1: return *p; + case 2: return *(Uint16*)p; + case 3: + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) + return (p[0] << 16) | (p[1] << 8) | p[2]; + else + return p[0] | (p[1] << 8) | (p[2] << 16); + case 4: return *(Uint32*)p; + } + return 0; +} + +static inline void BlendPixel(SDL_Surface* s, int x, int y, + Uint8 sr, Uint8 sg, Uint8 sb, Uint8 sa) +{ + if (!s || sa == 0) return; + if (x < 0 || y < 0 || x >= s->w || y >= s->h) return; + + Uint32 dst_pix = GetPixel(s, x, y); + + Uint8 dr, dg, db, da; + SDL_GetRGBA(dst_pix, s->format, &dr, &dg, &db, &da); + + float src_a = sa / 255.0f; + float dst_a = da / 255.0f; + float out_a = src_a + dst_a * (1.0f - src_a); + + float out_r = (sr * src_a + dr * dst_a * (1.0f - src_a)) / (out_a > 0 ? out_a : 1.0f); + float out_g = (sg * src_a + dg * dst_a * (1.0f - src_a)) / (out_a > 0 ? out_a : 1.0f); + float out_b = (sb * src_a + db * dst_a * (1.0f - src_a)) / (out_a > 0 ? out_a : 1.0f); + + Uint8 fr = (Uint8)std::clamp(out_r, 0.0f, 255.0f); + Uint8 fg = (Uint8)std::clamp(out_g, 0.0f, 255.0f); + Uint8 fb = (Uint8)std::clamp(out_b, 0.0f, 255.0f); + Uint8 fa = (Uint8)std::clamp(out_a * 255.0f, 0.0f, 255.0f); + + Uint32 out_pix = SDL_MapRGBA(s->format, fr, fg, fb, fa); + + Uint8* p = (Uint8*)s->pixels + y * s->pitch + x * s->format->BytesPerPixel; + switch (s->format->BytesPerPixel) + { + case 1: *p = (Uint8)out_pix; break; + case 2: *(Uint16*)p = (Uint16)out_pix; break; + case 3: + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) + { + p[0] = (out_pix >> 16) & 0xFF; + p[1] = (out_pix >> 8) & 0xFF; + p[2] = out_pix & 0xFF; + } + else + { + p[0] = out_pix & 0xFF; + p[1] = (out_pix >> 8) & 0xFF; + p[2] = (out_pix >> 16) & 0xFF; + } + break; + case 4: *(Uint32*)p = out_pix; break; + } } static inline float Edge(const ImVec2& a, const ImVec2& b, float x, float y) @@ -37,8 +93,9 @@ SDL_Surface* ImGui_ImplSDLSurface2_CreateFontAtlasSurface() if (!surf) return nullptr; SDL_LockSurface(surf); - memcpy(surf->pixels, pixels, width * height * 4); + std::memcpy(surf->pixels, pixels, width * height * 4); SDL_UnlockSurface(surf); + SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE); return surf; } @@ -91,17 +148,20 @@ void ImGui_ImplSDLSurface2_RenderDrawData(ImDrawData* draw_data) for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) { const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; - if (pcmd->ElemCount == 0) { idx_offset += 0; continue; } + if (pcmd->ElemCount == 0) + continue; - ImVec2 clip_min = ImVec2((pcmd->ClipRect.x - display_pos.x) * fb_scale.x, - (pcmd->ClipRect.y - display_pos.y) * fb_scale.y); - ImVec2 clip_max = ImVec2((pcmd->ClipRect.z - display_pos.x) * fb_scale.x, - (pcmd->ClipRect.w - display_pos.y) * fb_scale.y); + ImVec2 clip_min = ImVec2( + (pcmd->ClipRect.x - display_pos.x) * fb_scale.x, + (pcmd->ClipRect.y - display_pos.y) * fb_scale.y); + ImVec2 clip_max = ImVec2( + (pcmd->ClipRect.z - display_pos.x) * fb_scale.x, + (pcmd->ClipRect.w - display_pos.y) * fb_scale.y); - int cx0 = (int)floor(clip_min.x); - int cy0 = (int)floor(clip_min.y); - int cx1 = (int)ceil(clip_max.x); - int cy1 = (int)ceil(clip_max.y); + int cx0 = (int)std::floor(clip_min.x); + int cy0 = (int)std::floor(clip_min.y); + int cx1 = (int)std::ceil (clip_max.x); + int cy1 = (int)std::ceil (clip_max.y); cx0 = std::max(cx0, 0); cy0 = std::max(cy0, 0); @@ -114,11 +174,7 @@ void ImGui_ImplSDLSurface2_RenderDrawData(ImDrawData* draw_data) continue; } - SDL_Rect srect; - srect.x = cx0; - srect.y = cy0; - srect.w = cx1 - cx0; - srect.h = cy1 - cy0; + SDL_Rect srect{ cx0, cy0, cx1 - cx0, cy1 - cy0 }; SDL_SetClipRect(g_TargetSurface, &srect); SDL_Surface* tex = (SDL_Surface*)pcmd->GetTexID(); @@ -137,10 +193,10 @@ void ImGui_ImplSDLSurface2_RenderDrawData(ImDrawData* draw_data) ImVec2 p1 = ImVec2((v1.pos.x - display_pos.x) * fb_scale.x, (v1.pos.y - display_pos.y) * fb_scale.y); ImVec2 p2 = ImVec2((v2.pos.x - display_pos.x) * fb_scale.x, (v2.pos.y - display_pos.y) * fb_scale.y); - int minx = (int)floor(std::min(std::min(p0.x, p1.x), p2.x)); - int miny = (int)floor(std::min(std::min(p0.y, p1.y), p2.y)); - int maxx = (int)ceil(std::max(std::max(p0.x, p1.x), p2.x)); - int maxy = (int)ceil(std::max(std::max(p0.y, p1.y), p2.y)); + int minx = (int)std::floor(std::min({ p0.x, p1.x, p2.x })); + int miny = (int)std::floor(std::min({ p0.y, p1.y, p2.y })); + int maxx = (int)std::ceil (std::max({ p0.x, p1.x, p2.x })); + int maxy = (int)std::ceil (std::max({ p0.y, p1.y, p2.y })); minx = std::max(minx, cx0); miny = std::max(miny, cy0); @@ -180,37 +236,36 @@ void ImGui_ImplSDLSurface2_RenderDrawData(ImDrawData* draw_data) float b = w0 * c0[2] + w1 * c1[2] + w2 * c2[2]; float a = w0 * c0[3] + w1 * c1[3] + w2 * c2[3]; - Uint8 out_r = (Uint8)(std::min(1.0f, r) * 255.0f); - Uint8 out_g = (Uint8)(std::min(1.0f, g) * 255.0f); - Uint8 out_b = (Uint8)(std::min(1.0f, b) * 255.0f); - Uint8 out_a = (Uint8)(std::min(1.0f, a) * 255.0f); + Uint8 out_r = (Uint8)(std::clamp(r, 0.0f, 1.0f) * 255.0f); + Uint8 out_g = (Uint8)(std::clamp(g, 0.0f, 1.0f) * 255.0f); + Uint8 out_b = (Uint8)(std::clamp(b, 0.0f, 1.0f) * 255.0f); + Uint8 out_a = (Uint8)(std::clamp(a, 0.0f, 1.0f) * 255.0f); if (tex) { - ImVec2 uv0 = v0.uv; ImVec2 uv1 = v1.uv; ImVec2 uv2 = v2.uv; + ImVec2 uv0 = v0.uv, uv1 = v1.uv, uv2 = v2.uv; float u = w0 * uv0.x + w1 * uv1.x + w2 * uv2.x; float v = w0 * uv0.y + w1 * uv1.y + w2 * uv2.y; + int tx = (int)(u * (tex->w - 1) + 0.5f); int ty = (int)(v * (tex->h - 1) + 0.5f); tx = std::clamp(tx, 0, tex->w - 1); ty = std::clamp(ty, 0, tex->h - 1); - Uint8* tpx = (Uint8*)tex->pixels + ty * tex->pitch + tx * 4; - Uint8 tr = tpx[0]; - Uint8 tg = tpx[1]; - Uint8 tb = tpx[2]; - Uint8 ta = tpx[3]; + Uint32 tpx = GetPixel(tex, tx, ty); + Uint8 tr, tg, tb, ta; + SDL_GetRGBA(tpx, tex->format, &tr, &tg, &tb, &ta); Uint8 final_r = (Uint8)((tr * out_r) / 255); Uint8 final_g = (Uint8)((tg * out_g) / 255); Uint8 final_b = (Uint8)((tb * out_b) / 255); Uint8 final_a = (Uint8)((ta * out_a) / 255); - PutPixel(g_TargetSurface, x, y, final_r, final_g, final_b, final_a); + BlendPixel(g_TargetSurface, x, y, final_r, final_g, final_b, final_a); } else { - PutPixel(g_TargetSurface, x, y, out_r, out_g, out_b, out_a); + BlendPixel(g_TargetSurface, x, y, out_r, out_g, out_b, out_a); } } } From a59e03e1e74f5b11b1d3eee1c42973d7a1da2f48 Mon Sep 17 00:00:00 2001 From: moonpower Date: Thu, 25 Dec 2025 00:11:45 +0300 Subject: [PATCH 03/10] Examples: enhance sdl2 surface --- examples/example_sdl2_surface/main.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/example_sdl2_surface/main.cpp b/examples/example_sdl2_surface/main.cpp index e2e935d23..966660f59 100644 --- a/examples/example_sdl2_surface/main.cpp +++ b/examples/example_sdl2_surface/main.cpp @@ -41,10 +41,12 @@ int main(int, char**) IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; ImGui::StyleColorsDark(); - ImGui_ImplSDL2_InitForSDLRenderer(window, nullptr); + ImGui_ImplSDL2_SetGamepadMode(ImGui_ImplSDL2_GamepadMode_AutoFirst, nullptr, 0); ImGui_ImplSDLSurface2_Init(framebuffer); bool show_demo_window = true; From eeb20d048b465248957b718370cfba9032b28ece Mon Sep 17 00:00:00 2001 From: moonpower Date: Thu, 25 Dec 2025 00:25:31 +0300 Subject: [PATCH 04/10] Backends: Improve pixel blending and surface format conversion in SDL_Surface --- backends/imgui_impl_sdlsurface2.cpp | 57 +++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/backends/imgui_impl_sdlsurface2.cpp b/backends/imgui_impl_sdlsurface2.cpp index d432a2f2b..75092fe4b 100644 --- a/backends/imgui_impl_sdlsurface2.cpp +++ b/backends/imgui_impl_sdlsurface2.cpp @@ -33,26 +33,43 @@ static inline void BlendPixel(SDL_Surface* s, int x, int y, if (!s || sa == 0) return; if (x < 0 || y < 0 || x >= s->w || y >= s->h) return; - Uint32 dst_pix = GetPixel(s, x, y); + if (sa == 255) + { + Uint32 out_pix = SDL_MapRGBA(s->format, sr, sg, sb, sa); + Uint8* p = (Uint8*)s->pixels + y * s->pitch + x * s->format->BytesPerPixel; + switch (s->format->BytesPerPixel) + { + case 1: *p = (Uint8)out_pix; break; + case 2: *(Uint16*)p = (Uint16)out_pix; break; + case 3: + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) + { + p[0] = (out_pix >> 16) & 0xFF; + p[1] = (out_pix >> 8) & 0xFF; + p[2] = out_pix & 0xFF; + } + else + { + p[0] = out_pix & 0xFF; + p[1] = (out_pix >> 8) & 0xFF; + p[2] = (out_pix >> 16) & 0xFF; + } + break; + case 4: *(Uint32*)p = out_pix; break; + } + return; + } + Uint32 dst_pix = GetPixel(s, x, y); Uint8 dr, dg, db, da; SDL_GetRGBA(dst_pix, s->format, &dr, &dg, &db, &da); - float src_a = sa / 255.0f; - float dst_a = da / 255.0f; - float out_a = src_a + dst_a * (1.0f - src_a); - - float out_r = (sr * src_a + dr * dst_a * (1.0f - src_a)) / (out_a > 0 ? out_a : 1.0f); - float out_g = (sg * src_a + dg * dst_a * (1.0f - src_a)) / (out_a > 0 ? out_a : 1.0f); - float out_b = (sb * src_a + db * dst_a * (1.0f - src_a)) / (out_a > 0 ? out_a : 1.0f); - - Uint8 fr = (Uint8)std::clamp(out_r, 0.0f, 255.0f); - Uint8 fg = (Uint8)std::clamp(out_g, 0.0f, 255.0f); - Uint8 fb = (Uint8)std::clamp(out_b, 0.0f, 255.0f); - Uint8 fa = (Uint8)std::clamp(out_a * 255.0f, 0.0f, 255.0f); - - Uint32 out_pix = SDL_MapRGBA(s->format, fr, fg, fb, fa); + Uint8 out_r = (Uint8)((sr * sa + dr * (255 - sa)) / 255); + Uint8 out_g = (Uint8)((sg * sa + dg * (255 - sa)) / 255); + Uint8 out_b = (Uint8)((sb * sa + db * (255 - sa)) / 255); + Uint8 out_a = (Uint8)((sa + (da * (255 - sa)) / 255)); + Uint32 out_pix = SDL_MapRGBA(s->format, out_r, out_g, out_b, out_a); Uint8* p = (Uint8*)s->pixels + y * s->pitch + x * s->format->BytesPerPixel; switch (s->format->BytesPerPixel) { @@ -103,10 +120,18 @@ bool ImGui_ImplSDLSurface2_Init(SDL_Surface* surface) { if (!surface) return false; g_TargetSurface = surface; - g_FontSurface = ImGui_ImplSDLSurface2_CreateFontAtlasSurface(); if (g_FontSurface) { + if (g_TargetSurface && g_FontSurface->format->format != g_TargetSurface->format->format) + { + SDL_Surface* converted = SDL_ConvertSurfaceFormat(g_FontSurface, g_TargetSurface->format->format, 0); + if (converted) + { + SDL_FreeSurface(g_FontSurface); + g_FontSurface = converted; + } + } ImGui::GetIO().Fonts->TexID = (ImTextureID)g_FontSurface; } return true; From 9084ab8861462e645af31b9f697fb271df2be42b Mon Sep 17 00:00:00 2001 From: moonpower Date: Thu, 25 Dec 2025 21:53:27 +0100 Subject: [PATCH 05/10] Backends: Refactor Makefile for example_sdl2_surface to improve cross-platform compatibility --- examples/example_sdl2_surface/Makefile | 36 +++++++++++++++++++++----- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/examples/example_sdl2_surface/Makefile b/examples/example_sdl2_surface/Makefile index 400112ea5..7e6a4a48c 100644 --- a/examples/example_sdl2_surface/Makefile +++ b/examples/example_sdl2_surface/Makefile @@ -1,9 +1,7 @@ -# Makefile for example_sdl2_surface (MinGW) +# Makefile for example_sdl2_surface CXX = g++ CXXFLAGS = -std=c++17 -Wall -Wformat -INCLUDES = -I../.. -I../../backends -I/mingw64/include/SDL2 -LIBS = -lmingw32 -lSDL2main -lSDL2 -lgdi32 IMGUI_DIR = ../.. SOURCES = main.cpp @@ -13,6 +11,32 @@ SOURCES += $(IMGUI_DIR)/backends/imgui_impl_sdlsurface2.cpp OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES)))) +UNAME_S := $(shell uname -s) + +SDL_CFLAGS := $(shell pkg-config --cflags sdl2 2>/dev/null) +SDL_LIBS := $(shell pkg-config --libs sdl2 2>/dev/null) +EXEEXT := +RM = rm -f + +ifneq (,$(findstring MINGW,$(UNAME_S))) + INCLUDES = -I../.. -I../../backends -I/mingw64/include/SDL2 + LIBS = -lmingw32 -lSDL2main -lSDL2 -lgdi32 + EXEEXT = .exe + RM = del /Q +else + ifeq ($(UNAME_S),Darwin) + ifeq ($(SDL_CFLAGS),) + SDL_CFLAGS = -F/Library/Frameworks -I/Library/Frameworks/SDL2.framework/Headers + SDL_LIBS = -F/Library/Frameworks -framework SDL2 + endif + endif + + INCLUDES = -I../.. -I../../backends $(SDL_CFLAGS) + LIBS = $(SDL_LIBS) +endif + +TARGET = example_sdl2_surface$(EXEEXT) + %.o:%.cpp $(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $< @@ -22,10 +46,10 @@ OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES)))) %.o:$(IMGUI_DIR)/backends/%.cpp $(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $< -all: example_sdl2_surface.exe +all: $(TARGET) -example_sdl2_surface.exe: $(OBJS) +$(TARGET): $(OBJS) $(CXX) -o $@ $^ $(LIBS) clean: - del /Q *.o *.exe + $(RM) *.o $(TARGET) From b3c5626b1ff0b64b6c09e798087bf3d445b15104 Mon Sep 17 00:00:00 2001 From: moonpower Date: Fri, 26 Dec 2025 02:48:40 +0100 Subject: [PATCH 06/10] Backends: Initialize and shutdown renderer backend for SDL_Surface --- backends/imgui_impl_sdlsurface2.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/backends/imgui_impl_sdlsurface2.cpp b/backends/imgui_impl_sdlsurface2.cpp index 75092fe4b..5ee9ead66 100644 --- a/backends/imgui_impl_sdlsurface2.cpp +++ b/backends/imgui_impl_sdlsurface2.cpp @@ -119,6 +119,12 @@ SDL_Surface* ImGui_ImplSDLSurface2_CreateFontAtlasSurface() bool ImGui_ImplSDLSurface2_Init(SDL_Surface* surface) { if (!surface) return false; + + ImGuiIO& io = ImGui::GetIO(); + IM_ASSERT(io.BackendRendererName == nullptr && "Already initialized a renderer backend!"); + io.BackendRendererName = "imgui_impl_sdlsurface2"; + io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; + g_TargetSurface = surface; g_FontSurface = ImGui_ImplSDLSurface2_CreateFontAtlasSurface(); if (g_FontSurface) @@ -139,6 +145,10 @@ bool ImGui_ImplSDLSurface2_Init(SDL_Surface* surface) void ImGui_ImplSDLSurface2_Shutdown() { + ImGuiIO& io = ImGui::GetIO(); + io.BackendRendererName = nullptr; + io.BackendFlags &= ~ImGuiBackendFlags_RendererHasTextures; + ImGui::GetIO().Fonts->TexID = nullptr; if (g_FontSurface) { From 189330241b35a786e5827a4b09a936250c3706e9 Mon Sep 17 00:00:00 2001 From: moonpower Date: Fri, 26 Dec 2025 02:51:18 +0100 Subject: [PATCH 07/10] Backends: Remove redundant BackendFlags setting in SDL_Surface initialization --- backends/imgui_impl_sdlsurface2.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/backends/imgui_impl_sdlsurface2.cpp b/backends/imgui_impl_sdlsurface2.cpp index 5ee9ead66..b26b0f276 100644 --- a/backends/imgui_impl_sdlsurface2.cpp +++ b/backends/imgui_impl_sdlsurface2.cpp @@ -123,7 +123,6 @@ bool ImGui_ImplSDLSurface2_Init(SDL_Surface* surface) ImGuiIO& io = ImGui::GetIO(); IM_ASSERT(io.BackendRendererName == nullptr && "Already initialized a renderer backend!"); io.BackendRendererName = "imgui_impl_sdlsurface2"; - io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; g_TargetSurface = surface; g_FontSurface = ImGui_ImplSDLSurface2_CreateFontAtlasSurface(); From d92ad93fddbe367611ab4c38894a86ff0a42f8d7 Mon Sep 17 00:00:00 2001 From: moonpower Date: Fri, 26 Dec 2025 03:00:50 +0100 Subject: [PATCH 08/10] Backends: Enhance SDL_Surface example to match the renderer example --- examples/example_sdl2_surface/main.cpp | 110 ++++++++++++++++++++++--- 1 file changed, 100 insertions(+), 10 deletions(-) diff --git a/examples/example_sdl2_surface/main.cpp b/examples/example_sdl2_surface/main.cpp index 966660f59..85a086ced 100644 --- a/examples/example_sdl2_surface/main.cpp +++ b/examples/example_sdl2_surface/main.cpp @@ -9,15 +9,24 @@ int main(int, char**) { - if (SDL_Init(SDL_INIT_VIDEO) != 0) + // Setup SDL + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) { printf("Error: %s\n", SDL_GetError()); return -1; } + // From 2.0.18: Enable native IME. +#ifdef SDL_HINT_IME_SHOW_UI + SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1"); +#endif + + // Create window + float main_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(0); + SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_SHOWN); SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+Surface Example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - 1280, 720, SDL_WINDOW_SHOWN); + (int)(1280 * main_scale), (int)(720 * main_scale), window_flags); if (!window) { printf("Error creating window: %s\n", SDL_GetError()); @@ -31,53 +40,134 @@ int main(int, char**) return -1; } - SDL_Surface* framebuffer = SDL_CreateRGBSurfaceWithFormat(0, 1280, 720, 32, SDL_PIXELFORMAT_RGBA32); + int win_w = window_surface->w; + int win_h = window_surface->h; + SDL_Surface* framebuffer = SDL_CreateRGBSurfaceWithFormat(0, win_w, win_h, 32, SDL_PIXELFORMAT_RGBA32); if (!framebuffer) { printf("Error creating framebuffer: %s\n", SDL_GetError()); return -1; } + // Setup Dear ImGui context IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; - io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls + // Setup Dear ImGui style ImGui::StyleColorsDark(); - ImGui_ImplSDL2_InitForSDLRenderer(window, nullptr); + + // Setup scaling + ImGuiStyle& style = ImGui::GetStyle(); + style.ScaleAllSizes(main_scale); // Bake a fixed style scale. + style.FontScaleDpi = main_scale; + + // Setup Platform/Renderer backends + ImGui_ImplSDL2_InitForOther(window); ImGui_ImplSDL2_SetGamepadMode(ImGui_ImplSDL2_GamepadMode_AutoFirst, nullptr, 0); ImGui_ImplSDLSurface2_Init(framebuffer); + // Our state bool show_demo_window = true; + bool show_another_window = false; ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); + // Main loop bool done = false; while (!done) { + // Poll and handle events (inputs, window resize, etc.) SDL_Event event; while (SDL_PollEvent(&event)) { ImGui_ImplSDL2_ProcessEvent(&event); if (event.type == SDL_QUIT) done = true; - if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) - done = true; + + if (event.type == SDL_WINDOWEVENT) + { + if (event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) + done = true; + + if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED || event.window.event == SDL_WINDOWEVENT_RESIZED) + { + // Recreate framebuffer at new size + window_surface = SDL_GetWindowSurface(window); + int new_w = window_surface->w; + int new_h = window_surface->h; + if (new_w != win_w || new_h != win_h) + { + SDL_FreeSurface(framebuffer); + framebuffer = SDL_CreateRGBSurfaceWithFormat(0, new_w, new_h, 32, SDL_PIXELFORMAT_RGBA32); + if (!framebuffer) { printf("Error creating framebuffer after resize: %s\n", SDL_GetError()); return -1; } + win_w = new_w; win_h = new_h; + // Re-init the backend with the new framebuffer + ImGui_ImplSDLSurface2_Shutdown(); + ImGui_ImplSDLSurface2_Init(framebuffer); + } + } + } } + // Skip rendering when minimized to avoid busy-looping + if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) + { + SDL_Delay(10); + continue; + } + + // Start the Dear ImGui frame ImGui_ImplSDLSurface2_NewFrame(); ImGui_ImplSDL2_NewFrame(); ImGui::NewFrame(); + // 1. Show the big demo window if (show_demo_window) ImGui::ShowDemoWindow(&show_demo_window); + // 2. Show a simple window that we create ourselves. Mirror the SDL_Renderer example. + { + static float f = 0.0f; + static int counter = 0; + + ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. + + ImGui::Text("This is some useful text."); // Display some text + ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state + ImGui::Checkbox("Another Window", &show_another_window); + + ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f + ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color + + if (ImGui::Button("Button")) // Buttons return true when clicked + counter++; + ImGui::SameLine(); + ImGui::Text("counter = %d", counter); + + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); + ImGui::End(); + } + + // 3. Show another simple window. + if (show_another_window) + { + ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked) + ImGui::Text("Hello from another window!"); + if (ImGui::Button("Close Me")) + show_another_window = false; + ImGui::End(); + } + + // Rendering ImGui::Render(); - SDL_FillRect(framebuffer, nullptr, SDL_MapRGB(framebuffer->format, + SDL_FillRect(framebuffer, nullptr, SDL_MapRGBA(framebuffer->format, (Uint8)(clear_color.x * 255), (Uint8)(clear_color.y * 255), - (Uint8)(clear_color.z * 255))); + (Uint8)(clear_color.z * 255), + (Uint8)(clear_color.w * 255))); ImGui_ImplSDLSurface2_RenderDrawData(ImGui::GetDrawData()); From c57a5b31dba71b1ca8babd675294238e01da2033 Mon Sep 17 00:00:00 2001 From: moonpower Date: Fri, 26 Dec 2025 03:07:26 +0100 Subject: [PATCH 09/10] Backends: Update Makefile for example_sdl2_surface to match the renderer example --- examples/example_sdl2_surface/Makefile | 85 +++++++++++++++----------- 1 file changed, 51 insertions(+), 34 deletions(-) diff --git a/examples/example_sdl2_surface/Makefile b/examples/example_sdl2_surface/Makefile index 7e6a4a48c..c144bd6e2 100644 --- a/examples/example_sdl2_surface/Makefile +++ b/examples/example_sdl2_surface/Makefile @@ -1,55 +1,72 @@ -# Makefile for example_sdl2_surface - -CXX = g++ -CXXFLAGS = -std=c++17 -Wall -Wformat +# +# Cross Platform Makefile (example_sdl2_surface) +# Compatible with MSYS2/MINGW, Linux and macOS +# +# You will need SDL2 (http://www.libsdl.org): +# Linux: apt-get install libsdl2-dev +# macOS: brew install sdl2 +# MSYS2: pacman -S mingw-w64-i686-SDL2 +# +#EXE = example_sdl2_surface +EXE = example_sdl2_surface IMGUI_DIR = ../.. SOURCES = main.cpp SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp -SOURCES += $(IMGUI_DIR)/backends/imgui_impl_sdl2.cpp -SOURCES += $(IMGUI_DIR)/backends/imgui_impl_sdlsurface2.cpp - +SOURCES += $(IMGUI_DIR)/backends/imgui_impl_sdl2.cpp $(IMGUI_DIR)/backends/imgui_impl_sdlsurface2.cpp OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES)))) - UNAME_S := $(shell uname -s) -SDL_CFLAGS := $(shell pkg-config --cflags sdl2 2>/dev/null) -SDL_LIBS := $(shell pkg-config --libs sdl2 2>/dev/null) -EXEEXT := -RM = rm -f +CXXFLAGS = -std=c++11 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends +CXXFLAGS += -g -Wall -Wformat +LIBS = -ifneq (,$(findstring MINGW,$(UNAME_S))) - INCLUDES = -I../.. -I../../backends -I/mingw64/include/SDL2 - LIBS = -lmingw32 -lSDL2main -lSDL2 -lgdi32 - EXEEXT = .exe - RM = del /Q -else - ifeq ($(UNAME_S),Darwin) - ifeq ($(SDL_CFLAGS),) - SDL_CFLAGS = -F/Library/Frameworks -I/Library/Frameworks/SDL2.framework/Headers - SDL_LIBS = -F/Library/Frameworks -framework SDL2 - endif - endif +##--------------------------------------------------------------------- +## BUILD FLAGS PER PLATFORM +##--------------------------------------------------------------------- - INCLUDES = -I../.. -I../../backends $(SDL_CFLAGS) - LIBS = $(SDL_LIBS) +ifeq ($(UNAME_S), Linux) #LINUX + ECHO_MESSAGE = "Linux" + LIBS += `sdl2-config --libs` + + CXXFLAGS += `sdl2-config --cflags` + CFLAGS = $(CXXFLAGS) endif -TARGET = example_sdl2_surface$(EXEEXT) +ifeq ($(UNAME_S), Darwin) #APPLE + ECHO_MESSAGE = "Mac OS X" + LIBS += `sdl2-config --libs` + CXXFLAGS += `sdl2-config --cflags` + CXXFLAGS += -I/usr/local/include -I/opt/local/include + CFLAGS = $(CXXFLAGS) +endif + +ifeq ($(OS), Windows_NT) + ECHO_MESSAGE = "MinGW" + LIBS += -lgdi32 `pkg-config --static --libs sdl2` + + CXXFLAGS += `pkg-config --cflags sdl2` + CFLAGS = $(CXXFLAGS) +endif + +##--------------------------------------------------------------------- +## BUILD RULES +##--------------------------------------------------------------------- %.o:%.cpp - $(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $< + $(CXX) $(CXXFLAGS) -c -o $@ $< %.o:$(IMGUI_DIR)/%.cpp - $(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $< + $(CXX) $(CXXFLAGS) -c -o $@ $< %.o:$(IMGUI_DIR)/backends/%.cpp - $(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $< + $(CXX) $(CXXFLAGS) -c -o $@ $< -all: $(TARGET) +all: $(EXE) + @echo Build complete for $(ECHO_MESSAGE) -$(TARGET): $(OBJS) - $(CXX) -o $@ $^ $(LIBS) +$(EXE): $(OBJS) + $(CXX) -o $@ $^ $(CXXFLAGS) $(LIBS) clean: - $(RM) *.o $(TARGET) + rm -f $(EXE) $(OBJS) From a941c96d1f7d4f7d2a2656b31d87504d24bae355 Mon Sep 17 00:00:00 2001 From: moonpower Date: Fri, 26 Dec 2025 03:37:07 +0100 Subject: [PATCH 10/10] Backends: Replace std::clamp with ImClamp to satisfy C++11 --- backends/imgui_impl_sdlsurface2.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/backends/imgui_impl_sdlsurface2.cpp b/backends/imgui_impl_sdlsurface2.cpp index b26b0f276..05e55c177 100644 --- a/backends/imgui_impl_sdlsurface2.cpp +++ b/backends/imgui_impl_sdlsurface2.cpp @@ -7,6 +7,16 @@ #include #include +// No clamp in C++11 +template +static inline T ImClamp(T v, T lo, T hi) +{ + if (v < lo) return lo; + if (v > hi) return hi; + return v; +} + + static SDL_Surface* g_TargetSurface = nullptr; static SDL_Surface* g_FontSurface = nullptr; @@ -270,10 +280,10 @@ void ImGui_ImplSDLSurface2_RenderDrawData(ImDrawData* draw_data) float b = w0 * c0[2] + w1 * c1[2] + w2 * c2[2]; float a = w0 * c0[3] + w1 * c1[3] + w2 * c2[3]; - Uint8 out_r = (Uint8)(std::clamp(r, 0.0f, 1.0f) * 255.0f); - Uint8 out_g = (Uint8)(std::clamp(g, 0.0f, 1.0f) * 255.0f); - Uint8 out_b = (Uint8)(std::clamp(b, 0.0f, 1.0f) * 255.0f); - Uint8 out_a = (Uint8)(std::clamp(a, 0.0f, 1.0f) * 255.0f); + Uint8 out_r = (Uint8)(ImClamp(r, 0.0f, 1.0f) * 255.0f); + Uint8 out_g = (Uint8)(ImClamp(g, 0.0f, 1.0f) * 255.0f); + Uint8 out_b = (Uint8)(ImClamp(b, 0.0f, 1.0f) * 255.0f); + Uint8 out_a = (Uint8)(ImClamp(a, 0.0f, 1.0f) * 255.0f); if (tex) { @@ -283,8 +293,8 @@ void ImGui_ImplSDLSurface2_RenderDrawData(ImDrawData* draw_data) int tx = (int)(u * (tex->w - 1) + 0.5f); int ty = (int)(v * (tex->h - 1) + 0.5f); - tx = std::clamp(tx, 0, tex->w - 1); - ty = std::clamp(ty, 0, tex->h - 1); + tx = ImClamp(tx, 0, tex->w - 1); + ty = ImClamp(ty, 0, tex->h - 1); Uint32 tpx = GetPixel(tex, tx, ty); Uint8 tr, tg, tb, ta;