src/video/SDL_video.c
branchSDL-1.3
changeset 1683 396a35389351
parent 1682 7ae8018b2e5d
child 1684 c4aa1a2f48f1
     1.1 --- a/src/video/SDL_video.c	Fri Jun 16 06:00:31 2006 +0000
     1.2 +++ b/src/video/SDL_video.c	Sat Jun 17 06:45:14 2006 +0000
     1.3 @@ -517,7 +517,7 @@
     1.4  {
     1.5      SDL_VideoDisplay *display;
     1.6      SDL_DisplayMode display_mode;
     1.7 -    int i;
     1.8 +    int i, ncolors;
     1.9  
    1.10      if (!_this) {
    1.11          SDL_SetError("Video subsystem has not been initialized");
    1.12 @@ -560,26 +560,24 @@
    1.13          return 0;
    1.14      }
    1.15  
    1.16 +    /* Set up a palette, if necessary */
    1.17      if (SDL_ISPIXELFORMAT_INDEXED(display_mode.format)) {
    1.18 -        display->palette.ncolors =
    1.19 -            (1 << SDL_BITSPERPIXEL(display_mode.format));
    1.20 -        display->palette.colors =
    1.21 -            (SDL_Color *) SDL_realloc(display->palette.colors,
    1.22 -                                      display->palette.ncolors *
    1.23 -                                      sizeof(*display->palette.colors));
    1.24 -        if (!display->palette.colors) {
    1.25 -            SDL_OutOfMemory();
    1.26 -            return -1;
    1.27 +        ncolors = (1 << SDL_BITSPERPIXEL(display_mode.format));
    1.28 +    } else {
    1.29 +        ncolors = 0;
    1.30 +    }
    1.31 +    if ((!ncolors && display->palette) || (ncolors && !display->palette)
    1.32 +        || (ncolors != display->palette->ncolors)) {
    1.33 +        if (display->palette) {
    1.34 +            SDL_FreePalette(display->palette);
    1.35 +            display->palette = NULL;
    1.36          }
    1.37 -        SDL_memset(display->palette.colors, 0xff,
    1.38 -                   display->palette.ncolors *
    1.39 -                   sizeof(*display->palette.colors));
    1.40 -    } else {
    1.41 -        if (display->palette.colors) {
    1.42 -            SDL_free(display->palette.colors);
    1.43 +        if (ncolors) {
    1.44 +            display->palette = SDL_AllocPalette(ncolors);
    1.45 +            if (!display->palette) {
    1.46 +                return -1;
    1.47 +            }
    1.48          }
    1.49 -        display->palette.colors = NULL;
    1.50 -        display->palette.ncolors = 0;
    1.51      }
    1.52  
    1.53      return _this->SetDisplayMode(_this, &display_mode);
    1.54 @@ -589,31 +587,27 @@
    1.55  SDL_SetDisplayPalette(const SDL_Color * colors, int firstcolor, int ncolors)
    1.56  {
    1.57      SDL_Palette *palette;
    1.58 +    int status = 0;
    1.59  
    1.60      if (!_this) {
    1.61          SDL_SetError("Video subsystem has not been initialized");
    1.62          return -1;
    1.63      }
    1.64 -
    1.65 -    palette = &SDL_CurrentDisplay.palette;
    1.66 -    if (!palette->ncolors) {
    1.67 +    if (!SDL_CurrentDisplay.palette) {
    1.68          SDL_SetError("Display mode does not have a palette");
    1.69          return -1;
    1.70      }
    1.71  
    1.72 -    if (firstcolor < 0 || (firstcolor + ncolors) > palette->ncolors) {
    1.73 -        SDL_SetError("Palette indices are out of range");
    1.74 -        return -1;
    1.75 -    }
    1.76 -
    1.77 -    SDL_memcpy(&palette->colors[firstcolor], colors,
    1.78 -               ncolors * sizeof(*colors));
    1.79 +    status =
    1.80 +        SDL_SetPaletteColors(SDL_CurrentDisplay.palette, colors, firstcolor,
    1.81 +                             ncolors);
    1.82  
    1.83      if (_this->SetDisplayPalette) {
    1.84 -        return _this->SetDisplayPalette(_this, palette);
    1.85 -    } else {
    1.86 -        return 0;
    1.87 +        if (_this->SetDisplayPalette(_this, palette) < 0) {
    1.88 +            status = -1;
    1.89 +        }
    1.90      }
    1.91 +    return status;
    1.92  }
    1.93  
    1.94  int
    1.95 @@ -626,7 +620,7 @@
    1.96          return -1;
    1.97      }
    1.98  
    1.99 -    palette = &SDL_CurrentDisplay.palette;
   1.100 +    palette = SDL_CurrentDisplay.palette;
   1.101      if (!palette->ncolors) {
   1.102          SDL_SetError("Display mode does not have a palette");
   1.103          return -1;
   1.104 @@ -1717,10 +1711,9 @@
   1.105              SDL_free(display->windows);
   1.106              display->windows = NULL;
   1.107          }
   1.108 -        if (display->palette.colors) {
   1.109 -            SDL_free(display->palette.colors);
   1.110 -            display->palette.colors = NULL;
   1.111 -            display->palette.ncolors = 0;
   1.112 +        if (display->palette) {
   1.113 +            SDL_FreePalette(display->palette);
   1.114 +            display->palette = NULL;
   1.115          }
   1.116      }
   1.117      _this->VideoQuit(_this);