Skip to content

Commit 7555833

Browse files
committed
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
1 parent 12574f2 commit 7555833

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

docs.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ <H2>
1616
Major changes since SDL 1.0.0:
1717
</H2>
1818
<UL>
19+
<LI> 1.2.5: Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
1920
<LI> 1.2.5: Fixed setting OpenGL mode multiple times on Windows
2021
<LI> 1.2.5: Added support for Qtopia on embedded systems (thanks David!)
2122
<LI> 1.2.4: Added initial support for Atari (thanks Patrice!)

include/SDL_video.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ extern DECLSPEC SDL_Surface * SDLCALL SDL_ConvertSurface
645645
* alpha-blend (using the source per-surface alpha value);
646646
* set destination alpha to opaque.
647647
* SDL_SRCALPHA not set:
648-
* copy RGB, set destination alpha to opaque.
648+
* copy RGB, set destination alpha to source per-surface alpha value.
649649
* both:
650650
* if SDL_SRCCOLORKEY set, only copy the pixels matching the
651651
* source colour key.

src/video/SDL_blit_N.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,7 @@ static void BlitNtoN(SDL_BlitInfo *info)
12021202
int srcbpp = srcfmt->BytesPerPixel;
12031203
SDL_PixelFormat *dstfmt = info->dst;
12041204
int dstbpp = dstfmt->BytesPerPixel;
1205-
unsigned alpha = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
1205+
unsigned alpha = dstfmt->Amask ? srcfmt->alpha : 0;
12061206

12071207
while ( height-- ) {
12081208
DUFFS_LOOP(
@@ -1358,7 +1358,7 @@ static void BlitNtoNKey(SDL_BlitInfo *info)
13581358
SDL_PixelFormat *dstfmt = info->dst;
13591359
int srcbpp = srcfmt->BytesPerPixel;
13601360
int dstbpp = dstfmt->BytesPerPixel;
1361-
unsigned alpha = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
1361+
unsigned alpha = dstfmt->Amask ? srcfmt->alpha : 0;
13621362

13631363
while ( height-- ) {
13641364
DUFFS_LOOP(

src/video/SDL_surface.c

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ int SDL_SetColorKey (SDL_Surface *surface, Uint32 flag, Uint32 key)
211211
SDL_InvalidateMap(surface->map);
212212
return(0);
213213
}
214+
/* This function sets the alpha channel of a surface */
214215
int SDL_SetAlpha (SDL_Surface *surface, Uint32 flag, Uint8 value)
215216
{
216217
Uint32 oldflags = surface->flags;
@@ -269,6 +270,52 @@ int SDL_SetAlpha (SDL_Surface *surface, Uint32 flag, Uint8 value)
269270
SDL_InvalidateMap(surface->map);
270271
return(0);
271272
}
273+
int SDL_SetAlphaChannel(SDL_Surface *surface, Uint8 value)
274+
{
275+
int row, col;
276+
int offset;
277+
Uint8 *buf;
278+
279+
if ( (surface->format->Amask != 0xFF000000) &&
280+
(surface->format->Amask != 0x000000FF) ) {
281+
SDL_SetError("Unsupported surface alpha mask format");
282+
return -1;
283+
}
284+
285+
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
286+
if ( surface->format->Amask == 0xFF000000 ) {
287+
offset = 3;
288+
} else {
289+
offset = 0;
290+
}
291+
#else
292+
if ( surface->format->Amask == 0xFF000000 ) {
293+
offset = 0;
294+
} else {
295+
offset = 3;
296+
}
297+
#endif /* Byte ordering */
298+
299+
/* Quickly set the alpha channel of an RGBA or ARGB surface */
300+
if ( SDL_MUSTLOCK(surface) ) {
301+
if ( SDL_LockSurface(surface) < 0 ) {
302+
return -1;
303+
}
304+
}
305+
row = surface->h;
306+
while (row--) {
307+
col = surface->w;
308+
buf = (Uint8 *)surface->pixels + row * surface->pitch + offset;
309+
while(col--) {
310+
*buf = value;
311+
buf += 4;
312+
}
313+
}
314+
if ( SDL_MUSTLOCK(surface) ) {
315+
SDL_UnlockSurface(surface);
316+
}
317+
return 0;
318+
}
272319

273320
/*
274321
* A function to calculate the intersection of two rectangles:
@@ -748,8 +795,13 @@ SDL_Surface * SDL_ConvertSurface (SDL_Surface *surface,
748795
}
749796
}
750797
if ( (surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
751-
alpha = surface->format->alpha;
752-
SDL_SetAlpha(surface, 0, 0);
798+
/* Copy over the alpha channel to RGBA if requested */
799+
if ( format->Amask ) {
800+
surface->flags &= ~SDL_SRCALPHA;
801+
} else {
802+
alpha = surface->format->alpha;
803+
SDL_SetAlpha(surface, 0, 0);
804+
}
753805
}
754806

755807
/* Copy over the image data */
@@ -780,7 +832,11 @@ SDL_Surface * SDL_ConvertSurface (SDL_Surface *surface,
780832
SDL_SetAlpha(convert, aflags|(flags&SDL_RLEACCELOK),
781833
alpha);
782834
}
783-
SDL_SetAlpha(surface, aflags, alpha);
835+
if ( format->Amask ) {
836+
surface->flags |= SDL_SRCALPHA;
837+
} else {
838+
SDL_SetAlpha(surface, aflags, alpha);
839+
}
784840
}
785841

786842
/* We're ready to go! */

0 commit comments

Comments
 (0)