Skip to content

Commit

Permalink
texture: abstract get pixel color method into drawutils
Browse files Browse the repository at this point in the history
  • Loading branch information
vgmoose committed Dec 1, 2023
1 parent d99a95d commit 647a517
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/DrawUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions src/DrawUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 4 additions & 2 deletions src/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 647a517

Please sign in to comment.