src/video/SDL_surface.c
changeset 7023 1d49dc7b5ce9
parent 6920 a966229b4107
child 7024 72cb3e205571
     1.1 --- a/src/video/SDL_surface.c	Sat Mar 23 13:03:36 2013 -0700
     1.2 +++ b/src/video/SDL_surface.c	Sun Mar 24 09:51:01 2013 -0700
     1.3 @@ -187,7 +187,13 @@
     1.4      if (flag) {
     1.5          surface->map->info.flags |= SDL_COPY_COLORKEY;
     1.6          surface->map->info.colorkey = key;
     1.7 +        if (surface->format->palette) {
     1.8 +            surface->format->palette->colors[surface->map->info.colorkey].unused = SDL_ALPHA_TRANSPARENT;
     1.9 +        }
    1.10      } else {
    1.11 +        if (surface->format->palette) {
    1.12 +            surface->format->palette->colors[surface->map->info.colorkey].unused = SDL_ALPHA_OPAQUE;
    1.13 +        }
    1.14          surface->map->info.flags &= ~SDL_COPY_COLORKEY;
    1.15      }
    1.16      if (surface->map->info.flags != flags) {
    1.17 @@ -843,14 +849,35 @@
    1.18             SDL_COPY_RLE_ALPHAKEY));
    1.19      surface->map->info.flags = copy_flags;
    1.20      if (copy_flags & SDL_COPY_COLORKEY) {
    1.21 -        Uint8 keyR, keyG, keyB, keyA;
    1.22 +        SDL_bool set_colorkey_by_color = SDL_FALSE;
    1.23  
    1.24 -        SDL_GetRGBA(surface->map->info.colorkey, surface->format, &keyR,
    1.25 -                    &keyG, &keyB, &keyA);
    1.26 -        SDL_SetColorKey(convert, 1,
    1.27 -                        SDL_MapRGBA(convert->format, keyR, keyG, keyB, keyA));
    1.28 -        /* This is needed when converting for 3D texture upload */
    1.29 -        SDL_ConvertColorkeyToAlpha(convert);
    1.30 +        if (surface->format->palette) {
    1.31 +            if (format->palette && 
    1.32 +                surface->format->palette->ncolors <= format->palette->ncolors &&
    1.33 +                (SDL_memcmp(surface->format->palette->colors, format->palette->colors,
    1.34 +                  surface->format->palette->ncolors * sizeof(SDL_Color)) == 0)) {
    1.35 +                /* The palette is identical, just set the same colorkey */
    1.36 +                SDL_SetColorKey(convert, 1, surface->map->info.colorkey);
    1.37 +            } else if (format->Amask) {
    1.38 +                /* The alpha was set in the destination from the palette */
    1.39 +            } else {
    1.40 +                set_colorkey_by_color = SDL_TRUE;
    1.41 +            }
    1.42 +        } else {
    1.43 +            set_colorkey_by_color = SDL_TRUE;
    1.44 +        }
    1.45 +
    1.46 +        if (set_colorkey_by_color) {
    1.47 +            /* Set the colorkey by color, which needs to be unique */
    1.48 +            Uint8 keyR, keyG, keyB, keyA;
    1.49 +
    1.50 +            SDL_GetRGBA(surface->map->info.colorkey, surface->format, &keyR,
    1.51 +                        &keyG, &keyB, &keyA);
    1.52 +            SDL_SetColorKey(convert, 1,
    1.53 +                            SDL_MapRGBA(convert->format, keyR, keyG, keyB, keyA));
    1.54 +            /* This is needed when converting for 3D texture upload */
    1.55 +            SDL_ConvertColorkeyToAlpha(convert);
    1.56 +        }
    1.57      }
    1.58      SDL_SetClipRect(convert, &surface->clip_rect);
    1.59