Skip to content

Commit

Permalink
Fix blits of RGB+alpha->RGBA or RGB+alpha+colorkey->RGB.
Browse files Browse the repository at this point in the history
I believe the problem is inherent in the way SDL2 defines
SDL_BLENDMODE_BLEND to not update the destination's alpha
channel. To work around this, when in one of these problem
cases, we first blit to a new surace (using BLENDMODE_NONE,
which does update alpha), and then use the pygame alpha
blit to update the real destination.

This is slower, but these should be relatively rare cases -
this never happens with SRCALPHA on the source, and doesn't
happen with RGB->RGB, RGB+alpha->RGB, or RGB+colorkey->RGB,
which are the most common RGB blits in real games.

Fixes #24, hopefully.
  • Loading branch information
renpytom committed Jan 24, 2016
1 parent 1dd0307 commit 78eeb25
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/pygame_sdl2/surface.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,45 @@ cdef class Surface:
cdef SDL_Rect area_rect
cdef SDL_Rect *area_ptr = NULL

SDL_SetSurfaceBlendMode(source.surface, SDL_BLENDMODE_BLEND)
cdef Surface temp

cdef int err
cdef Uint32 key
cdef Uint8 alpha
cdef bint colorkey

colorkey = (SDL_GetColorKey(source.surface, &key) == 0)

if not source.surface.format.Amask:
if SDL_GetSurfaceAlphaMod(source.surface, &alpha):
raise error()

if alpha != 255 and (self.surface.format.Amask or colorkey):

if area:
source = source.subsurface(area)
area = None

SDL_SetSurfaceBlendMode(source.surface, SDL_BLENDMODE_NONE)
temp = Surface(source.get_size(), SRCALPHA)

with nogil:
SDL_UpperBlit(source.surface, NULL, temp.surface, NULL)

source = temp
colorkey = False

if colorkey:
SDL_SetSurfaceBlendMode(source.surface, SDL_BLENDMODE_NONE)
else:
SDL_SetSurfaceBlendMode(source.surface, SDL_BLENDMODE_BLEND)

to_sdl_rect(dest, &dest_rect, "dest")

if area is not None:
to_sdl_rect(area, &area_rect, "area")
area_ptr = &area_rect

cdef int err

with nogil:

if source.surface.format.Amask or special_flags:
Expand Down

0 comments on commit 78eeb25

Please sign in to comment.