src/video/SDL_surface.c
changeset 5288 d4381f3b0d1e
parent 5262 b530ef003506
child 5296 48067bfc300c
equal deleted inserted replaced
5287:d1823573d005 5288:d4381f3b0d1e
    37 SDL_CreateRGBSurface(Uint32 flags,
    37 SDL_CreateRGBSurface(Uint32 flags,
    38                      int width, int height, int depth,
    38                      int width, int height, int depth,
    39                      Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
    39                      Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
    40 {
    40 {
    41     SDL_Surface *surface;
    41     SDL_Surface *surface;
       
    42     Uint32 format;
    42 
    43 
    43     /* The flags are no longer used, make the compiler happy */
    44     /* The flags are no longer used, make the compiler happy */
    44     (void)flags;
    45     (void)flags;
       
    46 
       
    47     /* Get the pixel format */
       
    48     format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask);
       
    49     if (format == SDL_PIXELFORMAT_UNKNOWN) {
       
    50         SDL_SetError("Unknown pixel format");
       
    51         return NULL;
       
    52     }
    45 
    53 
    46     /* Allocate the surface */
    54     /* Allocate the surface */
    47     surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
    55     surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
    48     if (surface == NULL) {
    56     if (surface == NULL) {
    49         SDL_OutOfMemory();
    57         SDL_OutOfMemory();
    50         return NULL;
    58         return NULL;
    51     }
    59     }
    52 
    60 
    53     surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask);
    61     surface->format = SDL_AllocFormat(format);
    54     if (!surface->format) {
    62     if (!surface->format) {
    55         SDL_FreeSurface(surface);
    63         SDL_FreeSurface(surface);
    56         return NULL;
    64         return NULL;
    57     }
    65     }
    58     surface->w = width;
    66     surface->w = width;
    59     surface->h = height;
    67     surface->h = height;
    60     surface->pitch = SDL_CalculatePitch(surface);
    68     surface->pitch = SDL_CalculatePitch(surface);
    61     SDL_SetClipRect(surface, NULL);
    69     SDL_SetClipRect(surface, NULL);
    62 
    70 
    63     if (surface->format->BitsPerPixel <= 8) {
    71     if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
    64         SDL_Palette *palette =
    72         SDL_Palette *palette =
    65             SDL_AllocPalette((1 << surface->format->BitsPerPixel));
    73             SDL_AllocPalette((1 << surface->format->BitsPerPixel));
    66         if (!palette) {
    74         if (!palette) {
    67             SDL_FreeSurface(surface);
    75             SDL_FreeSurface(surface);
    68             return NULL;
    76             return NULL;
   133     surface->map = SDL_AllocBlitMap();
   141     surface->map = SDL_AllocBlitMap();
   134     if (!surface->map) {
   142     if (!surface->map) {
   135         SDL_FreeSurface(surface);
   143         SDL_FreeSurface(surface);
   136         return NULL;
   144         return NULL;
   137     }
   145     }
   138     SDL_FormatChanged(surface);
       
   139 
   146 
   140     /* By default surface with an alpha mask are set up for blending */
   147     /* By default surface with an alpha mask are set up for blending */
   141     if (Amask) {
   148     if (Amask) {
   142         SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
   149         SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
   143     }
   150     }
   169         SDL_SetClipRect(surface, NULL);
   176         SDL_SetClipRect(surface, NULL);
   170     }
   177     }
   171     return surface;
   178     return surface;
   172 }
   179 }
   173 
   180 
   174 static int
       
   175 SDL_SurfacePaletteChanged(void *userdata, SDL_Palette * palette)
       
   176 {
       
   177     SDL_Surface *surface = (SDL_Surface *) userdata;
       
   178 
       
   179     SDL_FormatChanged(surface);
       
   180 
       
   181     return 0;
       
   182 }
       
   183 
       
   184 int
   181 int
   185 SDL_SetSurfacePalette(SDL_Surface * surface, SDL_Palette * palette)
   182 SDL_SetSurfacePalette(SDL_Surface * surface, SDL_Palette * palette)
   186 {
   183 {
   187     if (!surface || !surface->format) {
   184     if (!surface) {
   188         SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
   185         SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
   189         return -1;
   186         return -1;
   190     }
   187     }
   191 
   188     return SDL_SetPixelFormatPalette(surface->format, palette);
   192     if (palette && palette->ncolors != (1 << surface->format->BitsPerPixel)) {
       
   193         SDL_SetError
       
   194             ("SDL_SetSurfacePalette() passed a palette that doesn't match the surface format");
       
   195         return -1;
       
   196     }
       
   197 
       
   198     if (surface->format->palette == palette) {
       
   199         return 0;
       
   200     }
       
   201 
       
   202     if (surface->format->palette) {
       
   203         SDL_DelPaletteWatch(surface->format->palette,
       
   204                             SDL_SurfacePaletteChanged, surface);
       
   205     }
       
   206 
       
   207     surface->format->palette = palette;
       
   208 
       
   209     if (surface->format->palette) {
       
   210         SDL_AddPaletteWatch(surface->format->palette,
       
   211                             SDL_SurfacePaletteChanged, surface);
       
   212     }
       
   213     return 0;
       
   214 }
   189 }
   215 
   190 
   216 int
   191 int
   217 SDL_SetSurfaceRLE(SDL_Surface * surface, int flag)
   192 SDL_SetSurfaceRLE(SDL_Surface * surface, int flag)
   218 {
   193 {
   554 SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
   529 SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
   555               SDL_Surface * dst, SDL_Rect * dstrect)
   530               SDL_Surface * dst, SDL_Rect * dstrect)
   556 {
   531 {
   557     /* Check to make sure the blit mapping is valid */
   532     /* Check to make sure the blit mapping is valid */
   558     if ((src->map->dst != dst) ||
   533     if ((src->map->dst != dst) ||
   559         (src->map->dst->format_version != src->map->format_version)) {
   534         (dst->format->palette &&
       
   535          src->map->palette_version != dst->format->palette->version)) {
   560         if (SDL_MapSurface(src, dst) < 0) {
   536         if (SDL_MapSurface(src, dst) < 0) {
   561             return (-1);
   537             return (-1);
   562         }
   538         }
   563         /* just here for debugging */
   539         /* just here for debugging */
   564 /*         printf */
   540 /*         printf */
   799 static __inline__ SDL_bool
   775 static __inline__ SDL_bool
   800 SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format,
   776 SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format,
   801                          void * pixels, int pitch, SDL_Surface * surface, 
   777                          void * pixels, int pitch, SDL_Surface * surface, 
   802                          SDL_PixelFormat * format, SDL_BlitMap * blitmap)
   778                          SDL_PixelFormat * format, SDL_BlitMap * blitmap)
   803 {
   779 {
   804     int bpp;
   780     if (SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
   805     Uint32 Rmask, Gmask, Bmask, Amask;
       
   806 
       
   807     if (!SDL_PixelFormatEnumToMasks(pixel_format,
       
   808                                     &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
       
   809         return SDL_FALSE;
       
   810     }
       
   811     if (bpp <= 8) {
       
   812         SDL_SetError("Indexed pixel formats not supported");
   781         SDL_SetError("Indexed pixel formats not supported");
   813         return SDL_FALSE;
   782         return SDL_FALSE;
   814     }
   783     }
       
   784     if (SDL_InitFormat(format, pixel_format) < 0) {
       
   785         return SDL_FALSE;
       
   786     }
   815 
   787 
   816     SDL_zerop(surface);
   788     SDL_zerop(surface);
   817     surface->flags = SDL_PREALLOC;
   789     surface->flags = SDL_PREALLOC;
   818     surface->format = SDL_InitFormat(format, bpp, Rmask, Gmask, Bmask, Amask);
   790     surface->format = format;
   819     surface->pixels = pixels;
   791     surface->pixels = pixels;
   820     surface->w = width;
   792     surface->w = width;
   821     surface->h = height;
   793     surface->h = height;
   822     surface->pitch = pitch;
   794     surface->pitch = pitch;
   823     /* We don't actually need to set up the clip rect for our purposes */
   795     /* We don't actually need to set up the clip rect for our purposes */
   828     blitmap->info.r = 0xFF;
   800     blitmap->info.r = 0xFF;
   829     blitmap->info.g = 0xFF;
   801     blitmap->info.g = 0xFF;
   830     blitmap->info.b = 0xFF;
   802     blitmap->info.b = 0xFF;
   831     blitmap->info.a = 0xFF;
   803     blitmap->info.a = 0xFF;
   832     surface->map = blitmap;
   804     surface->map = blitmap;
   833     SDL_FormatChanged(surface);
       
   834 
   805 
   835     /* The surface is ready to go */
   806     /* The surface is ready to go */
   836     surface->refcount = 1;
   807     surface->refcount = 1;
   837     return SDL_TRUE;
   808     return SDL_TRUE;
   838 }
   809 }
   903 SDL_FreeSurface(SDL_Surface * surface)
   874 SDL_FreeSurface(SDL_Surface * surface)
   904 {
   875 {
   905     if (surface == NULL) {
   876     if (surface == NULL) {
   906         return;
   877         return;
   907     }
   878     }
       
   879     if (surface->flags & SDL_DONTFREE) {
       
   880         return;
       
   881     }
   908     if (--surface->refcount > 0) {
   882     if (--surface->refcount > 0) {
   909         return;
   883         return;
   910     }
   884     }
   911     while (surface->locked > 0) {
   885     while (surface->locked > 0) {
   912         SDL_UnlockSurface(surface);
   886         SDL_UnlockSurface(surface);