src/video/SDL_video.c
changeset 2266 e61ad15a205f
parent 2257 340942cfda48
child 2267 c785543d1843
     1.1 --- a/src/video/SDL_video.c	Fri Aug 17 06:58:20 2007 +0000
     1.2 +++ b/src/video/SDL_video.c	Sat Aug 18 01:44:21 2007 +0000
     1.3 @@ -1535,8 +1535,7 @@
     1.4  SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
     1.5  {
     1.6      SDL_TextureID textureID;
     1.7 -    Uint32 surface_flags = surface->flags;
     1.8 -    SDL_PixelFormat *fmt = surface->format;
     1.9 +    SDL_PixelFormat *fmt;
    1.10      int bpp;
    1.11      Uint32 Rmask, Gmask, Bmask, Amask;
    1.12  
    1.13 @@ -1544,6 +1543,7 @@
    1.14          SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
    1.15          return 0;
    1.16      }
    1.17 +    fmt = surface->format;
    1.18  
    1.19      if (format) {
    1.20          if (!SDL_PixelFormatEnumToMasks
    1.21 @@ -1552,7 +1552,7 @@
    1.22              return 0;
    1.23          }
    1.24      } else {
    1.25 -        if (fmt->Amask || !(surface_flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA))) {
    1.26 +        if (surface->format->Amask || !(flags & (SDL_COPY_COLORKEY|SDL_COPY_MASK|SDL_COPY_BLEND))) {
    1.27              bpp = fmt->BitsPerPixel;
    1.28              Rmask = fmt->Rmask;
    1.29              Gmask = fmt->Gmask;
    1.30 @@ -1595,92 +1595,38 @@
    1.31                                surface->pitch);
    1.32          }
    1.33      } else {
    1.34 -        Uint32 cmod;
    1.35 -        SDL_Rect bounds;
    1.36 -        SDL_Surface dst;
    1.37 +        SDL_PixelFormat *dst_fmt;
    1.38 +        SDL_Surface *dst = NULL;
    1.39  
    1.40          /* Set up a destination surface for the texture update */
    1.41 -        SDL_zero(dst);
    1.42 -        dst.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
    1.43 -        if (!dst.format) {
    1.44 -            SDL_DestroyTexture(textureID);
    1.45 -            return 0;
    1.46 -        }
    1.47 -        dst.w = surface->w;
    1.48 -        dst.h = surface->h;
    1.49 -        dst.pitch = SDL_CalculatePitch(&dst);
    1.50 -        dst.pixels = SDL_malloc(dst.h * dst.pitch);
    1.51 -        if (!dst.pixels) {
    1.52 -            SDL_DestroyTexture(textureID);
    1.53 -            SDL_FreeFormat(dst.format);
    1.54 -            SDL_OutOfMemory();
    1.55 -            return 0;
    1.56 -        }
    1.57 -
    1.58 -        /* Copy the palette if any */
    1.59 -        if (SDL_ISPIXELFORMAT_INDEXED(format)) {
    1.60 -            if (fmt->palette) {
    1.61 -                SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
    1.62 -                                      fmt->palette->ncolors);
    1.63 -                SDL_SetSurfacePalette(&dst, fmt->palette);
    1.64 -            } else {
    1.65 -                dst.format->palette =
    1.66 -                    SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
    1.67 -                if (!dst.format->palette) {
    1.68 -                    SDL_DestroyTexture(textureID);
    1.69 -                    SDL_FreeFormat(dst.format);
    1.70 -                    return 0;
    1.71 +        dst_fmt = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
    1.72 +        if (dst_fmt) {
    1.73 +            if (SDL_ISPIXELFORMAT_INDEXED(format)) {
    1.74 +                dst_fmt->palette = SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
    1.75 +                if (dst_fmt->palette) {
    1.76 +                    if (fmt->palette) {
    1.77 +fixme
    1.78 +                    } else {
    1.79 +                        SDL_DitherColors(dst_fmt->palette->colors, SDL_BITSPERPIXEL(format));
    1.80 +                    }
    1.81                  }
    1.82 -                SDL_DitherColors(dst.format->palette->colors,
    1.83 -                                 SDL_BITSPERPIXEL(format));
    1.84 +                if (fmt->palette) {
    1.85 +                    dst_fmt->palette = fmt->palette;
    1.86 +                } else {
    1.87 +                }
    1.88              }
    1.89 -        }
    1.90  
    1.91 -        /* Make the texture transparent if the surface has colorkey */
    1.92 -        if (surface_flags & SDL_SRCCOLORKEY) {
    1.93 -            int row;
    1.94 -            int length = dst.w * dst.format->BytesPerPixel;
    1.95 -            Uint8 *p = (Uint8 *) dst.pixels;
    1.96 -            for (row = 0; row < dst.h; ++row) {
    1.97 -                SDL_memset(p, 0, length);
    1.98 -                p += dst.pitch;
    1.99 +            cvt = SDL_ConvertSurface(surface, fmt, 0);
   1.100 +            if (cvt) {
   1.101 +                SDL_UpdateTexture(textureID, NULL, cvt->pixels, cvt->pitch);
   1.102 +                SDL_FreeSurface(cvt);
   1.103              }
   1.104 -        }
   1.105 -
   1.106 -        /* Copy over the alpha channel */
   1.107 -        cmod = surface->map->cmod;
   1.108 -        if (surface_flags & SDL_SRCALPHA) {
   1.109 -            if (fmt->Amask) {
   1.110 -                surface->flags &= ~SDL_SRCALPHA;
   1.111 -            } else {
   1.112 -                /* FIXME: Need to make sure the texture has an alpha channel
   1.113 -                 *        and copy 'alpha' into the texture alpha channel.
   1.114 -                 */
   1.115 -                SDL_SetAlpha(surface, 0, 0);
   1.116 -            }
   1.117 +            SDL_FreeFormat(fmt);
   1.118          }
   1.119 -
   1.120 -        /* Copy over the image data */
   1.121 -        bounds.x = 0;
   1.122 -        bounds.y = 0;
   1.123 -        bounds.w = surface->w;
   1.124 -        bounds.h = surface->h;
   1.125 -        SDL_LowerBlit(surface, &bounds, &dst, &bounds);
   1.126 +    }
   1.127  
   1.128 -        /* Clean up the original surface */
   1.129 -        if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   1.130 -            Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
   1.131 -            if (fmt->Amask) {
   1.132 -                surface->flags |= SDL_SRCALPHA;
   1.133 -            } else {
   1.134 -                SDL_SetAlpha(surface, aflags, (cmod >> 24));
   1.135 -            }
   1.136 -        }
   1.137 -
   1.138 -        /* Update the texture */
   1.139 -        SDL_UpdateTexture(textureID, NULL, dst.pixels, dst.pitch);
   1.140 -        SDL_free(dst.pixels);
   1.141 -        SDL_FreeFormat(dst.format);
   1.142 +    if (SDL_ISPIXELFORMAT_INDEXED(format) && fmt->palette) {
   1.143 +        SDL_SetTexturePalette(textureID, fmt->palette->colors, 0, fmt->palette->ncolors);
   1.144      }
   1.145  
   1.146      return textureID;