From 636ab5c26242dce0072e3944878ea2750965a343 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Wed, 30 Jul 2014 18:43:17 +0200 Subject: [PATCH] Implemented support for rounded rectangles --- src/video/sdl/sdl_renderer.cpp | 63 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 6 deletions(-) diff --git a/src/video/sdl/sdl_renderer.cpp b/src/video/sdl/sdl_renderer.cpp index 798e17721..38cd046d6 100644 --- a/src/video/sdl/sdl_renderer.cpp +++ b/src/video/sdl/sdl_renderer.cpp @@ -498,16 +498,67 @@ SDLRenderer::draw_filled_rect(const DrawingRequest& request) rect.w = fillrectrequest->size.x; rect.h = fillrectrequest->size.y; - if((rect.w != 0) && (rect.h != 0)) + Uint8 r = static_cast(fillrectrequest->color.red * 255); + Uint8 g = static_cast(fillrectrequest->color.green * 255); + Uint8 b = static_cast(fillrectrequest->color.blue * 255); + Uint8 a = static_cast(fillrectrequest->color.alpha * 255); + + int radius = std::min(rect.h / 2, static_cast(fillrectrequest->radius)); + + if (radius) { - Uint8 r = static_cast(fillrectrequest->color.red * 255); - Uint8 g = static_cast(fillrectrequest->color.green * 255); - Uint8 b = static_cast(fillrectrequest->color.blue * 255); - Uint8 a = static_cast(fillrectrequest->color.alpha * 255); + int slices = radius; + + // rounded top and bottom parts + std::vector rects; + rects.reserve(2*slices + 1); + for(int i = 0; i < slices; ++i) + { + float p = (static_cast(i) + 0.5f) / static_cast(slices); + int xoff = radius - static_cast(sqrtf(1.0f - p*p) * radius); + + SDL_Rect tmp; + tmp.x = rect.x + xoff; + tmp.y = rect.y + (radius - i); + tmp.w = rect.w - 2*(xoff); + tmp.h = 1; + rects.push_back(tmp); + + SDL_Rect tmp2; + tmp2.x = rect.x + xoff; + tmp2.y = rect.y + rect.h - radius + i; + tmp2.w = rect.w - 2*xoff; + tmp2.h = 1; + + if (tmp2.y != tmp.y) + { + rects.push_back(tmp2); + } + } + + if (2*radius < rect.h) + { + // center rectangle + SDL_Rect tmp; + tmp.x = rect.x; + tmp.y = rect.y + radius + 1; + tmp.w = rect.w; + tmp.h = rect.h - 2*radius - 1; + rects.push_back(tmp); + } SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); SDL_SetRenderDrawColor(renderer, r, g, b, a); - SDL_RenderFillRect(renderer, &rect); + SDL_RenderFillRects(renderer, &*rects.begin(), rects.size()); + } + else + { + if((rect.w != 0) && (rect.h != 0)) + { + SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); + SDL_SetRenderDrawColor(renderer, r, g, b, a); + SDL_RenderFillRect(renderer, &rect); + } } #ifdef OLD_SDL1 -- 2.11.0