Finished palettized display handling. SDL-1.3
authorSam Lantinga <slouken@libsdl.org>
Sat, 17 Jun 2006 06:45:14 +0000
branchSDL-1.3
changeset 1683396a35389351
parent 1682 7ae8018b2e5d
child 1684 c4aa1a2f48f1
Finished palettized display handling.
Added support for surface palette sharing.
include/SDL_compat.h
include/SDL_pixels.h
include/SDL_video.h
src/SDL_compat.c
src/video/SDL_RLEaccel.c
src/video/SDL_bmp.c
src/video/SDL_pixels.c
src/video/SDL_pixels_c.h
src/video/SDL_renderer_sw.c
src/video/SDL_surface.c
src/video/SDL_sysvideo.h
src/video/SDL_video.c
src/video/dummy/SDL_nullrender.c
     1.1 --- a/include/SDL_compat.h	Fri Jun 16 06:00:31 2006 +0000
     1.2 +++ b/include/SDL_compat.h	Sat Jun 17 06:45:14 2006 +0000
     1.3 @@ -148,9 +148,9 @@
     1.4  extern DECLSPEC int SDLCALL SDL_SetPalette(SDL_Surface * surface, int flags,
     1.5                                             const SDL_Color * colors,
     1.6                                             int firstcolor, int ncolors);
     1.7 -extern DECLSPEC int SDLCALL SDL_SetScreenColors(SDL_Surface * screen,
     1.8 -                                                const SDL_Color * colors,
     1.9 -                                                int firstcolor, int ncolors);
    1.10 +extern DECLSPEC int SDLCALL SDL_SetColors(SDL_Surface * surface,
    1.11 +                                          const SDL_Color * colors,
    1.12 +                                          int firstcolor, int ncolors);
    1.13  extern DECLSPEC int SDLCALL SDL_GetWMInfo(SDL_SysWMinfo * info);
    1.14  extern DECLSPEC Uint8 SDLCALL SDL_GetAppState(void);
    1.15  extern DECLSPEC void SDLCALL SDL_WarpMouse(Uint16 x, Uint16 y);
     2.1 --- a/include/SDL_pixels.h	Fri Jun 16 06:00:31 2006 +0000
     2.2 +++ b/include/SDL_pixels.h	Sat Jun 17 06:45:14 2006 +0000
     2.3 @@ -195,11 +195,24 @@
     2.4  } SDL_Color;
     2.5  #define SDL_Colour SDL_Color
     2.6  
     2.7 -typedef struct SDL_Palette
     2.8 +typedef struct SDL_Palette SDL_Palette;
     2.9 +typedef int (*SDL_PaletteChangedFunc) (void *userdata, SDL_Palette * palette);
    2.10 +
    2.11 +typedef struct SDL_PaletteWatch
    2.12 +{
    2.13 +    SDL_PaletteChangedFunc callback;
    2.14 +    void *userdata;
    2.15 +    struct SDL_PaletteWatch *next;
    2.16 +} SDL_PaletteWatch;
    2.17 +
    2.18 +struct SDL_Palette
    2.19  {
    2.20      int ncolors;
    2.21      SDL_Color *colors;
    2.22 -} SDL_Palette;
    2.23 +
    2.24 +    int refcount;
    2.25 +    SDL_PaletteWatch *watch;
    2.26 +};
    2.27  
    2.28  /* Everything in the pixel format structure is read-only */
    2.29  typedef struct SDL_PixelFormat
    2.30 @@ -226,23 +239,96 @@
    2.31      Uint8 alpha;
    2.32  } SDL_PixelFormat;
    2.33  
    2.34 -/*
    2.35 - * Convert one of the enumerated formats above to a bpp and RGBA masks.
    2.36 - * Returns SDL_TRUE, or SDL_FALSE if the conversion wasn't possible.
    2.37 +/**
    2.38 + * \fn SDL_bool SDL_PixelFormatEnumToMasks(Uint32 format, int *bpp, Uint32 * Rmask, Uint32 * Gmask, Uint32 * Bmask, Uint32 * Amask)
    2.39 + *
    2.40 + * \brief Convert one of the enumerated pixel formats to a bpp and RGBA masks.
    2.41 + *
    2.42 + * \return SDL_TRUE, or SDL_FALSE if the conversion wasn't possible.
    2.43 + *
    2.44 + * \sa SDL_MasksToPixelFormatEnum()
    2.45 + */
    2.46 +extern DECLSPEC SDL_bool SDLCALL SDL_PixelFormatEnumToMasks(Uint32 format,
    2.47 +                                                            int *bpp,
    2.48 +                                                            Uint32 * Rmask,
    2.49 +                                                            Uint32 * Gmask,
    2.50 +                                                            Uint32 * Bmask,
    2.51 +                                                            Uint32 * Amask);
    2.52 +
    2.53 +/**
    2.54 + * \fn Uint32 SDL_MasksToPixelFormatEnum(int bpp, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
    2.55 + *
    2.56 + * \brief Convert a bpp and RGBA masks to an enumerated pixel format.
    2.57 + *
    2.58 + * \return The pixel format, or SDL_PixelFormat_Unknown if the conversion wasn't possible.
    2.59 + *
    2.60 + * \sa SDL_PixelFormatEnumToMasks()
    2.61 + */
    2.62 +extern DECLSPEC Uint32 SDLCALL SDL_MasksToPixelFormatEnum(int bpp,
    2.63 +                                                          Uint32 Rmask,
    2.64 +                                                          Uint32 Gmask,
    2.65 +                                                          Uint32 Bmask,
    2.66 +                                                          Uint32 Amask);
    2.67 +
    2.68 +/**
    2.69 + * \fn SDL_Palette *SDL_AllocPalette(int ncolors)
    2.70 + *
    2.71 + * \brief Create a palette structure with the specified number of color entries.
    2.72 + *
    2.73 + * \return A new palette, or NULL if there wasn't enough memory
    2.74 + *
    2.75 + * \note The palette entries are initialized to white.
    2.76 + *
    2.77 + * \sa SDL_FreePalette()
    2.78   */
    2.79 -extern DECLSPEC SDL_bool SDL_PixelFormatEnumToMasks(Uint32 format, int *bpp,
    2.80 -                                                    Uint32 * Rmask,
    2.81 -                                                    Uint32 * Gmask,
    2.82 -                                                    Uint32 * Bmask,
    2.83 -                                                    Uint32 * Amask);
    2.84 +extern DECLSPEC SDL_Palette *SDLCALL SDL_AllocPalette(int ncolors);
    2.85 +
    2.86 +/**
    2.87 + * \fn int SDL_AddPaletteWatch(SDL_Palette *palette, SDL_PaletteChangedFunc callback, void *userdata)
    2.88 + *
    2.89 + * \brief Add a callback function which is called when the palette changes.
    2.90 + *
    2.91 + * \sa SDL_DelPaletteWatch()
    2.92 + */
    2.93 +extern DECLSPEC int SDLCALL SDL_AddPaletteWatch(SDL_Palette * palette,
    2.94 +                                                SDL_PaletteChangedFunc
    2.95 +                                                callback, void *userdata);
    2.96 +
    2.97 +/**
    2.98 + * \fn void SDL_DelPaletteWatch(SDL_Palette *palette, SDL_PaletteChangedFunc callback, void *userdata)
    2.99 + *
   2.100 + * \brief Remove a callback function previously added with SDL_AddPaletteWatch()
   2.101 + *
   2.102 + * \sa SDL_AddPaletteWatch()
   2.103 + */
   2.104 +extern DECLSPEC void SDLCALL SDL_DelPaletteWatch(SDL_Palette * palette,
   2.105 +                                                 SDL_PaletteChangedFunc
   2.106 +                                                 callback, void *userdata);
   2.107  
   2.108 -/*
   2.109 - * Convert a bpp and RGBA masks to one of the enumerated formats above.
   2.110 - * Returns SDL_PixelFormat_Unknown if the conversion wasn't possible.
   2.111 +/**
   2.112 + * \fn int SDL_SetPaletteColors(SDL_Palette *palette, const SDL_Colors *colors, int firstcolor, int numcolors)
   2.113 + *
   2.114 + * \brief Set a range of colors in a palette.
   2.115 + *
   2.116 + * \param palette The palette to modify
   2.117 + * \param colors An array of colors to copy into the palette
   2.118 + * \param firstcolor The index of the first palette entry to modify
   2.119 + * \param ncolors The number of entries to modify
   2.120 + *
   2.121 + * \return 0 on success, or -1 if not all of the colors could be set
   2.122   */
   2.123 -extern DECLSPEC Uint32 SDL_MasksToPixelFormatEnum(int bpp, Uint32 Rmask,
   2.124 -                                                  Uint32 Gmask, Uint32 Bmask,
   2.125 -                                                  Uint32 Amask);
   2.126 +extern DECLSPEC int SDLCALL SDL_SetPaletteColors(SDL_Palette * palette,
   2.127 +                                                 const SDL_Color * colors,
   2.128 +                                                 int firstcolor, int ncolors);
   2.129 +
   2.130 +/**
   2.131 + * \fn void SDL_FreePalette(SDL_Palette *palette)
   2.132 + *
   2.133 + * \brief Free a palette created with SDL_AllocPalette()
   2.134 + *
   2.135 + * \sa SDL_AllocPalette()
   2.136 + */
   2.137 +extern DECLSPEC void SDLCALL SDL_FreePalette(SDL_Palette * palette);
   2.138  
   2.139  /* Ends C function definitions when using C++ */
   2.140  #ifdef __cplusplus
     3.1 --- a/include/SDL_video.h	Fri Jun 16 06:00:31 2006 +0000
     3.2 +++ b/include/SDL_video.h	Sat Jun 17 06:45:14 2006 +0000
     3.3 @@ -252,6 +252,9 @@
     3.4      int pitch;                  /* Read-only */
     3.5      void *pixels;               /* Read-write */
     3.6  
     3.7 +    /* texture associated with the surface, if any */
     3.8 +    SDL_TextureID textureID;
     3.9 +
    3.10      /* information needed for surfaces requiring locks */
    3.11      int locked;
    3.12      void *lock_data;
    3.13 @@ -1067,25 +1070,6 @@
    3.14                                               Uint16 * blue);
    3.15  
    3.16  /*
    3.17 - * Sets a portion of the colormap for the given 8-bit surface.  If 'surface'
    3.18 - * is not a palettized surface, this function does nothing, returning 0.
    3.19 - * If all of the colors were set as passed to SDL_SetColors(), it will
    3.20 - * return 1.  If not all the color entries were set exactly as given,
    3.21 - * it will return 0, and you should look at the surface palette to
    3.22 - * determine the actual color palette.
    3.23 - *
    3.24 - * When 'surface' is the surface associated with the current display, the
    3.25 - * display colormap will be updated with the requested colors.  If 
    3.26 - * SDL_HWPALETTE was set in SDL_SetVideoMode() flags, SDL_SetColors()
    3.27 - * will always return 1, and the palette is guaranteed to be set the way
    3.28 - * you desire, even if the window colormap has to be warped or run under
    3.29 - * emulation.
    3.30 - */
    3.31 -extern DECLSPEC int SDLCALL SDL_SetColors(SDL_Surface * surface,
    3.32 -                                          const SDL_Color * colors,
    3.33 -                                          int firstcolor, int ncolors);
    3.34 -
    3.35 -/*
    3.36   * Maps an RGB triple to an opaque pixel value for a given pixel format
    3.37   */
    3.38  extern DECLSPEC Uint32 SDLCALL SDL_MapRGB
    3.39 @@ -1138,6 +1122,18 @@
    3.40  SDL_CreateRGBSurfaceFromTexture(SDL_TextureID textureID);
    3.41  extern DECLSPEC void SDLCALL SDL_FreeSurface(SDL_Surface * surface);
    3.42  
    3.43 +/**
    3.44 + * \fn int SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
    3.45 + *
    3.46 + * \brief Set the palette used by a surface.
    3.47 + *
    3.48 + * \return 0, or -1 if the surface format doesn't use a palette.
    3.49 + *
    3.50 + * \note A single palette can be shared with many surfaces.
    3.51 + */
    3.52 +extern DECLSPEC int SDLCALL SDL_SetSurfacePalette(SDL_Surface * surface,
    3.53 +                                                  SDL_Palette * palette);
    3.54 +
    3.55  /*
    3.56   * SDL_LockSurface() sets up a surface for directly accessing the pixels.
    3.57   * Between calls to SDL_LockSurface()/SDL_UnlockSurface(), you can write
     4.1 --- a/src/SDL_compat.c	Fri Jun 16 06:00:31 2006 +0000
     4.2 +++ b/src/SDL_compat.c	Sat Jun 17 06:45:14 2006 +0000
     4.3 @@ -204,7 +204,25 @@
     4.4              break;
     4.5          }
     4.6      }
     4.7 -    return orig_eventfilter(event);
     4.8 +    if (orig_eventfilter) {
     4.9 +        return orig_eventfilter(event);
    4.10 +    } else {
    4.11 +        return 1;
    4.12 +    }
    4.13 +}
    4.14 +
    4.15 +static int
    4.16 +SDL_VideoPaletteChanged(void *userdata, SDL_Palette * palette)
    4.17 +{
    4.18 +    if (userdata == SDL_ShadowSurface) {
    4.19 +        /* If the shadow palette changed, make the changes visible */
    4.20 +        if (!SDL_VideoSurface->format->palette) {
    4.21 +            SDL_UpdateRect(SDL_ShadowSurface, 0, 0, 0, 0);
    4.22 +        }
    4.23 +    }
    4.24 +    if (userdata == SDL_VideoSurface) {
    4.25 +        return SDL_SetDisplayPalette(palette->colors, 0, palette->ncolors);
    4.26 +    }
    4.27  }
    4.28  
    4.29  SDL_Surface *
    4.30 @@ -233,7 +251,9 @@
    4.31          SDL_ShadowSurface = NULL;
    4.32      }
    4.33      if (SDL_VideoSurface) {
    4.34 -        SDL_FreeSurface(SDL_ShadowSurface);
    4.35 +        SDL_DelPaletteWatch(SDL_VideoSurface->format->palette,
    4.36 +                            SDL_VideoPaletteChanged, NULL);
    4.37 +        SDL_FreeSurface(SDL_VideoSurface);
    4.38          SDL_VideoSurface = NULL;
    4.39      }
    4.40      SDL_DestroyWindow(SDL_VideoWindow);
    4.41 @@ -380,11 +400,11 @@
    4.42          SDL_VideoSurface->flags |= SDL_HWPALETTE;
    4.43          SDL_DitherColors(SDL_VideoSurface->format->palette->colors,
    4.44                           SDL_VideoSurface->format->BitsPerPixel);
    4.45 -        SDL_SetTexturePalette(SDL_VideoTexture,
    4.46 -                              SDL_VideoSurface->format->palette->colors, 0,
    4.47 -                              SDL_VideoSurface->format->palette->ncolors);
    4.48 -        SDL_SetDisplayPalette(SDL_VideoSurface->format->palette->colors, 0,
    4.49 -                              SDL_VideoSurface->format->palette->ncolors);
    4.50 +        SDL_AddPaletteWatch(SDL_VideoSurface->format->palette,
    4.51 +                            SDL_VideoPaletteChanged, NULL);
    4.52 +        SDL_SetPaletteColors(SDL_VideoSurface->format->palette,
    4.53 +                             SDL_VideoSurface->format->palette->colors, 0,
    4.54 +                             SDL_VideoSurface->format->palette->ncolors);
    4.55      }
    4.56  
    4.57      /* Create a shadow surface if necessary */
    4.58 @@ -415,8 +435,13 @@
    4.59          /* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */
    4.60          if (SDL_ShadowSurface->format->palette) {
    4.61              SDL_ShadowSurface->flags |= SDL_HWPALETTE;
    4.62 -            SDL_DitherColors(SDL_ShadowSurface->format->palette->colors,
    4.63 -                             SDL_ShadowSurface->format->BitsPerPixel);
    4.64 +            if (SDL_VideoSurface->format->palette) {
    4.65 +                SDL_SetSurfacePalette(SDL_ShadowSurface,
    4.66 +                                      SDL_VideoSurface->format->palette);
    4.67 +            } else {
    4.68 +                SDL_DitherColors(SDL_ShadowSurface->format->palette->colors,
    4.69 +                                 SDL_ShadowSurface->format->BitsPerPixel);
    4.70 +            }
    4.71          }
    4.72      }
    4.73      SDL_PublicSurface =
    4.74 @@ -656,55 +681,15 @@
    4.75  }
    4.76  
    4.77  int
    4.78 -SDL_SetScreenColors(SDL_Surface * screen, const SDL_Color * colors,
    4.79 -                    int firstcolor, int ncolors)
    4.80 +SDL_SetColors(SDL_Surface * surface, const SDL_Color * colors, int firstcolor,
    4.81 +              int ncolors)
    4.82  {
    4.83 -    SDL_Palette *pal;
    4.84 -    int gotall;
    4.85 -    int palsize;
    4.86 -
    4.87 -    /* Verify the parameters */
    4.88 -    pal = screen->format->palette;
    4.89 -    if (!pal) {
    4.90 -        return 0;               /* not a palettized surface */
    4.91 -    }
    4.92 -    gotall = 1;
    4.93 -    palsize = 1 << screen->format->BitsPerPixel;
    4.94 -    if (ncolors > (palsize - firstcolor)) {
    4.95 -        ncolors = (palsize - firstcolor);
    4.96 -        gotall = 0;
    4.97 +    if (SDL_SetPaletteColors
    4.98 +        (surface->format->palette, colors, firstcolor, ncolors) == 0) {
    4.99 +        return 1;
   4.100 +    } else {
   4.101 +        return 0;
   4.102      }
   4.103 -
   4.104 -    if (screen == SDL_ShadowSurface) {
   4.105 -        SDL_Palette *vidpal;
   4.106 -
   4.107 -        vidpal = SDL_VideoSurface->format->palette;
   4.108 -        if (vidpal && vidpal->ncolors == pal->ncolors) {
   4.109 -            /* This is a shadow surface, and the physical
   4.110 -             * framebuffer is also indexed. Propagate the
   4.111 -             * changes to its logical palette so that
   4.112 -             * updates are always identity blits
   4.113 -             */
   4.114 -            SDL_memcpy(vidpal->colors + firstcolor, colors,
   4.115 -                       ncolors * sizeof(*colors));
   4.116 -        }
   4.117 -        if (SDL_VideoSurface->flags & SDL_HWPALETTE) {
   4.118 -            /* Set the physical palette */
   4.119 -            screen = SDL_VideoSurface;
   4.120 -        } else {
   4.121 -            SDL_UpdateRect(screen, 0, 0, 0, 0);
   4.122 -        }
   4.123 -    }
   4.124 -
   4.125 -    if (screen == SDL_VideoSurface) {
   4.126 -        SDL_SetTexturePalette(SDL_VideoTexture,
   4.127 -                              SDL_VideoSurface->format->palette->colors, 0,
   4.128 -                              SDL_VideoSurface->format->palette->ncolors);
   4.129 -        SDL_SetDisplayPalette(SDL_VideoSurface->format->palette->colors, 0,
   4.130 -                              SDL_VideoSurface->format->palette->ncolors);
   4.131 -    }
   4.132 -
   4.133 -    return gotall;
   4.134  }
   4.135  
   4.136  int
     5.1 --- a/src/video/SDL_RLEaccel.c	Fri Jun 16 06:00:31 2006 +0000
     5.2 +++ b/src/video/SDL_RLEaccel.c	Sat Jun 17 06:45:14 2006 +0000
     5.3 @@ -1618,8 +1618,7 @@
     5.4  #undef ADD_TRANSL_COUNTS
     5.5  
     5.6      /* Now that we have it encoded, release the original pixels */
     5.7 -    if ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC
     5.8 -        && (surface->flags & SDL_HWSURFACE) != SDL_HWSURFACE) {
     5.9 +    if (!(surface->flags & SDL_PREALLOC) && !(surface->flags & SDL_HWSURFACE)) {
    5.10          SDL_free(surface->pixels);
    5.11          surface->pixels = NULL;
    5.12      }
    5.13 @@ -1784,8 +1783,7 @@
    5.14  #undef ADD_COUNTS
    5.15  
    5.16      /* Now that we have it encoded, release the original pixels */
    5.17 -    if ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC
    5.18 -        && (surface->flags & SDL_HWSURFACE) != SDL_HWSURFACE) {
    5.19 +    if (!(surface->flags & SDL_PREALLOC) && !(surface->flags & SDL_HWSURFACE)) {
    5.20          SDL_free(surface->pixels);
    5.21          surface->pixels = NULL;
    5.22      }
    5.23 @@ -1936,8 +1934,8 @@
    5.24      if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
    5.25          surface->flags &= ~SDL_RLEACCEL;
    5.26  
    5.27 -        if (recode && (surface->flags & SDL_PREALLOC) != SDL_PREALLOC
    5.28 -            && (surface->flags & SDL_HWSURFACE) != SDL_HWSURFACE) {
    5.29 +        if (recode && !(surface->flags & SDL_PREALLOC)
    5.30 +            && !(surface->flags & SDL_HWSURFACE)) {
    5.31              if ((surface->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
    5.32                  SDL_Rect full;
    5.33                  unsigned alpha_flag;
     6.1 --- a/src/video/SDL_bmp.c	Fri Jun 16 06:00:31 2006 +0000
     6.2 +++ b/src/video/SDL_bmp.c	Sat Jun 17 06:45:14 2006 +0000
     6.3 @@ -222,7 +222,7 @@
     6.4                  SDL_RWread(src, &palette->colors[i].b, 1, 1);
     6.5                  SDL_RWread(src, &palette->colors[i].g, 1, 1);
     6.6                  SDL_RWread(src, &palette->colors[i].r, 1, 1);
     6.7 -                palette->colors[i].unused = 0;
     6.8 +                palette->colors[i].unused = SDL_ALPHA_OPAQUE;
     6.9              }
    6.10          } else {
    6.11              for (i = 0; i < (int) biClrUsed; ++i) {
     7.1 --- a/src/video/SDL_pixels.c	Fri Jun 16 06:00:31 2006 +0000
     7.2 +++ b/src/video/SDL_pixels.c	Sat Jun 17 06:45:14 2006 +0000
     7.3 @@ -215,6 +215,126 @@
     7.4      return SDL_PixelFormat_Unknown;
     7.5  }
     7.6  
     7.7 +
     7.8 +SDL_Palette *
     7.9 +SDL_AllocPalette(int ncolors)
    7.10 +{
    7.11 +    SDL_Palette *palette;
    7.12 +
    7.13 +    palette = (SDL_Palette *) SDL_malloc(sizeof(*palette));
    7.14 +    if (!palette) {
    7.15 +        SDL_OutOfMemory();
    7.16 +        return NULL;
    7.17 +    }
    7.18 +    palette->colors =
    7.19 +        (SDL_Color *) SDL_malloc(ncolors * sizeof(*palette->colors));
    7.20 +    if (!palette->colors) {
    7.21 +        SDL_free(palette);
    7.22 +        return NULL;
    7.23 +    }
    7.24 +    palette->ncolors = ncolors;
    7.25 +    palette->watch = NULL;
    7.26 +    palette->refcount = 1;
    7.27 +
    7.28 +    SDL_memset(palette->colors, 0xFF, ncolors * sizeof(*palette->colors));
    7.29 +
    7.30 +    return palette;
    7.31 +}
    7.32 +
    7.33 +int
    7.34 +SDL_AddPaletteWatch(SDL_Palette * palette, SDL_PaletteChangedFunc callback,
    7.35 +                    void *userdata)
    7.36 +{
    7.37 +    SDL_PaletteWatch *watch;
    7.38 +
    7.39 +    if (!palette) {
    7.40 +        return -1;
    7.41 +    }
    7.42 +
    7.43 +    watch = (SDL_PaletteWatch *) SDL_malloc(sizeof(*watch));
    7.44 +    if (!watch) {
    7.45 +        SDL_OutOfMemory();
    7.46 +        return -1;
    7.47 +    }
    7.48 +
    7.49 +    watch->callback = callback;
    7.50 +    watch->userdata = userdata;
    7.51 +    watch->next = palette->watch;
    7.52 +    palette->watch = watch;
    7.53 +    ++palette->refcount;
    7.54 +    return 0;
    7.55 +}
    7.56 +
    7.57 +void
    7.58 +SDL_DelPaletteWatch(SDL_Palette * palette, SDL_PaletteChangedFunc callback,
    7.59 +                    void *userdata)
    7.60 +{
    7.61 +    SDL_PaletteWatch *prev, *watch;
    7.62 +
    7.63 +    if (!palette) {
    7.64 +        return;
    7.65 +    }
    7.66 +
    7.67 +    for (prev = NULL, watch = palette->watch; watch;
    7.68 +         prev = watch, watch = watch->next) {
    7.69 +        if (watch->callback == callback && watch->userdata == userdata) {
    7.70 +            if (prev) {
    7.71 +                prev->next = watch->next;
    7.72 +            } else {
    7.73 +                palette->watch = watch->next;
    7.74 +            }
    7.75 +            SDL_free(watch);
    7.76 +            SDL_FreePalette(palette);
    7.77 +            return;
    7.78 +        }
    7.79 +    }
    7.80 +}
    7.81 +
    7.82 +int
    7.83 +SDL_SetPaletteColors(SDL_Palette * palette, const SDL_Color * colors,
    7.84 +                     int firstcolor, int ncolors)
    7.85 +{
    7.86 +    SDL_PaletteWatch *watch;
    7.87 +    int status = 0;
    7.88 +
    7.89 +    /* Verify the parameters */
    7.90 +    if (!palette) {
    7.91 +        return -1;
    7.92 +    }
    7.93 +    if (ncolors > (palette->ncolors - firstcolor)) {
    7.94 +        ncolors = (palette->ncolors - firstcolor);
    7.95 +        status = -1;
    7.96 +    }
    7.97 +
    7.98 +    if (colors != (palette->colors + firstcolor)) {
    7.99 +        SDL_memcpy(palette->colors + firstcolor, colors,
   7.100 +                   ncolors * sizeof(*colors));
   7.101 +    }
   7.102 +
   7.103 +    for (watch = palette->watch; watch; watch = watch->next) {
   7.104 +        if (watch->callback(watch->userdata, palette) < 0) {
   7.105 +            status = -1;
   7.106 +        }
   7.107 +    }
   7.108 +
   7.109 +    return status;
   7.110 +}
   7.111 +
   7.112 +void
   7.113 +SDL_FreePalette(SDL_Palette * palette)
   7.114 +{
   7.115 +    if (!palette) {
   7.116 +        return;
   7.117 +    }
   7.118 +    if (--palette->refcount > 0) {
   7.119 +        return;
   7.120 +    }
   7.121 +    if (palette->colors) {
   7.122 +        SDL_free(palette->colors);
   7.123 +    }
   7.124 +    SDL_free(palette);
   7.125 +}
   7.126 +
   7.127  /*
   7.128   * Allocate a pixel format structure and fill it according to the given info.
   7.129   */
   7.130 @@ -238,7 +358,6 @@
   7.131      format->BitsPerPixel = bpp;
   7.132      format->BytesPerPixel = (bpp + 7) / 8;
   7.133      if (Rmask || Bmask || Gmask) {      /* Packed pixels with custom mask */
   7.134 -        format->palette = NULL;
   7.135          format->Rshift = 0;
   7.136          format->Rloss = 8;
   7.137          if (Rmask) {
   7.138 @@ -303,121 +422,11 @@
   7.139          format->Bmask = 0;
   7.140          format->Amask = 0;
   7.141      }
   7.142 -    if (bpp <= 8) {             /* Palettized mode */
   7.143 -        int ncolors = 1 << bpp;
   7.144 -#ifdef DEBUG_PALETTE
   7.145 -        fprintf(stderr, "bpp=%d ncolors=%d\n", bpp, ncolors);
   7.146 -#endif
   7.147 -        format->palette = (SDL_Palette *) SDL_malloc(sizeof(SDL_Palette));
   7.148 -        if (format->palette == NULL) {
   7.149 -            SDL_FreeFormat(format);
   7.150 -            SDL_OutOfMemory();
   7.151 -            return (NULL);
   7.152 -        }
   7.153 -        (format->palette)->ncolors = ncolors;
   7.154 -        (format->palette)->colors = (SDL_Color *) SDL_malloc((format->
   7.155 -                                                              palette)->
   7.156 -                                                             ncolors *
   7.157 -                                                             sizeof
   7.158 -                                                             (SDL_Color));
   7.159 -        if ((format->palette)->colors == NULL) {
   7.160 -            SDL_FreeFormat(format);
   7.161 -            SDL_OutOfMemory();
   7.162 -            return (NULL);
   7.163 -        }
   7.164 -        if (Rmask || Bmask || Gmask) {
   7.165 -            /* create palette according to masks */
   7.166 -            int i;
   7.167 -            int Rm = 0, Gm = 0, Bm = 0;
   7.168 -            int Rw = 0, Gw = 0, Bw = 0;
   7.169 -#ifdef ENABLE_PALETTE_ALPHA
   7.170 -            int Am = 0, Aw = 0;
   7.171 -#endif
   7.172 -            if (Rmask) {
   7.173 -                Rw = 8 - format->Rloss;
   7.174 -                for (i = format->Rloss; i > 0; i -= Rw)
   7.175 -                    Rm |= 1 << i;
   7.176 -            }
   7.177 -#ifdef DEBUG_PALETTE
   7.178 -            fprintf(stderr, "Rw=%d Rm=0x%02X\n", Rw, Rm);
   7.179 -#endif
   7.180 -            if (Gmask) {
   7.181 -                Gw = 8 - format->Gloss;
   7.182 -                for (i = format->Gloss; i > 0; i -= Gw)
   7.183 -                    Gm |= 1 << i;
   7.184 -            }
   7.185 -#ifdef DEBUG_PALETTE
   7.186 -            fprintf(stderr, "Gw=%d Gm=0x%02X\n", Gw, Gm);
   7.187 -#endif
   7.188 -            if (Bmask) {
   7.189 -                Bw = 8 - format->Bloss;
   7.190 -                for (i = format->Bloss; i > 0; i -= Bw)
   7.191 -                    Bm |= 1 << i;
   7.192 -            }
   7.193 -#ifdef DEBUG_PALETTE
   7.194 -            fprintf(stderr, "Bw=%d Bm=0x%02X\n", Bw, Bm);
   7.195 -#endif
   7.196 -#ifdef ENABLE_PALETTE_ALPHA
   7.197 -            if (Amask) {
   7.198 -                Aw = 8 - format->Aloss;
   7.199 -                for (i = format->Aloss; i > 0; i -= Aw)
   7.200 -                    Am |= 1 << i;
   7.201 -            }
   7.202 -# ifdef DEBUG_PALETTE
   7.203 -            fprintf(stderr, "Aw=%d Am=0x%02X\n", Aw, Am);
   7.204 -# endif
   7.205 -#endif
   7.206 -            for (i = 0; i < ncolors; ++i) {
   7.207 -                int r, g, b;
   7.208 -                r = (i & Rmask) >> format->Rshift;
   7.209 -                r = (r << format->Rloss) | ((r * Rm) >> Rw);
   7.210 -                format->palette->colors[i].r = r;
   7.211 +    format->palette = NULL;
   7.212  
   7.213 -                g = (i & Gmask) >> format->Gshift;
   7.214 -                g = (g << format->Gloss) | ((g * Gm) >> Gw);
   7.215 -                format->palette->colors[i].g = g;
   7.216 -
   7.217 -                b = (i & Bmask) >> format->Bshift;
   7.218 -                b = (b << format->Bloss) | ((b * Bm) >> Bw);
   7.219 -                format->palette->colors[i].b = b;
   7.220 -
   7.221 -#ifdef ENABLE_PALETTE_ALPHA
   7.222 -                a = (i & Amask) >> format->Ashift;
   7.223 -                a = (a << format->Aloss) | ((a * Am) >> Aw);
   7.224 -                format->palette->colors[i].unused = a;
   7.225 -#else
   7.226 -                format->palette->colors[i].unused = SDL_ALPHA_OPAQUE;
   7.227 -#endif
   7.228 -            }
   7.229 -        } else if (ncolors == 2) {
   7.230 -            /* Create a black and white bitmap palette */
   7.231 -            format->palette->colors[0].r = 0xFF;
   7.232 -            format->palette->colors[0].g = 0xFF;
   7.233 -            format->palette->colors[0].b = 0xFF;
   7.234 -            format->palette->colors[1].r = 0x00;
   7.235 -            format->palette->colors[1].g = 0x00;
   7.236 -            format->palette->colors[1].b = 0x00;
   7.237 -        } else {
   7.238 -            /* Create an empty palette */
   7.239 -            SDL_memset((format->palette)->colors, 0xFF,
   7.240 -                       (format->palette)->ncolors * sizeof(SDL_Color));
   7.241 -        }
   7.242 -    }
   7.243      return (format);
   7.244  }
   7.245  
   7.246 -SDL_PixelFormat *
   7.247 -SDL_ReallocFormat(SDL_Surface * surface, int bpp,
   7.248 -                  Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
   7.249 -{
   7.250 -    if (surface->format) {
   7.251 -        SDL_FreeFormat(surface->format);
   7.252 -        SDL_FormatChanged(surface);
   7.253 -    }
   7.254 -    surface->format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
   7.255 -    return surface->format;
   7.256 -}
   7.257 -
   7.258  /*
   7.259   * Change any previous mappings from/to the new surface format
   7.260   */
   7.261 @@ -439,15 +448,10 @@
   7.262  void
   7.263  SDL_FreeFormat(SDL_PixelFormat * format)
   7.264  {
   7.265 -    if (format) {
   7.266 -        if (format->palette) {
   7.267 -            if (format->palette->colors) {
   7.268 -                SDL_free(format->palette->colors);
   7.269 -            }
   7.270 -            SDL_free(format->palette);
   7.271 -        }
   7.272 -        SDL_free(format);
   7.273 +    if (!format) {
   7.274 +        return;
   7.275      }
   7.276 +    SDL_free(format);
   7.277  }
   7.278  
   7.279  /*
   7.280 @@ -695,10 +699,6 @@
   7.281      SDL_Color colors[256];
   7.282      SDL_Palette *pal = dst->palette;
   7.283  
   7.284 -    /* SDL_DitherColors does not initialize the 'unused' component of colors,
   7.285 -       but Map1to1 compares it against pal, so we should initialize it. */
   7.286 -    SDL_memset(colors, 0xFF, sizeof(colors));
   7.287 -
   7.288      dithered.ncolors = 256;
   7.289      SDL_DitherColors(colors, 8);
   7.290      dithered.colors = colors;
   7.291 @@ -768,14 +768,8 @@
   7.292          switch (dstfmt->BytesPerPixel) {
   7.293          case 1:
   7.294              /* Palette --> Palette */
   7.295 -            /* If both SDL_HWSURFACE, assume have same palette */
   7.296 -            if (((src->flags & SDL_HWSURFACE) == SDL_HWSURFACE) &&
   7.297 -                ((dst->flags & SDL_HWSURFACE) == SDL_HWSURFACE)) {
   7.298 -                map->identity = 1;
   7.299 -            } else {
   7.300 -                map->table = Map1to1(srcfmt->palette,
   7.301 -                                     dstfmt->palette, &map->identity);
   7.302 -            }
   7.303 +            map->table =
   7.304 +                Map1to1(srcfmt->palette, dstfmt->palette, &map->identity);
   7.305              if (!map->identity) {
   7.306                  if (map->table == NULL) {
   7.307                      return (-1);
     8.1 --- a/src/video/SDL_pixels_c.h	Fri Jun 16 06:00:31 2006 +0000
     8.2 +++ b/src/video/SDL_pixels_c.h	Sat Jun 17 06:45:14 2006 +0000
     8.3 @@ -29,9 +29,6 @@
     8.4  extern SDL_PixelFormat *SDL_AllocFormat(int bpp,
     8.5                                          Uint32 Rmask, Uint32 Gmask,
     8.6                                          Uint32 Bmask, Uint32 Amask);
     8.7 -extern SDL_PixelFormat *SDL_ReallocFormat(SDL_Surface * surface, int bpp,
     8.8 -                                          Uint32 Rmask, Uint32 Gmask,
     8.9 -                                          Uint32 Bmask, Uint32 Amask);
    8.10  extern void SDL_FormatChanged(SDL_Surface * surface);
    8.11  extern void SDL_FreeFormat(SDL_PixelFormat * format);
    8.12  
     9.1 --- a/src/video/SDL_renderer_sw.c	Fri Jun 16 06:00:31 2006 +0000
     9.2 +++ b/src/video/SDL_renderer_sw.c	Sat Jun 17 06:45:14 2006 +0000
     9.3 @@ -178,6 +178,7 @@
     9.4              SDL_SW_DestroyRenderer(renderer);
     9.5              return NULL;
     9.6          }
     9.7 +        SDL_SetSurfacePalette(data->screens[i], window->display->palette);
     9.8      }
     9.9      data->current_screen = 0;
    9.10      data->target = data->screens[0];
    9.11 @@ -248,8 +249,8 @@
    9.12  {
    9.13      SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
    9.14  
    9.15 -    SDL_SetColors(surface, colors, firstcolor, ncolors);
    9.16 -    return 0;
    9.17 +    return SDL_SetPaletteColors(surface->format->palette, colors, firstcolor,
    9.18 +                                ncolors);
    9.19  }
    9.20  
    9.21  static int
    9.22 @@ -314,7 +315,12 @@
    9.23  SDL_SW_SelectRenderTexture(SDL_Renderer * renderer, SDL_Texture * texture)
    9.24  {
    9.25      SDL_SW_RenderData *data = (SDL_SW_RenderData *) renderer->driverdata;
    9.26 -    data->target = (SDL_Surface *) texture->driverdata;
    9.27 +
    9.28 +    if (texture) {
    9.29 +        data->target = (SDL_Surface *) texture->driverdata;
    9.30 +    } else {
    9.31 +        data->target = data->screens[data->current_screen];
    9.32 +    }
    9.33  }
    9.34  
    9.35  static void
    10.1 --- a/src/video/SDL_surface.c	Fri Jun 16 06:00:31 2006 +0000
    10.2 +++ b/src/video/SDL_surface.c	Sat Jun 17 06:45:14 2006 +0000
    10.3 @@ -47,20 +47,21 @@
    10.4      /* Next time I write a library like SDL, I'll use int for size. :) */
    10.5      if (width >= 16384 || height >= 65536) {
    10.6          SDL_SetError("Width or height is too large");
    10.7 -        return (NULL);
    10.8 +        return NULL;
    10.9      }
   10.10  
   10.11      /* Allocate the surface */
   10.12      surface = (SDL_Surface *) SDL_malloc(sizeof(*surface));
   10.13      if (surface == NULL) {
   10.14          SDL_OutOfMemory();
   10.15 -        return (NULL);
   10.16 +        return NULL;
   10.17      }
   10.18 -    surface->flags = 0;
   10.19 +    SDL_zerop(surface);
   10.20 +
   10.21      surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask);
   10.22 -    if (surface->format == NULL) {
   10.23 -        SDL_free(surface);
   10.24 -        return (NULL);
   10.25 +    if (!surface->format) {
   10.26 +        SDL_FreeSurface(surface);
   10.27 +        return NULL;
   10.28      }
   10.29      if (Amask) {
   10.30          surface->flags |= SDL_SRCALPHA;
   10.31 @@ -68,16 +69,69 @@
   10.32      surface->w = width;
   10.33      surface->h = height;
   10.34      surface->pitch = SDL_CalculatePitch(surface);
   10.35 -    surface->pixels = NULL;
   10.36 -    surface->locked = 0;
   10.37 -    surface->map = NULL;
   10.38      SDL_SetClipRect(surface, NULL);
   10.39 -    SDL_FormatChanged(surface);
   10.40 +
   10.41 +    if (surface->format->BitsPerPixel <= 8) {
   10.42 +        SDL_Palette *palette =
   10.43 +            SDL_AllocPalette((1 << surface->format->BitsPerPixel));
   10.44 +        if (!palette) {
   10.45 +            SDL_FreeSurface(surface);
   10.46 +            return NULL;
   10.47 +        }
   10.48 +        if (Rmask || Bmask || Gmask) {
   10.49 +            const SDL_PixelFormat *format = surface->format;
   10.50 +
   10.51 +            /* create palette according to masks */
   10.52 +            int i;
   10.53 +            int Rm = 0, Gm = 0, Bm = 0;
   10.54 +            int Rw = 0, Gw = 0, Bw = 0;
   10.55 +
   10.56 +            if (Rmask) {
   10.57 +                Rw = 8 - format->Rloss;
   10.58 +                for (i = format->Rloss; i > 0; i -= Rw)
   10.59 +                    Rm |= 1 << i;
   10.60 +            }
   10.61 +            if (Gmask) {
   10.62 +                Gw = 8 - format->Gloss;
   10.63 +                for (i = format->Gloss; i > 0; i -= Gw)
   10.64 +                    Gm |= 1 << i;
   10.65 +            }
   10.66 +            if (Bmask) {
   10.67 +                Bw = 8 - format->Bloss;
   10.68 +                for (i = format->Bloss; i > 0; i -= Bw)
   10.69 +                    Bm |= 1 << i;
   10.70 +            }
   10.71 +            for (i = 0; i < palette->ncolors; ++i) {
   10.72 +                int r, g, b;
   10.73 +                r = (i & Rmask) >> format->Rshift;
   10.74 +                r = (r << format->Rloss) | ((r * Rm) >> Rw);
   10.75 +                palette->colors[i].r = r;
   10.76 +
   10.77 +                g = (i & Gmask) >> format->Gshift;
   10.78 +                g = (g << format->Gloss) | ((g * Gm) >> Gw);
   10.79 +                palette->colors[i].g = g;
   10.80 +
   10.81 +                b = (i & Bmask) >> format->Bshift;
   10.82 +                b = (b << format->Bloss) | ((b * Bm) >> Bw);
   10.83 +                palette->colors[i].b = b;
   10.84 +            }
   10.85 +        } else if (palette->ncolors == 2) {
   10.86 +            /* Create a black and white bitmap palette */
   10.87 +            palette->colors[0].r = 0xFF;
   10.88 +            palette->colors[0].g = 0xFF;
   10.89 +            palette->colors[0].b = 0xFF;
   10.90 +            palette->colors[1].r = 0x00;
   10.91 +            palette->colors[1].g = 0x00;
   10.92 +            palette->colors[1].b = 0x00;
   10.93 +        }
   10.94 +        SDL_SetSurfacePalette(surface, palette);
   10.95 +        SDL_FreePalette(palette);
   10.96 +    }
   10.97  
   10.98      /* Get the pixels */
   10.99      if (surface->w && surface->h) {
  10.100          surface->pixels = SDL_malloc(surface->h * surface->pitch);
  10.101 -        if (surface->pixels == NULL) {
  10.102 +        if (!surface->pixels) {
  10.103              SDL_FreeSurface(surface);
  10.104              SDL_OutOfMemory();
  10.105              return NULL;
  10.106 @@ -88,17 +142,18 @@
  10.107  
  10.108      /* Allocate an empty mapping */
  10.109      surface->map = SDL_AllocBlitMap();
  10.110 -    if (surface->map == NULL) {
  10.111 +    if (!surface->map) {
  10.112          SDL_FreeSurface(surface);
  10.113 -        return (NULL);
  10.114 +        return NULL;
  10.115      }
  10.116 +    SDL_FormatChanged(surface);
  10.117  
  10.118      /* The surface is ready to go */
  10.119      surface->refcount = 1;
  10.120  #ifdef CHECK_LEAKS
  10.121      ++surfaces_allocated;
  10.122  #endif
  10.123 -    return (surface);
  10.124 +    return surface;
  10.125  }
  10.126  
  10.127  /*
  10.128 @@ -157,47 +212,64 @@
  10.129              surface->flags |= SDL_HWSURFACE;
  10.130              surface->w = w;
  10.131              surface->h = h;
  10.132 -            surface->lock_data = (void *) textureID;
  10.133              SDL_SetClipRect(surface, NULL);
  10.134          }
  10.135      }
  10.136 +    if (surface) {
  10.137 +        surface->textureID = textureID;
  10.138 +    }
  10.139  
  10.140      return surface;
  10.141  }
  10.142  
  10.143 -/*
  10.144 - * Set the palette in a blittable surface
  10.145 - */
  10.146 -int
  10.147 -SDL_SetColors(SDL_Surface * surface, const SDL_Color * colors, int firstcolor,
  10.148 -              int ncolors)
  10.149 +static int
  10.150 +SDL_SurfacePaletteChanged(void *userdata, SDL_Palette * palette)
  10.151  {
  10.152 -    SDL_Palette *pal;
  10.153 -    int gotall;
  10.154 -    int palsize;
  10.155 +    SDL_Surface *surface = (SDL_Surface *) userdata;
  10.156  
  10.157 -    /* Verify the parameters */
  10.158 -    pal = surface->format->palette;
  10.159 -    if (!pal) {
  10.160 -        return 0;               /* not a palettized surface */
  10.161 -    }
  10.162 -    gotall = 1;
  10.163 -    palsize = 1 << surface->format->BitsPerPixel;
  10.164 -    if (ncolors > (palsize - firstcolor)) {
  10.165 -        ncolors = (palsize - firstcolor);
  10.166 -        gotall = 0;
  10.167 -    }
  10.168 -
  10.169 -    if (colors != (pal->colors + firstcolor)) {
  10.170 -        SDL_memcpy(pal->colors + firstcolor, colors,
  10.171 -                   ncolors * sizeof(*colors));
  10.172 +    if (surface->textureID) {
  10.173 +        if (SDL_SetTexturePalette
  10.174 +            (surface->textureID, palette->colors, 0, palette->ncolors) < 0) {
  10.175 +            SDL_GetTexturePalette(surface->textureID, palette->colors, 0,
  10.176 +                                  palette->ncolors);
  10.177 +            return -1;
  10.178 +        }
  10.179      }
  10.180      SDL_FormatChanged(surface);
  10.181  
  10.182 -    if (surface->flags & (SDL_SHADOW_SURFACE | SDL_SCREEN_SURFACE)) {
  10.183 -        gotall &= SDL_SetScreenColors(surface, colors, firstcolor, ncolors);
  10.184 +    return 0;
  10.185 +}
  10.186 +
  10.187 +int
  10.188 +SDL_SetSurfacePalette(SDL_Surface * surface, SDL_Palette * palette)
  10.189 +{
  10.190 +    if (!surface || !surface->format) {
  10.191 +        SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
  10.192 +        return -1;
  10.193 +    }
  10.194 +
  10.195 +    if (palette && palette->ncolors != (1 << surface->format->BitsPerPixel)) {
  10.196 +        SDL_SetError
  10.197 +            ("SDL_SetSurfacePalette() passed a palette that doesn't match the surface format");
  10.198 +        return -1;
  10.199      }
  10.200 -    return gotall;
  10.201 +
  10.202 +    if (surface->format->palette == palette) {
  10.203 +        return 0;
  10.204 +    }
  10.205 +
  10.206 +    if (surface->format->palette) {
  10.207 +        SDL_DelPaletteWatch(surface->format->palette,
  10.208 +                            SDL_SurfacePaletteChanged, surface);
  10.209 +    }
  10.210 +
  10.211 +    surface->format->palette = palette;
  10.212 +
  10.213 +    if (surface->format->palette) {
  10.214 +        SDL_AddPaletteWatch(surface->format->palette,
  10.215 +                            SDL_SurfacePaletteChanged, surface);
  10.216 +    }
  10.217 +    return 0;
  10.218  }
  10.219  
  10.220  /*
  10.221 @@ -729,8 +801,8 @@
  10.222          /* Perform the lock */
  10.223          if (surface->flags & SDL_HWSURFACE) {
  10.224              if (SDL_LockTexture
  10.225 -                ((SDL_TextureID) surface->lock_data, NULL, 1,
  10.226 -                 &surface->pixels, &surface->pitch) < 0) {
  10.227 +                (surface->textureID, NULL, 1, &surface->pixels,
  10.228 +                 &surface->pitch) < 0) {
  10.229                  return (-1);
  10.230              }
  10.231          }
  10.232 @@ -760,13 +832,13 @@
  10.233  
  10.234      /* Unlock hardware or accelerated surfaces */
  10.235      if (surface->flags & SDL_HWSURFACE) {
  10.236 -        SDL_UnlockTexture((SDL_TextureID) surface->lock_data);
  10.237 -    } else {
  10.238 -        /* Update RLE encoded surface with new data */
  10.239 -        if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
  10.240 -            surface->flags &= ~SDL_RLEACCEL;    /* stop lying */
  10.241 -            SDL_RLESurface(surface);
  10.242 -        }
  10.243 +        SDL_UnlockTexture(surface->textureID);
  10.244 +    }
  10.245 +
  10.246 +    /* Update RLE encoded surface with new data */
  10.247 +    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
  10.248 +        surface->flags &= ~SDL_RLEACCEL;        /* stop lying */
  10.249 +        SDL_RLESurface(surface);
  10.250      }
  10.251  }
  10.252  
  10.253 @@ -894,6 +966,7 @@
  10.254          SDL_UnRLESurface(surface, 0);
  10.255      }
  10.256      if (surface->format) {
  10.257 +        SDL_SetSurfacePalette(surface, NULL);
  10.258          SDL_FreeFormat(surface->format);
  10.259          surface->format = NULL;
  10.260      }
  10.261 @@ -902,8 +975,8 @@
  10.262          surface->map = NULL;
  10.263      }
  10.264      /* Should we destroy the texture too?
  10.265 -       if (surface->flags & SDL_HWSURFACE) {
  10.266 -       SDL_DestroyTexture((SDL_TextureID)surface->lock_data);
  10.267 +       if (surface->textureID) {
  10.268 +       SDL_DestroyTexture(surface->textureID);
  10.269         }
  10.270       */
  10.271      if (surface->pixels && ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC)) {
    11.1 --- a/src/video/SDL_sysvideo.h	Fri Jun 16 06:00:31 2006 +0000
    11.2 +++ b/src/video/SDL_sysvideo.h	Sat Jun 17 06:45:14 2006 +0000
    11.3 @@ -146,7 +146,7 @@
    11.4      SDL_DisplayMode *display_modes;
    11.5      SDL_DisplayMode desktop_mode;
    11.6      SDL_DisplayMode current_mode;
    11.7 -    SDL_Palette palette;
    11.8 +    SDL_Palette *palette;
    11.9  
   11.10      int num_render_drivers;
   11.11      SDL_RenderDriver *render_drivers;
    12.1 --- a/src/video/SDL_video.c	Fri Jun 16 06:00:31 2006 +0000
    12.2 +++ b/src/video/SDL_video.c	Sat Jun 17 06:45:14 2006 +0000
    12.3 @@ -517,7 +517,7 @@
    12.4  {
    12.5      SDL_VideoDisplay *display;
    12.6      SDL_DisplayMode display_mode;
    12.7 -    int i;
    12.8 +    int i, ncolors;
    12.9  
   12.10      if (!_this) {
   12.11          SDL_SetError("Video subsystem has not been initialized");
   12.12 @@ -560,26 +560,24 @@
   12.13          return 0;
   12.14      }
   12.15  
   12.16 +    /* Set up a palette, if necessary */
   12.17      if (SDL_ISPIXELFORMAT_INDEXED(display_mode.format)) {
   12.18 -        display->palette.ncolors =
   12.19 -            (1 << SDL_BITSPERPIXEL(display_mode.format));
   12.20 -        display->palette.colors =
   12.21 -            (SDL_Color *) SDL_realloc(display->palette.colors,
   12.22 -                                      display->palette.ncolors *
   12.23 -                                      sizeof(*display->palette.colors));
   12.24 -        if (!display->palette.colors) {
   12.25 -            SDL_OutOfMemory();
   12.26 -            return -1;
   12.27 +        ncolors = (1 << SDL_BITSPERPIXEL(display_mode.format));
   12.28 +    } else {
   12.29 +        ncolors = 0;
   12.30 +    }
   12.31 +    if ((!ncolors && display->palette) || (ncolors && !display->palette)
   12.32 +        || (ncolors != display->palette->ncolors)) {
   12.33 +        if (display->palette) {
   12.34 +            SDL_FreePalette(display->palette);
   12.35 +            display->palette = NULL;
   12.36          }
   12.37 -        SDL_memset(display->palette.colors, 0xff,
   12.38 -                   display->palette.ncolors *
   12.39 -                   sizeof(*display->palette.colors));
   12.40 -    } else {
   12.41 -        if (display->palette.colors) {
   12.42 -            SDL_free(display->palette.colors);
   12.43 +        if (ncolors) {
   12.44 +            display->palette = SDL_AllocPalette(ncolors);
   12.45 +            if (!display->palette) {
   12.46 +                return -1;
   12.47 +            }
   12.48          }
   12.49 -        display->palette.colors = NULL;
   12.50 -        display->palette.ncolors = 0;
   12.51      }
   12.52  
   12.53      return _this->SetDisplayMode(_this, &display_mode);
   12.54 @@ -589,31 +587,27 @@
   12.55  SDL_SetDisplayPalette(const SDL_Color * colors, int firstcolor, int ncolors)
   12.56  {
   12.57      SDL_Palette *palette;
   12.58 +    int status = 0;
   12.59  
   12.60      if (!_this) {
   12.61          SDL_SetError("Video subsystem has not been initialized");
   12.62          return -1;
   12.63      }
   12.64 -
   12.65 -    palette = &SDL_CurrentDisplay.palette;
   12.66 -    if (!palette->ncolors) {
   12.67 +    if (!SDL_CurrentDisplay.palette) {
   12.68          SDL_SetError("Display mode does not have a palette");
   12.69          return -1;
   12.70      }
   12.71  
   12.72 -    if (firstcolor < 0 || (firstcolor + ncolors) > palette->ncolors) {
   12.73 -        SDL_SetError("Palette indices are out of range");
   12.74 -        return -1;
   12.75 -    }
   12.76 -
   12.77 -    SDL_memcpy(&palette->colors[firstcolor], colors,
   12.78 -               ncolors * sizeof(*colors));
   12.79 +    status =
   12.80 +        SDL_SetPaletteColors(SDL_CurrentDisplay.palette, colors, firstcolor,
   12.81 +                             ncolors);
   12.82  
   12.83      if (_this->SetDisplayPalette) {
   12.84 -        return _this->SetDisplayPalette(_this, palette);
   12.85 -    } else {
   12.86 -        return 0;
   12.87 +        if (_this->SetDisplayPalette(_this, palette) < 0) {
   12.88 +            status = -1;
   12.89 +        }
   12.90      }
   12.91 +    return status;
   12.92  }
   12.93  
   12.94  int
   12.95 @@ -626,7 +620,7 @@
   12.96          return -1;
   12.97      }
   12.98  
   12.99 -    palette = &SDL_CurrentDisplay.palette;
  12.100 +    palette = SDL_CurrentDisplay.palette;
  12.101      if (!palette->ncolors) {
  12.102          SDL_SetError("Display mode does not have a palette");
  12.103          return -1;
  12.104 @@ -1717,10 +1711,9 @@
  12.105              SDL_free(display->windows);
  12.106              display->windows = NULL;
  12.107          }
  12.108 -        if (display->palette.colors) {
  12.109 -            SDL_free(display->palette.colors);
  12.110 -            display->palette.colors = NULL;
  12.111 -            display->palette.ncolors = 0;
  12.112 +        if (display->palette) {
  12.113 +            SDL_FreePalette(display->palette);
  12.114 +            display->palette = NULL;
  12.115          }
  12.116      }
  12.117      _this->VideoQuit(_this);
    13.1 --- a/src/video/dummy/SDL_nullrender.c	Fri Jun 16 06:00:31 2006 +0000
    13.2 +++ b/src/video/dummy/SDL_nullrender.c	Sat Jun 17 06:45:14 2006 +0000
    13.3 @@ -106,16 +106,7 @@
    13.4          SDL_DUMMY_DestroyRenderer(renderer);
    13.5          return NULL;
    13.6      }
    13.7 -
    13.8 -    /* If the display has a palette, use it for the window surfaces */
    13.9 -    if (window->display->palette.ncolors) {
   13.10 -        SDL_PixelFormat *format = data->surface->format;
   13.11 -        if (format->palette->colors) {
   13.12 -            SDL_free(format->palette->colors);
   13.13 -        }
   13.14 -        SDL_free(format->palette);
   13.15 -        format->palette = &window->display->palette;
   13.16 -    }
   13.17 +    SDL_SetSurfacePalette(data->surface, window->display->palette);
   13.18  
   13.19      return renderer;
   13.20  }
   13.21 @@ -178,8 +169,8 @@
   13.22  
   13.23      if (SDL_getenv("SDL_VIDEO_DUMMY_SAVE_FRAMES")) {
   13.24          char file[128];
   13.25 -        SDL_snprintf(file, sizeof(file), "SDL_frame-%8.8d.bmp",
   13.26 -                     ++frame_number);
   13.27 +        SDL_snprintf(file, sizeof(file), "SDL_window%d-%8.8d.bmp",
   13.28 +                     renderer->window->id, ++frame_number);
   13.29          SDL_SaveBMP(surface, file);
   13.30      }
   13.31  }