diff options
author | June McEnroe <june@causal.agency> | 2019-03-27 14:27:00 -0400 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2019-03-27 14:27:00 -0400 |
commit | 154f94d53dc73fad3622b6497fc85e9b93f824b7 (patch) | |
tree | c0d212dfef5d257f6183da01326580122f9a8f8a /cards.c | |
parent | Add flags to dump (diff) | |
download | wep-154f94d53dc73fad3622b6497fc85e9b93f824b7.tar.gz wep-154f94d53dc73fad3622b6497fc85e9b93f824b7.zip |
Add Cards_Invert
Diffstat (limited to '')
-rw-r--r-- | cards.c | 45 |
1 files changed, 45 insertions, 0 deletions
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; |