SDL  2.0
SDL_surface.h File Reference
#include "SDL_stdinc.h"
#include "SDL_pixels.h"
#include "SDL_rect.h"
#include "SDL_blendmode.h"
#include "SDL_rwops.h"
#include "begin_code.h"
#include "close_code.h"
+ Include dependency graph for SDL_surface.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SDL_Surface
 A collection of pixels used in software blitting. More...
 

Macros

#define SDL_MUSTLOCK(S)   (((S)->flags & SDL_RLEACCEL) != 0)
 
#define SDL_LoadBMP(file)   SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)
 
#define SDL_SaveBMP(surface, file)   SDL_SaveBMP_RW(surface, SDL_RWFromFile(file, "wb"), 1)
 
#define SDL_BlitSurface   SDL_UpperBlit
 
#define SDL_BlitScaled   SDL_UpperBlitScaled
 
Surface flags

These are the currently supported flags for the SDL_Surface.

Used internally (read-only).

#define SDL_SWSURFACE   0
 
#define SDL_PREALLOC   0x00000001
 
#define SDL_RLEACCEL   0x00000002
 
#define SDL_DONTFREE   0x00000004
 

Typedefs

typedef int(* SDL_blit) (struct SDL_Surface *src, SDL_Rect *srcrect, struct SDL_Surface *dst, SDL_Rect *dstrect)
 The type of function used for surface blitting functions. More...
 

Functions

SDL_SurfaceSDL_CreateRGBSurface (Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
 
SDL_SurfaceSDL_CreateRGBSurfaceWithFormat (Uint32 flags, int width, int height, int depth, Uint32 format)
 
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)
 
void SDL_FreeSurface (SDL_Surface *surface)
 
int SDL_SetSurfacePalette (SDL_Surface *surface, SDL_Palette *palette)
 Set the palette used by a surface. More...
 
int SDL_LockSurface (SDL_Surface *surface)
 Sets up a surface for directly accessing the pixels. More...
 
void SDL_UnlockSurface (SDL_Surface *surface)
 
SDL_SurfaceSDL_LoadBMP_RW (SDL_RWops *src, int freesrc)
 
int SDL_SaveBMP_RW (SDL_Surface *surface, SDL_RWops *dst, int freedst)
 
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...
 
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)
 
SDL_SurfaceSDL_ConvertSurface (SDL_Surface *src, const SDL_PixelFormat *fmt, Uint32 flags)
 
SDL_SurfaceSDL_ConvertSurfaceFormat (SDL_Surface *src, Uint32 pixel_format, Uint32 flags)
 
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...
 
int SDL_FillRect (SDL_Surface *dst, const SDL_Rect *rect, Uint32 color)
 
int SDL_FillRects (SDL_Surface *dst, const SDL_Rect *rects, int count, Uint32 color)
 
int SDL_UpperBlit (SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
 
int SDL_LowerBlit (SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
 
int SDL_SoftStretch (SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, const SDL_Rect *dstrect)
 Perform a fast, low quality, stretch blit between two surfaces of the same pixel format. More...
 
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)
 

Detailed Description

Header file for SDL_Surface definition and management functions.

Definition in file SDL_surface.h.

Macro Definition Documentation

◆ SDL_BlitScaled

#define SDL_BlitScaled   SDL_UpperBlitScaled

Definition at line 486 of file SDL_surface.h.

Referenced by SW_RenderCopy(), and SW_RenderCopyEx().

◆ SDL_BlitSurface

#define SDL_BlitSurface   SDL_UpperBlit

Performs a fast blit from the source surface to the destination surface.

This assumes that the source and destination rectangles are the same size. If either srcrect or dstrect are NULL, the entire surface (src or dst) is copied. The final blit rectangles are saved in srcrect and dstrect after all clipping is performed.

Returns
If the blit is successful, it returns 0, otherwise it returns -1.

The blit function should not be called on a locked surface.

The blit semantics for surfaces with and without blending and colorkey are defined as follows:

RGBA->RGB:
  Source surface blend mode set to SDL_BLENDMODE_BLEND:
    alpha-blend (using the source alpha-channel and per-surface alpha)
    SDL_SRCCOLORKEY ignored.
  Source surface blend mode set to SDL_BLENDMODE_NONE:
    copy RGB.
    if SDL_SRCCOLORKEY set, only copy the pixels matching the
    RGB values of the source color key, ignoring alpha in the
    comparison.

RGB->RGBA:
  Source surface blend mode set to SDL_BLENDMODE_BLEND:
    alpha-blend (using the source per-surface alpha)
  Source surface blend mode set to SDL_BLENDMODE_NONE:
    copy RGB, set destination alpha to source per-surface alpha value.
  both:
    if SDL_SRCCOLORKEY set, only copy the pixels matching the
    source color key.

RGBA->RGBA:
  Source surface blend mode set to SDL_BLENDMODE_BLEND:
    alpha-blend (using the source alpha-channel and per-surface alpha)
    SDL_SRCCOLORKEY ignored.
  Source surface blend mode set to SDL_BLENDMODE_NONE:
    copy all of RGBA to the destination.
    if SDL_SRCCOLORKEY set, only copy the pixels matching the
    RGB values of the source color key, ignoring alpha in the
    comparison.

RGB->RGB:
  Source surface blend mode set to SDL_BLENDMODE_BLEND:
    alpha-blend (using the source per-surface alpha)
  Source surface blend mode set to SDL_BLENDMODE_NONE:
    copy RGB.
  both:
    if SDL_SRCCOLORKEY set, only copy the pixels matching the
    source color key.

You should call SDL_BlitSurface() unless you know exactly how SDL blitting works internally and how to use the other blit functions.

Definition at line 457 of file SDL_surface.h.

Referenced by _testBlitBlendMode(), SDL_UpperBlitScaled(), SW_RenderCopy(), and SW_RenderCopyEx().

◆ SDL_DONTFREE

#define SDL_DONTFREE   0x00000004

Surface is referenced internally

Definition at line 55 of file SDL_surface.h.

Referenced by SDL_DestroyWindow(), SDL_FreeSurface(), SDL_GetWindowSurface(), and SDL_RecreateWindow().

◆ SDL_LoadBMP

#define SDL_LoadBMP (   file)    SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)

Load a surface from a file.

Convenience macro.

Definition at line 186 of file SDL_surface.h.

Referenced by init_color_cursor(), LoadSprite(), LoadTexture(), main(), SDLTest_LoadIcon(), surface_testLoadFailure(), and surface_testSaveLoadBitmap().

◆ SDL_MUSTLOCK

#define SDL_MUSTLOCK (   S)    (((S)->flags & SDL_RLEACCEL) != 0)

Evaluates to true if the surface needs to be locked before access.

Definition at line 61 of file SDL_surface.h.

Referenced by SDL_CalculateShapeBitmap(), SDL_CalculateShapeTree(), SDL_CreateTextureFromSurface(), SDL_RLEAlphaBlit(), SDL_RLEBlit(), SDL_SoftBlit(), SDL_SoftStretch(), SDLgfx_rotateSurface(), and SW_UpdateTexture().

◆ SDL_PREALLOC

#define SDL_PREALLOC   0x00000001

◆ SDL_RLEACCEL

◆ SDL_SaveBMP

#define SDL_SaveBMP (   surface,
  file 
)    SDL_SaveBMP_RW(surface, SDL_RWFromFile(file, "wb"), 1)

Save a surface to a file.

Convenience macro.

Definition at line 209 of file SDL_surface.h.

Referenced by SDLTest_CompareSurfaces(), SDLTest_ScreenShot(), and surface_testSaveLoadBitmap().

◆ SDL_SWSURFACE

#define SDL_SWSURFACE   0

Just here for compatibility

Definition at line 52 of file SDL_surface.h.

Referenced by _surfaceSetUp(), SDLgfx_rotateSurface(), SDLTest_DrawCharacter(), and SW_RenderCopyEx().

Typedef Documentation

◆ SDL_blit

typedef int(* SDL_blit) (struct SDL_Surface *src, SDL_Rect *srcrect, struct SDL_Surface *dst, SDL_Rect *dstrect)

The type of function used for surface blitting functions.

Definition at line 97 of file SDL_surface.h.

Function Documentation

◆ 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 1067 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.

1070 {
1071  SDL_Surface src_surface, dst_surface;
1072  SDL_PixelFormat src_fmt, dst_fmt;
1073  SDL_BlitMap src_blitmap, dst_blitmap;
1074  SDL_Rect rect;
1075  void *nonconst_src = (void *) src;
1076 
1077  /* Check to make sure we are blitting somewhere, so we don't crash */
1078  if (!dst) {
1079  return SDL_InvalidParamError("dst");
1080  }
1081  if (!dst_pitch) {
1082  return SDL_InvalidParamError("dst_pitch");
1083  }
1084 
1085  /* Fast path for same format copy */
1086  if (src_format == dst_format) {
1087  int bpp, i;
1088 
1089  if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
1090  switch (src_format) {
1091  case SDL_PIXELFORMAT_YUY2:
1092  case SDL_PIXELFORMAT_UYVY:
1093  case SDL_PIXELFORMAT_YVYU:
1094  bpp = 2;
1095  break;
1096  case SDL_PIXELFORMAT_YV12:
1097  case SDL_PIXELFORMAT_IYUV:
1098  case SDL_PIXELFORMAT_NV12:
1099  case SDL_PIXELFORMAT_NV21:
1100  bpp = 1;
1101  break;
1102  default:
1103  return SDL_SetError("Unknown FOURCC pixel format");
1104  }
1105  } else {
1106  bpp = SDL_BYTESPERPIXEL(src_format);
1107  }
1108  width *= bpp;
1109 
1110  for (i = height; i--;) {
1111  SDL_memcpy(dst, src, width);
1112  src = (Uint8*)src + src_pitch;
1113  dst = (Uint8*)dst + dst_pitch;
1114  }
1115 
1116  if (src_format == SDL_PIXELFORMAT_YV12 || src_format == SDL_PIXELFORMAT_IYUV) {
1117  /* U and V planes are a quarter the size of the Y plane */
1118  width /= 2;
1119  height /= 2;
1120  src_pitch /= 2;
1121  dst_pitch /= 2;
1122  for (i = height * 2; i--;) {
1123  SDL_memcpy(dst, src, width);
1124  src = (Uint8*)src + src_pitch;
1125  dst = (Uint8*)dst + dst_pitch;
1126  }
1127  } else if (src_format == SDL_PIXELFORMAT_NV12 || src_format == SDL_PIXELFORMAT_NV21) {
1128  /* U/V plane is half the height of the Y plane */
1129  height /= 2;
1130  for (i = height; i--;) {
1131  SDL_memcpy(dst, src, width);
1132  src = (Uint8*)src + src_pitch;
1133  dst = (Uint8*)dst + dst_pitch;
1134  }
1135  }
1136  return 0;
1137  }
1138 
1139  if (!SDL_CreateSurfaceOnStack(width, height, src_format, nonconst_src,
1140  src_pitch,
1141  &src_surface, &src_fmt, &src_blitmap)) {
1142  return -1;
1143  }
1144  if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst, dst_pitch,
1145  &dst_surface, &dst_fmt, &dst_blitmap)) {
1146  return -1;
1147  }
1148 
1149  /* Set up the rect and go! */
1150  rect.x = 0;
1151  rect.y = 0;
1152  rect.w = width;
1153  rect.h = height;
1154  return SDL_LowerBlit(&src_surface, &rect, &dst_surface, &rect);
1155 }
GLenum GLenum dst
GLint GLint GLsizei width
Definition: SDL_opengl.h:1565
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:1029
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
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1565
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:545
#define SDL_memcpy
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
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
int h
Definition: SDL_rect.h:67
#define SDL_ISPIXELFORMAT_FOURCC(format)
Definition: SDL_pixels.h:167
GLenum src
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 887 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_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_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_GetRGBA, SDL_InvalidateMap(), SDL_LowerBlit(), SDL_MapRGBA, SDL_memcmp, SDL_memcpy, SDL_RLEACCEL, SDL_SetClipRect(), SDL_SetColorKey(), SDL_SetError, SDL_SetSurfaceBlendMode(), SDL_SetSurfaceRLE(), SDL_TRUE, SDL_Rect::w, SDL_Surface::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by SDL_ConvertSurfaceFormat().

889 {
890  SDL_Surface *convert;
891  Uint32 copy_flags;
892  SDL_Color copy_color;
893  SDL_Rect bounds;
894 
895  /* Check for empty destination palette! (results in empty image) */
896  if (format->palette != NULL) {
897  int i;
898  for (i = 0; i < format->palette->ncolors; ++i) {
899  if ((format->palette->colors[i].r != 0xFF) ||
900  (format->palette->colors[i].g != 0xFF) ||
901  (format->palette->colors[i].b != 0xFF))
902  break;
903  }
904  if (i == format->palette->ncolors) {
905  SDL_SetError("Empty destination palette");
906  return (NULL);
907  }
908  }
909 
910  /* Create a new surface with the desired format */
911  convert = SDL_CreateRGBSurface(flags, surface->w, surface->h,
912  format->BitsPerPixel, format->Rmask,
913  format->Gmask, format->Bmask,
914  format->Amask);
915  if (convert == NULL) {
916  return (NULL);
917  }
918 
919  /* Copy the palette if any */
920  if (format->palette && convert->format->palette) {
921  SDL_memcpy(convert->format->palette->colors,
922  format->palette->colors,
923  format->palette->ncolors * sizeof(SDL_Color));
924  convert->format->palette->ncolors = format->palette->ncolors;
925  }
926 
927  /* Save the original copy flags */
928  copy_flags = surface->map->info.flags;
929  copy_color.r = surface->map->info.r;
930  copy_color.g = surface->map->info.g;
931  copy_color.b = surface->map->info.b;
932  copy_color.a = surface->map->info.a;
933  surface->map->info.r = 0xFF;
934  surface->map->info.g = 0xFF;
935  surface->map->info.b = 0xFF;
936  surface->map->info.a = 0xFF;
937  surface->map->info.flags = 0;
938  SDL_InvalidateMap(surface->map);
939 
940  /* Copy over the image data */
941  bounds.x = 0;
942  bounds.y = 0;
943  bounds.w = surface->w;
944  bounds.h = surface->h;
945  SDL_LowerBlit(surface, &bounds, convert, &bounds);
946 
947  /* Clean up the original surface, and update converted surface */
948  convert->map->info.r = copy_color.r;
949  convert->map->info.g = copy_color.g;
950  convert->map->info.b = copy_color.b;
951  convert->map->info.a = copy_color.a;
952  convert->map->info.flags =
953  (copy_flags &
957  surface->map->info.r = copy_color.r;
958  surface->map->info.g = copy_color.g;
959  surface->map->info.b = copy_color.b;
960  surface->map->info.a = copy_color.a;
961  surface->map->info.flags = copy_flags;
962  SDL_InvalidateMap(surface->map);
963  if (copy_flags & SDL_COPY_COLORKEY) {
964  SDL_bool set_colorkey_by_color = SDL_FALSE;
965 
966  if (surface->format->palette) {
967  if (format->palette &&
968  surface->format->palette->ncolors <= format->palette->ncolors &&
969  (SDL_memcmp(surface->format->palette->colors, format->palette->colors,
970  surface->format->palette->ncolors * sizeof(SDL_Color)) == 0)) {
971  /* The palette is identical, just set the same colorkey */
972  SDL_SetColorKey(convert, 1, surface->map->info.colorkey);
973  } else if (format->Amask) {
974  /* The alpha was set in the destination from the palette */
975  } else {
976  set_colorkey_by_color = SDL_TRUE;
977  }
978  } else {
979  set_colorkey_by_color = SDL_TRUE;
980  }
981 
982  if (set_colorkey_by_color) {
983  /* Set the colorkey by color, which needs to be unique */
984  Uint8 keyR, keyG, keyB, keyA;
985 
986  SDL_GetRGBA(surface->map->info.colorkey, surface->format, &keyR,
987  &keyG, &keyB, &keyA);
988  SDL_SetColorKey(convert, 1,
989  SDL_MapRGBA(convert->format, keyR, keyG, keyB, keyA));
990  /* This is needed when converting for 3D texture upload */
992  }
993  }
994  SDL_SetClipRect(convert, &surface->clip_rect);
995 
996  /* Enable alpha blending by default if the new surface has an
997  * alpha channel or alpha modulation */
998  if ((surface->format->Amask && format->Amask) ||
999  (copy_flags & (SDL_COPY_COLORKEY|SDL_COPY_MODULATE_ALPHA))) {
1001  }
1002  if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
1003  SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
1004  }
1005 
1006  /* We're ready to go! */
1007  return (convert);
1008 }
Uint8 r
Definition: SDL_blit.h:70
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1565
Uint8 b
Definition: SDL_blit.h:70
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:502
#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:201
Uint8 g
Definition: SDL_pixels.h:296
#define SDL_MapRGBA
Uint8 g
Definition: SDL_blit.h:70
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:436
static void SDL_ConvertColorkeyToAlpha(SDL_Surface *surface)
Definition: SDL_surface.c:285
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:161
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:545
int SDL_SetColorKey(SDL_Surface *surface, int flag, Uint32 key)
Sets the color key (transparent pixel) in a blittable surface.
Definition: SDL_surface.c:222
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:974
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
#define SDL_memcpy
Uint8 r
Definition: SDL_pixels.h:295
Uint8 a
Definition: SDL_pixels.h:298
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
#define SDL_memcmp
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
#define SDL_GetRGBA
SDL_Rect clip_rect
Definition: SDL_surface.h:85
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:143
SDL_bool
Definition: SDL_stdinc.h:130
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:124
#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
GLbitfield flags
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:91
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 src,
Uint32  pixel_format,
Uint32  flags 
)

Definition at line 1011 of file SDL_surface.c.

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

1013 {
1014  SDL_PixelFormat *fmt;
1015  SDL_Surface *convert = NULL;
1016 
1018  if (fmt) {
1019  convert = SDL_ConvertSurface(surface, fmt, flags);
1020  SDL_FreeFormat(fmt);
1021  }
1022  return convert;
1023 }
SDL_Surface * SDL_ConvertSurface(SDL_Surface *surface, const SDL_PixelFormat *format, Uint32 flags)
Definition: SDL_surface.c:887
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:143
Uint32 pixel_format
Definition: testoverlay2.c:152
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 124 of file SDL_surface.c.

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

Referenced by SDL_ConvertSurface(), and SDL_CreateRGBSurfaceFrom().

127 {
128  Uint32 format;
129 
130  /* Get the pixel format */
131  format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask);
132  if (format == SDL_PIXELFORMAT_UNKNOWN) {
133  SDL_SetError("Unknown pixel format");
134  return NULL;
135  }
136 
138 }
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1565
GLint GLint GLsizei width
Definition: SDL_opengl.h:1565
#define SDL_MasksToPixelFormatEnum
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:161
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1565
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:143
GLint GLint GLsizei GLsizei GLsizei depth
Definition: SDL_opengl.h:1565
#define SDL_SetError
GLbitfield flags

◆ 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 144 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.

148 {
149  SDL_Surface *surface;
150 
151  surface = SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
152  if (surface != NULL) {
153  surface->flags |= SDL_PREALLOC;
154  surface->pixels = pixels;
155  surface->w = width;
156  surface->h = height;
157  surface->pitch = pitch;
158  SDL_SetClipRect(surface, NULL);
159  }
160  return surface;
161 }
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:502
GLint GLint GLsizei width
Definition: SDL_opengl.h:1565
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1565
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1565
Uint32 flags
Definition: SDL_surface.h:71
void * pixels
Definition: SDL_surface.h:75
#define NULL
Definition: begin_code.h:143
GLint GLint GLsizei GLsizei GLsizei depth
Definition: SDL_opengl.h:1565
SDL_Surface * SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
Definition: SDL_surface.c:124
#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_MAX_SINT32, SDL_memset, SDL_OutOfMemory, SDL_SetClipRect(), SDL_SetSurfaceBlendMode(), SDL_SetSurfacePalette(), void, and SDL_Surface::w.

Referenced by SDL_CreateRGBSurface(), and SDL_CreateRGBSurfaceWithFormatFrom().

38 {
39  Sint64 pitch;
40  SDL_Surface *surface;
41 
42  /* The flags are no longer used, make the compiler happy */
43  (void)flags;
44 
45  /* Allocate the surface */
46  surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
47  if (surface == NULL) {
49  return NULL;
50  }
51 
52  surface->format = SDL_AllocFormat(format);
53  if (!surface->format) {
54  SDL_FreeSurface(surface);
55  return NULL;
56  }
57 
58  surface->w = width;
59  surface->h = height;
60 
61  pitch = SDL_CalculatePitch(surface);
62  if (pitch < 0 || pitch > SDL_MAX_SINT32) {
63  /* Overflow... */
65  return NULL;
66  }
67  surface->pitch = (int)pitch;
68 
69  SDL_SetClipRect(surface, NULL);
70 
71  if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
72  SDL_Palette *palette =
73  SDL_AllocPalette((1 << surface->format->BitsPerPixel));
74  if (!palette) {
75  SDL_FreeSurface(surface);
76  return NULL;
77  }
78  if (palette->ncolors == 2) {
79  /* Create a black and white bitmap palette */
80  palette->colors[0].r = 0xFF;
81  palette->colors[0].g = 0xFF;
82  palette->colors[0].b = 0xFF;
83  palette->colors[1].r = 0x00;
84  palette->colors[1].g = 0x00;
85  palette->colors[1].b = 0x00;
86  }
87  SDL_SetSurfacePalette(surface, palette);
88  SDL_FreePalette(palette);
89  }
90 
91  /* Get the pixels */
92  if (surface->w && surface->h) {
93  surface->pixels = SDL_malloc(surface->h * surface->pitch);
94  if (!surface->pixels) {
95  SDL_FreeSurface(surface);
97  return NULL;
98  }
99  /* This is important for bitmaps */
100  SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
101  }
102 
103  /* Allocate an empty mapping */
104  surface->map = SDL_AllocBlitMap();
105  if (!surface->map) {
106  SDL_FreeSurface(surface);
107  return NULL;
108  }
109 
110  /* By default surface with an alpha mask are set up for blending */
111  if (surface->format->Amask) {
113  }
114 
115  /* The surface is ready to go */
116  surface->refcount = 1;
117  return surface;
118 }
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1565
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:502
GLint GLint GLsizei width
Definition: SDL_opengl.h:1565
Uint8 g
Definition: SDL_pixels.h:296
#define SDL_ISPIXELFORMAT_INDEXED(format)
Definition: SDL_pixels.h:134
Sint64 SDL_CalculatePitch(SDL_Surface *surface)
Definition: SDL_pixels.c:748
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:436
Uint8 b
Definition: SDL_pixels.h:297
#define SDL_AllocFormat
#define SDL_MAX_SINT32
A signed 32-bit integer type.
Definition: SDL_stdinc.h:155
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1565
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:187
#define SDL_AllocPalette
void SDL_FreeSurface(SDL_Surface *surface)
Definition: SDL_surface.c:1161
#define NULL
Definition: begin_code.h:143
#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
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:954
GLbitfield flags
#define SDL_malloc
int64_t Sint64
A signed 64-bit integer type.
Definition: SDL_stdinc.h:166
#define SDL_memset

◆ SDL_CreateRGBSurfaceWithFormatFrom()

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

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

171 {
172  SDL_Surface *surface;
173 
174  surface = SDL_CreateRGBSurfaceWithFormat(0, 0, 0, depth, format);
175  if (surface != NULL) {
176  surface->flags |= SDL_PREALLOC;
177  surface->pixels = pixels;
178  surface->w = width;
179  surface->h = height;
180  surface->pitch = pitch;
181  SDL_SetClipRect(surface, NULL);
182  }
183  return surface;
184 }
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1565
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:502
GLint GLint GLsizei width
Definition: SDL_opengl.h:1565
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1565
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1565
Uint32 flags
Definition: SDL_surface.h:71
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
#define NULL
Definition: begin_code.h:143
GLint GLint GLsizei GLsizei GLsizei depth
Definition: SDL_opengl.h:1565
#define SDL_PREALLOC
Definition: SDL_surface.h:53

◆ SDL_FillRect()

int SDL_FillRect ( SDL_Surface dst,
const SDL_Rect rect,
Uint32  color 
)

Performs a fast fill of the given rectangle with color.

If rect is NULL, the whole surface will be filled with color.

The color should be a pixel of the format used by the surface, and can be generated by the SDL_MapRGB() function.

Returns
0 on success, or -1 on error.

Definition at line 237 of file SDL_fillrect.c.

References SDL_PixelFormat::BitsPerPixel, SDL_PixelFormat::BytesPerPixel, SDL_Surface::clip_rect, SDL_Surface::format, SDL_Rect::h, SDL_Surface::pitch, SDL_Surface::pixels, SDL_FillRect1(), SDL_FillRect2(), SDL_FillRect3(), SDL_FillRect4(), SDL_HasSSE, SDL_IntersectRect, SDL_RectEmpty(), SDL_SetError, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by SDL_FillRects().

238 {
239  SDL_Rect clipped;
240  Uint8 *pixels;
241 
242  if (!dst) {
243  return SDL_SetError("Passed NULL destination surface");
244  }
245 
246  /* This function doesn't work on surfaces < 8 bpp */
247  if (dst->format->BitsPerPixel < 8) {
248  return SDL_SetError("SDL_FillRect(): Unsupported surface format");
249  }
250 
251  /* If 'rect' == NULL, then fill the whole surface */
252  if (rect) {
253  /* Perform clipping */
254  if (!SDL_IntersectRect(rect, &dst->clip_rect, &clipped)) {
255  return 0;
256  }
257  rect = &clipped;
258  } else {
259  rect = &dst->clip_rect;
260  /* Don't attempt to fill if the surface's clip_rect is empty */
261  if (SDL_RectEmpty(rect)) {
262  return 0;
263  }
264  }
265 
266  /* Perform software fill */
267  if (!dst->pixels) {
268  return SDL_SetError("SDL_FillRect(): You must lock the surface");
269  }
270 
271  pixels = (Uint8 *) dst->pixels + rect->y * dst->pitch +
272  rect->x * dst->format->BytesPerPixel;
273 
274  switch (dst->format->BytesPerPixel) {
275  case 1:
276  {
277  color |= (color << 8);
278  color |= (color << 16);
279 #ifdef __SSE__
280  if (SDL_HasSSE()) {
281  SDL_FillRect1SSE(pixels, dst->pitch, color, rect->w, rect->h);
282  break;
283  }
284 #endif
285  SDL_FillRect1(pixels, dst->pitch, color, rect->w, rect->h);
286  break;
287  }
288 
289  case 2:
290  {
291  color |= (color << 16);
292 #ifdef __SSE__
293  if (SDL_HasSSE()) {
294  SDL_FillRect2SSE(pixels, dst->pitch, color, rect->w, rect->h);
295  break;
296  }
297 #endif
298  SDL_FillRect2(pixels, dst->pitch, color, rect->w, rect->h);
299  break;
300  }
301 
302  case 3:
303  /* 24-bit RGB is a slow path, at least for now. */
304  {
305  SDL_FillRect3(pixels, dst->pitch, color, rect->w, rect->h);
306  break;
307  }
308 
309  case 4:
310  {
311 #ifdef __SSE__
312  if (SDL_HasSSE()) {
313  SDL_FillRect4SSE(pixels, dst->pitch, color, rect->w, rect->h);
314  break;
315  }
316 #endif
317  SDL_FillRect4(pixels, dst->pitch, color, rect->w, rect->h);
318  break;
319  }
320  }
321 
322  /* We're done! */
323  return 0;
324 }
Uint8 BytesPerPixel
Definition: SDL_pixels.h:318
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1565
#define SDL_IntersectRect
SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
Returns true if the rectangle has no area.
Definition: SDL_rect.h:82
static void SDL_FillRect3(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:197
void * pixels
Definition: SDL_surface.h:75
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
Uint8 BitsPerPixel
Definition: SDL_pixels.h:317
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
SDL_Rect clip_rect
Definition: SDL_surface.h:85
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_SetError
static void SDL_FillRect2(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:173
int h
Definition: SDL_rect.h:67
GLuint color
static void SDL_FillRect4(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:225
static void SDL_FillRect1(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:134
#define SDL_HasSSE
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64

◆ SDL_FillRects()

int SDL_FillRects ( SDL_Surface dst,
const SDL_Rect rects,
int  count,
Uint32  color 
)

Definition at line 327 of file SDL_fillrect.c.

References i, SDL_FillRect(), and SDL_SetError.

329 {
330  int i;
331  int status = 0;
332 
333  if (!rects) {
334  return SDL_SetError("SDL_FillRects() passed NULL rects");
335  }
336 
337  for (i = 0; i < count; ++i) {
338  status += SDL_FillRect(dst, &rects[i], color);
339  }
340  return status;
341 }
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1564
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
int SDL_FillRect(SDL_Surface *dst, const SDL_Rect *rect, Uint32 color)
Definition: SDL_fillrect.c:237
#define SDL_SetError
GLuint color

◆ SDL_FreeSurface()

void SDL_FreeSurface ( SDL_Surface surface)

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

1162 {
1163  if (surface == NULL) {
1164  return;
1165  }
1166  if (surface->flags & SDL_DONTFREE) {
1167  return;
1168  }
1169  if (--surface->refcount > 0) {
1170  return;
1171  }
1172  while (surface->locked > 0) {
1173  SDL_UnlockSurface(surface);
1174  }
1175  if (surface->flags & SDL_RLEACCEL) {
1176  SDL_UnRLESurface(surface, 0);
1177  }
1178  if (surface->format) {
1179  SDL_SetSurfacePalette(surface, NULL);
1180  SDL_FreeFormat(surface->format);
1181  surface->format = NULL;
1182  }
1183  if (surface->map != NULL) {
1184  SDL_FreeBlitMap(surface->map);
1185  surface->map = NULL;
1186  }
1187  if (!(surface->flags & SDL_PREALLOC)) {
1188  SDL_free(surface->pixels);
1189  }
1190  SDL_free(surface);
1191 }
void SDL_UnlockSurface(SDL_Surface *surface)
Definition: SDL_surface.c:869
#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:187
void SDL_free(void *mem)
#define SDL_FreeFormat
#define NULL
Definition: begin_code.h:143
SDL_PixelFormat * format
Definition: SDL_surface.h:72
void SDL_FreeBlitMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:1079
#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 526 of file SDL_surface.c.

References SDL_Surface::clip_rect.

527 {
528  if (surface && rect) {
529  *rect = surface->clip_rect;
530  }
531 }
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 267 of file SDL_surface.c.

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

268 {
269  if (!surface) {
270  return -1;
271  }
272 
273  if (!(surface->map->info.flags & SDL_COPY_COLORKEY)) {
274  return -1;
275  }
276 
277  if (key) {
278  *key = surface->map->info.colorkey;
279  }
280  return 0;
281 }
#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
SDL_BlitInfo info
Definition: SDL_blit.h:91

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

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

424 {
425  if (!surface) {
426  return -1;
427  }
428 
429  if (alpha) {
430  *alpha = surface->map->info.a;
431  }
432  return 0;
433 }
GLfloat GLfloat GLfloat alpha
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
SDL_BlitInfo info
Definition: SDL_blit.h:91
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 473 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.

474 {
475  if (!surface) {
476  return -1;
477  }
478 
479  if (!blendMode) {
480  return 0;
481  }
482 
483  switch (surface->map->
484  info.flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD)) {
485  case SDL_COPY_BLEND:
487  break;
488  case SDL_COPY_ADD:
490  break;
491  case SDL_COPY_MOD:
493  break;
494  default:
496  break;
497  }
498  return 0;
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
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 381 of file SDL_surface.c.

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

382 {
383  if (!surface) {
384  return -1;
385  }
386 
387  if (r) {
388  *r = surface->map->info.r;
389  }
390  if (g) {
391  *g = surface->map->info.g;
392  }
393  if (b) {
394  *b = surface->map->info.b;
395  }
396  return 0;
397 }
Uint8 r
Definition: SDL_blit.h:70
Uint8 b
Definition: SDL_blit.h:70
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2072
Uint8 g
Definition: SDL_blit.h:70
GLboolean GLboolean g
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLboolean GLboolean GLboolean b
SDL_BlitInfo info
Definition: SDL_blit.h:91

◆ SDL_LoadBMP_RW()

SDL_Surface* SDL_LoadBMP_RW ( SDL_RWops src,
int  freesrc 
)

Load a surface from a seekable SDL data stream (memory or file).

If freesrc is non-zero, the stream will be closed after being read.

The new surface should be freed with SDL_FreeSurface().

Returns
the new surface, or NULL if there was an error.

Definition at line 87 of file SDL_bmp.c.

References SDL_Color::a, SDL_Color::b, BI_BITFIELDS, BI_RGB, colors, SDL_Palette::colors, CorrectAlphaChannel(), done, SDL_Surface::format, SDL_Color::g, SDL_Surface::h, i, SDL_Palette::ncolors, NULL, SDL_Surface::pitch, SDL_Surface::pixels, SDL_Color::r, RW_SEEK_CUR, RW_SEEK_SET, SDL_ALPHA_OPAQUE, SDL_assert, SDL_ClearError, SDL_CreateRGBSurface, SDL_EFREAD, SDL_EFSEEK, SDL_Error, SDL_FALSE, SDL_FreeSurface, SDL_GetError, SDL_OutOfMemory, SDL_ReadLE16, SDL_ReadLE32, SDL_realloc, SDL_RWclose, SDL_RWread, SDL_RWseek, SDL_RWtell, SDL_SetError, SDL_strcmp, SDL_strncmp, SDL_Swap16(), SDL_Swap32(), SDL_TRUE, and SDL_Surface::w.

88 {
89  SDL_bool was_error;
90  Sint64 fp_offset = 0;
91  int bmpPitch;
92  int i, pad;
93  SDL_Surface *surface;
94  Uint32 Rmask = 0;
95  Uint32 Gmask = 0;
96  Uint32 Bmask = 0;
97  Uint32 Amask = 0;
98  SDL_Palette *palette;
99  Uint8 *bits;
100  Uint8 *top, *end;
101  SDL_bool topDown;
102  int ExpandBMP;
103  SDL_bool haveRGBMasks = SDL_FALSE;
104  SDL_bool haveAlphaMask = SDL_FALSE;
105  SDL_bool correctAlpha = SDL_FALSE;
106 
107  /* The Win32 BMP file header (14 bytes) */
108  char magic[2];
109  /* Uint32 bfSize = 0; */
110  /* Uint16 bfReserved1 = 0; */
111  /* Uint16 bfReserved2 = 0; */
112  Uint32 bfOffBits = 0;
113 
114  /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
115  Uint32 biSize = 0;
116  Sint32 biWidth = 0;
117  Sint32 biHeight = 0;
118  /* Uint16 biPlanes = 0; */
119  Uint16 biBitCount = 0;
120  Uint32 biCompression = 0;
121  /* Uint32 biSizeImage = 0; */
122  /* Sint32 biXPelsPerMeter = 0; */
123  /* Sint32 biYPelsPerMeter = 0; */
124  Uint32 biClrUsed = 0;
125  /* Uint32 biClrImportant = 0; */
126 
127  /* Make sure we are passed a valid data source */
128  surface = NULL;
129  was_error = SDL_FALSE;
130  if (src == NULL) {
131  was_error = SDL_TRUE;
132  goto done;
133  }
134 
135  /* Read in the BMP file header */
136  fp_offset = SDL_RWtell(src);
137  SDL_ClearError();
138  if (SDL_RWread(src, magic, 1, 2) != 2) {
140  was_error = SDL_TRUE;
141  goto done;
142  }
143  if (SDL_strncmp(magic, "BM", 2) != 0) {
144  SDL_SetError("File is not a Windows BMP file");
145  was_error = SDL_TRUE;
146  goto done;
147  }
148  /* bfSize = */ SDL_ReadLE32(src);
149  /* bfReserved1 = */ SDL_ReadLE16(src);
150  /* bfReserved2 = */ SDL_ReadLE16(src);
151  bfOffBits = SDL_ReadLE32(src);
152 
153  /* Read the Win32 BITMAPINFOHEADER */
154  biSize = SDL_ReadLE32(src);
155  if (biSize == 12) { /* really old BITMAPCOREHEADER */
156  biWidth = (Uint32) SDL_ReadLE16(src);
157  biHeight = (Uint32) SDL_ReadLE16(src);
158  /* biPlanes = */ SDL_ReadLE16(src);
159  biBitCount = SDL_ReadLE16(src);
160  biCompression = BI_RGB;
161  } else if (biSize >= 40) { /* some version of BITMAPINFOHEADER */
162  Uint32 headerSize;
163  biWidth = SDL_ReadLE32(src);
164  biHeight = SDL_ReadLE32(src);
165  /* biPlanes = */ SDL_ReadLE16(src);
166  biBitCount = SDL_ReadLE16(src);
167  biCompression = SDL_ReadLE32(src);
168  /* biSizeImage = */ SDL_ReadLE32(src);
169  /* biXPelsPerMeter = */ SDL_ReadLE32(src);
170  /* biYPelsPerMeter = */ SDL_ReadLE32(src);
171  biClrUsed = SDL_ReadLE32(src);
172  /* biClrImportant = */ SDL_ReadLE32(src);
173 
174  /* 64 == BITMAPCOREHEADER2, an incompatible OS/2 2.x extension. Skip this stuff for now. */
175  if (biSize == 64) {
176  /* ignore these extra fields. */
177  if (biCompression == BI_BITFIELDS) {
178  /* this value is actually huffman compression in this variant. */
179  SDL_SetError("Compressed BMP files not supported");
180  was_error = SDL_TRUE;
181  goto done;
182  }
183  } else {
184  /* This is complicated. If compression is BI_BITFIELDS, then
185  we have 3 DWORDS that specify the RGB masks. This is either
186  stored here in an BITMAPV2INFOHEADER (which only differs in
187  that it adds these RGB masks) and biSize >= 52, or we've got
188  these masks stored in the exact same place, but strictly
189  speaking, this is the bmiColors field in BITMAPINFO immediately
190  following the legacy v1 info header, just past biSize. */
191  if (biCompression == BI_BITFIELDS) {
192  haveRGBMasks = SDL_TRUE;
193  Rmask = SDL_ReadLE32(src);
194  Gmask = SDL_ReadLE32(src);
195  Bmask = SDL_ReadLE32(src);
196 
197  /* ...v3 adds an alpha mask. */
198  if (biSize >= 56) { /* BITMAPV3INFOHEADER; adds alpha mask */
199  haveAlphaMask = SDL_TRUE;
200  Amask = SDL_ReadLE32(src);
201  }
202  } else {
203  /* the mask fields are ignored for v2+ headers if not BI_BITFIELD. */
204  if (biSize >= 52) { /* BITMAPV2INFOHEADER; adds RGB masks */
205  /*Rmask = */ SDL_ReadLE32(src);
206  /*Gmask = */ SDL_ReadLE32(src);
207  /*Bmask = */ SDL_ReadLE32(src);
208  }
209  if (biSize >= 56) { /* BITMAPV3INFOHEADER; adds alpha mask */
210  /*Amask = */ SDL_ReadLE32(src);
211  }
212  }
213 
214  /* Insert other fields here; Wikipedia and MSDN say we're up to
215  v5 of this header, but we ignore those for now (they add gamma,
216  color spaces, etc). Ignoring the weird OS/2 2.x format, we
217  currently parse up to v3 correctly (hopefully!). */
218  }
219 
220  /* skip any header bytes we didn't handle... */
221  headerSize = (Uint32) (SDL_RWtell(src) - (fp_offset + 14));
222  if (biSize > headerSize) {
223  SDL_RWseek(src, (biSize - headerSize), RW_SEEK_CUR);
224  }
225  }
226  if (biWidth <= 0 || biHeight == 0) {
227  SDL_SetError("BMP file with bad dimensions (%dx%d)", biWidth, biHeight);
228  was_error = SDL_TRUE;
229  goto done;
230  }
231  if (biHeight < 0) {
232  topDown = SDL_TRUE;
233  biHeight = -biHeight;
234  } else {
235  topDown = SDL_FALSE;
236  }
237 
238  /* Check for read error */
239  if (SDL_strcmp(SDL_GetError(), "") != 0) {
240  was_error = SDL_TRUE;
241  goto done;
242  }
243 
244  /* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
245  switch (biBitCount) {
246  case 1:
247  case 4:
248  ExpandBMP = biBitCount;
249  biBitCount = 8;
250  break;
251  case 2:
252  case 3:
253  case 5:
254  case 6:
255  case 7:
256  SDL_SetError("%d-bpp BMP images are not supported", biBitCount);
257  was_error = SDL_TRUE;
258  goto done;
259  default:
260  ExpandBMP = 0;
261  break;
262  }
263 
264  /* We don't support any BMP compression right now */
265  switch (biCompression) {
266  case BI_RGB:
267  /* If there are no masks, use the defaults */
268  SDL_assert(!haveRGBMasks);
269  SDL_assert(!haveAlphaMask);
270  /* Default values for the BMP format */
271  switch (biBitCount) {
272  case 15:
273  case 16:
274  Rmask = 0x7C00;
275  Gmask = 0x03E0;
276  Bmask = 0x001F;
277  break;
278  case 24:
279 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
280  Rmask = 0x000000FF;
281  Gmask = 0x0000FF00;
282  Bmask = 0x00FF0000;
283 #else
284  Rmask = 0x00FF0000;
285  Gmask = 0x0000FF00;
286  Bmask = 0x000000FF;
287 #endif
288  break;
289  case 32:
290  /* We don't know if this has alpha channel or not */
291  correctAlpha = SDL_TRUE;
292  Amask = 0xFF000000;
293  Rmask = 0x00FF0000;
294  Gmask = 0x0000FF00;
295  Bmask = 0x000000FF;
296  break;
297  default:
298  break;
299  }
300  break;
301 
302  case BI_BITFIELDS:
303  break; /* we handled this in the info header. */
304 
305  default:
306  SDL_SetError("Compressed BMP files not supported");
307  was_error = SDL_TRUE;
308  goto done;
309  }
310 
311  /* Create a compatible surface, note that the colors are RGB ordered */
312  surface =
313  SDL_CreateRGBSurface(0, biWidth, biHeight, biBitCount, Rmask, Gmask,
314  Bmask, Amask);
315  if (surface == NULL) {
316  was_error = SDL_TRUE;
317  goto done;
318  }
319 
320  /* Load the palette, if any */
321  palette = (surface->format)->palette;
322  if (palette) {
323  SDL_assert(biBitCount <= 8);
324  if (biClrUsed == 0) {
325  biClrUsed = 1 << biBitCount;
326  } else if (biClrUsed > (1 << biBitCount)) {
327  SDL_SetError("BMP file has an invalid number of colors");
328  was_error = SDL_TRUE;
329  goto done;
330  }
331  if ((int) biClrUsed > palette->ncolors) {
332  SDL_Color *colors;
333  int ncolors = biClrUsed;
334  colors =
335  (SDL_Color *) SDL_realloc(palette->colors,
336  ncolors *
337  sizeof(*palette->colors));
338  if (!colors) {
339  SDL_OutOfMemory();
340  was_error = SDL_TRUE;
341  goto done;
342  }
343  palette->ncolors = ncolors;
344  palette->colors = colors;
345  } else if ((int) biClrUsed < palette->ncolors) {
346  palette->ncolors = biClrUsed;
347  }
348  if (biSize == 12) {
349  for (i = 0; i < (int) biClrUsed; ++i) {
350  SDL_RWread(src, &palette->colors[i].b, 1, 1);
351  SDL_RWread(src, &palette->colors[i].g, 1, 1);
352  SDL_RWread(src, &palette->colors[i].r, 1, 1);
353  palette->colors[i].a = SDL_ALPHA_OPAQUE;
354  }
355  } else {
356  for (i = 0; i < (int) biClrUsed; ++i) {
357  SDL_RWread(src, &palette->colors[i].b, 1, 1);
358  SDL_RWread(src, &palette->colors[i].g, 1, 1);
359  SDL_RWread(src, &palette->colors[i].r, 1, 1);
360  SDL_RWread(src, &palette->colors[i].a, 1, 1);
361 
362  /* According to Microsoft documentation, the fourth element
363  is reserved and must be zero, so we shouldn't treat it as
364  alpha.
365  */
366  palette->colors[i].a = SDL_ALPHA_OPAQUE;
367  }
368  }
369  }
370 
371  /* Read the surface pixels. Note that the bmp image is upside down */
372  if (SDL_RWseek(src, fp_offset + bfOffBits, RW_SEEK_SET) < 0) {
374  was_error = SDL_TRUE;
375  goto done;
376  }
377  top = (Uint8 *)surface->pixels;
378  end = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
379  switch (ExpandBMP) {
380  case 1:
381  bmpPitch = (biWidth + 7) >> 3;
382  pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
383  break;
384  case 4:
385  bmpPitch = (biWidth + 1) >> 1;
386  pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
387  break;
388  default:
389  pad = ((surface->pitch % 4) ? (4 - (surface->pitch % 4)) : 0);
390  break;
391  }
392  if (topDown) {
393  bits = top;
394  } else {
395  bits = end - surface->pitch;
396  }
397  while (bits >= top && bits < end) {
398  switch (ExpandBMP) {
399  case 1:
400  case 4:{
401  Uint8 pixel = 0;
402  int shift = (8 - ExpandBMP);
403  for (i = 0; i < surface->w; ++i) {
404  if (i % (8 / ExpandBMP) == 0) {
405  if (!SDL_RWread(src, &pixel, 1, 1)) {
406  SDL_SetError("Error reading from BMP");
407  was_error = SDL_TRUE;
408  goto done;
409  }
410  }
411  bits[i] = (pixel >> shift);
412  if (bits[i] >= biClrUsed) {
413  SDL_SetError("A BMP image contains a pixel with a color out of the palette");
414  was_error = SDL_TRUE;
415  goto done;
416  }
417  pixel <<= ExpandBMP;
418  }
419  }
420  break;
421 
422  default:
423  if (SDL_RWread(src, bits, 1, surface->pitch) != surface->pitch) {
425  was_error = SDL_TRUE;
426  goto done;
427  }
428  if (biBitCount == 8 && palette && biClrUsed < (1 << biBitCount)) {
429  for (i = 0; i < surface->w; ++i) {
430  if (bits[i] >= biClrUsed) {
431  SDL_SetError("A BMP image contains a pixel with a color out of the palette");
432  was_error = SDL_TRUE;
433  goto done;
434  }
435  }
436  }
437 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
438  /* Byte-swap the pixels if needed. Note that the 24bpp
439  case has already been taken care of above. */
440  switch (biBitCount) {
441  case 15:
442  case 16:{
443  Uint16 *pix = (Uint16 *) bits;
444  for (i = 0; i < surface->w; i++)
445  pix[i] = SDL_Swap16(pix[i]);
446  break;
447  }
448 
449  case 32:{
450  Uint32 *pix = (Uint32 *) bits;
451  for (i = 0; i < surface->w; i++)
452  pix[i] = SDL_Swap32(pix[i]);
453  break;
454  }
455  }
456 #endif
457  break;
458  }
459  /* Skip padding bytes, ugh */
460  if (pad) {
461  Uint8 padbyte;
462  for (i = 0; i < pad; ++i) {
463  SDL_RWread(src, &padbyte, 1, 1);
464  }
465  }
466  if (topDown) {
467  bits += surface->pitch;
468  } else {
469  bits -= surface->pitch;
470  }
471  }
472  if (correctAlpha) {
473  CorrectAlphaChannel(surface);
474  }
475  done:
476  if (was_error) {
477  if (src) {
478  SDL_RWseek(src, fp_offset, RW_SEEK_SET);
479  }
480  SDL_FreeSurface(surface);
481  surface = NULL;
482  }
483  if (freesrc && src) {
484  SDL_RWclose(src);
485  }
486  return (surface);
487 }
#define BI_RGB
Definition: SDL_bmp.c:45
#define SDL_ClearError
#define SDL_GetError
Uint8 g
Definition: SDL_pixels.h:296
GLuint GLuint end
Definition: SDL_opengl.h:1564
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_ReadLE32
#define SDL_RWread(ctx, ptr, size, n)
Definition: SDL_rwops.h:187
Uint8 b
Definition: SDL_pixels.h:297
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const void * bits
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:161
#define SDL_realloc
static void CorrectAlphaChannel(SDL_Surface *surface)
Definition: SDL_bmp.c:57
GLdouble GLdouble GLdouble GLdouble top
#define SDL_strncmp
#define SDL_Error
#define SDL_RWseek(ctx, offset, whence)
Definition: SDL_rwops.h:185
#define SDL_ReadLE16
SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)
Definition: SDL_endian.h:141
Uint8 r
Definition: SDL_pixels.h:295
void * pixels
Definition: SDL_surface.h:75
#define SDL_FreeSurface
Uint8 a
Definition: SDL_pixels.h:298
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
int done
Definition: checkkeys.c:28
SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)
Definition: SDL_endian.h:101
int32_t Sint32
Definition: SDL_stdinc.h:157
#define BI_BITFIELDS
Definition: SDL_bmp.c:48
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_assert(condition)
Definition: SDL_assert.h:167
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_bool
Definition: SDL_stdinc.h:130
SDL_Color * colors
Definition: SDL_pixels.h:305
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_RWclose(ctx)
Definition: SDL_rwops.h:189
#define SDL_SetError
#define SDL_CreateRGBSurface
#define RW_SEEK_SET
Definition: SDL_rwops.h:174
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:151
#define SDL_strcmp
#define RW_SEEK_CUR
Definition: SDL_rwops.h:175
int64_t Sint64
A signed 64-bit integer type.
Definition: SDL_stdinc.h:166
static int colors[7]
Definition: testgesture.c:40
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
#define SDL_RWtell(ctx)
Definition: SDL_rwops.h:186

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

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

Referenced by SDL_ConvertColorkeyToAlpha().

849 {
850  if (!surface->locked) {
851  /* Perform the lock */
852  if (surface->flags & SDL_RLEACCEL) {
853  SDL_UnRLESurface(surface, 1);
854  surface->flags |= SDL_RLEACCEL; /* save accel'd state */
855  }
856  }
857 
858  /* Increment the surface lock count, for recursive locks */
859  ++surface->locked;
860 
861  /* Ready to go.. */
862  return (0);
863 }
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 545 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().

547 {
548  /* Check to make sure the blit mapping is valid */
549  if ((src->map->dst != dst) ||
550  (dst->format->palette &&
551  src->map->dst_palette_version != dst->format->palette->version) ||
552  (src->format->palette &&
553  src->map->src_palette_version != src->format->palette->version)) {
554  if (SDL_MapSurface(src, dst) < 0) {
555  return (-1);
556  }
557  /* just here for debugging */
558 /* printf */
559 /* ("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", */
560 /* src, dst->flags, src->map->info.flags, dst, dst->flags, */
561 /* dst->map->info.flags, src->map->blit); */
562  }
563  return (src->map->blit(src, srcrect, dst, dstrect));
564 }
Uint32 version
Definition: SDL_pixels.h:306
SDL_blit blit
Definition: SDL_blit.h:89
Uint32 dst_palette_version
Definition: SDL_blit.h:95
Uint32 src_palette_version
Definition: SDL_blit.h:96
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
int SDL_MapSurface(SDL_Surface *src, SDL_Surface *dst)
Definition: SDL_pixels.c:993
SDL_Surface * dst
Definition: SDL_blit.h:87
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 821 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().

823 {
824  static const Uint32 complex_copy_flags = (
828  );
829 
830  if (!(src->map->info.flags & SDL_COPY_NEAREST)) {
831  src->map->info.flags |= SDL_COPY_NEAREST;
832  SDL_InvalidateMap(src->map);
833  }
834 
835  if ( !(src->map->info.flags & complex_copy_flags) &&
836  src->format->format == dst->format->format &&
838  return SDL_SoftStretch( src, srcrect, dst, dstrect );
839  } else {
840  return SDL_LowerBlit( src, srcrect, dst, dstrect );
841  }
842 }
#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:161
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:545
#define SDL_COPY_ADD
Definition: SDL_blit.h:37
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:974
#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:91

◆ SDL_SaveBMP_RW()

int SDL_SaveBMP_RW ( SDL_Surface surface,
SDL_RWops dst,
int  freedst 
)

Save a surface to a seekable SDL data stream (memory or file).

Surfaces with a 24-bit, 32-bit and paletted 8-bit format get saved in the BMP directly. Other RGB formats with 8-bit or higher get converted to a 24-bit surface or, if they have an alpha mask or a colorkey, to a 32-bit surface before they are saved. YUV and paletted 1-bit and 4-bit formats are not supported.

If freedst is non-zero, the stream will be closed after being written.

Returns
0 if successful or -1 if there was an error.

Definition at line 490 of file SDL_bmp.c.

References SDL_PixelFormat::Amask, BI_BITFIELDS, BI_RGB, SDL_PixelFormat::BitsPerPixel, SDL_PixelFormat::Bmask, SDL_PixelFormat::BytesPerPixel, colors, SDL_Palette::colors, SDL_BlitInfo::flags, SDL_Surface::format, SDL_PixelFormat::Gmask, SDL_Surface::h, i, SDL_BlitMap::info, LCS_WINDOWS_COLOR_SPACE, SDL_Surface::map, SDL_Palette::ncolors, NULL, SDL_PixelFormat::palette, SDL_Surface::pitch, SDL_Surface::pixels, SDL_PixelFormat::Rmask, RW_SEEK_SET, SDL_BYTEORDER, SDL_ClearError, SDL_ConvertSurface, SDL_COPY_COLORKEY, SDL_EFSEEK, SDL_EFWRITE, SDL_Error, SDL_FALSE, SDL_FreeSurface, SDL_GetError, SDL_GetHintBoolean, SDL_HINT_BMP_SAVE_LEGACY_FORMAT, SDL_InitFormat(), SDL_LIL_ENDIAN, SDL_LockSurface, SDL_PIXELFORMAT_BGR24, SDL_PIXELFORMAT_BGRA32, SDL_RWclose, SDL_RWseek, SDL_RWtell, SDL_RWwrite, SDL_SetError, SDL_strcmp, SDL_TRUE, SDL_UnlockSurface, SDL_WriteLE16, SDL_WriteLE32, and SDL_Surface::w.

491 {
492  Sint64 fp_offset;
493  int i, pad;
494  SDL_Surface *surface;
495  Uint8 *bits;
496  SDL_bool save32bit = SDL_FALSE;
497  SDL_bool saveLegacyBMP = SDL_FALSE;
498 
499  /* The Win32 BMP file header (14 bytes) */
500  char magic[2] = { 'B', 'M' };
501  Uint32 bfSize;
502  Uint16 bfReserved1;
503  Uint16 bfReserved2;
504  Uint32 bfOffBits;
505 
506  /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
507  Uint32 biSize;
508  Sint32 biWidth;
509  Sint32 biHeight;
510  Uint16 biPlanes;
511  Uint16 biBitCount;
512  Uint32 biCompression;
513  Uint32 biSizeImage;
514  Sint32 biXPelsPerMeter;
515  Sint32 biYPelsPerMeter;
516  Uint32 biClrUsed;
517  Uint32 biClrImportant;
518 
519  /* The additional header members from the Win32 BITMAPV4HEADER struct (108 bytes in total) */
520  Uint32 bV4RedMask = 0;
521  Uint32 bV4GreenMask = 0;
522  Uint32 bV4BlueMask = 0;
523  Uint32 bV4AlphaMask = 0;
524  Uint32 bV4CSType = 0;
525  Sint32 bV4Endpoints[3 * 3] = {0};
526  Uint32 bV4GammaRed = 0;
527  Uint32 bV4GammaGreen = 0;
528  Uint32 bV4GammaBlue = 0;
529 
530  /* Make sure we have somewhere to save */
531  surface = NULL;
532  if (dst) {
533 #ifdef SAVE_32BIT_BMP
534  /* We can save alpha information in a 32-bit BMP */
535  if (saveme->format->BitsPerPixel >= 8 && (saveme->format->Amask ||
536  saveme->map->info.flags & SDL_COPY_COLORKEY)) {
537  save32bit = SDL_TRUE;
538  }
539 #endif /* SAVE_32BIT_BMP */
540 
541  if (saveme->format->palette && !save32bit) {
542  if (saveme->format->BitsPerPixel == 8) {
543  surface = saveme;
544  } else {
545  SDL_SetError("%d bpp BMP files not supported",
546  saveme->format->BitsPerPixel);
547  }
548  } else if ((saveme->format->BitsPerPixel == 24) && !save32bit &&
550  (saveme->format->Rmask == 0x00FF0000) &&
551  (saveme->format->Gmask == 0x0000FF00) &&
552  (saveme->format->Bmask == 0x000000FF)
553 #else
554  (saveme->format->Rmask == 0x000000FF) &&
555  (saveme->format->Gmask == 0x0000FF00) &&
556  (saveme->format->Bmask == 0x00FF0000)
557 #endif
558  ) {
559  surface = saveme;
560  } else {
562 
563  /* If the surface has a colorkey or alpha channel we'll save a
564  32-bit BMP with alpha channel, otherwise save a 24-bit BMP. */
565  if (save32bit) {
567  } else {
569  }
570  surface = SDL_ConvertSurface(saveme, &format, 0);
571  if (!surface) {
572  SDL_SetError("Couldn't convert image to %d bpp",
573  format.BitsPerPixel);
574  }
575  }
576  } else {
577  /* Set no error here because it may overwrite a more useful message from
578  SDL_RWFromFile() if SDL_SaveBMP_RW() is called from SDL_SaveBMP(). */
579  return -1;
580  }
581 
582  if (save32bit) {
584  }
585 
586  if (surface && (SDL_LockSurface(surface) == 0)) {
587  const int bw = surface->w * surface->format->BytesPerPixel;
588 
589  /* Set the BMP file header values */
590  bfSize = 0; /* We'll write this when we're done */
591  bfReserved1 = 0;
592  bfReserved2 = 0;
593  bfOffBits = 0; /* We'll write this when we're done */
594 
595  /* Write the BMP file header values */
596  fp_offset = SDL_RWtell(dst);
597  SDL_ClearError();
598  SDL_RWwrite(dst, magic, 2, 1);
599  SDL_WriteLE32(dst, bfSize);
600  SDL_WriteLE16(dst, bfReserved1);
601  SDL_WriteLE16(dst, bfReserved2);
602  SDL_WriteLE32(dst, bfOffBits);
603 
604  /* Set the BMP info values */
605  biSize = 40;
606  biWidth = surface->w;
607  biHeight = surface->h;
608  biPlanes = 1;
609  biBitCount = surface->format->BitsPerPixel;
610  biCompression = BI_RGB;
611  biSizeImage = surface->h * surface->pitch;
612  biXPelsPerMeter = 0;
613  biYPelsPerMeter = 0;
614  if (surface->format->palette) {
615  biClrUsed = surface->format->palette->ncolors;
616  } else {
617  biClrUsed = 0;
618  }
619  biClrImportant = 0;
620 
621  /* Set the BMP info values for the version 4 header */
622  if (save32bit && !saveLegacyBMP) {
623  biSize = 108;
624  biCompression = BI_BITFIELDS;
625  /* The BMP format is always little endian, these masks stay the same */
626  bV4RedMask = 0x00ff0000;
627  bV4GreenMask = 0x0000ff00;
628  bV4BlueMask = 0x000000ff;
629  bV4AlphaMask = 0xff000000;
630  bV4CSType = LCS_WINDOWS_COLOR_SPACE;
631  bV4GammaRed = 0;
632  bV4GammaGreen = 0;
633  bV4GammaBlue = 0;
634  }
635 
636  /* Write the BMP info values */
637  SDL_WriteLE32(dst, biSize);
638  SDL_WriteLE32(dst, biWidth);
639  SDL_WriteLE32(dst, biHeight);
640  SDL_WriteLE16(dst, biPlanes);
641  SDL_WriteLE16(dst, biBitCount);
642  SDL_WriteLE32(dst, biCompression);
643  SDL_WriteLE32(dst, biSizeImage);
644  SDL_WriteLE32(dst, biXPelsPerMeter);
645  SDL_WriteLE32(dst, biYPelsPerMeter);
646  SDL_WriteLE32(dst, biClrUsed);
647  SDL_WriteLE32(dst, biClrImportant);
648 
649  /* Write the BMP info values for the version 4 header */
650  if (save32bit && !saveLegacyBMP) {
651  SDL_WriteLE32(dst, bV4RedMask);
652  SDL_WriteLE32(dst, bV4GreenMask);
653  SDL_WriteLE32(dst, bV4BlueMask);
654  SDL_WriteLE32(dst, bV4AlphaMask);
655  SDL_WriteLE32(dst, bV4CSType);
656  for (i = 0; i < 3 * 3; i++) {
657  SDL_WriteLE32(dst, bV4Endpoints[i]);
658  }
659  SDL_WriteLE32(dst, bV4GammaRed);
660  SDL_WriteLE32(dst, bV4GammaGreen);
661  SDL_WriteLE32(dst, bV4GammaBlue);
662  }
663 
664  /* Write the palette (in BGR color order) */
665  if (surface->format->palette) {
666  SDL_Color *colors;
667  int ncolors;
668 
669  colors = surface->format->palette->colors;
670  ncolors = surface->format->palette->ncolors;
671  for (i = 0; i < ncolors; ++i) {
672  SDL_RWwrite(dst, &colors[i].b, 1, 1);
673  SDL_RWwrite(dst, &colors[i].g, 1, 1);
674  SDL_RWwrite(dst, &colors[i].r, 1, 1);
675  SDL_RWwrite(dst, &colors[i].a, 1, 1);
676  }
677  }
678 
679  /* Write the bitmap offset */
680  bfOffBits = (Uint32)(SDL_RWtell(dst) - fp_offset);
681  if (SDL_RWseek(dst, fp_offset + 10, RW_SEEK_SET) < 0) {
683  }
684  SDL_WriteLE32(dst, bfOffBits);
685  if (SDL_RWseek(dst, fp_offset + bfOffBits, RW_SEEK_SET) < 0) {
687  }
688 
689  /* Write the bitmap image upside down */
690  bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
691  pad = ((bw % 4) ? (4 - (bw % 4)) : 0);
692  while (bits > (Uint8 *) surface->pixels) {
693  bits -= surface->pitch;
694  if (SDL_RWwrite(dst, bits, 1, bw) != bw) {
696  break;
697  }
698  if (pad) {
699  const Uint8 padbyte = 0;
700  for (i = 0; i < pad; ++i) {
701  SDL_RWwrite(dst, &padbyte, 1, 1);
702  }
703  }
704  }
705 
706  /* Write the BMP file size */
707  bfSize = (Uint32)(SDL_RWtell(dst) - fp_offset);
708  if (SDL_RWseek(dst, fp_offset + 2, RW_SEEK_SET) < 0) {
710  }
711  SDL_WriteLE32(dst, bfSize);
712  if (SDL_RWseek(dst, fp_offset + bfSize, RW_SEEK_SET) < 0) {
714  }
715 
716  /* Close it up.. */
717  SDL_UnlockSurface(surface);
718  if (surface != saveme) {
719  SDL_FreeSurface(surface);
720  }
721  }
722 
723  if (freedst && dst) {
724  SDL_RWclose(dst);
725  }
726  return ((SDL_strcmp(SDL_GetError(), "") == 0) ? 0 : -1);
727 }
#define BI_RGB
Definition: SDL_bmp.c:45
#define SDL_ClearError
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1565
#define SDL_GetError
#define SDL_UnlockSurface
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2072
#define SDL_COPY_COLORKEY
Definition: SDL_blit.h:39
#define LCS_WINDOWS_COLOR_SPACE
Definition: SDL_bmp.c:54
#define SDL_RWwrite(ctx, ptr, size, n)
Definition: SDL_rwops.h:188
Uint8 BytesPerPixel
Definition: SDL_pixels.h:318
#define SDL_ConvertSurface
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_WriteLE16
#define SDL_LIL_ENDIAN
Definition: SDL_endian.h:37
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const void * bits
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:161
#define SDL_Error
#define SDL_GetHintBoolean
#define SDL_RWseek(ctx, offset, whence)
Definition: SDL_rwops.h:185
GLboolean GLboolean g
void * pixels
Definition: SDL_surface.h:75
#define SDL_FreeSurface
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
Uint8 BitsPerPixel
Definition: SDL_pixels.h:317
#define SDL_HINT_BMP_SAVE_LEGACY_FORMAT
Prevent SDL from using version 4 of the bitmap header when saving BMPs.
Definition: SDL_hints.h:689
int32_t Sint32
Definition: SDL_stdinc.h:157
int SDL_InitFormat(SDL_PixelFormat *format, Uint32 pixel_format)
Definition: SDL_pixels.c:521
#define BI_BITFIELDS
Definition: SDL_bmp.c:48
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:143
SDL_bool
Definition: SDL_stdinc.h:130
SDL_Color * colors
Definition: SDL_pixels.h:305
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_RWclose(ctx)
Definition: SDL_rwops.h:189
#define SDL_SetError
#define SDL_LockSurface
#define SDL_WriteLE32
#define RW_SEEK_SET
Definition: SDL_rwops.h:174
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:151
SDL_Palette * palette
Definition: SDL_pixels.h:316
#define SDL_strcmp
int64_t Sint64
A signed 64-bit integer type.
Definition: SDL_stdinc.h:166
static int colors[7]
Definition: testgesture.c:40
GLboolean GLboolean GLboolean GLboolean a
GLboolean GLboolean GLboolean b
#define SDL_RWtell(ctx)
Definition: SDL_rwops.h:186
#define SDL_BYTEORDER

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

503 {
504  SDL_Rect full_rect;
505 
506  /* Don't do anything if there's no surface to act on */
507  if (!surface) {
508  return SDL_FALSE;
509  }
510 
511  /* Set up the full surface rectangle */
512  full_rect.x = 0;
513  full_rect.y = 0;
514  full_rect.w = surface->w;
515  full_rect.h = surface->h;
516 
517  /* Set the clipping rectangle */
518  if (!rect) {
519  surface->clip_rect = full_rect;
520  return SDL_TRUE;
521  }
522  return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
523 }
#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 222 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().

223 {
224  int flags;
225 
226  if (!surface) {
227  return SDL_InvalidParamError("surface");
228  }
229 
230  if (surface->format->palette && key >= ((Uint32) surface->format->palette->ncolors)) {
231  return SDL_InvalidParamError("key");
232  }
233 
234  if (flag & SDL_RLEACCEL) {
235  SDL_SetSurfaceRLE(surface, 1);
236  }
237 
238  flags = surface->map->info.flags;
239  if (flag) {
240  surface->map->info.flags |= SDL_COPY_COLORKEY;
241  surface->map->info.colorkey = key;
242  if (surface->format->palette) {
243  surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_TRANSPARENT;
244  ++surface->format->palette->version;
245  if (!surface->format->palette->version) {
246  surface->format->palette->version = 1;
247  }
248  }
249  } else {
250  if (surface->format->palette) {
251  surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_OPAQUE;
252  ++surface->format->palette->version;
253  if (!surface->format->palette->version) {
254  surface->format->palette->version = 1;
255  }
256  }
257  surface->map->info.flags &= ~SDL_COPY_COLORKEY;
258  }
259  if (surface->map->info.flags != flags) {
260  SDL_InvalidateMap(surface->map);
261  }
262 
263  return 0;
264 }
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:201
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:161
#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:974
#define SDL_ALPHA_TRANSPARENT
Definition: SDL_pixels.h:47
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
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:91
#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 400 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().

401 {
402  int flags;
403 
404  if (!surface) {
405  return -1;
406  }
407 
408  surface->map->info.a = alpha;
409 
410  flags = surface->map->info.flags;
411  if (alpha != 0xFF) {
412  surface->map->info.flags |= SDL_COPY_MODULATE_ALPHA;
413  } else {
414  surface->map->info.flags &= ~SDL_COPY_MODULATE_ALPHA;
415  }
416  if (surface->map->info.flags != flags) {
417  SDL_InvalidateMap(surface->map);
418  }
419  return 0;
420 }
GLfloat GLfloat GLfloat alpha
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:974
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
#define SDL_COPY_MODULATE_ALPHA
Definition: SDL_blit.h:35
GLbitfield flags
SDL_BlitInfo info
Definition: SDL_blit.h:91
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 436 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().

437 {
438  int flags, status;
439 
440  if (!surface) {
441  return -1;
442  }
443 
444  status = 0;
445  flags = surface->map->info.flags;
446  surface->map->info.flags &=
448  switch (blendMode) {
449  case SDL_BLENDMODE_NONE:
450  break;
451  case SDL_BLENDMODE_BLEND:
452  surface->map->info.flags |= SDL_COPY_BLEND;
453  break;
454  case SDL_BLENDMODE_ADD:
455  surface->map->info.flags |= SDL_COPY_ADD;
456  break;
457  case SDL_BLENDMODE_MOD:
458  surface->map->info.flags |= SDL_COPY_MOD;
459  break;
460  default:
461  status = SDL_Unsupported();
462  break;
463  }
464 
465  if (surface->map->info.flags != flags) {
466  SDL_InvalidateMap(surface->map);
467  }
468 
469  return status;
470 }
#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:974
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:91

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

356 {
357  int flags;
358 
359  if (!surface) {
360  return -1;
361  }
362 
363  surface->map->info.r = r;
364  surface->map->info.g = g;
365  surface->map->info.b = b;
366 
367  flags = surface->map->info.flags;
368  if (r != 0xFF || g != 0xFF || b != 0xFF) {
369  surface->map->info.flags |= SDL_COPY_MODULATE_COLOR;
370  } else {
371  surface->map->info.flags &= ~SDL_COPY_MODULATE_COLOR;
372  }
373  if (surface->map->info.flags != flags) {
374  SDL_InvalidateMap(surface->map);
375  }
376  return 0;
377 }
Uint8 r
Definition: SDL_blit.h:70
#define SDL_COPY_MODULATE_COLOR
Definition: SDL_blit.h:34
Uint8 b
Definition: SDL_blit.h:70
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2072
Uint8 g
Definition: SDL_blit.h:70
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:974
GLboolean GLboolean g
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLbitfield flags
GLboolean GLboolean GLboolean b
SDL_BlitInfo info
Definition: SDL_blit.h:91

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

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

Referenced by SDL_CreateRGBSurfaceWithFormat(), and SDL_FreeSurface().

188 {
189  if (!surface) {
190  return SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
191  }
192  if (SDL_SetPixelFormatPalette(surface->format, palette) < 0) {
193  return -1;
194  }
195  SDL_InvalidateMap(surface->map);
196 
197  return 0;
198 }
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:974
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 201 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().

202 {
203  int flags;
204 
205  if (!surface) {
206  return -1;
207  }
208 
209  flags = surface->map->info.flags;
210  if (flag) {
211  surface->map->info.flags |= SDL_COPY_RLE_DESIRED;
212  } else {
213  surface->map->info.flags &= ~SDL_COPY_RLE_DESIRED;
214  }
215  if (surface->map->info.flags != flags) {
216  SDL_InvalidateMap(surface->map);
217  }
218  return 0;
219 }
#define SDL_COPY_RLE_DESIRED
Definition: SDL_blit.h:41
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:974
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLbitfield flags
SDL_BlitInfo info
Definition: SDL_blit.h:91

◆ SDL_SoftStretch()

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

Perform a fast, low quality, stretch blit between two surfaces of the same pixel format.

Note
This function uses a static buffer, and is not thread-safe.

Definition at line 203 of file SDL_stretch.c.

References SDL_PixelFormat::BytesPerPixel, copy_row3(), SDL_Surface::format, SDL_PixelFormat::format, SDL_Rect::h, SDL_Surface::h, NULL, SDL_Surface::pitch, SDL_Surface::pixels, pop, SDL_FALSE, SDL_LockSurface, SDL_MUSTLOCK, SDL_SetError, SDL_TRUE, SDL_UnlockSurface, SDL_Rect::w, SDL_Surface::w, SDL_Rect::x, and SDL_Rect::y.

205 {
206  int src_locked;
207  int dst_locked;
208  int pos, inc;
209  int dst_maxrow;
210  int src_row, dst_row;
211  Uint8 *srcp = NULL;
212  Uint8 *dstp;
213  SDL_Rect full_src;
214  SDL_Rect full_dst;
215 #ifdef USE_ASM_STRETCH
216  SDL_bool use_asm = SDL_TRUE;
217 #ifdef __GNUC__
218  int u1, u2;
219 #endif
220 #endif /* USE_ASM_STRETCH */
221  const int bpp = dst->format->BytesPerPixel;
222 
223  if (src->format->format != dst->format->format) {
224  return SDL_SetError("Only works with same format surfaces");
225  }
226 
227  /* Verify the blit rectangles */
228  if (srcrect) {
229  if ((srcrect->x < 0) || (srcrect->y < 0) ||
230  ((srcrect->x + srcrect->w) > src->w) ||
231  ((srcrect->y + srcrect->h) > src->h)) {
232  return SDL_SetError("Invalid source blit rectangle");
233  }
234  } else {
235  full_src.x = 0;
236  full_src.y = 0;
237  full_src.w = src->w;
238  full_src.h = src->h;
239  srcrect = &full_src;
240  }
241  if (dstrect) {
242  if ((dstrect->x < 0) || (dstrect->y < 0) ||
243  ((dstrect->x + dstrect->w) > dst->w) ||
244  ((dstrect->y + dstrect->h) > dst->h)) {
245  return SDL_SetError("Invalid destination blit rectangle");
246  }
247  } else {
248  full_dst.x = 0;
249  full_dst.y = 0;
250  full_dst.w = dst->w;
251  full_dst.h = dst->h;
252  dstrect = &full_dst;
253  }
254 
255  /* Lock the destination if it's in hardware */
256  dst_locked = 0;
257  if (SDL_MUSTLOCK(dst)) {
258  if (SDL_LockSurface(dst) < 0) {
259  return SDL_SetError("Unable to lock destination surface");
260  }
261  dst_locked = 1;
262  }
263  /* Lock the source if it's in hardware */
264  src_locked = 0;
265  if (SDL_MUSTLOCK(src)) {
266  if (SDL_LockSurface(src) < 0) {
267  if (dst_locked) {
268  SDL_UnlockSurface(dst);
269  }
270  return SDL_SetError("Unable to lock source surface");
271  }
272  src_locked = 1;
273  }
274 
275  /* Set up the data... */
276  pos = 0x10000;
277  inc = (srcrect->h << 16) / dstrect->h;
278  src_row = srcrect->y;
279  dst_row = dstrect->y;
280 
281 #ifdef USE_ASM_STRETCH
282  /* Write the opcodes for this stretch */
283  if ((bpp == 3) || (generate_rowbytes(srcrect->w, dstrect->w, bpp) < 0)) {
284  use_asm = SDL_FALSE;
285  }
286 #endif
287 
288  /* Perform the stretch blit */
289  for (dst_maxrow = dst_row + dstrect->h; dst_row < dst_maxrow; ++dst_row) {
290  dstp = (Uint8 *) dst->pixels + (dst_row * dst->pitch)
291  + (dstrect->x * bpp);
292  while (pos >= 0x10000L) {
293  srcp = (Uint8 *) src->pixels + (src_row * src->pitch)
294  + (srcrect->x * bpp);
295  ++src_row;
296  pos -= 0x10000L;
297  }
298 #ifdef USE_ASM_STRETCH
299  if (use_asm) {
300 #ifdef __GNUC__
301  __asm__ __volatile__("call *%4":"=&D"(u1), "=&S"(u2)
302  :"0"(dstp), "1"(srcp), "r"(copy_row)
303  :"memory");
304 #elif defined(_MSC_VER) || defined(__WATCOMC__)
305  /* *INDENT-OFF* */
306  {
307  void *code = copy_row;
308  __asm {
309  push edi
310  push esi
311  mov edi, dstp
312  mov esi, srcp
313  call dword ptr code
314  pop esi
315  pop edi
316  }
317  }
318  /* *INDENT-ON* */
319 #else
320 #error Need inline assembly for this compiler
321 #endif
322  } else
323 #endif
324  switch (bpp) {
325  case 1:
326  copy_row1(srcp, srcrect->w, dstp, dstrect->w);
327  break;
328  case 2:
329  copy_row2((Uint16 *) srcp, srcrect->w,
330  (Uint16 *) dstp, dstrect->w);
331  break;
332  case 3:
333  copy_row3(srcp, srcrect->w, dstp, dstrect->w);
334  break;
335  case 4:
336  copy_row4((Uint32 *) srcp, srcrect->w,
337  (Uint32 *) dstp, dstrect->w);
338  break;
339  }
340  pos += inc;
341  }
342 
343  /* We need to unlock the surfaces if they're locked */
344  if (dst_locked) {
345  SDL_UnlockSurface(dst);
346  }
347  if (src_locked) {
348  SDL_UnlockSurface(src);
349  }
350  return (0);
351 }
#define SDL_UnlockSurface
Uint8 BytesPerPixel
Definition: SDL_pixels.h:318
GLfixed u1
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:161
static void copy_row3(Uint8 *src, int src_w, Uint8 *dst, int dst_w)
Definition: SDL_stretch.c:177
GLfixed GLfixed u2
void * pixels
Definition: SDL_surface.h:75
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
#define pop
Definition: SDL_qsort.c:192
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
#define NULL
Definition: begin_code.h:143
SDL_bool
Definition: SDL_stdinc.h:130
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_SetError
#define SDL_LockSurface
#define SDL_MUSTLOCK(S)
Definition: SDL_surface.h:61
int h
Definition: SDL_rect.h:67
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:151
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64

◆ SDL_UnlockSurface()

void SDL_UnlockSurface ( SDL_Surface surface)
See also
SDL_LockSurface()

Definition at line 869 of file SDL_surface.c.

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

Referenced by SDL_ConvertColorkeyToAlpha(), and SDL_FreeSurface().

870 {
871  /* Only perform an unlock if we are locked */
872  if (!surface->locked || (--surface->locked > 0)) {
873  return;
874  }
875 
876  /* Update RLE encoded surface with new data */
877  if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
878  surface->flags &= ~SDL_RLEACCEL; /* stop lying */
879  SDL_RLESurface(surface);
880  }
881 }
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 568 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.

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

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