src/video/SDL_video.c
changeset 3166 3a63a5824557
parent 3162 dc1eb82ffdaa
child 3169 f294338ca6eb
     1.1 --- a/src/video/SDL_video.c	Fri Jun 05 07:30:51 2009 +0000
     1.2 +++ b/src/video/SDL_video.c	Fri Jun 05 07:35:06 2009 +0000
     1.3 @@ -48,7 +48,7 @@
     1.4  #endif /* SDL_VIDEO_OPENGL */
     1.5  
     1.6  /* Available video drivers */
     1.7 -static const VideoBootStrap *const bootstrap[] = {
     1.8 +static VideoBootStrap *bootstrap[] = {
     1.9  #if SDL_VIDEO_DRIVER_COCOA
    1.10      &COCOA_bootstrap,
    1.11  #endif
    1.12 @@ -1598,6 +1598,7 @@
    1.13      SDL_TextureID textureID;
    1.14      Uint32 requested_format = format;
    1.15      SDL_PixelFormat *fmt;
    1.16 +    SDL_Renderer *renderer;
    1.17      int bpp;
    1.18      Uint32 Rmask, Gmask, Bmask, Amask;
    1.19  
    1.20 @@ -1607,6 +1608,11 @@
    1.21      }
    1.22      fmt = surface->format;
    1.23  
    1.24 +    renderer = SDL_CurrentDisplay.current_renderer;
    1.25 +    if (!renderer) {
    1.26 +        return 0;
    1.27 +    }
    1.28 +
    1.29      if (format) {
    1.30          if (!SDL_PixelFormatEnumToMasks
    1.31              (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
    1.32 @@ -1625,12 +1631,89 @@
    1.33              Amask = fmt->Amask;
    1.34          } else {
    1.35              /* Need a format with alpha */
    1.36 +            int it;
    1.37 +            int apfmt;
    1.38 +
    1.39 +            /* Pixel formats with alpha, sorted by best first */
    1.40 +            static const Uint32 sdl_alpha_pformats[]={
    1.41 +               SDL_PIXELFORMAT_ARGB8888,
    1.42 +               SDL_PIXELFORMAT_RGBA8888,
    1.43 +               SDL_PIXELFORMAT_ABGR8888,
    1.44 +               SDL_PIXELFORMAT_BGRA8888,
    1.45 +               SDL_PIXELFORMAT_ARGB1555,
    1.46 +               SDL_PIXELFORMAT_ABGR1555,
    1.47 +               SDL_PIXELFORMAT_ARGB4444,
    1.48 +               SDL_PIXELFORMAT_ABGR4444,
    1.49 +               SDL_PIXELFORMAT_ARGB2101010,
    1.50 +               SDL_PIXELFORMAT_UNKNOWN};
    1.51 +
    1.52              bpp = 32;
    1.53              Rmask = 0x00FF0000;
    1.54              Gmask = 0x0000FF00;
    1.55              Bmask = 0x000000FF;
    1.56              Amask = 0xFF000000;
    1.57 +
    1.58 +            format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
    1.59 +            if (!format) {
    1.60 +                SDL_SetError("Unknown pixel format");
    1.61 +                return 0;
    1.62 +            }
    1.63 +
    1.64 +            /* Search this format in the supported texture formats */
    1.65 +            /* by current renderer                                 */
    1.66 +            for (it=0; it<renderer->info.num_texture_formats; it++)
    1.67 +            {
    1.68 +                if (renderer->info.texture_formats[it]==format)
    1.69 +                {
    1.70 +                   break;
    1.71 +                }
    1.72 +            }
    1.73 +
    1.74 +            /* If this format can't be found, search any best       */
    1.75 +            /* compatible format with alpha which renderer provides */
    1.76 +            if (it==renderer->info.num_texture_formats)
    1.77 +            {
    1.78 +                apfmt=0;
    1.79 +                for (;;)
    1.80 +                {
    1.81 +                    if (sdl_alpha_pformats[apfmt]==SDL_PIXELFORMAT_UNKNOWN)
    1.82 +                    {
    1.83 +                        break;
    1.84 +                    }
    1.85 +
    1.86 +                    for (it=0; it<renderer->info.num_texture_formats; it++)
    1.87 +                    {
    1.88 +                       if (renderer->info.texture_formats[it]==sdl_alpha_pformats[apfmt])
    1.89 +                       {
    1.90 +                          break;
    1.91 +                       }
    1.92 +                    }
    1.93 +
    1.94 +                    if (it!=renderer->info.num_texture_formats)
    1.95 +                    {
    1.96 +                       /* Compatible format has been found */
    1.97 +                       break;
    1.98 +                    }
    1.99 +                    apfmt++;
   1.100 +                }
   1.101 +
   1.102 +                /* If compatible format can't be found, then return an error */
   1.103 +                if (it==renderer->info.num_texture_formats)
   1.104 +                {
   1.105 +                    SDL_SetError("Compatible pixel format can't be found");
   1.106 +                    return 0;
   1.107 +                }
   1.108 +
   1.109 +                /* Convert found pixel format back to color masks */
   1.110 +                if (SDL_PixelFormatEnumToMasks(renderer->info.texture_formats[it],
   1.111 +                       &bpp, &Rmask, &Gmask, &Bmask, &Amask)!=SDL_TRUE)
   1.112 +                {
   1.113 +                    SDL_SetError("Unknown pixel format");
   1.114 +                    return 0;
   1.115 +                }
   1.116 +            }
   1.117          }
   1.118 +
   1.119          format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
   1.120          if (!format) {
   1.121              SDL_SetError("Unknown pixel format");