From 154f94d53dc73fad3622b6497fc85e9b93f824b7 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Wed, 27 Mar 2019 14:27:00 -0400 Subject: Add Cards_Invert --- cards.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'cards.c') diff --git a/cards.c b/cards.c index 174b081..29aa7a4 100644 --- a/cards.c +++ b/cards.c @@ -358,6 +358,51 @@ fail: return NULL; } +int invertPalette(SDL_Surface *surface) { + const SDL_Palette *palette = surface->format->palette; + SDL_Palette *invert = SDL_AllocPalette(palette->ncolors); + if (!invert) return -1; + for (int i = 0; i < invert->ncolors; ++i) { + invert->colors[i].r = ~palette->colors[i].r; + invert->colors[i].g = ~palette->colors[i].g; + invert->colors[i].b = ~palette->colors[i].b; + invert->colors[i].a = palette->colors[i].a; + } + if (SDL_SetSurfacePalette(surface, invert) < 0) return -1; + SDL_FreePalette(invert); + return 0; +} + +int invertPixels(SDL_Surface *surface) { + if (SDL_LockSurface(surface) < 0) return -1; + Uint8 *pixels = surface->pixels; + for (int y = 0; y < surface->h; ++y) { + Uint32 *row = (Uint32 *)&pixels[y * surface->pitch]; + for (int x = 0; x < surface->w; ++x) { + Uint32 color = ~row[x] & ~surface->format->Amask; + Uint32 alpha = row[x] & surface->format->Amask; + row[x] = color | alpha; + } + } + SDL_UnlockSurface(surface); + return 0; +} + +int Cards_Invert(struct Cards *cards) { + for (int i = 0; i < Cards_Count; ++i) { + if (!cards->surfaces[i]) continue; + if (cards->surfaces[i]->format->palette) { + if (invertPalette(cards->surfaces[i]) < 0) return -1; + } else if (cards->surfaces[i]->format->BytesPerPixel == 4) { + if (invertPixels(cards->surfaces[i]) < 0) return -1; + } else { + SDL_SetError("unexpected surface format"); + return -1; + } + } + return 0; +} + void Cards_Free(struct Cards *cards) { for (int i = 0; i < Cards_Count; ++i) { if (!cards->surfaces[i]) continue; -- cgit 1.4.1