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 "SDL_yuv_c.h"
+ Include dependency graph for SDL_surface.c:

Go to the source code of this file.

Functions

 SDL_COMPILE_TIME_ASSERT (surface_size_assumptions, sizeof(int)==sizeof(Sint32) &&sizeof(size_t) >=sizeof(Sint32))
 
int SDL_CalculatePitch (Uint32 format, int width)
 
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_CalculatePitch()

int SDL_CalculatePitch ( Uint32  format,
int  width 
)

Definition at line 41 of file SDL_surface.c.

References SDL_BITSPERPIXEL, and SDL_BYTESPERPIXEL.

Referenced by SDL_CreateRGBSurfaceWithFormat().

42 {
43  int pitch;
44 
45  /* Surface should be 4-byte aligned for speed */
46  pitch = width * SDL_BYTESPERPIXEL(format);
47  switch (SDL_BITSPERPIXEL(format)) {
48  case 1:
49  pitch = (pitch + 7) / 8;
50  break;
51  case 4:
52  pitch = (pitch + 1) / 2;
53  break;
54  default:
55  break;
56  }
57  pitch = (pitch + 3) & ~3; /* 4-byte aligning */
58  return pitch;
59 }
#define SDL_BYTESPERPIXEL(X)
Definition: SDL_pixels.h:128
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
#define SDL_BITSPERPIXEL(X)
Definition: SDL_pixels.h:127

◆ SDL_COMPILE_TIME_ASSERT()

SDL_COMPILE_TIME_ASSERT ( surface_size_assumptions  ,
sizeof(int)  = =sizeof(Sint32) &&sizeof(size_t) >=sizeof(Sint32) 
)

◆ SDL_ConvertColorkeyToAlpha()

static void SDL_ConvertColorkeyToAlpha ( SDL_Surface surface)
static

Definition at line 314 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().

315 {
316  int x, y;
317 
318  if (!surface) {
319  return;
320  }
321 
322  if (!(surface->map->info.flags & SDL_COPY_COLORKEY) ||
323  !surface->format->Amask) {
324  return;
325  }
326 
327  SDL_LockSurface(surface);
328 
329  switch (surface->format->BytesPerPixel) {
330  case 2:
331  {
332  Uint16 *row, *spot;
333  Uint16 ckey = (Uint16) surface->map->info.colorkey;
334  Uint16 mask = (Uint16) (~surface->format->Amask);
335 
336  /* Ignore alpha in colorkey comparison */
337  ckey &= mask;
338  row = (Uint16 *) surface->pixels;
339  for (y = surface->h; y--;) {
340  spot = row;
341  for (x = surface->w; x--;) {
342  if ((*spot & mask) == ckey) {
343  *spot &= mask;
344  }
345  ++spot;
346  }
347  row += surface->pitch / 2;
348  }
349  }
350  break;
351  case 3:
352  /* FIXME */
353  break;
354  case 4:
355  {
356  Uint32 *row, *spot;
357  Uint32 ckey = surface->map->info.colorkey;
358  Uint32 mask = ~surface->format->Amask;
359 
360  /* Ignore alpha in colorkey comparison */
361  ckey &= mask;
362  row = (Uint32 *) surface->pixels;
363  for (y = surface->h; y--;) {
364  spot = row;
365  for (x = surface->w; x--;) {
366  if ((*spot & mask) == ckey) {
367  *spot &= mask;
368  }
369  ++spot;
370  }
371  row += surface->pitch / 4;
372  }
373  }
374  break;
375  }
376 
377  SDL_UnlockSurface(surface);
378 
379  SDL_SetColorKey(surface, 0, 0);
381 }
void SDL_UnlockSurface(SDL_Surface *surface)
Definition: SDL_surface.c:898
#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:877
Uint8 BytesPerPixel
Definition: SDL_pixels.h:320
int SDL_SetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode blendMode)
Set the blend mode used for blit operations.
Definition: SDL_surface.c:465
uint32_t Uint32
Definition: SDL_stdinc.h:181
int SDL_SetColorKey(SDL_Surface *surface, int flag, Uint32 key)
Sets the color key (transparent pixel) in a blittable surface.
Definition: SDL_surface.c:251
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
Definition: SDL_stdinc.h:169
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 1138 of file SDL_surface.c.

References SDL_Rect::h, i, rect, SDL_BYTESPERPIXEL, SDL_ConvertPixels_RGB_to_YUV(), SDL_ConvertPixels_YUV_to_RGB(), SDL_ConvertPixels_YUV_to_YUV(), SDL_CreateSurfaceOnStack(), SDL_InvalidParamError, SDL_ISPIXELFORMAT_FOURCC, SDL_LowerBlit(), SDL_memcpy, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

1141 {
1142  SDL_Surface src_surface, dst_surface;
1143  SDL_PixelFormat src_fmt, dst_fmt;
1144  SDL_BlitMap src_blitmap, dst_blitmap;
1145  SDL_Rect rect;
1146  void *nonconst_src = (void *) src;
1147 
1148  /* Check to make sure we are blitting somewhere, so we don't crash */
1149  if (!dst) {
1150  return SDL_InvalidParamError("dst");
1151  }
1152  if (!dst_pitch) {
1153  return SDL_InvalidParamError("dst_pitch");
1154  }
1155 
1156  if (SDL_ISPIXELFORMAT_FOURCC(src_format) && SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
1157  return SDL_ConvertPixels_YUV_to_YUV(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
1158  } else if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
1159  return SDL_ConvertPixels_YUV_to_RGB(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
1160  } else if (SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
1161  return SDL_ConvertPixels_RGB_to_YUV(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
1162  }
1163 
1164  /* Fast path for same format copy */
1165  if (src_format == dst_format) {
1166  int i;
1167  const int bpp = SDL_BYTESPERPIXEL(src_format);
1168  width *= bpp;
1169  for (i = height; i--;) {
1170  SDL_memcpy(dst, src, width);
1171  src = (const Uint8*)src + src_pitch;
1172  dst = (Uint8*)dst + dst_pitch;
1173  }
1174  return 0;
1175  }
1176 
1177  if (!SDL_CreateSurfaceOnStack(width, height, src_format, nonconst_src,
1178  src_pitch,
1179  &src_surface, &src_fmt, &src_blitmap)) {
1180  return -1;
1181  }
1182  if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst, dst_pitch,
1183  &dst_surface, &dst_fmt, &dst_blitmap)) {
1184  return -1;
1185  }
1186 
1187  /* Set up the rect and go! */
1188  rect.x = 0;
1189  rect.y = 0;
1190  rect.w = width;
1191  rect.h = height;
1192  return SDL_LowerBlit(&src_surface, &rect, &dst_surface, &rect);
1193 }
int SDL_ConvertPixels_YUV_to_YUV(int width, int height, Uint32 src_format, const void *src, int src_pitch, Uint32 dst_format, void *dst, int dst_pitch)
Definition: SDL_yuv.c:1809
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:1100
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:574
int SDL_ConvertPixels_YUV_to_RGB(int width, int height, Uint32 src_format, const void *src, int src_pitch, Uint32 dst_format, void *dst, int dst_pitch)
Definition: SDL_yuv.c:394
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
#define SDL_memcpy
uint8_t Uint8
Definition: SDL_stdinc.h:157
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
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 SDL_ConvertPixels_RGB_to_YUV(int width, int height, Uint32 src_format, const void *src, int src_pitch, Uint32 dst_format, void *dst, int dst_pitch)
Definition: SDL_yuv.c:783
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 925 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().

927 {
928  SDL_Surface *convert;
929  Uint32 copy_flags;
930  SDL_Color copy_color;
931  SDL_Rect bounds;
932 
933  if (!surface) {
934  SDL_InvalidParamError("surface");
935  return NULL;
936  }
937  if (!format) {
938  SDL_InvalidParamError("format");
939  return NULL;
940  }
941 
942  /* Check for empty destination palette! (results in empty image) */
943  if (format->palette != NULL) {
944  int i;
945  for (i = 0; i < format->palette->ncolors; ++i) {
946  if ((format->palette->colors[i].r != 0xFF) ||
947  (format->palette->colors[i].g != 0xFF) ||
948  (format->palette->colors[i].b != 0xFF))
949  break;
950  }
951  if (i == format->palette->ncolors) {
952  SDL_SetError("Empty destination palette");
953  return (NULL);
954  }
955  }
956 
957  /* Create a new surface with the desired format */
958  convert = SDL_CreateRGBSurface(flags, surface->w, surface->h,
959  format->BitsPerPixel, format->Rmask,
960  format->Gmask, format->Bmask,
961  format->Amask);
962  if (convert == NULL) {
963  return (NULL);
964  }
965 
966  /* Copy the palette if any */
967  if (format->palette && convert->format->palette) {
968  SDL_memcpy(convert->format->palette->colors,
969  format->palette->colors,
970  format->palette->ncolors * sizeof(SDL_Color));
971  convert->format->palette->ncolors = format->palette->ncolors;
972  }
973 
974  /* Save the original copy flags */
975  copy_flags = surface->map->info.flags;
976  copy_color.r = surface->map->info.r;
977  copy_color.g = surface->map->info.g;
978  copy_color.b = surface->map->info.b;
979  copy_color.a = surface->map->info.a;
980  surface->map->info.r = 0xFF;
981  surface->map->info.g = 0xFF;
982  surface->map->info.b = 0xFF;
983  surface->map->info.a = 0xFF;
984  surface->map->info.flags = 0;
985  SDL_InvalidateMap(surface->map);
986 
987  /* Copy over the image data */
988  bounds.x = 0;
989  bounds.y = 0;
990  bounds.w = surface->w;
991  bounds.h = surface->h;
992  SDL_LowerBlit(surface, &bounds, convert, &bounds);
993 
994  /* Clean up the original surface, and update converted surface */
995  convert->map->info.r = copy_color.r;
996  convert->map->info.g = copy_color.g;
997  convert->map->info.b = copy_color.b;
998  convert->map->info.a = copy_color.a;
999  convert->map->info.flags =
1000  (copy_flags &
1004  surface->map->info.r = copy_color.r;
1005  surface->map->info.g = copy_color.g;
1006  surface->map->info.b = copy_color.b;
1007  surface->map->info.a = copy_color.a;
1008  surface->map->info.flags = copy_flags;
1009  SDL_InvalidateMap(surface->map);
1010  if (copy_flags & SDL_COPY_COLORKEY) {
1011  SDL_bool set_colorkey_by_color = SDL_FALSE;
1012 
1013  if (surface->format->palette) {
1014  if (format->palette &&
1015  surface->format->palette->ncolors <= format->palette->ncolors &&
1016  (SDL_memcmp(surface->format->palette->colors, format->palette->colors,
1017  surface->format->palette->ncolors * sizeof(SDL_Color)) == 0)) {
1018  /* The palette is identical, just set the same colorkey */
1019  SDL_SetColorKey(convert, 1, surface->map->info.colorkey);
1020  } else if (format->Amask) {
1021  /* The alpha was set in the destination from the palette */
1022  } else {
1023  set_colorkey_by_color = SDL_TRUE;
1024  }
1025  } else {
1026  set_colorkey_by_color = SDL_TRUE;
1027  }
1028 
1029  if (set_colorkey_by_color) {
1030  SDL_Surface *tmp;
1031  SDL_Surface *tmp2;
1032  int converted_colorkey = 0;
1033 
1034  /* Create a dummy surface to get the colorkey converted */
1035  tmp = SDL_CreateRGBSurface(0, 1, 1,
1036  surface->format->BitsPerPixel, surface->format->Rmask,
1037  surface->format->Gmask, surface->format->Bmask,
1038  surface->format->Amask);
1039 
1040  /* Share the palette, if any */
1041  if (surface->format->palette) {
1042  SDL_SetSurfacePalette(tmp, surface->format->palette);
1043  }
1044 
1045  SDL_FillRect(tmp, NULL, surface->map->info.colorkey);
1046 
1047  tmp->map->info.flags &= ~SDL_COPY_COLORKEY;
1048 
1049  /* Convertion of the colorkey */
1050  tmp2 = SDL_ConvertSurface(tmp, format, 0);
1051 
1052  /* Get the converted colorkey */
1053  SDL_memcpy(&converted_colorkey, tmp2->pixels, tmp2->format->BytesPerPixel);
1054 
1055  SDL_FreeSurface(tmp);
1056  SDL_FreeSurface(tmp2);
1057 
1058  /* Set the converted colorkey on the new surface */
1059  SDL_SetColorKey(convert, 1, converted_colorkey);
1060 
1061  /* This is needed when converting for 3D texture upload */
1062  SDL_ConvertColorkeyToAlpha(convert);
1063  }
1064  }
1065  SDL_SetClipRect(convert, &surface->clip_rect);
1066 
1067  /* Enable alpha blending by default if the new surface has an
1068  * alpha channel or alpha modulation */
1069  if ((surface->format->Amask && format->Amask) ||
1070  (copy_flags & SDL_COPY_MODULATE_ALPHA)) {
1072  }
1073  if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
1074  SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
1075  }
1076 
1077  /* We're ready to go! */
1078  return (convert);
1079 }
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:531
#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:230
Uint8 g
Definition: SDL_pixels.h:298
Uint8 BytesPerPixel
Definition: SDL_pixels.h:320
Uint8 g
Definition: SDL_blit.h:70
SDL_Surface * SDL_ConvertSurface(SDL_Surface *surface, const SDL_PixelFormat *format, Uint32 flags)
Definition: SDL_surface.c:925
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:465
static void SDL_ConvertColorkeyToAlpha(SDL_Surface *surface)
Definition: SDL_surface.c:314
Uint8 b
Definition: SDL_pixels.h:299
#define SDL_COPY_RLE_COLORKEY
Definition: SDL_blit.h:42
uint32_t Uint32
Definition: SDL_stdinc.h:181
#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:574
int SDL_SetColorKey(SDL_Surface *surface, int flag, Uint32 key)
Sets the color key (transparent pixel) in a blittable surface.
Definition: SDL_surface.c:251
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:972
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
#define SDL_memcpy
Uint8 r
Definition: SDL_pixels.h:297
void * pixels
Definition: SDL_surface.h:75
Uint8 a
Definition: SDL_pixels.h:300
Uint8 BitsPerPixel
Definition: SDL_pixels.h:319
int SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
Set the palette used by a surface.
Definition: SDL_surface.c:216
#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:1199
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:307
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:153
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:318
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 1082 of file SDL_surface.c.

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

1084 {
1085  SDL_PixelFormat *fmt;
1086  SDL_Surface *convert = NULL;
1087 
1088  fmt = SDL_AllocFormat(pixel_format);
1089  if (fmt) {
1090  convert = SDL_ConvertSurface(surface, fmt, flags);
1091  SDL_FreeFormat(fmt);
1092  }
1093  return convert;
1094 }
SDL_Surface * SDL_ConvertSurface(SDL_Surface *surface, const SDL_PixelFormat *format, Uint32 flags)
Definition: SDL_surface.c:925
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

◆ 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 153 of file SDL_surface.c.

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

Referenced by SDL_ConvertSurface(), and SDL_CreateRGBSurfaceFrom().

156 {
157  Uint32 format;
158 
159  /* Get the pixel format */
160  format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask);
161  if (format == SDL_PIXELFORMAT_UNKNOWN) {
162  SDL_SetError("Unknown pixel format");
163  return NULL;
164  }
165 
167 }
#define SDL_MasksToPixelFormatEnum
uint32_t Uint32
Definition: SDL_stdinc.h:181
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:66
#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 173 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.

177 {
179 
180  surface = SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
181  if (surface != NULL) {
182  surface->flags |= SDL_PREALLOC;
183  surface->pixels = pixels;
184  surface->w = width;
185  surface->h = height;
186  surface->pitch = pitch;
187  SDL_SetClipRect(surface, NULL);
188  }
189  return surface;
190 }
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:531
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:153
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 66 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_MAX_SINT32, SDL_memset, SDL_OutOfMemory, SDL_SetClipRect(), SDL_SetSurfaceBlendMode(), SDL_SetSurfacePalette(), void, and SDL_Surface::w.

Referenced by SDL_CreateRGBSurface(), and SDL_CreateRGBSurfaceWithFormatFrom().

68 {
70 
71  /* The flags are no longer used, make the compiler happy */
72  (void)flags;
73 
74  /* Allocate the surface */
75  surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
76  if (surface == NULL) {
78  return NULL;
79  }
80 
81  surface->format = SDL_AllocFormat(format);
82  if (!surface->format) {
83  SDL_FreeSurface(surface);
84  return NULL;
85  }
86  surface->w = width;
87  surface->h = height;
88  surface->pitch = SDL_CalculatePitch(format, width);
89  SDL_SetClipRect(surface, NULL);
90 
91  if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
92  SDL_Palette *palette =
93  SDL_AllocPalette((1 << surface->format->BitsPerPixel));
94  if (!palette) {
95  SDL_FreeSurface(surface);
96  return NULL;
97  }
98  if (palette->ncolors == 2) {
99  /* Create a black and white bitmap palette */
100  palette->colors[0].r = 0xFF;
101  palette->colors[0].g = 0xFF;
102  palette->colors[0].b = 0xFF;
103  palette->colors[1].r = 0x00;
104  palette->colors[1].g = 0x00;
105  palette->colors[1].b = 0x00;
106  }
107  SDL_SetSurfacePalette(surface, palette);
108  SDL_FreePalette(palette);
109  }
110 
111  /* Get the pixels */
112  if (surface->w && surface->h) {
113  /* Assumptions checked in surface_size_assumptions assert above */
114  Sint64 size = ((Sint64)surface->h * surface->pitch);
116  /* Overflow... */
117  SDL_FreeSurface(surface);
118  SDL_OutOfMemory();
119  return NULL;
120  }
121 
122  surface->pixels = SDL_malloc((size_t)size);
123  if (!surface->pixels) {
124  SDL_FreeSurface(surface);
125  SDL_OutOfMemory();
126  return NULL;
127  }
128  /* This is important for bitmaps */
129  SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
130  }
131 
132  /* Allocate an empty mapping */
133  surface->map = SDL_AllocBlitMap();
134  if (!surface->map) {
135  SDL_FreeSurface(surface);
136  return NULL;
137  }
138 
139  /* By default surface with an alpha mask are set up for blending */
140  if (surface->format->Amask) {
142  }
143 
144  /* The surface is ready to go */
145  surface->refcount = 1;
146  return surface;
147 }
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:531
Uint8 g
Definition: SDL_pixels.h:298
#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:465
Uint8 b
Definition: SDL_pixels.h:299
#define SDL_AllocFormat
#define SDL_MAX_SINT32
A signed 32-bit integer type.
Definition: SDL_stdinc.h:173
int SDL_CalculatePitch(Uint32 format, int width)
Definition: SDL_surface.c:41
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
Uint8 r
Definition: SDL_pixels.h:297
void * pixels
Definition: SDL_surface.h:75
Uint8 BitsPerPixel
Definition: SDL_pixels.h:319
int SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
Set the palette used by a surface.
Definition: SDL_surface.c:216
GLsizeiptr size
#define SDL_AllocPalette
void SDL_FreeSurface(SDL_Surface *surface)
Definition: SDL_surface.c:1199
#define NULL
Definition: begin_code.h:164
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_Color * colors
Definition: SDL_pixels.h:307
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_FreePalette
GLbitfield flags
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
#define SDL_calloc
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:952
#define SDL_malloc
int64_t Sint64
Definition: SDL_stdinc.h:188
#define SDL_memset

◆ SDL_CreateRGBSurfaceWithFormatFrom()

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

Definition at line 197 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.

200 {
202 
203  surface = SDL_CreateRGBSurfaceWithFormat(0, 0, 0, depth, format);
204  if (surface != NULL) {
205  surface->flags |= SDL_PREALLOC;
206  surface->pixels = pixels;
207  surface->w = width;
208  surface->h = height;
209  surface->pitch = pitch;
210  SDL_SetClipRect(surface, NULL);
211  }
212  return surface;
213 }
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:531
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:66
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 1100 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().

1103 {
1104  if (SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
1105  SDL_SetError("Indexed pixel formats not supported");
1106  return SDL_FALSE;
1107  }
1108  if (SDL_InitFormat(format, pixel_format) < 0) {
1109  return SDL_FALSE;
1110  }
1111 
1112  SDL_zerop(surface);
1113  surface->flags = SDL_PREALLOC;
1114  surface->format = format;
1115  surface->pixels = pixels;
1116  surface->w = width;
1117  surface->h = height;
1118  surface->pitch = pitch;
1119  /* We don't actually need to set up the clip rect for our purposes */
1120  /* SDL_SetClipRect(surface, NULL); */
1121 
1122  /* Allocate an empty mapping */
1123  SDL_zerop(blitmap);
1124  blitmap->info.r = 0xFF;
1125  blitmap->info.g = 0xFF;
1126  blitmap->info.b = 0xFF;
1127  blitmap->info.a = 0xFF;
1128  surface->map = blitmap;
1129 
1130  /* The surface is ready to go */
1131  surface->refcount = 1;
1132  return SDL_TRUE;
1133 }
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:417
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:537
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_SetError
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
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 916 of file SDL_surface.c.

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

917 {
918  return SDL_ConvertSurface(surface, surface->format, surface->flags);
919 }
SDL_Surface * SDL_ConvertSurface(SDL_Surface *surface, const SDL_PixelFormat *format, Uint32 flags)
Definition: SDL_surface.c:925
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 1199 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_InvalidateMap(), SDL_PREALLOC, SDL_RLEACCEL, SDL_SetSurfacePalette(), SDL_UnlockSurface(), and SDL_UnRLESurface().

Referenced by SDL_ConvertSurface(), and SDL_CreateRGBSurfaceWithFormat().

1200 {
1201  if (surface == NULL) {
1202  return;
1203  }
1204  if (surface->flags & SDL_DONTFREE) {
1205  return;
1206  }
1207  SDL_InvalidateMap(surface->map);
1208 
1209  if (--surface->refcount > 0) {
1210  return;
1211  }
1212  while (surface->locked > 0) {
1213  SDL_UnlockSurface(surface);
1214  }
1215  if (surface->flags & SDL_RLEACCEL) {
1216  SDL_UnRLESurface(surface, 0);
1217  }
1218  if (surface->format) {
1219  SDL_SetSurfacePalette(surface, NULL);
1220  SDL_FreeFormat(surface->format);
1221  surface->format = NULL;
1222  }
1223  if (!(surface->flags & SDL_PREALLOC)) {
1224  SDL_free(surface->pixels);
1225  }
1226  if (surface->map) {
1227  SDL_FreeBlitMap(surface->map);
1228  }
1229  SDL_free(surface);
1230 }
void SDL_UnlockSurface(SDL_Surface *surface)
Definition: SDL_surface.c:898
#define SDL_DONTFREE
Definition: SDL_surface.h:55
void SDL_UnRLESurface(SDL_Surface *surface, int recode)
Uint32 flags
Definition: SDL_surface.h:71
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:972
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
void * pixels
Definition: SDL_surface.h:75
#define SDL_free
int SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
Set the palette used by a surface.
Definition: SDL_surface.c:216
#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:1077
#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 555 of file SDL_surface.c.

References SDL_Surface::clip_rect.

556 {
557  if (surface && rect) {
558  *rect = surface->clip_rect;
559  }
560 }
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 296 of file SDL_surface.c.

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

297 {
298  if (!surface) {
299  return SDL_InvalidParamError("surface");
300  }
301 
302  if (!(surface->map->info.flags & SDL_COPY_COLORKEY)) {
303  return SDL_SetError("Surface doesn't have a colorkey");
304  }
305 
306  if (key) {
307  *key = surface->map->info.colorkey;
308  }
309  return 0;
310 }
#define SDL_COPY_COLORKEY
Definition: SDL_blit.h:39
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
Uint32 colorkey
Definition: SDL_blit.h:69
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLuint64 key
Definition: gl2ext.h:2192
#define SDL_SetError
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 452 of file SDL_surface.c.

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

453 {
454  if (!surface) {
455  return -1;
456  }
457 
458  if (alpha) {
459  *alpha = surface->map->info.a;
460  }
461  return 0;
462 }
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 502 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.

503 {
504  if (!surface) {
505  return -1;
506  }
507 
508  if (!blendMode) {
509  return 0;
510  }
511 
512  switch (surface->map->
513  info.flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD)) {
514  case SDL_COPY_BLEND:
516  break;
517  case SDL_COPY_ADD:
519  break;
520  case SDL_COPY_MOD:
522  break;
523  default:
525  break;
526  }
527  return 0;
528 }
#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 410 of file SDL_surface.c.

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

411 {
412  if (!surface) {
413  return -1;
414  }
415 
416  if (r) {
417  *r = surface->map->info.r;
418  }
419  if (g) {
420  *g = surface->map->info.g;
421  }
422  if (b) {
423  *b = surface->map->info.b;
424  }
425  return 0;
426 }
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 877 of file SDL_surface.c.

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

Referenced by SDL_ConvertColorkeyToAlpha().

878 {
879  if (!surface->locked) {
880  /* Perform the lock */
881  if (surface->flags & SDL_RLEACCEL) {
882  SDL_UnRLESurface(surface, 1);
883  surface->flags |= SDL_RLEACCEL; /* save accel'd state */
884  }
885  }
886 
887  /* Increment the surface lock count, for recursive locks */
888  ++surface->locked;
889 
890  /* Ready to go.. */
891  return (0);
892 }
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 574 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().

576 {
577  /* Check to make sure the blit mapping is valid */
578  if ((src->map->dst != dst) ||
579  (dst->format->palette &&
580  src->map->dst_palette_version != dst->format->palette->version) ||
581  (src->format->palette &&
582  src->map->src_palette_version != src->format->palette->version)) {
583  if (SDL_MapSurface(src, dst) < 0) {
584  return (-1);
585  }
586  /* just here for debugging */
587 /* printf */
588 /* ("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", */
589 /* src, dst->flags, src->map->info.flags, dst, dst->flags, */
590 /* dst->map->info.flags, src->map->blit); */
591  }
592  return (src->map->blit(src, srcrect, dst, dstrect));
593 }
Uint32 version
Definition: SDL_pixels.h:308
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:991
SDL_Surface * dst
Definition: SDL_blit.h:88
SDL_PixelFormat * format
Definition: SDL_surface.h:72
SDL_Palette * palette
Definition: SDL_pixels.h:318

◆ 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 850 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().

852 {
853  static const Uint32 complex_copy_flags = (
857  );
858 
859  if (!(src->map->info.flags & SDL_COPY_NEAREST)) {
860  src->map->info.flags |= SDL_COPY_NEAREST;
861  SDL_InvalidateMap(src->map);
862  }
863 
864  if ( !(src->map->info.flags & complex_copy_flags) &&
865  src->format->format == dst->format->format &&
867  return SDL_SoftStretch( src, srcrect, dst, dstrect );
868  } else {
869  return SDL_LowerBlit( src, srcrect, dst, dstrect );
870  }
871 }
#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
Definition: SDL_stdinc.h:181
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:574
#define SDL_COPY_ADD
Definition: SDL_blit.h:37
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:972
#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 531 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().

532 {
533  SDL_Rect full_rect;
534 
535  /* Don't do anything if there's no surface to act on */
536  if (!surface) {
537  return SDL_FALSE;
538  }
539 
540  /* Set up the full surface rectangle */
541  full_rect.x = 0;
542  full_rect.y = 0;
543  full_rect.w = surface->w;
544  full_rect.h = surface->h;
545 
546  /* Set the clipping rectangle */
547  if (!rect) {
548  surface->clip_rect = full_rect;
549  return SDL_TRUE;
550  }
551  return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
552 }
#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 251 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().

252 {
253  int flags;
254 
255  if (!surface) {
256  return SDL_InvalidParamError("surface");
257  }
258 
259  if (surface->format->palette && key >= ((Uint32) surface->format->palette->ncolors)) {
260  return SDL_InvalidParamError("key");
261  }
262 
263  if (flag & SDL_RLEACCEL) {
264  SDL_SetSurfaceRLE(surface, 1);
265  }
266 
267  flags = surface->map->info.flags;
268  if (flag) {
269  surface->map->info.flags |= SDL_COPY_COLORKEY;
270  surface->map->info.colorkey = key;
271  if (surface->format->palette) {
272  surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_TRANSPARENT;
273  ++surface->format->palette->version;
274  if (!surface->format->palette->version) {
275  surface->format->palette->version = 1;
276  }
277  }
278  } else {
279  if (surface->format->palette) {
280  surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_OPAQUE;
281  ++surface->format->palette->version;
282  if (!surface->format->palette->version) {
283  surface->format->palette->version = 1;
284  }
285  }
286  surface->map->info.flags &= ~SDL_COPY_COLORKEY;
287  }
288  if (surface->map->info.flags != flags) {
289  SDL_InvalidateMap(surface->map);
290  }
291 
292  return 0;
293 }
Uint32 version
Definition: SDL_pixels.h:308
#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:230
uint32_t Uint32
Definition: SDL_stdinc.h:181
#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:972
#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:300
SDL_Color * colors
Definition: SDL_pixels.h:307
SDL_PixelFormat * format
Definition: SDL_surface.h:72
GLbitfield flags
SDL_Palette * palette
Definition: SDL_pixels.h:318
#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 429 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().

430 {
431  int flags;
432 
433  if (!surface) {
434  return -1;
435  }
436 
437  surface->map->info.a = alpha;
438 
439  flags = surface->map->info.flags;
440  if (alpha != 0xFF) {
441  surface->map->info.flags |= SDL_COPY_MODULATE_ALPHA;
442  } else {
443  surface->map->info.flags &= ~SDL_COPY_MODULATE_ALPHA;
444  }
445  if (surface->map->info.flags != flags) {
446  SDL_InvalidateMap(surface->map);
447  }
448  return 0;
449 }
GLfloat GLfloat GLfloat alpha
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:972
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 465 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().

466 {
467  int flags, status;
468 
469  if (!surface) {
470  return -1;
471  }
472 
473  status = 0;
474  flags = surface->map->info.flags;
475  surface->map->info.flags &=
477  switch (blendMode) {
478  case SDL_BLENDMODE_NONE:
479  break;
480  case SDL_BLENDMODE_BLEND:
481  surface->map->info.flags |= SDL_COPY_BLEND;
482  break;
483  case SDL_BLENDMODE_ADD:
484  surface->map->info.flags |= SDL_COPY_ADD;
485  break;
486  case SDL_BLENDMODE_MOD:
487  surface->map->info.flags |= SDL_COPY_MOD;
488  break;
489  default:
490  status = SDL_Unsupported();
491  break;
492  }
493 
494  if (surface->map->info.flags != flags) {
495  SDL_InvalidateMap(surface->map);
496  }
497 
498  return status;
499 }
#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:972
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 384 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().

385 {
386  int flags;
387 
388  if (!surface) {
389  return -1;
390  }
391 
392  surface->map->info.r = r;
393  surface->map->info.g = g;
394  surface->map->info.b = b;
395 
396  flags = surface->map->info.flags;
397  if (r != 0xFF || g != 0xFF || b != 0xFF) {
398  surface->map->info.flags |= SDL_COPY_MODULATE_COLOR;
399  } else {
400  surface->map->info.flags &= ~SDL_COPY_MODULATE_COLOR;
401  }
402  if (surface->map->info.flags != flags) {
403  SDL_InvalidateMap(surface->map);
404  }
405  return 0;
406 }
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:972
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 216 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().

217 {
218  if (!surface) {
219  return SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
220  }
221  if (SDL_SetPixelFormatPalette(surface->format, palette) < 0) {
222  return -1;
223  }
224  SDL_InvalidateMap(surface->map);
225 
226  return 0;
227 }
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:972
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 230 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().

231 {
232  int flags;
233 
234  if (!surface) {
235  return -1;
236  }
237 
238  flags = surface->map->info.flags;
239  if (flag) {
240  surface->map->info.flags |= SDL_COPY_RLE_DESIRED;
241  } else {
242  surface->map->info.flags &= ~SDL_COPY_RLE_DESIRED;
243  }
244  if (surface->map->info.flags != flags) {
245  SDL_InvalidateMap(surface->map);
246  }
247  return 0;
248 }
#define SDL_COPY_RLE_DESIRED
Definition: SDL_blit.h:41
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:972
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 898 of file SDL_surface.c.

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

Referenced by SDL_ConvertColorkeyToAlpha(), and SDL_FreeSurface().

899 {
900  /* Only perform an unlock if we are locked */
901  if (!surface->locked || (--surface->locked > 0)) {
902  return;
903  }
904 
905  /* Update RLE encoded surface with new data */
906  if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
907  surface->flags &= ~SDL_RLEACCEL; /* stop lying */
908  SDL_RLESurface(surface);
909  }
910 }
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 597 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.

599 {
600  SDL_Rect fulldst;
601  int srcx, srcy, w, h;
602 
603  /* Make sure the surfaces aren't locked */
604  if (!src || !dst) {
605  return SDL_SetError("SDL_UpperBlit: passed a NULL surface");
606  }
607  if (src->locked || dst->locked) {
608  return SDL_SetError("Surfaces must not be locked during blit");
609  }
610 
611  /* If the destination rectangle is NULL, use the entire dest surface */
612  if (dstrect == NULL) {
613  fulldst.x = fulldst.y = 0;
614  fulldst.w = dst->w;
615  fulldst.h = dst->h;
616  dstrect = &fulldst;
617  }
618 
619  /* clip the source rectangle to the source surface */
620  if (srcrect) {
621  int maxw, maxh;
622 
623  srcx = srcrect->x;
624  w = srcrect->w;
625  if (srcx < 0) {
626  w += srcx;
627  dstrect->x -= srcx;
628  srcx = 0;
629  }
630  maxw = src->w - srcx;
631  if (maxw < w)
632  w = maxw;
633 
634  srcy = srcrect->y;
635  h = srcrect->h;
636  if (srcy < 0) {
637  h += srcy;
638  dstrect->y -= srcy;
639  srcy = 0;
640  }
641  maxh = src->h - srcy;
642  if (maxh < h)
643  h = maxh;
644 
645  } else {
646  srcx = srcy = 0;
647  w = src->w;
648  h = src->h;
649  }
650 
651  /* clip the destination rectangle against the clip rectangle */
652  {
653  SDL_Rect *clip = &dst->clip_rect;
654  int dx, dy;
655 
656  dx = clip->x - dstrect->x;
657  if (dx > 0) {
658  w -= dx;
659  dstrect->x += dx;
660  srcx += dx;
661  }
662  dx = dstrect->x + w - clip->x - clip->w;
663  if (dx > 0)
664  w -= dx;
665 
666  dy = clip->y - dstrect->y;
667  if (dy > 0) {
668  h -= dy;
669  dstrect->y += dy;
670  srcy += dy;
671  }
672  dy = dstrect->y + h - clip->y - clip->h;
673  if (dy > 0)
674  h -= dy;
675  }
676 
677  /* Switch back to a fast blit if we were previously stretching */
678  if (src->map->info.flags & SDL_COPY_NEAREST) {
679  src->map->info.flags &= ~SDL_COPY_NEAREST;
680  SDL_InvalidateMap(src->map);
681  }
682 
683  if (w > 0 && h > 0) {
684  SDL_Rect sr;
685  sr.x = srcx;
686  sr.y = srcy;
687  sr.w = dstrect->w = w;
688  sr.h = dstrect->h = h;
689  return SDL_LowerBlit(src, &sr, dst, dstrect);
690  }
691  dstrect->w = dstrect->h = 0;
692  return 0;
693 }
GLfloat GLfloat GLfloat GLfloat h
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:574
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:972
#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 696 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.

698 {
699  double src_x0, src_y0, src_x1, src_y1;
700  double dst_x0, dst_y0, dst_x1, dst_y1;
701  SDL_Rect final_src, final_dst;
702  double scaling_w, scaling_h;
703  int src_w, src_h;
704  int dst_w, dst_h;
705 
706  /* Make sure the surfaces aren't locked */
707  if (!src || !dst) {
708  return SDL_SetError("SDL_UpperBlitScaled: passed a NULL surface");
709  }
710  if (src->locked || dst->locked) {
711  return SDL_SetError("Surfaces must not be locked during blit");
712  }
713 
714  if (NULL == srcrect) {
715  src_w = src->w;
716  src_h = src->h;
717  } else {
718  src_w = srcrect->w;
719  src_h = srcrect->h;
720  }
721 
722  if (NULL == dstrect) {
723  dst_w = dst->w;
724  dst_h = dst->h;
725  } else {
726  dst_w = dstrect->w;
727  dst_h = dstrect->h;
728  }
729 
730  if (dst_w == src_w && dst_h == src_h) {
731  /* No scaling, defer to regular blit */
732  return SDL_BlitSurface(src, srcrect, dst, dstrect);
733  }
734 
735  scaling_w = (double)dst_w / src_w;
736  scaling_h = (double)dst_h / src_h;
737 
738  if (NULL == dstrect) {
739  dst_x0 = 0;
740  dst_y0 = 0;
741  dst_x1 = dst_w - 1;
742  dst_y1 = dst_h - 1;
743  } else {
744  dst_x0 = dstrect->x;
745  dst_y0 = dstrect->y;
746  dst_x1 = dst_x0 + dst_w - 1;
747  dst_y1 = dst_y0 + dst_h - 1;
748  }
749 
750  if (NULL == srcrect) {
751  src_x0 = 0;
752  src_y0 = 0;
753  src_x1 = src_w - 1;
754  src_y1 = src_h - 1;
755  } else {
756  src_x0 = srcrect->x;
757  src_y0 = srcrect->y;
758  src_x1 = src_x0 + src_w - 1;
759  src_y1 = src_y0 + src_h - 1;
760 
761  /* Clip source rectangle to the source surface */
762 
763  if (src_x0 < 0) {
764  dst_x0 -= src_x0 * scaling_w;
765  src_x0 = 0;
766  }
767 
768  if (src_x1 >= src->w) {
769  dst_x1 -= (src_x1 - src->w + 1) * scaling_w;
770  src_x1 = src->w - 1;
771  }
772 
773  if (src_y0 < 0) {
774  dst_y0 -= src_y0 * scaling_h;
775  src_y0 = 0;
776  }
777 
778  if (src_y1 >= src->h) {
779  dst_y1 -= (src_y1 - src->h + 1) * scaling_h;
780  src_y1 = src->h - 1;
781  }
782  }
783 
784  /* Clip destination rectangle to the clip rectangle */
785 
786  /* Translate to clip space for easier calculations */
787  dst_x0 -= dst->clip_rect.x;
788  dst_x1 -= dst->clip_rect.x;
789  dst_y0 -= dst->clip_rect.y;
790  dst_y1 -= dst->clip_rect.y;
791 
792  if (dst_x0 < 0) {
793  src_x0 -= dst_x0 / scaling_w;
794  dst_x0 = 0;
795  }
796 
797  if (dst_x1 >= dst->clip_rect.w) {
798  src_x1 -= (dst_x1 - dst->clip_rect.w + 1) / scaling_w;
799  dst_x1 = dst->clip_rect.w - 1;
800  }
801 
802  if (dst_y0 < 0) {
803  src_y0 -= dst_y0 / scaling_h;
804  dst_y0 = 0;
805  }
806 
807  if (dst_y1 >= dst->clip_rect.h) {
808  src_y1 -= (dst_y1 - dst->clip_rect.h + 1) / scaling_h;
809  dst_y1 = dst->clip_rect.h - 1;
810  }
811 
812  /* Translate back to surface coordinates */
813  dst_x0 += dst->clip_rect.x;
814  dst_x1 += dst->clip_rect.x;
815  dst_y0 += dst->clip_rect.y;
816  dst_y1 += dst->clip_rect.y;
817 
818  final_src.x = (int)SDL_floor(src_x0 + 0.5);
819  final_src.y = (int)SDL_floor(src_y0 + 0.5);
820  final_src.w = (int)SDL_floor(src_x1 + 1 + 0.5) - (int)SDL_floor(src_x0 + 0.5);
821  final_src.h = (int)SDL_floor(src_y1 + 1 + 0.5) - (int)SDL_floor(src_y0 + 0.5);
822 
823  final_dst.x = (int)SDL_floor(dst_x0 + 0.5);
824  final_dst.y = (int)SDL_floor(dst_y0 + 0.5);
825  final_dst.w = (int)SDL_floor(dst_x1 - dst_x0 + 1.5);
826  final_dst.h = (int)SDL_floor(dst_y1 - dst_y0 + 1.5);
827 
828  if (final_dst.w < 0)
829  final_dst.w = 0;
830  if (final_dst.h < 0)
831  final_dst.h = 0;
832 
833  if (dstrect)
834  *dstrect = final_dst;
835 
836  if (final_dst.w == 0 || final_dst.h == 0 ||
837  final_src.w <= 0 || final_src.h <= 0) {
838  /* No-op. */
839  return 0;
840  }
841 
842  return SDL_LowerBlitScaled(src, &final_src, dst, &final_dst);
843 }
#define SDL_BlitSurface
Definition: SDL_surface.h:476
#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:850
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64