src/video/SDL_surface.c
changeset 5288 d4381f3b0d1e
parent 5262 b530ef003506
child 5296 48067bfc300c
     1.1 --- a/src/video/SDL_surface.c	Sun Feb 13 01:31:07 2011 -0800
     1.2 +++ b/src/video/SDL_surface.c	Sun Feb 13 13:46:10 2011 -0800
     1.3 @@ -39,10 +39,18 @@
     1.4                       Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
     1.5  {
     1.6      SDL_Surface *surface;
     1.7 +    Uint32 format;
     1.8  
     1.9      /* The flags are no longer used, make the compiler happy */
    1.10      (void)flags;
    1.11  
    1.12 +    /* Get the pixel format */
    1.13 +    format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask);
    1.14 +    if (format == SDL_PIXELFORMAT_UNKNOWN) {
    1.15 +        SDL_SetError("Unknown pixel format");
    1.16 +        return NULL;
    1.17 +    }
    1.18 +
    1.19      /* Allocate the surface */
    1.20      surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
    1.21      if (surface == NULL) {
    1.22 @@ -50,7 +58,7 @@
    1.23          return NULL;
    1.24      }
    1.25  
    1.26 -    surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask);
    1.27 +    surface->format = SDL_AllocFormat(format);
    1.28      if (!surface->format) {
    1.29          SDL_FreeSurface(surface);
    1.30          return NULL;
    1.31 @@ -60,7 +68,7 @@
    1.32      surface->pitch = SDL_CalculatePitch(surface);
    1.33      SDL_SetClipRect(surface, NULL);
    1.34  
    1.35 -    if (surface->format->BitsPerPixel <= 8) {
    1.36 +    if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
    1.37          SDL_Palette *palette =
    1.38              SDL_AllocPalette((1 << surface->format->BitsPerPixel));
    1.39          if (!palette) {
    1.40 @@ -135,7 +143,6 @@
    1.41          SDL_FreeSurface(surface);
    1.42          return NULL;
    1.43      }
    1.44 -    SDL_FormatChanged(surface);
    1.45  
    1.46      /* By default surface with an alpha mask are set up for blending */
    1.47      if (Amask) {
    1.48 @@ -171,46 +178,14 @@
    1.49      return surface;
    1.50  }
    1.51  
    1.52 -static int
    1.53 -SDL_SurfacePaletteChanged(void *userdata, SDL_Palette * palette)
    1.54 -{
    1.55 -    SDL_Surface *surface = (SDL_Surface *) userdata;
    1.56 -
    1.57 -    SDL_FormatChanged(surface);
    1.58 -
    1.59 -    return 0;
    1.60 -}
    1.61 -
    1.62  int
    1.63  SDL_SetSurfacePalette(SDL_Surface * surface, SDL_Palette * palette)
    1.64  {
    1.65 -    if (!surface || !surface->format) {
    1.66 +    if (!surface) {
    1.67          SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
    1.68          return -1;
    1.69      }
    1.70 -
    1.71 -    if (palette && palette->ncolors != (1 << surface->format->BitsPerPixel)) {
    1.72 -        SDL_SetError
    1.73 -            ("SDL_SetSurfacePalette() passed a palette that doesn't match the surface format");
    1.74 -        return -1;
    1.75 -    }
    1.76 -
    1.77 -    if (surface->format->palette == palette) {
    1.78 -        return 0;
    1.79 -    }
    1.80 -
    1.81 -    if (surface->format->palette) {
    1.82 -        SDL_DelPaletteWatch(surface->format->palette,
    1.83 -                            SDL_SurfacePaletteChanged, surface);
    1.84 -    }
    1.85 -
    1.86 -    surface->format->palette = palette;
    1.87 -
    1.88 -    if (surface->format->palette) {
    1.89 -        SDL_AddPaletteWatch(surface->format->palette,
    1.90 -                            SDL_SurfacePaletteChanged, surface);
    1.91 -    }
    1.92 -    return 0;
    1.93 +    return SDL_SetPixelFormatPalette(surface->format, palette);
    1.94  }
    1.95  
    1.96  int
    1.97 @@ -556,7 +531,8 @@
    1.98  {
    1.99      /* Check to make sure the blit mapping is valid */
   1.100      if ((src->map->dst != dst) ||
   1.101 -        (src->map->dst->format_version != src->map->format_version)) {
   1.102 +        (dst->format->palette &&
   1.103 +         src->map->palette_version != dst->format->palette->version)) {
   1.104          if (SDL_MapSurface(src, dst) < 0) {
   1.105              return (-1);
   1.106          }
   1.107 @@ -801,21 +777,17 @@
   1.108                           void * pixels, int pitch, SDL_Surface * surface, 
   1.109                           SDL_PixelFormat * format, SDL_BlitMap * blitmap)
   1.110  {
   1.111 -    int bpp;
   1.112 -    Uint32 Rmask, Gmask, Bmask, Amask;
   1.113 -
   1.114 -    if (!SDL_PixelFormatEnumToMasks(pixel_format,
   1.115 -                                    &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
   1.116 +    if (SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
   1.117 +        SDL_SetError("Indexed pixel formats not supported");
   1.118          return SDL_FALSE;
   1.119      }
   1.120 -    if (bpp <= 8) {
   1.121 -        SDL_SetError("Indexed pixel formats not supported");
   1.122 +    if (SDL_InitFormat(format, pixel_format) < 0) {
   1.123          return SDL_FALSE;
   1.124      }
   1.125  
   1.126      SDL_zerop(surface);
   1.127      surface->flags = SDL_PREALLOC;
   1.128 -    surface->format = SDL_InitFormat(format, bpp, Rmask, Gmask, Bmask, Amask);
   1.129 +    surface->format = format;
   1.130      surface->pixels = pixels;
   1.131      surface->w = width;
   1.132      surface->h = height;
   1.133 @@ -830,7 +802,6 @@
   1.134      blitmap->info.b = 0xFF;
   1.135      blitmap->info.a = 0xFF;
   1.136      surface->map = blitmap;
   1.137 -    SDL_FormatChanged(surface);
   1.138  
   1.139      /* The surface is ready to go */
   1.140      surface->refcount = 1;
   1.141 @@ -905,6 +876,9 @@
   1.142      if (surface == NULL) {
   1.143          return;
   1.144      }
   1.145 +    if (surface->flags & SDL_DONTFREE) {
   1.146 +        return;
   1.147 +    }
   1.148      if (--surface->refcount > 0) {
   1.149          return;
   1.150      }