From 9a6cd2b453a10350876ee56fcd4f3987e3a9c0da Mon Sep 17 00:00:00 2001 From: Guldoman Date: Thu, 3 Feb 2022 01:50:43 +0100 Subject: [PATCH] Use SDL to manage color format mapping in `ren_draw_rect` --- src/renderer.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/renderer.c b/src/renderer.c index b0e17f58..5e26a3f5 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -331,18 +332,23 @@ void ren_draw_rect(RenRect rect, RenColor color) { y2 = y2 > clip.y + clip.height ? clip.y + clip.height : y2; SDL_Surface *surface = renwin_get_surface(&window_renderer); - RenColor *d = (RenColor*) surface->pixels; + uint32_t *d = surface->pixels; d += x1 + y1 * surface->w; int dr = surface->w - (x2 - x1); - unsigned int translated = SDL_MapRGB(surface->format, color.r, color.g, color.b); if (color.a == 0xff) { + uint32_t translated = SDL_MapRGB(surface->format, color.r, color.g, color.b); SDL_Rect rect = { x1, y1, x2 - x1, y2 - y1 }; SDL_FillRect(surface, &rect, translated); } else { - RenColor translated_color = (RenColor){ translated & 0xFF, (translated >> 8) & 0xFF, (translated >> 16) & 0xFF, color.a }; + RenColor current_color; + RenColor blended_color; for (int j = y1; j < y2; j++) { for (int i = x1; i < x2; i++, d++) - *d = blend_pixel(*d, translated_color); + { + SDL_GetRGB(*d, surface->format, ¤t_color.r, ¤t_color.g, ¤t_color.b); + blended_color = blend_pixel(current_color, color); + *d = SDL_MapRGB(surface->format, blended_color.r, blended_color.g, blended_color.b); + } d += dr; } }