From 647a517ec8512b96f98f42ca5c22a3b9b4ffb53c Mon Sep 17 00:00:00 2001 From: vgmoose Date: Thu, 30 Nov 2023 20:19:48 -0500 Subject: [PATCH] texture: abstract get pixel color method into drawutils --- src/DrawUtils.cpp | 10 ++++++++++ src/DrawUtils.hpp | 1 + src/Texture.cpp | 6 ++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/DrawUtils.cpp b/src/DrawUtils.cpp index 20ddcbe..10c8d42 100644 --- a/src/DrawUtils.cpp +++ b/src/DrawUtils.cpp @@ -154,6 +154,16 @@ void CST_MixerInit(RootDisplay* root) #endif } + +void CST_GetRGBA(Uint32 pixel, SDL_PixelFormat* format, CST_Color* cstColor) +{ +#ifndef SDL1 + SDL_GetRGBA(pixel, format, &cstColor->r, &cstColor->g, &cstColor->b, &cstColor->a); +#else + SDL_GetRGB(pixel, format, &cstColor->r, &cstColor->g, &cstColor->b); +#endif +} + // https://stackoverflow.com/a/51238719/4953343 bool CST_SavePNG(CST_Texture* texture, const char* file_name) { diff --git a/src/DrawUtils.hpp b/src/DrawUtils.hpp index a21b11a..02a779e 100644 --- a/src/DrawUtils.hpp +++ b/src/DrawUtils.hpp @@ -83,6 +83,7 @@ void CST_Delay(int time); int CST_GetTicks(); bool CST_isRectOffscreen(CST_Rect* rect); +void CST_GetRGBA(Uint32 pixel, SDL_PixelFormat* format, CST_Color* cstColor); bool CST_SavePNG(CST_Texture* texture, const char* filename); void CST_SetWindowTitle(const char* title); diff --git a/src/Texture.cpp b/src/Texture.cpp index 1058164..79ca9ab 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -62,7 +62,7 @@ bool Texture::loadFromSurface(CST_Surface *surface) // load first pixel color auto pixelcolor = getpixel(surface, 0, 0); - SDL_GetRGBA(pixelcolor, surface->format, &texFirstPixel.r, &texFirstPixel.g, &texFirstPixel.b, &texFirstPixel.a); + CST_GetRGBA(pixelcolor, surface->format, &texFirstPixel); // load texture size CST_QueryTexture(texture, &texW, &texH); @@ -134,11 +134,13 @@ void Texture::render(Element* parent) // draw colored background CST_SetDrawColor(renderer, texFirstPixel); - auto color = (CST_Color){texFirstPixel.r,texFirstPixel.g,texFirstPixel.b,0xFF}; + auto color = (CST_Color){texFirstPixel.r, texFirstPixel.g, texFirstPixel.b, 0xFF}; // if the first pixel is transparent, use white +#ifndef SDL1 if (texFirstPixel.a == 0) color = (CST_Color){0xFF,0xFF,0xFF,0xFF}; +#endif CST_SetDrawColor(renderer, color); CST_FillRect(renderer, &rect);