--- src/video/SDL_surface.c.orig 2017-09-12 22:40:34.696199701 +0200 +++ src/video/SDL_surface.c 2017-09-13 23:18:16.939879983 +0200 @@ -988,31 +988,43 @@ } if (set_colorkey_by_color) { - SDL_Surface *tmp; - SDL_Surface *tmp2; - int converted_colorkey = 0; + if (surface->format->BitsPerPixel == 16) { + /* 16-bit "565 format" surfaces need special handling */ - /* Create a dummy surface to get the colorkey converted */ - tmp = SDL_CreateRGBSurface(0, 1, 1, - surface->format->BitsPerPixel, surface->format->Rmask, - surface->format->Gmask, surface->format->Bmask, - surface->format->Amask); - - SDL_FillRect(tmp, NULL, surface->map->info.colorkey); - - tmp->map->info.flags &= ~SDL_COPY_COLORKEY; - - /* Convertion of the colorkey */ - tmp2 = SDL_ConvertSurface(tmp, format, 0); - - /* Get the converted colorkey */ - SDL_memcpy(&converted_colorkey, tmp2->pixels, tmp2->format->BytesPerPixel); - - SDL_FreeSurface(tmp); - SDL_FreeSurface(tmp2); - - /* Set the converted colorkey on the new surface */ - SDL_SetColorKey(convert, 1, converted_colorkey); + SDL_Surface *tmp; + SDL_Surface *tmp2; + int converted_colorkey = 0; + + /* Create a dummy surface to get the colorkey converted */ + tmp = SDL_CreateRGBSurface(0, 1, 1, + surface->format->BitsPerPixel, surface->format->Rmask, + surface->format->Gmask, surface->format->Bmask, + surface->format->Amask); + + SDL_FillRect(tmp, NULL, surface->map->info.colorkey); + + tmp->map->info.flags &= ~SDL_COPY_COLORKEY; + + /* Convertion of the colorkey */ + tmp2 = SDL_ConvertSurface(tmp, format, 0); + + /* Get the converted colorkey */ + SDL_memcpy(&converted_colorkey, tmp2->pixels, tmp2->format->BytesPerPixel); + + SDL_FreeSurface(tmp); + SDL_FreeSurface(tmp2); + + /* Set the converted colorkey on the new surface */ + SDL_SetColorKey(convert, 1, converted_colorkey); + } else { + /* Set the colorkey by color, which needs to be unique */ + Uint8 keyR, keyG, keyB, keyA; + + SDL_GetRGBA(surface->map->info.colorkey, surface->format, &keyR, + &keyG, &keyB, &keyA); + SDL_SetColorKey(convert, 1, + SDL_MapRGBA(convert->format, keyR, keyG, keyB, keyA)); + } /* This is needed when converting for 3D texture upload */ SDL_ConvertColorkeyToAlpha(convert);