Complete the refactoring of the SDL graphics libs

This commit is contained in:
Michael Martin
2019-08-24 17:58:24 -07:00
parent 846315325b
commit 371a42acdd
5 changed files with 130 additions and 92 deletions
+49 -44
View File
@@ -112,10 +112,11 @@ TFB_DrawCanvas_Rect (RECT *rect, Color color, DrawMode mode, TFB_Canvas target)
if (mode.kind == DRAW_REPLACE)
{ // Standard SDL fillrect rendering
if (fmt->Amask && (dst->flags & SDL_SRCCOLORKEY))
Uint32 colorkey;
if (fmt->Amask && (TFB_GetColorKey (dst, &colorkey) == 0))
{ // special case -- alpha surface with colorkey
// colorkey rects are transparent
if ((sdlColor & ~fmt->Amask) == (fmt->colorkey & ~fmt->Amask))
if ((sdlColor & ~fmt->Amask) == (colorkey & ~fmt->Amask))
sdlColor &= ~fmt->Amask; // make transparent
}
SDL_FillRect (dst, &sr, sdlColor);
@@ -150,11 +151,12 @@ TFB_DrawCanvas_Blit (SDL_Surface *src, SDL_Rect *src_r,
{ // Standard SDL surface-alpha blit
// Note that surface alpha and per-pixel alpha cannot work
// at the same time, which is why the Amask test
assert (!(src->flags & SDL_SRCALPHA));
int hasAlpha = TFB_HasSurfaceAlphaMod (src);
assert (!hasAlpha);
// Set surface alpha temporarily
SDL_SetAlpha (src, SDL_SRCALPHA, mode.factor);
TFB_SetSurfaceAlphaMod (src, mode.factor);
SDL_BlitSurface (src, src_r, dst, dst_r);
SDL_SetAlpha (src, 0, 255);
TFB_DisableSurfaceAlphaMod (src);
}
else
{ // Custom blit
@@ -213,7 +215,7 @@ TFB_DrawCanvas_Image (TFB_Image *img, int x, int y, int scale,
NormalPal = ((SDL_Surface *)img->NormalImg)->format->palette;
// only set the new palette if it changed
if (NormalPal && cmap && img->colormap_version != cmap->version)
SDL_SetColors (img->NormalImg, cmap->palette->colors, 0, 256);
TFB_SetColors (img->NormalImg, cmap->palette->colors, 0, 256);
if (scale != 0 && scale != GSCALE_IDENTITY)
{
@@ -222,7 +224,7 @@ TFB_DrawCanvas_Image (TFB_Image *img, int x, int y, int scale,
// only set the new palette if it changed
if (TFB_DrawCanvas_IsPaletted (img->MipmapImg)
&& cmap && img->colormap_version != cmap->version)
SDL_SetColors (img->MipmapImg, cmap->palette->colors, 0, 256);
TFB_SetColors (img->MipmapImg, cmap->palette->colors, 0, 256);
}
else if (scaleMode == TFB_SCALE_TRILINEAR && !img->MipmapImg)
{ // Do bilinear scaling instead when mipmap is unavailable
@@ -236,7 +238,7 @@ TFB_DrawCanvas_Image (TFB_Image *img, int x, int y, int scale,
// We may only get a paletted scaled image if the source is
// paletted. Currently, all scaling targets are truecolor.
assert (NormalPal && NormalPal->colors);
SDL_SetColors (surf, NormalPal->colors, 0, NormalPal->ncolors);
TFB_SetColors (surf, NormalPal->colors, 0, NormalPal->ncolors);
}
srcRect.x = 0;
@@ -285,7 +287,7 @@ TFB_DrawCanvas_Fill (SDL_Surface *src, Uint32 fillcolor, SDL_Surface *dst)
Uint32 *src_p;
Uint32 *dst_p;
int x, y;
Uint32 dstkey = 0; // 0 means alpha=0 too
Uint32 srckey = 0, dstkey = 0; // 0 means alpha=0 too
Uint32 amask = srcfmt->Amask;
int alpha = (fillcolor & amask) >> srcfmt->Ashift;
@@ -339,9 +341,8 @@ TFB_DrawCanvas_Fill (SDL_Surface *src, Uint32 fillcolor, SDL_Surface *dst)
}
}
}
else if (src->flags & SDL_SRCCOLORKEY)
else if (TFB_GetColorKey (src, &srckey) == 0)
{ // colorkey-based fill
Uint32 srckey = srcfmt->colorkey;
for (y = 0; y < height; ++y, dst_p += ddst, src_p += dsrc)
{
@@ -363,7 +364,7 @@ TFB_DrawCanvas_Fill (SDL_Surface *src, Uint32 fillcolor, SDL_Surface *dst)
SDL_UnlockSurface(src);
// save the colorkey (dynamic image -- not using RLE coding here)
SDL_SetColorKey (dst, SDL_SRCCOLORKEY, dstkey);
TFB_SetColorKey (dst, dstkey, 0);
// if the filled surface is RGBA, colorkey will only be used
// when SDL_SRCALPHA flag is cleared. this allows us to blit
// the surface in different ways to diff targets
@@ -431,7 +432,7 @@ TFB_DrawCanvas_FilledImage (TFB_Image *img, int x, int y, int scale,
palette = surf->format->palette;
if (palette)
{ // set palette for fill-stamp
// Calling SDL_SetColors() results in an expensive src -> dst
// Calling TFB_SetColors() results in an expensive src -> dst
// color-mapping operation for an SDL blit, following the call.
// We want to avoid that as much as possible.
@@ -443,7 +444,7 @@ TFB_DrawCanvas_FilledImage (TFB_Image *img, int x, int y, int scale,
for (i = 1; i < palette->ncolors; i++)
colors[i] = colors[0];
SDL_SetColors (surf, colors, 0, palette->ncolors);
TFB_SetColors (surf, colors, 0, palette->ncolors);
// reflect the change in *actual* image palette
img->colormap_version--;
}
@@ -671,11 +672,11 @@ TFB_DrawCanvas_New_Paletted (int w, int h, Color palette[256],
}
if (transparent_index >= 0)
{
SDL_SetColorKey (new_surf, SDL_SRCCOLORKEY, transparent_index);
TFB_SetColorKey (new_surf, transparent_index, 0);
}
else
{
SDL_SetColorKey (new_surf, 0, 0);
TFB_DisableColorKey (new_surf);
}
return new_surf;
}
@@ -888,14 +889,17 @@ TFB_DrawCanvas_SetPalette (TFB_Canvas target, Color palette[256])
for (i = 0; i < 256; ++i)
colors[i] = ColorToNative (palette[i]);
SDL_SetColors (target, colors, 0, 256);
TFB_SetColors (target, colors, 0, 256);
}
int
TFB_DrawCanvas_GetTransparentIndex (TFB_Canvas canvas)
{
if (((SDL_Surface *)canvas)->flags & SDL_SRCCOLORKEY)
return ((SDL_Surface *)canvas)->format->colorkey;
Uint32 colorkey;
if (TFB_GetColorKey (canvas, &colorkey))
{
return colorkey;
}
return -1;
}
@@ -904,20 +908,17 @@ TFB_DrawCanvas_SetTransparentIndex (TFB_Canvas canvas, int index, BOOLEAN rleacc
{
if (index >= 0)
{
int flags = SDL_SRCCOLORKEY;
if (rleaccel)
flags |= SDL_RLEACCEL;
SDL_SetColorKey (canvas, flags, index);
TFB_SetColorKey (canvas, index, rleaccel);
if (!TFB_DrawCanvas_IsPaletted (canvas))
{
// disables surface alpha so color key transparency actually works
SDL_SetAlpha (canvas, 0, 255);
TFB_DisableSurfaceAlphaMod (canvas);
}
}
else
{
SDL_SetColorKey (canvas, 0, 0);
TFB_DisableColorKey (canvas);
}
}
@@ -944,11 +945,11 @@ TFB_DrawCanvas_CopyTransparencyInfo (TFB_Canvas src_canvas,
BOOLEAN
TFB_DrawCanvas_GetTransparentColor (TFB_Canvas canvas, Color *color)
{
Uint32 colorkey;
if (!TFB_DrawCanvas_IsPaletted (canvas)
&& (((SDL_Surface *)canvas)->flags & SDL_SRCCOLORKEY) )
&& (TFB_GetColorKey ((SDL_Surface *)canvas, &colorkey) == 0))
{
Uint8 ur, ug, ub;
int colorkey = ((SDL_Surface *)canvas)->format->colorkey;
SDL_GetRGB (colorkey, ((SDL_Surface *)canvas)->format, &ur, &ug, &ub);
color->r = ur;
color->g = ug;
@@ -964,17 +965,14 @@ TFB_DrawCanvas_SetTransparentColor (TFB_Canvas canvas, Color color,
BOOLEAN rleaccel)
{
Uint32 sdlColor;
int flags = SDL_SRCCOLORKEY;
if (rleaccel)
flags |= SDL_RLEACCEL;
sdlColor = SDL_MapRGBA (((SDL_Surface *)canvas)->format,
color.r, color.g, color.b, 0);
SDL_SetColorKey (canvas, flags, sdlColor);
TFB_SetColorKey (canvas, sdlColor, rleaccel);
if (!TFB_DrawCanvas_IsPaletted (canvas))
{
// disables surface alpha so color key transparency actually works
SDL_SetAlpha (canvas, 0, 255);
TFB_DisableSurfaceAlphaMod (canvas);
}
}
@@ -1284,8 +1282,7 @@ TFB_DrawCanvas_Rescale_Trilinear (TFB_Canvas src_canvas, TFB_Canvas src_mipmap,
const int slen = src->pitch;
const int mmlen = mm->pitch;
const int dst_has_alpha = (dstfmt->Amask != 0);
const int transparent = (dst->flags & SDL_SRCCOLORKEY) ?
dstfmt->colorkey : 0;
Uint32 transparent = 0;
const int alpha_threshold = dst_has_alpha ? 0 : 127;
// src v. mipmap importance factor
int ratio = scale * 2 - GSCALE_IDENTITY;
@@ -1299,6 +1296,8 @@ TFB_DrawCanvas_Rescale_Trilinear (TFB_Canvas src_canvas, TFB_Canvas src_mipmap,
int ssx0 = 0, ssy0 = 0, ssx1 = 0, ssy1 = 0;
int x, y, w, h;
TFB_GetColorKey (dst, &transparent);
if (mmfmt->palette && !srcpal)
{
log_add (log_Warning, "TFB_DrawCanvas_Rescale_Trilinear: "
@@ -1385,10 +1384,10 @@ TFB_DrawCanvas_Rescale_Trilinear (TFB_Canvas src_canvas, TFB_Canvas src_mipmap,
mk0 = srcfmt->Amask;
ck0 = 0;
}
else if (src->flags & SDL_SRCCOLORKEY)
else if (TFB_GetColorKey (src, &ck0) == 0)
{ // colorkey transparency
mk0 = ~srcfmt->Amask;
ck0 = srcfmt->colorkey & mk0;
ck0 &= mk0;
}
if (mmfmt->Amask)
@@ -1396,10 +1395,10 @@ TFB_DrawCanvas_Rescale_Trilinear (TFB_Canvas src_canvas, TFB_Canvas src_mipmap,
mk1 = mmfmt->Amask;
ck1 = 0;
}
else if (mm->flags & SDL_SRCCOLORKEY)
else if (TFB_GetColorKey (mm, &ck1) == 0)
{ // colorkey transparency
mk1 = ~mmfmt->Amask;
ck1 = mmfmt->colorkey & mk1;
ck1 &= mk1;
}
SDL_LockSurface(src);
@@ -1598,8 +1597,7 @@ TFB_DrawCanvas_Rescale_Bilinear (TFB_Canvas src_canvas, TFB_Canvas dst_canvas,
const int sbpp = srcfmt->BytesPerPixel;
const int slen = src->pitch;
const int dst_has_alpha = (dstfmt->Amask != 0);
const int transparent = (dst->flags & SDL_SRCCOLORKEY) ?
dstfmt->colorkey : 0;
Uint32 srckey = 0, transparent = 0;
const int alpha_threshold = dst_has_alpha ? 0 : 127;
// source masks and keys
Uint32 mk = 0, ck = ~0;
@@ -1611,6 +1609,9 @@ TFB_DrawCanvas_Rescale_Bilinear (TFB_Canvas src_canvas, TFB_Canvas dst_canvas,
int ssx = 0, ssy = 0;
int x, y, w, h;
// Get destination transparent color if it exists
TFB_GetColorKey (dst, &transparent);
if (scale > 0)
{
// Use (scale / GSCALE_IDENTITY) sizing factor
@@ -1663,10 +1664,10 @@ TFB_DrawCanvas_Rescale_Bilinear (TFB_Canvas src_canvas, TFB_Canvas dst_canvas,
mk = srcfmt->Amask;
ck = 0;
}
else if (src->flags & SDL_SRCCOLORKEY)
else if (TFB_GetColorKey (src, &srckey) == 0)
{ // colorkey transparency
mk = ~srcfmt->Amask;
ck = srcfmt->colorkey & mk;
ck = srckey & mk;
}
SDL_LockSurface(src);
@@ -1977,8 +1978,10 @@ TFB_DrawCanvas_Intersect (TFB_Canvas canvas1, POINT c1org,
}
else
{ // colorkey transparency
Uint32 colorkey = 0;
TFB_GetColorKey(surf1, &colorkey);
s1mask = ~surf1->format->Amask;
s1key = surf1->format->colorkey & s1mask;
s1key = colorkey & s1mask;
}
if (surf2->format->Amask)
@@ -1989,8 +1992,10 @@ TFB_DrawCanvas_Intersect (TFB_Canvas canvas1, POINT c1org,
}
else
{ // colorkey transparency
Uint32 colorkey = 0;
TFB_GetColorKey(surf2, &colorkey);
s2mask = ~surf2->format->Amask;
s2key = surf2->format->colorkey & s2mask;
s2key = colorkey & s2mask;
}
// convert surface origins to pixel offsets within
+16 -7
View File
@@ -11,6 +11,7 @@
#include <stdlib.h>
#include <string.h>
#include "sdl_common.h"
#include "libs/memlib.h"
#include "port.h"
#include "rotozoom.h"
@@ -502,6 +503,7 @@ void transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int
int x, y, dx, dy, xd, yd, sdx, sdy, ax, ay, sw, sh;
tColorY *pc, *sp;
int gap;
Uint32 colorkey = 0;
/*
* Variable setup
@@ -517,7 +519,8 @@ void transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int
/*
* Clear surface to colorkey
*/
memset(pc, (unsigned char) (src->format->colorkey & 0xff), dst->pitch * dst->h);
TFB_GetColorKey (src, &colorkey);
memset(pc, (unsigned char) (colorkey & 0xff), dst->pitch * dst->h);
/*
* Iterate through destination surface
*/
@@ -704,11 +707,13 @@ SDL_Surface *rotozoomSurface(SDL_Surface * src, double angle, double zoom, int s
/*
* Turn on source-alpha support
*/
SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);
TFB_SetSurfaceAlphaMod (rz_dst, 255);
} else {
/*
* Copy palette and colorkey info
*/
Uint32 srckey = 0;
TFB_GetColorKey (rz_src, &srckey);
for (i = 0; i < rz_src->format->palette->ncolors; i++) {
rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
}
@@ -718,7 +723,7 @@ SDL_Surface *rotozoomSurface(SDL_Surface * src, double angle, double zoom, int s
*/
transformSurfaceY(rz_src, rz_dst, dstwidthhalf, dstheighthalf,
(int) (sanglezoominv), (int) (canglezoominv));
SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, rz_src->format->colorkey);
TFB_SetColorKey(rz_dst, srckey, 1);
}
/*
* Unlock source surface
@@ -773,11 +778,13 @@ SDL_Surface *rotozoomSurface(SDL_Surface * src, double angle, double zoom, int s
/*
* Turn on source-alpha support
*/
SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);
TFB_SetSurfaceAlphaMod (rz_dst, 255);
} else {
/*
* Copy palette and colorkey info
*/
Uint32 srckey = 0;
TFB_GetColorKey (rz_src, &srckey);
for (i = 0; i < rz_src->format->palette->ncolors; i++) {
rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
}
@@ -786,7 +793,7 @@ SDL_Surface *rotozoomSurface(SDL_Surface * src, double angle, double zoom, int s
* Call the 8bit transformation routine to do the zooming
*/
zoomSurfaceY(rz_src, rz_dst);
SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, rz_src->format->colorkey);
TFB_SetColorKey(rz_dst, srckey, 1);
}
/*
* Unlock source surface
@@ -990,11 +997,13 @@ SDL_Surface *zoomSurface(SDL_Surface * src, double zoomx, double zoomy, int smoo
/*
* Turn on source-alpha support
*/
SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);
TFB_SetSurfaceAlphaMod (rz_dst, 255);
} else {
/*
* Copy palette and colorkey info
*/
Uint32 srckey = 0;
TFB_GetColorKey (rz_src, &srckey);
for (i = 0; i < rz_src->format->palette->ncolors; i++) {
rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
}
@@ -1003,7 +1012,7 @@ SDL_Surface *zoomSurface(SDL_Surface * src, double zoomx, double zoomy, int smoo
* Call the 8bit transformation routine to do the zooming
*/
zoomSurfaceY(rz_src, rz_dst);
SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY, rz_src->format->colorkey);
TFB_SetColorKey(rz_dst, srckey, 0);
}
/*
* Unlock source surface
+9 -2
View File
@@ -184,13 +184,13 @@ TFB_GetColorKey (SDL_Surface *surface, Uint32 *key)
}
int
TFB_SetColorKey (SDL_Surface *surface, Uint32 key)
TFB_SetColorKey (SDL_Surface *surface, Uint32 key, int rleaccel)
{
if (!surface)
{
return -1;
}
return SDL_SetColorKey (surface, SDL_SRCCOLORKEY, key);
return SDL_SetColorKey (surface, SDL_SRCCOLORKEY | (rleaccel ? SDL_RLEACCEL : 0), key);
}
int
@@ -202,4 +202,11 @@ TFB_DisableColorKey (SDL_Surface *surface)
}
return SDL_SetColorKey (surface, 0, 0);
}
int
TFB_SetColors (SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors)
{
return SDL_SetColors (surface, colors, firstcolor, ncolors);
}
#endif
+17 -1
View File
@@ -168,12 +168,13 @@ TFB_GetColorKey (SDL_Surface *surface, Uint32 *key)
}
int
TFB_SetColorKey (SDL_Surface *surface, Uint32 key)
TFB_SetColorKey (SDL_Surface *surface, Uint32 key, int rleaccel)
{
if (!surface)
{
return -1;
}
SDL_SetSurfaceRLE (surface, rleaccel);
return SDL_SetColorKey (surface, SDL_TRUE, key);
}
@@ -187,4 +188,19 @@ TFB_DisableColorKey (SDL_Surface *surface)
return SDL_SetColorKey (surface, SDL_FALSE, 0);
}
int
TFB_SetColors (SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors)
{
if (!surface || !colors || !surface->format || !surface->format->palette)
{
return 0;
}
if (SDL_SetPaletteColors (surface->format->palette, colors, firstcolor, ncolors) == 0)
{
// SDL2's success code is opposite from SDL1's SDL_SetColors
return 1;
}
return 0;
}
#endif
+2 -1
View File
@@ -51,7 +51,8 @@ int TFB_SetSurfaceAlphaMod (SDL_Surface *surface, Uint8 alpha);
int TFB_DisableSurfaceAlphaMod (SDL_Surface *surface);
int TFB_HasColorKey (SDL_Surface *surface);
int TFB_GetColorKey (SDL_Surface *surface, Uint32 *key);
int TFB_SetColorKey (SDL_Surface *surface, Uint32 key);
int TFB_SetColorKey (SDL_Surface *surface, Uint32 key, int rleaccel);
int TFB_DisableColorKey (SDL_Surface *surface);
int TFB_SetColors (SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors);
#endif