SDL  2.0
SDL_surface.c File Reference
#include "../SDL_internal.h"
#include "SDL_video.h"
#include "SDL_sysvideo.h"
#include "SDL_blit.h"
#include "SDL_RLEaccel_c.h"
#include "SDL_pixels_c.h"
+ Include dependency graph for SDL_surface.c:

Go to the source code of this file.

Functions

SDL_SurfaceSDL_CreateRGBSurfaceWithFormat (Uint32 flags, int width, int height, int depth, Uint32 format)
 
SDL_SurfaceSDL_CreateRGBSurface (Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
 
SDL_SurfaceSDL_CreateRGBSurfaceFrom (void *pixels, int width, int height, int depth, int pitch, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
 
SDL_SurfaceSDL_CreateRGBSurfaceWithFormatFrom (void *pixels, int width, int height, int depth, int pitch, Uint32 format)
 
int SDL_SetSurfacePalette (SDL_Surface *surface, SDL_Palette *palette)
 Set the palette used by a surface. More...
 
int SDL_SetSurfaceRLE (SDL_Surface *surface, int flag)
 Sets the RLE acceleration hint for a surface. More...
 
int SDL_SetColorKey (SDL_Surface *surface, int flag, Uint32 key)
 Sets the color key (transparent pixel) in a blittable surface. More...
 
int SDL_GetColorKey (SDL_Surface *surface, Uint32 *key)
 Gets the color key (transparent pixel) in a blittable surface. More...
 
static void SDL_ConvertColorkeyToAlpha (SDL_Surface *surface)
 
int SDL_SetSurfaceColorMod (SDL_Surface *surface, Uint8 r, Uint8 g, Uint8 b)
 Set an additional color value used in blit operations. More...
 
int SDL_GetSurfaceColorMod (SDL_Surface *surface, Uint8 *r, Uint8 *g, Uint8 *b)
 Get the additional color value used in blit operations. More...
 
int SDL_SetSurfaceAlphaMod (SDL_Surface *surface, Uint8 alpha)
 Set an additional alpha value used in blit operations. More...
 
int SDL_GetSurfaceAlphaMod (SDL_Surface *surface, Uint8 *alpha)
 Get the additional alpha value used in blit operations. More...
 
int SDL_SetSurfaceBlendMode (SDL_Surface *surface, SDL_BlendMode blendMode)
 Set the blend mode used for blit operations. More...
 
int SDL_GetSurfaceBlendMode (SDL_Surface *surface, SDL_BlendMode *blendMode)
 Get the blend mode used for blit operations. More...
 
SDL_bool SDL_SetClipRect (SDL_Surface *surface, const SDL_Rect *rect)
 
void SDL_GetClipRect (SDL_Surface *surface, SDL_Rect *rect)
 
int SDL_LowerBlit (SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
 
int SDL_UpperBlit (SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
 
int SDL_UpperBlitScaled (SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
 
int SDL_LowerBlitScaled (SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
 
int SDL_LockSurface (SDL_Surface *surface)
 Sets up a surface for directly accessing the pixels. More...
 
void SDL_UnlockSurface (SDL_Surface *surface)
 
SDL_SurfaceSDL_DuplicateSurface (SDL_Surface *surface)
 
SDL_SurfaceSDL_ConvertSurface (SDL_Surface *surface, const SDL_PixelFormat *format, Uint32 flags)
 
SDL_SurfaceSDL_ConvertSurfaceFormat (SDL_Surface *surface, Uint32 pixel_format, Uint32 flags)
 
static SDL_INLINE SDL_bool SDL_CreateSurfaceOnStack (int width, int height, Uint32 pixel_format, void *pixels, int pitch, SDL_Surface *surface, SDL_PixelFormat *format, SDL_BlitMap *blitmap)
 
int SDL_ConvertPixels (int width, int height, Uint32 src_format, const void *src, int src_pitch, Uint32 dst_format, void *dst, int dst_pitch)
 Copy a block of pixels of one format to another format. More...
 
void SDL_FreeSurface (SDL_Surface *surface)
 

Function Documentation

◆ SDL_ConvertColorkeyToAlpha()

static void SDL_ConvertColorkeyToAlpha ( SDL_Surface surface)
static

Definition at line 275 of file SDL_surface.c.

References SDL_PixelFormat::Amask, SDL_PixelFormat::BytesPerPixel, SDL_BlitInfo::colorkey, SDL_BlitInfo::flags, SDL_Surface::format, SDL_Surface::h, SDL_BlitMap::info, SDL_Surface::map, SDL_Surface::pitch, SDL_Surface::pixels, SDL_BLENDMODE_BLEND, SDL_COPY_COLORKEY, SDL_LockSurface(), SDL_SetColorKey(), SDL_SetSurfaceBlendMode(), SDL_UnlockSurface(), and SDL_Surface::w.

Referenced by SDL_ConvertSurface().

276 {
277  int x, y;
278 
279  if (!surface) {
280  return;
281  }
282 
283  if (!(surface->map->info.flags & SDL_COPY_COLORKEY) ||
284  !surface->format->Amask) {
285  return;
286  }
287 
288  SDL_LockSurface(surface);
289 
290  switch (surface->format->BytesPerPixel) {
291  case 2:
292  {
293  Uint16 *row, *spot;
294  Uint16 ckey = (Uint16) surface->map->info.colorkey;
295  Uint16 mask = (Uint16) (~surface->format->Amask);
296 
297  /* Ignore alpha in colorkey comparison */
298  ckey &= mask;
299  row = (Uint16 *) surface->pixels;
300  for (y = surface->h; y--;) {
301  spot = row;
302  for (x = surface->w; x--;) {
303  if ((*spot & mask) == ckey) {
304  *spot &= mask;
305  }
306  ++spot;
307  }
308  row += surface->pitch / 2;
309  }
310  }
311  break;
312  case 3:
313  /* FIXME */
314  break;
315  case 4:
316  {
317  Uint32 *row, *spot;
318  Uint32 ckey = surface->map->info.colorkey;
319  Uint32 mask = ~surface->format->Amask;
320 
321  /* Ignore alpha in colorkey comparison */
322  ckey &= mask;
323  row = (Uint32 *) surface->pixels;
324  for (y = surface->h; y--;) {
325  spot = row;
326  for (x = surface->w; x--;) {
327  if ((*spot & mask) == ckey) {
328  *spot &= mask;
329  }
330  ++spot;
331  }
332  row += surface->pitch / 4;
333  }
334  }
335  break;
336  }
337 
338  SDL_UnlockSurface(surface);
339 
340  SDL_SetColorKey(surface, 0, 0);
342 }
void SDL_UnlockSurface(SDL_Surface *surface)
Definition: SDL_surface.c:859
#define SDL_COPY_COLORKEY
Definition: SDL_blit.h:39
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
int SDL_LockSurface(SDL_Surface *surface)
Sets up a surface for directly accessing the pixels.
Definition: SDL_surface.c:838
Uint8 BytesPerPixel
Definition: SDL_pixels.h:318
int SDL_SetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode blendMode)
Set the blend mode used for blit operations.
Definition: SDL_surface.c:426
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:169
int SDL_SetColorKey(SDL_Surface *surface, int flag, Uint32 key)
Sets the color key (transparent pixel) in a blittable surface.
Definition: SDL_surface.c:212
Uint32 colorkey
Definition: SDL_blit.h:69
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
void * pixels
Definition: SDL_surface.h:75
GLenum GLint GLuint mask
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
SDL_PixelFormat * format
Definition: SDL_surface.h:72
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:161
GLenum GLenum void * row
SDL_BlitInfo info
Definition: SDL_blit.h:92

◆ SDL_ConvertPixels()

int SDL_ConvertPixels ( int  width,
int  height,
Uint32  src_format,
const void src,
int  src_pitch,
Uint32  dst_format,
void dst,
int  dst_pitch 
)

Copy a block of pixels of one format to another format.

Returns
0 on success, or -1 if there was an error

Definition at line 1099 of file SDL_surface.c.

References SDL_Rect::h, i, rect, SDL_BYTESPERPIXEL, SDL_CreateSurfaceOnStack(), SDL_InvalidParamError, SDL_ISPIXELFORMAT_FOURCC, SDL_LowerBlit(), SDL_memcpy, SDL_PIXELFORMAT_IYUV, SDL_PIXELFORMAT_NV12, SDL_PIXELFORMAT_NV21, SDL_PIXELFORMAT_UYVY, SDL_PIXELFORMAT_YUY2, SDL_PIXELFORMAT_YV12, SDL_PIXELFORMAT_YVYU, SDL_SetError, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

1102 {
1103  SDL_Surface src_surface, dst_surface;
1104  SDL_PixelFormat src_fmt, dst_fmt;
1105  SDL_BlitMap src_blitmap, dst_blitmap;
1106  SDL_Rect rect;
1107  void *nonconst_src = (void *) src;
1108 
1109  /* Check to make sure we are blitting somewhere, so we don't crash */
1110  if (!dst) {
1111  return SDL_InvalidParamError("dst");
1112  }
1113  if (!dst_pitch) {
1114  return SDL_InvalidParamError("dst_pitch");
1115  }
1116 
1117  /* Fast path for same format copy */
1118  if (src_format == dst_format) {
1119  int bpp, i;
1120 
1121  if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
1122  switch (src_format) {
1123  case SDL_PIXELFORMAT_YUY2:
1124  case SDL_PIXELFORMAT_UYVY:
1125  case SDL_PIXELFORMAT_YVYU:
1126  bpp = 2;
1127  break;
1128  case SDL_PIXELFORMAT_YV12:
1129  case SDL_PIXELFORMAT_IYUV:
1130  case SDL_PIXELFORMAT_NV12:
1131  case SDL_PIXELFORMAT_NV21:
1132  bpp = 1;
1133  break;
1134  default:
1135  return SDL_SetError("Unknown FOURCC pixel format");
1136  }
1137  } else {
1138  bpp = SDL_BYTESPERPIXEL(src_format);
1139  }
1140  width *= bpp;
1141 
1142  for (i = height; i--;) {
1143  SDL_memcpy(dst, src, width);
1144  src = (Uint8*)src + src_pitch;
1145  dst = (Uint8*)dst + dst_pitch;
1146  }
1147 
1148  if (src_format == SDL_PIXELFORMAT_YV12 || src_format == SDL_PIXELFORMAT_IYUV) {
1149  /* U and V planes are a quarter the size of the Y plane */
1150  width /= 2;
1151  height /= 2;
1152  src_pitch /= 2;
1153  dst_pitch /= 2;
1154  for (i = height * 2; i--;) {
1155  SDL_memcpy(dst, src, width);
1156  src = (Uint8*)src + src_pitch;
1157  dst = (Uint8*)dst + dst_pitch;
1158  }
1159  } else if (src_format == SDL_PIXELFORMAT_NV12 || src_format == SDL_PIXELFORMAT_NV21) {
1160  /* U/V plane is half the height of the Y plane */
1161  height /= 2;
1162  for (i = height; i--;) {
1163  SDL_memcpy(dst, src, width);
1164  src = (Uint8*)src + src_pitch;
1165  dst = (Uint8*)dst + dst_pitch;
1166  }
1167  }
1168  return 0;
1169  }
1170 
1171  if (!SDL_CreateSurfaceOnStack(width, height, src_format, nonconst_src,
1172  src_pitch,
1173  &src_surface, &src_fmt, &src_blitmap)) {
1174  return -1;
1175  }
1176  if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst, dst_pitch,
1177  &dst_surface, &dst_fmt, &dst_blitmap)) {
1178  return -1;
1179  }
1180 
1181  /* Set up the rect and go! */
1182  rect.x = 0;
1183  rect.y = 0;
1184  rect.w = width;
1185  rect.h = height;
1186  return SDL_LowerBlit(&src_surface, &rect, &dst_surface, &rect);
1187 }
GLenum GLenum dst
SDL_Rect rect
Definition: testrelative.c:27
static SDL_INLINE SDL_bool SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format, void *pixels, int pitch, SDL_Surface *surface, SDL_PixelFormat *format, SDL_BlitMap *blitmap)
Definition: SDL_surface.c:1061
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_BYTESPERPIXEL(X)
Definition: SDL_pixels.h:128
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
GLenum src
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:535
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
#define SDL_memcpy
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:153
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define SDL_SetError
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
int h
Definition: SDL_rect.h:67
#define SDL_ISPIXELFORMAT_FOURCC(format)
Definition: SDL_pixels.h:167
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64

◆ SDL_ConvertSurface()

SDL_Surface* SDL_ConvertSurface ( SDL_Surface src,
const SDL_PixelFormat fmt,
Uint32  flags 
)

Creates a new surface of the specified format, and then copies and maps the given surface to it so the blit of the converted surface will be as fast as possible. If this function fails, it returns NULL.

The flags parameter is passed to SDL_CreateRGBSurface() and has those semantics. You can also pass SDL_RLEACCEL in the flags parameter and SDL will try to RLE accelerate colorkey and alpha blits in the resulting surface.

Definition at line 886 of file SDL_surface.c.

References SDL_BlitInfo::a, SDL_Color::a, SDL_PixelFormat::Amask, SDL_BlitInfo::b, SDL_Color::b, SDL_PixelFormat::BitsPerPixel, SDL_PixelFormat::Bmask, SDL_PixelFormat::BytesPerPixel, SDL_Surface::clip_rect, SDL_BlitInfo::colorkey, SDL_Palette::colors, SDL_BlitInfo::flags, SDL_Surface::format, SDL_BlitInfo::g, SDL_Color::g, SDL_PixelFormat::Gmask, SDL_Rect::h, SDL_Surface::h, i, SDL_BlitMap::info, SDL_Surface::map, SDL_Palette::ncolors, NULL, SDL_PixelFormat::palette, SDL_Surface::pixels, SDL_BlitInfo::r, SDL_Color::r, SDL_PixelFormat::Rmask, SDL_BLENDMODE_BLEND, SDL_ConvertColorkeyToAlpha(), SDL_COPY_BLEND, SDL_COPY_COLORKEY, SDL_COPY_MODULATE_ALPHA, SDL_COPY_RLE_ALPHAKEY, SDL_COPY_RLE_COLORKEY, SDL_COPY_RLE_DESIRED, SDL_CreateRGBSurface(), SDL_FALSE, SDL_FillRect, SDL_FreeSurface(), SDL_InvalidateMap(), SDL_InvalidParamError, SDL_LowerBlit(), SDL_memcmp, SDL_memcpy, SDL_RLEACCEL, SDL_SetClipRect(), SDL_SetColorKey(), SDL_SetError, SDL_SetSurfaceBlendMode(), SDL_SetSurfacePalette(), SDL_SetSurfaceRLE(), SDL_TRUE, SDL_Rect::w, SDL_Surface::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by SDL_ConvertSurfaceFormat(), and SDL_DuplicateSurface().

888 {
889  SDL_Surface *convert;
890  Uint32 copy_flags;
891  SDL_Color copy_color;
892  SDL_Rect bounds;
893 
894  if (!surface) {
895  SDL_InvalidParamError("surface");
896  return NULL;
897  }
898  if (!format) {
899  SDL_InvalidParamError("format");
900  return NULL;
901  }
902 
903  /* Check for empty destination palette! (results in empty image) */
904  if (format->palette != NULL) {
905  int i;
906  for (i = 0; i < format->palette->ncolors; ++i) {
907  if ((format->palette->colors[i].r != 0xFF) ||
908  (format->palette->colors[i].g != 0xFF) ||
909  (format->palette->colors[i].b != 0xFF))
910  break;
911  }
912  if (i == format->palette->ncolors) {
913  SDL_SetError("Empty destination palette");
914  return (NULL);
915  }
916  }
917 
918  /* Create a new surface with the desired format */
919  convert = SDL_CreateRGBSurface(flags, surface->w, surface->h,
920  format->BitsPerPixel, format->Rmask,
921  format->Gmask, format->Bmask,
922  format->Amask);
923  if (convert == NULL) {
924  return (NULL);
925  }
926 
927  /* Copy the palette if any */
928  if (format->palette && convert->format->palette) {
929  SDL_memcpy(convert->format->palette->colors,
930  format->palette->colors,
931  format->palette->ncolors * sizeof(SDL_Color));
932  convert->format->palette->ncolors = format->palette->ncolors;
933  }
934 
935  /* Save the original copy flags */
936  copy_flags = surface->map->info.flags;
937  copy_color.r = surface->map->info.r;
938  copy_color.g = surface->map->info.g;
939  copy_color.b = surface->map->info.b;
940  copy_color.a = surface->map->info.a;
941  surface->map->info.r = 0xFF;
942  surface->map->info.g = 0xFF;
943  surface->map->info.b = 0xFF;
944  surface->map->info.a = 0xFF;
945  surface->map->info.flags = 0;
946  SDL_InvalidateMap(surface->map);
947 
948  /* Copy over the image data */
949  bounds.x = 0;
950  bounds.y = 0;
951  bounds.w = surface->w;
952  bounds.h = surface->h;
953  SDL_LowerBlit(surface, &bounds, convert, &bounds);
954 
955  /* Clean up the original surface, and update converted surface */
956  convert->map->info.r = copy_color.r;
957  convert->map->info.g = copy_color.g;
958  convert->map->info.b = copy_color.b;
959  convert->map->info.a = copy_color.a;
960  convert->map->info.flags =
961  (copy_flags &
965  surface->map->info.r = copy_color.r;
966  surface->map->info.g = copy_color.g;
967  surface->map->info.b = copy_color.b;
968  surface->map->info.a = copy_color.a;
969  surface->map->info.flags = copy_flags;
970  SDL_InvalidateMap(surface->map);
971  if (copy_flags & SDL_COPY_COLORKEY) {
972  SDL_bool set_colorkey_by_color = SDL_FALSE;
973 
974  if (surface->format->palette) {
975  if (format->palette &&
976  surface->format->palette->ncolors <= format->palette->ncolors &&
977  (SDL_memcmp(surface->format->palette->colors, format->palette->colors,
978  surface->format->palette->ncolors * sizeof(SDL_Color)) == 0)) {
979  /* The palette is identical, just set the same colorkey */
980  SDL_SetColorKey(convert, 1, surface->map->info.colorkey);
981  } else if (format->Amask) {
982  /* The alpha was set in the destination from the palette */
983  } else {
984  set_colorkey_by_color = SDL_TRUE;
985  }
986  } else {
987  set_colorkey_by_color = SDL_TRUE;
988  }
989 
990  if (set_colorkey_by_color) {
991  SDL_Surface *tmp;
992  SDL_Surface *tmp2;
993  int converted_colorkey = 0;
994 
995  /* Create a dummy surface to get the colorkey converted */
996  tmp = SDL_CreateRGBSurface(0, 1, 1,
997  surface->format->BitsPerPixel, surface->format->Rmask,
998  surface->format->Gmask, surface->format->Bmask,
999  surface->format->Amask);
1000 
1001  /* Share the palette, if any */
1002  if (surface->format->palette) {
1003  SDL_SetSurfacePalette(tmp, surface->format->palette);
1004  }
1005 
1006  SDL_FillRect(tmp, NULL, surface->map->info.colorkey);
1007 
1008  tmp->map->info.flags &= ~SDL_COPY_COLORKEY;
1009 
1010  /* Convertion of the colorkey */
1011  tmp2 = SDL_ConvertSurface(tmp, format, 0);
1012 
1013  /* Get the converted colorkey */
1014  SDL_memcpy(&converted_colorkey, tmp2->pixels, tmp2->format->BytesPerPixel);
1015 
1016  SDL_FreeSurface(tmp);
1017  SDL_FreeSurface(tmp2);
1018 
1019  /* Set the converted colorkey on the new surface */
1020  SDL_SetColorKey(convert, 1, converted_colorkey);
1021 
1022  /* This is needed when converting for 3D texture upload */
1023  SDL_ConvertColorkeyToAlpha(convert);
1024  }
1025  }
1026  SDL_SetClipRect(convert, &surface->clip_rect);
1027 
1028  /* Enable alpha blending by default if the new surface has an
1029  * alpha channel or alpha modulation */
1030  if ((surface->format->Amask && format->Amask) ||
1031  (copy_flags & SDL_COPY_MODULATE_ALPHA)) {
1033  }
1034  if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
1035  SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
1036  }
1037 
1038  /* We're ready to go! */
1039  return (convert);
1040 }
Uint8 r
Definition: SDL_blit.h:70
Uint8 b
Definition: SDL_blit.h:70
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:492
#define SDL_COPY_COLORKEY
Definition: SDL_blit.h:39
int SDL_SetSurfaceRLE(SDL_Surface *surface, int flag)
Sets the RLE acceleration hint for a surface.
Definition: SDL_surface.c:191
Uint8 g
Definition: SDL_pixels.h:296
Uint8 BytesPerPixel
Definition: SDL_pixels.h:318
Uint8 g
Definition: SDL_blit.h:70
SDL_Surface * SDL_ConvertSurface(SDL_Surface *surface, const SDL_PixelFormat *format, Uint32 flags)
Definition: SDL_surface.c:886
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
int SDL_SetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode blendMode)
Set the blend mode used for blit operations.
Definition: SDL_surface.c:426
static void SDL_ConvertColorkeyToAlpha(SDL_Surface *surface)
Definition: SDL_surface.c:275
Uint8 b
Definition: SDL_pixels.h:297
#define SDL_COPY_RLE_COLORKEY
Definition: SDL_blit.h:42
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:169
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:535
int SDL_SetColorKey(SDL_Surface *surface, int flag, Uint32 key)
Sets the color key (transparent pixel) in a blittable surface.
Definition: SDL_surface.c:212
Uint32 colorkey
Definition: SDL_blit.h:69
#define SDL_COPY_RLE_DESIRED
Definition: SDL_blit.h:41
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
#define SDL_memcpy
Uint8 r
Definition: SDL_pixels.h:295
void * pixels
Definition: SDL_surface.h:75
Uint8 a
Definition: SDL_pixels.h:298
Uint8 BitsPerPixel
Definition: SDL_pixels.h:317
int SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
Set the palette used by a surface.
Definition: SDL_surface.c:177
#define SDL_memcmp
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
SDL_Rect clip_rect
Definition: SDL_surface.h:85
void SDL_FreeSurface(SDL_Surface *surface)
Definition: SDL_surface.c:1193
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:164
SDL_bool
Definition: SDL_stdinc.h:139
SDL_Color * colors
Definition: SDL_pixels.h:305
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_SetError
SDL_Surface * SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
Definition: SDL_surface.c:114
GLbitfield flags
#define SDL_COPY_MODULATE_ALPHA
Definition: SDL_blit.h:35
int h
Definition: SDL_rect.h:67
#define SDL_COPY_RLE_ALPHAKEY
Definition: SDL_blit.h:43
#define SDL_FillRect
SDL_Palette * palette
Definition: SDL_pixels.h:316
int y
Definition: SDL_rect.h:66
#define SDL_COPY_BLEND
Definition: SDL_blit.h:36
SDL_BlitInfo info
Definition: SDL_blit.h:92
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
#define SDL_RLEACCEL
Definition: SDL_surface.h:54
Uint8 a
Definition: SDL_blit.h:70

◆ SDL_ConvertSurfaceFormat()

SDL_Surface* SDL_ConvertSurfaceFormat ( SDL_Surface surface,
Uint32  pixel_format,
Uint32  flags 
)

Definition at line 1043 of file SDL_surface.c.

References NULL, SDL_AllocFormat, SDL_ConvertSurface(), SDL_FreeFormat, and SDL_INLINE.

1045 {
1046  SDL_PixelFormat *fmt;
1047  SDL_Surface *convert = NULL;
1048 
1050  if (fmt) {
1051  convert = SDL_ConvertSurface(surface, fmt, flags);
1052  SDL_FreeFormat(fmt);
1053  }
1054  return convert;
1055 }
SDL_Surface * SDL_ConvertSurface(SDL_Surface *surface, const SDL_PixelFormat *format, Uint32 flags)
Definition: SDL_surface.c:886
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_AllocFormat
#define SDL_FreeFormat
#define NULL
Definition: begin_code.h:164
GLbitfield flags
Uint32 pixel_format
Definition: testoverlay2.c:152

◆ SDL_CreateRGBSurface()

SDL_Surface* SDL_CreateRGBSurface ( Uint32  flags,
int  width,
int  height,
int  depth,
Uint32  Rmask,
Uint32  Gmask,
Uint32  Bmask,
Uint32  Amask 
)

Allocate and free an RGB surface.

If the depth is 4 or 8 bits, an empty palette is allocated for the surface. If the depth is greater than 8 bits, the pixel format is set using the flags '[RGB]mask'.

If the function runs out of memory, it will return NULL.

Parameters
flagsThe flags are obsolete and should be set to 0.
widthThe width in pixels of the surface to create.
heightThe height in pixels of the surface to create.
depthThe depth in bits of the surface to create.
RmaskThe red mask of the surface to create.
GmaskThe green mask of the surface to create.
BmaskThe blue mask of the surface to create.
AmaskThe alpha mask of the surface to create.

Definition at line 114 of file SDL_surface.c.

References NULL, SDL_CreateRGBSurfaceWithFormat(), SDL_MasksToPixelFormatEnum, SDL_PIXELFORMAT_UNKNOWN, and SDL_SetError.

Referenced by SDL_ConvertSurface(), and SDL_CreateRGBSurfaceFrom().

117 {
118  Uint32 format;
119 
120  /* Get the pixel format */
121  format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask);
122  if (format == SDL_PIXELFORMAT_UNKNOWN) {
123  SDL_SetError("Unknown pixel format");
124  return NULL;
125  }
126 
128 }
#define SDL_MasksToPixelFormatEnum
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:169
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
SDL_Surface * SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth, Uint32 format)
Definition: SDL_surface.c:36
#define NULL
Definition: begin_code.h:164
GLint GLint GLsizei GLsizei GLsizei depth
Definition: SDL_opengl.h:1572
#define SDL_SetError
GLbitfield flags
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572

◆ SDL_CreateRGBSurfaceFrom()

SDL_Surface* SDL_CreateRGBSurfaceFrom ( void pixels,
int  width,
int  height,
int  depth,
int  pitch,
Uint32  Rmask,
Uint32  Gmask,
Uint32  Bmask,
Uint32  Amask 
)

Definition at line 134 of file SDL_surface.c.

References SDL_Surface::flags, SDL_Surface::h, NULL, SDL_Surface::pitch, SDL_Surface::pixels, SDL_CreateRGBSurface(), SDL_PREALLOC, SDL_SetClipRect(), and SDL_Surface::w.

138 {
140 
141  surface = SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
142  if (surface != NULL) {
143  surface->flags |= SDL_PREALLOC;
144  surface->pixels = pixels;
145  surface->w = width;
146  surface->h = height;
147  surface->pitch = pitch;
148  SDL_SetClipRect(surface, NULL);
149  }
150  return surface;
151 }
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:492
EGLSurface surface
Definition: eglext.h:248
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
Uint32 flags
Definition: SDL_surface.h:71
void * pixels
Definition: SDL_surface.h:75
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
#define NULL
Definition: begin_code.h:164
GLint GLint GLsizei GLsizei GLsizei depth
Definition: SDL_opengl.h:1572
SDL_Surface * SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
Definition: SDL_surface.c:114
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
#define SDL_PREALLOC
Definition: SDL_surface.h:53

◆ SDL_CreateRGBSurfaceWithFormat()

SDL_Surface* SDL_CreateRGBSurfaceWithFormat ( Uint32  flags,
int  width,
int  height,
int  depth,
Uint32  format 
)

Definition at line 36 of file SDL_surface.c.

References SDL_PixelFormat::Amask, SDL_Color::b, SDL_PixelFormat::BitsPerPixel, SDL_Palette::colors, SDL_Surface::format, SDL_PixelFormat::format, SDL_Color::g, SDL_Surface::h, SDL_Surface::map, SDL_Palette::ncolors, NULL, SDL_Surface::pitch, SDL_Surface::pixels, SDL_Color::r, SDL_Surface::refcount, SDL_AllocBlitMap(), SDL_AllocFormat, SDL_AllocPalette, SDL_BLENDMODE_BLEND, SDL_CalculatePitch(), SDL_calloc(), SDL_FreePalette, SDL_FreeSurface(), SDL_ISPIXELFORMAT_INDEXED, SDL_malloc, SDL_memset, SDL_OutOfMemory, SDL_SetClipRect(), SDL_SetSurfaceBlendMode(), SDL_SetSurfacePalette(), void, and SDL_Surface::w.

Referenced by SDL_CreateRGBSurface(), and SDL_CreateRGBSurfaceWithFormatFrom().

38 {
40 
41  /* The flags are no longer used, make the compiler happy */
42  (void)flags;
43 
44  /* Allocate the surface */
45  surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
46  if (surface == NULL) {
48  return NULL;
49  }
50 
51  surface->format = SDL_AllocFormat(format);
52  if (!surface->format) {
53  SDL_FreeSurface(surface);
54  return NULL;
55  }
56  surface->w = width;
57  surface->h = height;
58  surface->pitch = SDL_CalculatePitch(surface);
59  SDL_SetClipRect(surface, NULL);
60 
61  if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
62  SDL_Palette *palette =
63  SDL_AllocPalette((1 << surface->format->BitsPerPixel));
64  if (!palette) {
65  SDL_FreeSurface(surface);
66  return NULL;
67  }
68  if (palette->ncolors == 2) {
69  /* Create a black and white bitmap palette */
70  palette->colors[0].r = 0xFF;
71  palette->colors[0].g = 0xFF;
72  palette->colors[0].b = 0xFF;
73  palette->colors[1].r = 0x00;
74  palette->colors[1].g = 0x00;
75  palette->colors[1].b = 0x00;
76  }
77  SDL_SetSurfacePalette(surface, palette);
78  SDL_FreePalette(palette);
79  }
80 
81  /* Get the pixels */
82  if (surface->w && surface->h) {
83  surface->pixels = SDL_malloc(surface->h * surface->pitch);
84  if (!surface->pixels) {
85  SDL_FreeSurface(surface);
87  return NULL;
88  }
89  /* This is important for bitmaps */
90  SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
91  }
92 
93  /* Allocate an empty mapping */
94  surface->map = SDL_AllocBlitMap();
95  if (!surface->map) {
96  SDL_FreeSurface(surface);
97  return NULL;
98  }
99 
100  /* By default surface with an alpha mask are set up for blending */
101  if (surface->format->Amask) {
103  }
104 
105  /* The surface is ready to go */
106  surface->refcount = 1;
107  return surface;
108 }
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:492
Uint8 g
Definition: SDL_pixels.h:296
#define SDL_ISPIXELFORMAT_INDEXED(format)
Definition: SDL_pixels.h:134
EGLSurface surface
Definition: eglext.h:248
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
int SDL_SetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode blendMode)
Set the blend mode used for blit operations.
Definition: SDL_surface.c:426
Uint8 b
Definition: SDL_pixels.h:297
#define SDL_AllocFormat
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
void * SDL_calloc(size_t nmemb, size_t size)
Uint8 r
Definition: SDL_pixels.h:295
void * pixels
Definition: SDL_surface.h:75
Uint8 BitsPerPixel
Definition: SDL_pixels.h:317
int SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
Set the palette used by a surface.
Definition: SDL_surface.c:177
#define SDL_AllocPalette
void SDL_FreeSurface(SDL_Surface *surface)
Definition: SDL_surface.c:1193
#define NULL
Definition: begin_code.h:164
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_Color * colors
Definition: SDL_pixels.h:305
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_FreePalette
GLbitfield flags
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 void
SDL_BlitMap * SDL_AllocBlitMap(void)
Definition: SDL_pixels.c:961
int SDL_CalculatePitch(SDL_Surface *surface)
Definition: SDL_pixels.c:755
#define SDL_malloc
#define SDL_memset

◆ SDL_CreateRGBSurfaceWithFormatFrom()

SDL_Surface* SDL_CreateRGBSurfaceWithFormatFrom ( void pixels,
int  width,
int  height,
int  depth,
int  pitch,
Uint32  format 
)

Definition at line 158 of file SDL_surface.c.

References SDL_Surface::flags, SDL_Surface::h, NULL, SDL_Surface::pitch, SDL_Surface::pixels, SDL_CreateRGBSurfaceWithFormat(), SDL_PREALLOC, SDL_SetClipRect(), and SDL_Surface::w.

161 {
163 
164  surface = SDL_CreateRGBSurfaceWithFormat(0, 0, 0, depth, format);
165  if (surface != NULL) {
166  surface->flags |= SDL_PREALLOC;
167  surface->pixels = pixels;
168  surface->w = width;
169  surface->h = height;
170  surface->pitch = pitch;
171  SDL_SetClipRect(surface, NULL);
172  }
173  return surface;
174 }
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:492
EGLSurface surface
Definition: eglext.h:248
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
Uint32 flags
Definition: SDL_surface.h:71
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
SDL_Surface * SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth, Uint32 format)
Definition: SDL_surface.c:36
void * pixels
Definition: SDL_surface.h:75
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
#define NULL
Definition: begin_code.h:164
GLint GLint GLsizei GLsizei GLsizei depth
Definition: SDL_opengl.h:1572
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
#define SDL_PREALLOC
Definition: SDL_surface.h:53

◆ SDL_CreateSurfaceOnStack()

static SDL_INLINE SDL_bool SDL_CreateSurfaceOnStack ( int  width,
int  height,
Uint32  pixel_format,
void pixels,
int  pitch,
SDL_Surface surface,
SDL_PixelFormat format,
SDL_BlitMap blitmap 
)
static

Definition at line 1061 of file SDL_surface.c.

References SDL_BlitInfo::a, SDL_BlitInfo::b, SDL_Surface::flags, SDL_Surface::format, SDL_BlitInfo::g, SDL_Surface::h, SDL_BlitMap::info, SDL_Surface::map, SDL_Surface::pitch, SDL_Surface::pixels, SDL_BlitInfo::r, SDL_Surface::refcount, SDL_FALSE, SDL_InitFormat(), SDL_ISPIXELFORMAT_INDEXED, SDL_PREALLOC, SDL_SetError, SDL_TRUE, SDL_zerop, and SDL_Surface::w.

Referenced by SDL_ConvertPixels().

1064 {
1066  SDL_SetError("Indexed pixel formats not supported");
1067  return SDL_FALSE;
1068  }
1069  if (SDL_InitFormat(format, pixel_format) < 0) {
1070  return SDL_FALSE;
1071  }
1072 
1073  SDL_zerop(surface);
1074  surface->flags = SDL_PREALLOC;
1075  surface->format = format;
1076  surface->pixels = pixels;
1077  surface->w = width;
1078  surface->h = height;
1079  surface->pitch = pitch;
1080  /* We don't actually need to set up the clip rect for our purposes */
1081  /* SDL_SetClipRect(surface, NULL); */
1082 
1083  /* Allocate an empty mapping */
1084  SDL_zerop(blitmap);
1085  blitmap->info.r = 0xFF;
1086  blitmap->info.g = 0xFF;
1087  blitmap->info.b = 0xFF;
1088  blitmap->info.a = 0xFF;
1089  surface->map = blitmap;
1090 
1091  /* The surface is ready to go */
1092  surface->refcount = 1;
1093  return SDL_TRUE;
1094 }
Uint8 r
Definition: SDL_blit.h:70
Uint8 b
Definition: SDL_blit.h:70
#define SDL_ISPIXELFORMAT_INDEXED(format)
Definition: SDL_pixels.h:134
Uint8 g
Definition: SDL_blit.h:70
#define SDL_zerop(x)
Definition: SDL_stdinc.h:370
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
Uint32 flags
Definition: SDL_surface.h:71
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
void * pixels
Definition: SDL_surface.h:75
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
int SDL_InitFormat(SDL_PixelFormat *format, Uint32 pixel_format)
Definition: SDL_pixels.c:528
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_SetError
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
Uint32 pixel_format
Definition: testoverlay2.c:152
SDL_BlitInfo info
Definition: SDL_blit.h:92
#define SDL_PREALLOC
Definition: SDL_surface.h:53
Uint8 a
Definition: SDL_blit.h:70

◆ SDL_DuplicateSurface()

SDL_Surface* SDL_DuplicateSurface ( SDL_Surface surface)

Definition at line 877 of file SDL_surface.c.

References SDL_Surface::flags, SDL_Surface::format, and SDL_ConvertSurface().

878 {
879  return SDL_ConvertSurface(surface, surface->format, surface->flags);
880 }
SDL_Surface * SDL_ConvertSurface(SDL_Surface *surface, const SDL_PixelFormat *format, Uint32 flags)
Definition: SDL_surface.c:886
Uint32 flags
Definition: SDL_surface.h:71
SDL_PixelFormat * format
Definition: SDL_surface.h:72

◆ SDL_FreeSurface()

void SDL_FreeSurface ( SDL_Surface surface)

Definition at line 1193 of file SDL_surface.c.

References SDL_Surface::flags, SDL_Surface::format, SDL_Surface::locked, SDL_Surface::map, NULL, SDL_Surface::pixels, SDL_Surface::refcount, SDL_DONTFREE, SDL_free(), SDL_FreeBlitMap(), SDL_FreeFormat, SDL_PREALLOC, SDL_RLEACCEL, SDL_SetSurfacePalette(), SDL_UnlockSurface(), and SDL_UnRLESurface().

Referenced by SDL_ConvertSurface(), and SDL_CreateRGBSurfaceWithFormat().

1194 {
1195  if (surface == NULL) {
1196  return;
1197  }
1198  if (surface->flags & SDL_DONTFREE) {
1199  return;
1200  }
1201  if (surface->map != NULL) {
1202  SDL_FreeBlitMap(surface->map);
1203  surface->map = NULL;
1204  }
1205  if (--surface->refcount > 0) {
1206  return;
1207  }
1208  while (surface->locked > 0) {
1209  SDL_UnlockSurface(surface);
1210  }
1211  if (surface->flags & SDL_RLEACCEL) {
1212  SDL_UnRLESurface(surface, 0);
1213  }
1214  if (surface->format) {
1215  SDL_SetSurfacePalette(surface, NULL);
1216  SDL_FreeFormat(surface->format);
1217  surface->format = NULL;
1218  }
1219  if (!(surface->flags & SDL_PREALLOC)) {
1220  SDL_free(surface->pixels);
1221  }
1222  SDL_free(surface);
1223 }
void SDL_UnlockSurface(SDL_Surface *surface)
Definition: SDL_surface.c:859
#define SDL_DONTFREE
Definition: SDL_surface.h:55
void SDL_UnRLESurface(SDL_Surface *surface, int recode)
Uint32 flags
Definition: SDL_surface.h:71
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
void * pixels
Definition: SDL_surface.h:75
int SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
Set the palette used by a surface.
Definition: SDL_surface.c:177
void SDL_free(void *mem)
#define SDL_FreeFormat
#define NULL
Definition: begin_code.h:164
SDL_PixelFormat * format
Definition: SDL_surface.h:72
void SDL_FreeBlitMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:1086
#define SDL_PREALLOC
Definition: SDL_surface.h:53
#define SDL_RLEACCEL
Definition: SDL_surface.h:54

◆ SDL_GetClipRect()

void SDL_GetClipRect ( SDL_Surface surface,
SDL_Rect rect 
)

Gets the clipping rectangle for the destination surface in a blit.

rect must be a pointer to a valid rectangle which will be filled with the correct values.

Definition at line 516 of file SDL_surface.c.

References SDL_Surface::clip_rect.

517 {
518  if (surface && rect) {
519  *rect = surface->clip_rect;
520  }
521 }
SDL_Rect clip_rect
Definition: SDL_surface.h:85

◆ SDL_GetColorKey()

int SDL_GetColorKey ( SDL_Surface surface,
Uint32 key 
)

Gets the color key (transparent pixel) in a blittable surface.

Parameters
surfaceThe surface to update
keyA pointer filled in with the transparent pixel in the native surface format
Returns
0 on success, or -1 if the surface is not valid or colorkey is not enabled.

Definition at line 257 of file SDL_surface.c.

References SDL_BlitInfo::colorkey, SDL_BlitInfo::flags, SDL_BlitMap::info, SDL_Surface::map, and SDL_COPY_COLORKEY.

258 {
259  if (!surface) {
260  return -1;
261  }
262 
263  if (!(surface->map->info.flags & SDL_COPY_COLORKEY)) {
264  return -1;
265  }
266 
267  if (key) {
268  *key = surface->map->info.colorkey;
269  }
270  return 0;
271 }
#define SDL_COPY_COLORKEY
Definition: SDL_blit.h:39
Uint32 colorkey
Definition: SDL_blit.h:69
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLuint64 key
Definition: gl2ext.h:2192
SDL_BlitInfo info
Definition: SDL_blit.h:92

◆ SDL_GetSurfaceAlphaMod()

int SDL_GetSurfaceAlphaMod ( SDL_Surface surface,
Uint8 alpha 
)

Get the additional alpha value used in blit operations.

Parameters
surfaceThe surface to query.
alphaA pointer filled in with the current alpha value.
Returns
0 on success, or -1 if the surface is not valid.
See also
SDL_SetSurfaceAlphaMod()

Definition at line 413 of file SDL_surface.c.

References SDL_BlitInfo::a, SDL_BlitMap::info, and SDL_Surface::map.

414 {
415  if (!surface) {
416  return -1;
417  }
418 
419  if (alpha) {
420  *alpha = surface->map->info.a;
421  }
422  return 0;
423 }
GLfloat GLfloat GLfloat alpha
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
SDL_BlitInfo info
Definition: SDL_blit.h:92
Uint8 a
Definition: SDL_blit.h:70

◆ SDL_GetSurfaceBlendMode()

int SDL_GetSurfaceBlendMode ( SDL_Surface surface,
SDL_BlendMode blendMode 
)

Get the blend mode used for blit operations.

Parameters
surfaceThe surface to query.
blendModeA pointer filled in with the current blend mode.
Returns
0 on success, or -1 if the surface is not valid.
See also
SDL_SetSurfaceBlendMode()

Definition at line 463 of file SDL_surface.c.

References SDL_Surface::map, SDL_BLENDMODE_ADD, SDL_BLENDMODE_BLEND, SDL_BLENDMODE_MOD, SDL_BLENDMODE_NONE, SDL_COPY_ADD, SDL_COPY_BLEND, and SDL_COPY_MOD.

464 {
465  if (!surface) {
466  return -1;
467  }
468 
469  if (!blendMode) {
470  return 0;
471  }
472 
473  switch (surface->map->
474  info.flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD)) {
475  case SDL_COPY_BLEND:
477  break;
478  case SDL_COPY_ADD:
480  break;
481  case SDL_COPY_MOD:
483  break;
484  default:
486  break;
487  }
488  return 0;
489 }
#define SDL_COPY_MOD
Definition: SDL_blit.h:38
#define SDL_COPY_ADD
Definition: SDL_blit.h:37
static SDL_BlendMode blendMode
Definition: testdraw2.c:34
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
#define SDL_COPY_BLEND
Definition: SDL_blit.h:36

◆ SDL_GetSurfaceColorMod()

int SDL_GetSurfaceColorMod ( SDL_Surface surface,
Uint8 r,
Uint8 g,
Uint8 b 
)

Get the additional color value used in blit operations.

Parameters
surfaceThe surface to query.
rA pointer filled in with the current red color value.
gA pointer filled in with the current green color value.
bA pointer filled in with the current blue color value.
Returns
0 on success, or -1 if the surface is not valid.
See also
SDL_SetSurfaceColorMod()

Definition at line 371 of file SDL_surface.c.

References SDL_BlitInfo::b, SDL_BlitInfo::g, SDL_BlitMap::info, SDL_Surface::map, and SDL_BlitInfo::r.

372 {
373  if (!surface) {
374  return -1;
375  }
376 
377  if (r) {
378  *r = surface->map->info.r;
379  }
380  if (g) {
381  *g = surface->map->info.g;
382  }
383  if (b) {
384  *b = surface->map->info.b;
385  }
386  return 0;
387 }
Uint8 r
Definition: SDL_blit.h:70
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
Uint8 b
Definition: SDL_blit.h:70
Uint8 g
Definition: SDL_blit.h:70
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLboolean GLboolean g
GLboolean GLboolean GLboolean b
SDL_BlitInfo info
Definition: SDL_blit.h:92

◆ SDL_LockSurface()

int SDL_LockSurface ( SDL_Surface surface)

Sets up a surface for directly accessing the pixels.

Between calls to SDL_LockSurface() / SDL_UnlockSurface(), you can write to and read from surface->pixels, using the pixel format stored in surface->format. Once you are done accessing the surface, you should use SDL_UnlockSurface() to release it.

Not all surfaces require locking. If SDL_MUSTLOCK(surface) evaluates to 0, then you can read and write to the surface at any time, and the pixel format of the surface will not change.

No operating system or library calls should be made between lock/unlock pairs, as critical system locks may be held during this time.

SDL_LockSurface() returns 0, or -1 if the surface couldn't be locked.

See also
SDL_UnlockSurface()

Definition at line 838 of file SDL_surface.c.

References SDL_Surface::flags, SDL_Surface::locked, SDL_RLEACCEL, and SDL_UnRLESurface().

Referenced by SDL_ConvertColorkeyToAlpha().

839 {
840  if (!surface->locked) {
841  /* Perform the lock */
842  if (surface->flags & SDL_RLEACCEL) {
843  SDL_UnRLESurface(surface, 1);
844  surface->flags |= SDL_RLEACCEL; /* save accel'd state */
845  }
846  }
847 
848  /* Increment the surface lock count, for recursive locks */
849  ++surface->locked;
850 
851  /* Ready to go.. */
852  return (0);
853 }
void SDL_UnRLESurface(SDL_Surface *surface, int recode)
Uint32 flags
Definition: SDL_surface.h:71
#define SDL_RLEACCEL
Definition: SDL_surface.h:54

◆ SDL_LowerBlit()

int SDL_LowerBlit ( SDL_Surface src,
SDL_Rect srcrect,
SDL_Surface dst,
SDL_Rect dstrect 
)

This is a semi-private blit function and it performs low-level surface blitting only.

Definition at line 535 of file SDL_surface.c.

References SDL_BlitMap::blit, SDL_BlitMap::dst, SDL_BlitMap::dst_palette_version, SDL_Surface::format, SDL_Surface::map, SDL_PixelFormat::palette, SDL_MapSurface(), SDL_BlitMap::src_palette_version, and SDL_Palette::version.

Referenced by SDL_ConvertPixels(), SDL_ConvertSurface(), SDL_LowerBlitScaled(), and SDL_UpperBlit().

537 {
538  /* Check to make sure the blit mapping is valid */
539  if ((src->map->dst != dst) ||
540  (dst->format->palette &&
541  src->map->dst_palette_version != dst->format->palette->version) ||
542  (src->format->palette &&
543  src->map->src_palette_version != src->format->palette->version)) {
544  if (SDL_MapSurface(src, dst) < 0) {
545  return (-1);
546  }
547  /* just here for debugging */
548 /* printf */
549 /* ("src = 0x%08X src->flags = %08X src->map->info.flags = %08x\ndst = 0x%08X dst->flags = %08X dst->map->info.flags = %08X\nsrc->map->blit = 0x%08x\n", */
550 /* src, dst->flags, src->map->info.flags, dst, dst->flags, */
551 /* dst->map->info.flags, src->map->blit); */
552  }
553  return (src->map->blit(src, srcrect, dst, dstrect));
554 }
Uint32 version
Definition: SDL_pixels.h:306
SDL_blit blit
Definition: SDL_blit.h:90
Uint32 dst_palette_version
Definition: SDL_blit.h:96
Uint32 src_palette_version
Definition: SDL_blit.h:97
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
int SDL_MapSurface(SDL_Surface *src, SDL_Surface *dst)
Definition: SDL_pixels.c:1000
SDL_Surface * dst
Definition: SDL_blit.h:88
SDL_PixelFormat * format
Definition: SDL_surface.h:72
SDL_Palette * palette
Definition: SDL_pixels.h:316

◆ SDL_LowerBlitScaled()

int SDL_LowerBlitScaled ( SDL_Surface src,
SDL_Rect srcrect,
SDL_Surface dst,
SDL_Rect dstrect 
)

This is a semi-private blit function and it performs low-level surface scaled blitting only.

Definition at line 811 of file SDL_surface.c.

References SDL_BlitInfo::flags, SDL_Surface::format, SDL_PixelFormat::format, SDL_BlitMap::info, SDL_Surface::map, SDL_COPY_ADD, SDL_COPY_BLEND, SDL_COPY_COLORKEY, SDL_COPY_MOD, SDL_COPY_MODULATE_ALPHA, SDL_COPY_MODULATE_COLOR, SDL_COPY_NEAREST, SDL_InvalidateMap(), SDL_ISPIXELFORMAT_INDEXED, SDL_LowerBlit(), and SDL_SoftStretch.

Referenced by SDL_UpperBlitScaled().

813 {
814  static const Uint32 complex_copy_flags = (
818  );
819 
820  if (!(src->map->info.flags & SDL_COPY_NEAREST)) {
821  src->map->info.flags |= SDL_COPY_NEAREST;
822  SDL_InvalidateMap(src->map);
823  }
824 
825  if ( !(src->map->info.flags & complex_copy_flags) &&
826  src->format->format == dst->format->format &&
828  return SDL_SoftStretch( src, srcrect, dst, dstrect );
829  } else {
830  return SDL_LowerBlit( src, srcrect, dst, dstrect );
831  }
832 }
#define SDL_COPY_MODULATE_COLOR
Definition: SDL_blit.h:34
#define SDL_COPY_COLORKEY
Definition: SDL_blit.h:39
#define SDL_SoftStretch
#define SDL_ISPIXELFORMAT_INDEXED(format)
Definition: SDL_pixels.h:134
#define SDL_COPY_MOD
Definition: SDL_blit.h:38
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:169
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:535
#define SDL_COPY_ADD
Definition: SDL_blit.h:37
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
#define SDL_COPY_NEAREST
Definition: SDL_blit.h:40
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_COPY_MODULATE_ALPHA
Definition: SDL_blit.h:35
#define SDL_COPY_BLEND
Definition: SDL_blit.h:36
SDL_BlitInfo info
Definition: SDL_blit.h:92

◆ SDL_SetClipRect()

SDL_bool SDL_SetClipRect ( SDL_Surface surface,
const SDL_Rect rect 
)

Sets the clipping rectangle for the destination surface in a blit.

If the clip rectangle is NULL, clipping will be disabled.

If the clip rectangle doesn't intersect the surface, the function will return SDL_FALSE and blits will be completely clipped. Otherwise the function returns SDL_TRUE and blits to the surface will be clipped to the intersection of the surface area and the clipping rectangle.

Note that blits are automatically clipped to the edges of the source and destination surfaces.

Definition at line 492 of file SDL_surface.c.

References SDL_Surface::clip_rect, SDL_Rect::h, SDL_Surface::h, SDL_FALSE, SDL_IntersectRect, SDL_TRUE, SDL_Rect::w, SDL_Surface::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by SDL_ConvertSurface(), SDL_CreateRGBSurfaceFrom(), SDL_CreateRGBSurfaceWithFormat(), and SDL_CreateRGBSurfaceWithFormatFrom().

493 {
494  SDL_Rect full_rect;
495 
496  /* Don't do anything if there's no surface to act on */
497  if (!surface) {
498  return SDL_FALSE;
499  }
500 
501  /* Set up the full surface rectangle */
502  full_rect.x = 0;
503  full_rect.y = 0;
504  full_rect.w = surface->w;
505  full_rect.h = surface->h;
506 
507  /* Set the clipping rectangle */
508  if (!rect) {
509  surface->clip_rect = full_rect;
510  return SDL_TRUE;
511  }
512  return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
513 }
#define SDL_IntersectRect
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
SDL_Rect clip_rect
Definition: SDL_surface.h:85
int h
Definition: SDL_rect.h:67
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64

◆ SDL_SetColorKey()

int SDL_SetColorKey ( SDL_Surface surface,
int  flag,
Uint32  key 
)

Sets the color key (transparent pixel) in a blittable surface.

Parameters
surfaceThe surface to update
flagNon-zero to enable colorkey and 0 to disable colorkey
keyThe transparent pixel in the native surface format
Returns
0 on success, or -1 if the surface is not valid

You can pass SDL_RLEACCEL to enable RLE accelerated blits.

Definition at line 212 of file SDL_surface.c.

References SDL_Color::a, SDL_BlitInfo::colorkey, SDL_Palette::colors, SDL_BlitInfo::flags, SDL_Surface::format, SDL_BlitMap::info, SDL_Surface::map, SDL_Palette::ncolors, SDL_PixelFormat::palette, SDL_ALPHA_OPAQUE, SDL_ALPHA_TRANSPARENT, SDL_COPY_COLORKEY, SDL_InvalidateMap(), SDL_InvalidParamError, SDL_RLEACCEL, SDL_SetSurfaceRLE(), and SDL_Palette::version.

Referenced by SDL_ConvertColorkeyToAlpha(), and SDL_ConvertSurface().

213 {
214  int flags;
215 
216  if (!surface) {
217  return SDL_InvalidParamError("surface");
218  }
219 
220  if (surface->format->palette && key >= ((Uint32) surface->format->palette->ncolors)) {
221  return SDL_InvalidParamError("key");
222  }
223 
224  if (flag & SDL_RLEACCEL) {
225  SDL_SetSurfaceRLE(surface, 1);
226  }
227 
228  flags = surface->map->info.flags;
229  if (flag) {
230  surface->map->info.flags |= SDL_COPY_COLORKEY;
231  surface->map->info.colorkey = key;
232  if (surface->format->palette) {
233  surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_TRANSPARENT;
234  ++surface->format->palette->version;
235  if (!surface->format->palette->version) {
236  surface->format->palette->version = 1;
237  }
238  }
239  } else {
240  if (surface->format->palette) {
241  surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_OPAQUE;
242  ++surface->format->palette->version;
243  if (!surface->format->palette->version) {
244  surface->format->palette->version = 1;
245  }
246  }
247  surface->map->info.flags &= ~SDL_COPY_COLORKEY;
248  }
249  if (surface->map->info.flags != flags) {
250  SDL_InvalidateMap(surface->map);
251  }
252 
253  return 0;
254 }
Uint32 version
Definition: SDL_pixels.h:306
#define SDL_COPY_COLORKEY
Definition: SDL_blit.h:39
int SDL_SetSurfaceRLE(SDL_Surface *surface, int flag)
Sets the RLE acceleration hint for a surface.
Definition: SDL_surface.c:191
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:169
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
Uint32 colorkey
Definition: SDL_blit.h:69
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
#define SDL_ALPHA_TRANSPARENT
Definition: SDL_pixels.h:47
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLuint64 key
Definition: gl2ext.h:2192
Uint8 a
Definition: SDL_pixels.h:298
SDL_Color * colors
Definition: SDL_pixels.h:305
SDL_PixelFormat * format
Definition: SDL_surface.h:72
GLbitfield flags
SDL_Palette * palette
Definition: SDL_pixels.h:316
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
SDL_BlitInfo info
Definition: SDL_blit.h:92
#define SDL_RLEACCEL
Definition: SDL_surface.h:54

◆ SDL_SetSurfaceAlphaMod()

int SDL_SetSurfaceAlphaMod ( SDL_Surface surface,
Uint8  alpha 
)

Set an additional alpha value used in blit operations.

Parameters
surfaceThe surface to update.
alphaThe alpha value multiplied into blit operations.
Returns
0 on success, or -1 if the surface is not valid.
See also
SDL_GetSurfaceAlphaMod()

Definition at line 390 of file SDL_surface.c.

References SDL_BlitInfo::a, SDL_BlitInfo::flags, SDL_BlitMap::info, SDL_Surface::map, SDL_COPY_MODULATE_ALPHA, and SDL_InvalidateMap().

391 {
392  int flags;
393 
394  if (!surface) {
395  return -1;
396  }
397 
398  surface->map->info.a = alpha;
399 
400  flags = surface->map->info.flags;
401  if (alpha != 0xFF) {
402  surface->map->info.flags |= SDL_COPY_MODULATE_ALPHA;
403  } else {
404  surface->map->info.flags &= ~SDL_COPY_MODULATE_ALPHA;
405  }
406  if (surface->map->info.flags != flags) {
407  SDL_InvalidateMap(surface->map);
408  }
409  return 0;
410 }
GLfloat GLfloat GLfloat alpha
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLbitfield flags
#define SDL_COPY_MODULATE_ALPHA
Definition: SDL_blit.h:35
SDL_BlitInfo info
Definition: SDL_blit.h:92
Uint8 a
Definition: SDL_blit.h:70

◆ SDL_SetSurfaceBlendMode()

int SDL_SetSurfaceBlendMode ( SDL_Surface surface,
SDL_BlendMode  blendMode 
)

Set the blend mode used for blit operations.

Parameters
surfaceThe surface to update.
blendModeSDL_BlendMode to use for blit blending.
Returns
0 on success, or -1 if the parameters are not valid.
See also
SDL_GetSurfaceBlendMode()

Definition at line 426 of file SDL_surface.c.

References SDL_BlitInfo::flags, SDL_BlitMap::info, SDL_Surface::map, SDL_BLENDMODE_ADD, SDL_BLENDMODE_BLEND, SDL_BLENDMODE_MOD, SDL_BLENDMODE_NONE, SDL_COPY_ADD, SDL_COPY_BLEND, SDL_COPY_MOD, SDL_InvalidateMap(), and SDL_Unsupported.

Referenced by SDL_ConvertColorkeyToAlpha(), SDL_ConvertSurface(), and SDL_CreateRGBSurfaceWithFormat().

427 {
428  int flags, status;
429 
430  if (!surface) {
431  return -1;
432  }
433 
434  status = 0;
435  flags = surface->map->info.flags;
436  surface->map->info.flags &=
438  switch (blendMode) {
439  case SDL_BLENDMODE_NONE:
440  break;
441  case SDL_BLENDMODE_BLEND:
442  surface->map->info.flags |= SDL_COPY_BLEND;
443  break;
444  case SDL_BLENDMODE_ADD:
445  surface->map->info.flags |= SDL_COPY_ADD;
446  break;
447  case SDL_BLENDMODE_MOD:
448  surface->map->info.flags |= SDL_COPY_MOD;
449  break;
450  default:
451  status = SDL_Unsupported();
452  break;
453  }
454 
455  if (surface->map->info.flags != flags) {
456  SDL_InvalidateMap(surface->map);
457  }
458 
459  return status;
460 }
#define SDL_COPY_MOD
Definition: SDL_blit.h:38
#define SDL_COPY_ADD
Definition: SDL_blit.h:37
static SDL_BlendMode blendMode
Definition: testdraw2.c:34
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLbitfield flags
#define SDL_Unsupported()
Definition: SDL_error.h:53
#define SDL_COPY_BLEND
Definition: SDL_blit.h:36
SDL_BlitInfo info
Definition: SDL_blit.h:92

◆ SDL_SetSurfaceColorMod()

int SDL_SetSurfaceColorMod ( SDL_Surface surface,
Uint8  r,
Uint8  g,
Uint8  b 
)

Set an additional color value used in blit operations.

Parameters
surfaceThe surface to update.
rThe red color value multiplied into blit operations.
gThe green color value multiplied into blit operations.
bThe blue color value multiplied into blit operations.
Returns
0 on success, or -1 if the surface is not valid.
See also
SDL_GetSurfaceColorMod()

Definition at line 345 of file SDL_surface.c.

References SDL_BlitInfo::b, SDL_BlitInfo::flags, SDL_BlitInfo::g, SDL_BlitMap::info, SDL_Surface::map, SDL_BlitInfo::r, SDL_COPY_MODULATE_COLOR, and SDL_InvalidateMap().

346 {
347  int flags;
348 
349  if (!surface) {
350  return -1;
351  }
352 
353  surface->map->info.r = r;
354  surface->map->info.g = g;
355  surface->map->info.b = b;
356 
357  flags = surface->map->info.flags;
358  if (r != 0xFF || g != 0xFF || b != 0xFF) {
359  surface->map->info.flags |= SDL_COPY_MODULATE_COLOR;
360  } else {
361  surface->map->info.flags &= ~SDL_COPY_MODULATE_COLOR;
362  }
363  if (surface->map->info.flags != flags) {
364  SDL_InvalidateMap(surface->map);
365  }
366  return 0;
367 }
Uint8 r
Definition: SDL_blit.h:70
#define SDL_COPY_MODULATE_COLOR
Definition: SDL_blit.h:34
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
Uint8 b
Definition: SDL_blit.h:70
Uint8 g
Definition: SDL_blit.h:70
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLbitfield flags
GLboolean GLboolean g
GLboolean GLboolean GLboolean b
SDL_BlitInfo info
Definition: SDL_blit.h:92

◆ SDL_SetSurfacePalette()

int SDL_SetSurfacePalette ( SDL_Surface surface,
SDL_Palette palette 
)

Set the palette used by a surface.

Returns
0, or -1 if the surface format doesn't use a palette.
Note
A single palette can be shared with many surfaces.

Definition at line 177 of file SDL_surface.c.

References SDL_Surface::format, SDL_Surface::map, SDL_InvalidateMap(), SDL_SetError, and SDL_SetPixelFormatPalette.

Referenced by SDL_ConvertSurface(), SDL_CreateRGBSurfaceWithFormat(), and SDL_FreeSurface().

178 {
179  if (!surface) {
180  return SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
181  }
182  if (SDL_SetPixelFormatPalette(surface->format, palette) < 0) {
183  return -1;
184  }
185  SDL_InvalidateMap(surface->map);
186 
187  return 0;
188 }
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_SetError
#define SDL_SetPixelFormatPalette

◆ SDL_SetSurfaceRLE()

int SDL_SetSurfaceRLE ( SDL_Surface surface,
int  flag 
)

Sets the RLE acceleration hint for a surface.

Returns
0 on success, or -1 if the surface is not valid
Note
If RLE is enabled, colorkey and alpha blending blits are much faster, but the surface must be locked before directly accessing the pixels.

Definition at line 191 of file SDL_surface.c.

References SDL_BlitInfo::flags, SDL_BlitMap::info, SDL_Surface::map, SDL_COPY_RLE_DESIRED, and SDL_InvalidateMap().

Referenced by SDL_ConvertSurface(), and SDL_SetColorKey().

192 {
193  int flags;
194 
195  if (!surface) {
196  return -1;
197  }
198 
199  flags = surface->map->info.flags;
200  if (flag) {
201  surface->map->info.flags |= SDL_COPY_RLE_DESIRED;
202  } else {
203  surface->map->info.flags &= ~SDL_COPY_RLE_DESIRED;
204  }
205  if (surface->map->info.flags != flags) {
206  SDL_InvalidateMap(surface->map);
207  }
208  return 0;
209 }
#define SDL_COPY_RLE_DESIRED
Definition: SDL_blit.h:41
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLbitfield flags
SDL_BlitInfo info
Definition: SDL_blit.h:92

◆ SDL_UnlockSurface()

void SDL_UnlockSurface ( SDL_Surface surface)
See also
SDL_LockSurface()

Definition at line 859 of file SDL_surface.c.

References SDL_Surface::flags, SDL_Surface::locked, SDL_RLEACCEL, and SDL_RLESurface().

Referenced by SDL_ConvertColorkeyToAlpha(), and SDL_FreeSurface().

860 {
861  /* Only perform an unlock if we are locked */
862  if (!surface->locked || (--surface->locked > 0)) {
863  return;
864  }
865 
866  /* Update RLE encoded surface with new data */
867  if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
868  surface->flags &= ~SDL_RLEACCEL; /* stop lying */
869  SDL_RLESurface(surface);
870  }
871 }
Uint32 flags
Definition: SDL_surface.h:71
int SDL_RLESurface(SDL_Surface *surface)
#define SDL_RLEACCEL
Definition: SDL_surface.h:54

◆ SDL_UpperBlit()

int SDL_UpperBlit ( SDL_Surface src,
const SDL_Rect srcrect,
SDL_Surface dst,
SDL_Rect dstrect 
)

This is the public blit function, SDL_BlitSurface(), and it performs rectangle validation and clipping before passing it to SDL_LowerBlit()

Definition at line 558 of file SDL_surface.c.

References SDL_Surface::clip_rect, SDL_BlitInfo::flags, SDL_Rect::h, SDL_Surface::h, SDL_BlitMap::info, SDL_Surface::locked, SDL_Surface::map, NULL, SDL_COPY_NEAREST, SDL_InvalidateMap(), SDL_LowerBlit(), SDL_SetError, SDL_Rect::w, SDL_Surface::w, SDL_Rect::x, and SDL_Rect::y.

560 {
561  SDL_Rect fulldst;
562  int srcx, srcy, w, h;
563 
564  /* Make sure the surfaces aren't locked */
565  if (!src || !dst) {
566  return SDL_SetError("SDL_UpperBlit: passed a NULL surface");
567  }
568  if (src->locked || dst->locked) {
569  return SDL_SetError("Surfaces must not be locked during blit");
570  }
571 
572  /* If the destination rectangle is NULL, use the entire dest surface */
573  if (dstrect == NULL) {
574  fulldst.x = fulldst.y = 0;
575  fulldst.w = dst->w;
576  fulldst.h = dst->h;
577  dstrect = &fulldst;
578  }
579 
580  /* clip the source rectangle to the source surface */
581  if (srcrect) {
582  int maxw, maxh;
583 
584  srcx = srcrect->x;
585  w = srcrect->w;
586  if (srcx < 0) {
587  w += srcx;
588  dstrect->x -= srcx;
589  srcx = 0;
590  }
591  maxw = src->w - srcx;
592  if (maxw < w)
593  w = maxw;
594 
595  srcy = srcrect->y;
596  h = srcrect->h;
597  if (srcy < 0) {
598  h += srcy;
599  dstrect->y -= srcy;
600  srcy = 0;
601  }
602  maxh = src->h - srcy;
603  if (maxh < h)
604  h = maxh;
605 
606  } else {
607  srcx = srcy = 0;
608  w = src->w;
609  h = src->h;
610  }
611 
612  /* clip the destination rectangle against the clip rectangle */
613  {
614  SDL_Rect *clip = &dst->clip_rect;
615  int dx, dy;
616 
617  dx = clip->x - dstrect->x;
618  if (dx > 0) {
619  w -= dx;
620  dstrect->x += dx;
621  srcx += dx;
622  }
623  dx = dstrect->x + w - clip->x - clip->w;
624  if (dx > 0)
625  w -= dx;
626 
627  dy = clip->y - dstrect->y;
628  if (dy > 0) {
629  h -= dy;
630  dstrect->y += dy;
631  srcy += dy;
632  }
633  dy = dstrect->y + h - clip->y - clip->h;
634  if (dy > 0)
635  h -= dy;
636  }
637 
638  /* Switch back to a fast blit if we were previously stretching */
639  if (src->map->info.flags & SDL_COPY_NEAREST) {
640  src->map->info.flags &= ~SDL_COPY_NEAREST;
641  SDL_InvalidateMap(src->map);
642  }
643 
644  if (w > 0 && h > 0) {
645  SDL_Rect sr;
646  sr.x = srcx;
647  sr.y = srcy;
648  sr.w = dstrect->w = w;
649  sr.h = dstrect->h = h;
650  return SDL_LowerBlit(src, &sr, dst, dstrect);
651  }
652  dstrect->w = dstrect->h = 0;
653  return 0;
654 }
GLfloat GLfloat GLfloat GLfloat h
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:535
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
#define SDL_COPY_NEAREST
Definition: SDL_blit.h:40
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLubyte GLubyte GLubyte GLubyte w
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
SDL_Rect clip_rect
Definition: SDL_surface.h:85
#define NULL
Definition: begin_code.h:164
#define SDL_SetError
int h
Definition: SDL_rect.h:67
int y
Definition: SDL_rect.h:66
SDL_BlitInfo info
Definition: SDL_blit.h:92
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64

◆ SDL_UpperBlitScaled()

int SDL_UpperBlitScaled ( SDL_Surface src,
const SDL_Rect srcrect,
SDL_Surface dst,
SDL_Rect dstrect 
)

This is the public scaled blit function, SDL_BlitScaled(), and it performs rectangle validation and clipping before passing it to SDL_LowerBlitScaled()

Definition at line 657 of file SDL_surface.c.

References SDL_Surface::clip_rect, SDL_Rect::h, SDL_Surface::h, SDL_Surface::locked, NULL, SDL_BlitSurface, SDL_floor, SDL_LowerBlitScaled(), SDL_SetError, SDL_Rect::w, SDL_Surface::w, SDL_Rect::x, and SDL_Rect::y.

659 {
660  double src_x0, src_y0, src_x1, src_y1;
661  double dst_x0, dst_y0, dst_x1, dst_y1;
662  SDL_Rect final_src, final_dst;
663  double scaling_w, scaling_h;
664  int src_w, src_h;
665  int dst_w, dst_h;
666 
667  /* Make sure the surfaces aren't locked */
668  if (!src || !dst) {
669  return SDL_SetError("SDL_UpperBlitScaled: passed a NULL surface");
670  }
671  if (src->locked || dst->locked) {
672  return SDL_SetError("Surfaces must not be locked during blit");
673  }
674 
675  if (NULL == srcrect) {
676  src_w = src->w;
677  src_h = src->h;
678  } else {
679  src_w = srcrect->w;
680  src_h = srcrect->h;
681  }
682 
683  if (NULL == dstrect) {
684  dst_w = dst->w;
685  dst_h = dst->h;
686  } else {
687  dst_w = dstrect->w;
688  dst_h = dstrect->h;
689  }
690 
691  if (dst_w == src_w && dst_h == src_h) {
692  /* No scaling, defer to regular blit */
693  return SDL_BlitSurface(src, srcrect, dst, dstrect);
694  }
695 
696  scaling_w = (double)dst_w / src_w;
697  scaling_h = (double)dst_h / src_h;
698 
699  if (NULL == dstrect) {
700  dst_x0 = 0;
701  dst_y0 = 0;
702  dst_x1 = dst_w - 1;
703  dst_y1 = dst_h - 1;
704  } else {
705  dst_x0 = dstrect->x;
706  dst_y0 = dstrect->y;
707  dst_x1 = dst_x0 + dst_w - 1;
708  dst_y1 = dst_y0 + dst_h - 1;
709  }
710 
711  if (NULL == srcrect) {
712  src_x0 = 0;
713  src_y0 = 0;
714  src_x1 = src_w - 1;
715  src_y1 = src_h - 1;
716  } else {
717  src_x0 = srcrect->x;
718  src_y0 = srcrect->y;
719  src_x1 = src_x0 + src_w - 1;
720  src_y1 = src_y0 + src_h - 1;
721 
722  /* Clip source rectangle to the source surface */
723 
724  if (src_x0 < 0) {
725  dst_x0 -= src_x0 * scaling_w;
726  src_x0 = 0;
727  }
728 
729  if (src_x1 >= src->w) {
730  dst_x1 -= (src_x1 - src->w + 1) * scaling_w;
731  src_x1 = src->w - 1;
732  }
733 
734  if (src_y0 < 0) {
735  dst_y0 -= src_y0 * scaling_h;
736  src_y0 = 0;
737  }
738 
739  if (src_y1 >= src->h) {
740  dst_y1 -= (src_y1 - src->h + 1) * scaling_h;
741  src_y1 = src->h - 1;
742  }
743  }
744 
745  /* Clip destination rectangle to the clip rectangle */
746 
747  /* Translate to clip space for easier calculations */
748  dst_x0 -= dst->clip_rect.x;
749  dst_x1 -= dst->clip_rect.x;
750  dst_y0 -= dst->clip_rect.y;
751  dst_y1 -= dst->clip_rect.y;
752 
753  if (dst_x0 < 0) {
754  src_x0 -= dst_x0 / scaling_w;
755  dst_x0 = 0;
756  }
757 
758  if (dst_x1 >= dst->clip_rect.w) {
759  src_x1 -= (dst_x1 - dst->clip_rect.w + 1) / scaling_w;
760  dst_x1 = dst->clip_rect.w - 1;
761  }
762 
763  if (dst_y0 < 0) {
764  src_y0 -= dst_y0 / scaling_h;
765  dst_y0 = 0;
766  }
767 
768  if (dst_y1 >= dst->clip_rect.h) {
769  src_y1 -= (dst_y1 - dst->clip_rect.h + 1) / scaling_h;
770  dst_y1 = dst->clip_rect.h - 1;
771  }
772 
773  /* Translate back to surface coordinates */
774  dst_x0 += dst->clip_rect.x;
775  dst_x1 += dst->clip_rect.x;
776  dst_y0 += dst->clip_rect.y;
777  dst_y1 += dst->clip_rect.y;
778 
779  final_src.x = (int)SDL_floor(src_x0 + 0.5);
780  final_src.y = (int)SDL_floor(src_y0 + 0.5);
781  final_src.w = (int)SDL_floor(src_x1 + 1 + 0.5) - (int)SDL_floor(src_x0 + 0.5);
782  final_src.h = (int)SDL_floor(src_y1 + 1 + 0.5) - (int)SDL_floor(src_y0 + 0.5);
783 
784  final_dst.x = (int)SDL_floor(dst_x0 + 0.5);
785  final_dst.y = (int)SDL_floor(dst_y0 + 0.5);
786  final_dst.w = (int)SDL_floor(dst_x1 - dst_x0 + 1.5);
787  final_dst.h = (int)SDL_floor(dst_y1 - dst_y0 + 1.5);
788 
789  if (final_dst.w < 0)
790  final_dst.w = 0;
791  if (final_dst.h < 0)
792  final_dst.h = 0;
793 
794  if (dstrect)
795  *dstrect = final_dst;
796 
797  if (final_dst.w == 0 || final_dst.h == 0 ||
798  final_src.w <= 0 || final_src.h <= 0) {
799  /* No-op. */
800  return 0;
801  }
802 
803  return SDL_LowerBlitScaled(src, &final_src, dst, &final_dst);
804 }
#define SDL_BlitSurface
Definition: SDL_surface.h:465
#define SDL_floor
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
SDL_Rect clip_rect
Definition: SDL_surface.h:85
#define NULL
Definition: begin_code.h:164
#define SDL_SetError
int h
Definition: SDL_rect.h:67
int SDL_LowerBlitScaled(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:811
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64