include/SDL_surface.h
author Sam Lantinga <slouken@libsdl.org>
Mon, 08 Dec 2008 00:27:32 +0000
changeset 2859 99210400e8b9
parent 2828 7e5ff6cd05bf
child 2888 32e8bbba1e94
permissions -rw-r--r--
Updated copyright date
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /**
    24  * \file SDL_surface.h
    25  *
    26  * Header file for SDL_surface definition and management functions
    27  */
    28 
    29 #ifndef _SDL_surface_h
    30 #define _SDL_surface_h
    31 
    32 #include "SDL_stdinc.h"
    33 #include "SDL_pixels.h"
    34 #include "SDL_rect.h"
    35 #include "SDL_rwops.h"
    36 
    37 #include "begin_code.h"
    38 /* Set up for C function definitions, even when using C++ */
    39 #ifdef __cplusplus
    40 /* *INDENT-OFF* */
    41 extern "C" {
    42 /* *INDENT-ON* */
    43 #endif
    44 
    45 /* These are the currently supported flags for the SDL_surface */
    46 /* Used internally (read-only) */
    47 #define SDL_PREALLOC        0x00000001  /* Surface uses preallocated memory */
    48 #define SDL_RLEACCEL        0x00000002  /* Surface is RLE encoded */
    49 
    50 /* Evaluates to true if the surface needs to be locked before access */
    51 #define SDL_MUSTLOCK(S)	(((S)->flags & SDL_RLEACCEL) != 0)
    52 
    53 /**
    54  * \struct SDL_Surface
    55  *
    56  * \brief A collection of pixels used in software blitting
    57  *
    58  * \note  This structure should be treated as read-only, except for 'pixels',
    59  *        which, if not NULL, contains the raw pixel data for the surface.
    60  */
    61 typedef struct SDL_Surface
    62 {
    63     Uint32 flags;               /**< Read-only */
    64     SDL_PixelFormat *format;    /**< Read-only */
    65     int w, h;                   /**< Read-only */
    66     int pitch;                  /**< Read-only */
    67     void *pixels;               /**< Read-write */
    68 
    69     /* Application data associated with the surfade */
    70     void *userdata;             /**< Read-write */
    71 
    72     /* information needed for surfaces requiring locks */
    73     int locked;                 /**< Read-only */
    74     void *lock_data;            /**< Read-only */
    75 
    76     /* clipping information */
    77     SDL_Rect clip_rect;         /**< Read-only */
    78 
    79     /* info for fast blit mapping to other surfaces */
    80     struct SDL_BlitMap *map;    /**< Private */
    81 
    82     /* format version, bumped at every change to invalidate blit maps */
    83     unsigned int format_version;        /**< Private */
    84 
    85     /* Reference count -- used when freeing surface */
    86     int refcount;               /**< Read-mostly */
    87 } SDL_Surface;
    88 
    89 /**
    90  * \typedef SDL_blit
    91  *
    92  * \brief The type of function used for surface blitting functions
    93  */
    94 typedef int (*SDL_blit) (struct SDL_Surface * src, SDL_Rect * srcrect,
    95                          struct SDL_Surface * dst, SDL_Rect * dstrect);
    96 
    97 /*
    98  * Allocate and free an RGB surface (must be called after SDL_SetVideoMode)
    99  * If the depth is 4 or 8 bits, an empty palette is allocated for the surface.
   100  * If the depth is greater than 8 bits, the pixel format is set using the
   101  * flags '[RGB]mask'.
   102  * If the function runs out of memory, it will return NULL.
   103  *
   104  * The 'flags' are obsolete and should be set to 0.
   105  */
   106 extern DECLSPEC SDL_Surface *SDLCALL SDL_CreateRGBSurface
   107     (Uint32 flags, int width, int height, int depth,
   108      Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
   109 extern DECLSPEC SDL_Surface *SDLCALL SDL_CreateRGBSurfaceFrom(void *pixels,
   110                                                               int width,
   111                                                               int height,
   112                                                               int depth,
   113                                                               int pitch,
   114                                                               Uint32 Rmask,
   115                                                               Uint32 Gmask,
   116                                                               Uint32 Bmask,
   117                                                               Uint32 Amask);
   118 extern DECLSPEC void SDLCALL SDL_FreeSurface(SDL_Surface * surface);
   119 
   120 /**
   121  * \fn int SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
   122  *
   123  * \brief Set the palette used by a surface.
   124  *
   125  * \return 0, or -1 if the surface format doesn't use a palette.
   126  *
   127  * \note A single palette can be shared with many surfaces.
   128  */
   129 extern DECLSPEC int SDLCALL SDL_SetSurfacePalette(SDL_Surface * surface,
   130                                                   SDL_Palette * palette);
   131 
   132 /*
   133  * SDL_LockSurface() sets up a surface for directly accessing the pixels.
   134  * Between calls to SDL_LockSurface()/SDL_UnlockSurface(), you can write
   135  * to and read from 'surface->pixels', using the pixel format stored in 
   136  * 'surface->format'.  Once you are done accessing the surface, you should 
   137  * use SDL_UnlockSurface() to release it.
   138  *
   139  * Not all surfaces require locking.  If SDL_MUSTLOCK(surface) evaluates
   140  * to 0, then you can read and write to the surface at any time, and the
   141  * pixel format of the surface will not change.
   142  * 
   143  * No operating system or library calls should be made between lock/unlock
   144  * pairs, as critical system locks may be held during this time.
   145  *
   146  * SDL_LockSurface() returns 0, or -1 if the surface couldn't be locked.
   147  */
   148 extern DECLSPEC int SDLCALL SDL_LockSurface(SDL_Surface * surface);
   149 extern DECLSPEC void SDLCALL SDL_UnlockSurface(SDL_Surface * surface);
   150 
   151 /*
   152  * Load a surface from a seekable SDL data source (memory or file.)
   153  * If 'freesrc' is non-zero, the source will be closed after being read.
   154  * Returns the new surface, or NULL if there was an error.
   155  * The new surface should be freed with SDL_FreeSurface().
   156  */
   157 extern DECLSPEC SDL_Surface *SDLCALL SDL_LoadBMP_RW(SDL_RWops * src,
   158                                                     int freesrc);
   159 
   160 /* Convenience macro -- load a surface from a file */
   161 #define SDL_LoadBMP(file)	SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)
   162 
   163 /*
   164  * Save a surface to a seekable SDL data source (memory or file.)
   165  * If 'freedst' is non-zero, the source will be closed after being written.
   166  * Returns 0 if successful or -1 if there was an error.
   167  */
   168 extern DECLSPEC int SDLCALL SDL_SaveBMP_RW
   169     (SDL_Surface * surface, SDL_RWops * dst, int freedst);
   170 
   171 /* Convenience macro -- save a surface to a file */
   172 #define SDL_SaveBMP(surface, file) \
   173 		SDL_SaveBMP_RW(surface, SDL_RWFromFile(file, "wb"), 1)
   174 
   175 /*
   176  * \fn int SDL_SetSurfaceRLE(SDL_Surface *surface, int flag)
   177  *
   178  * \brief Sets the RLE acceleration hint for a surface.
   179  *
   180  * \return 0 on success, or -1 if the surface is not valid
   181  *
   182  * \note If RLE is enabled, colorkey and alpha blending blits are much faster,
   183  *       but the surface must be locked before directly accessing the pixels.
   184  */
   185 extern DECLSPEC int SDLCALL SDL_SetSurfaceRLE(SDL_Surface * surface,
   186                                               int flag);
   187 
   188 /*
   189  * \fn int SDL_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key)
   190  *
   191  * \brief Sets the color key (transparent pixel) in a blittable surface.
   192  *
   193  * \param surface The surface to update
   194  * \param flag Non-zero to enable colorkey and 0 to disable colorkey 
   195  * \param key The transparent pixel in the native surface format
   196  *
   197  * \return 0 on success, or -1 if the surface is not valid
   198  */
   199 extern DECLSPEC int SDLCALL SDL_SetColorKey(SDL_Surface * surface,
   200                                             Uint32 flag, Uint32 key);
   201 
   202 /**
   203  * \fn int SDL_SetSurfaceColorMod(SDL_Surface *surface, Uint8 r, Uint8 g, Uint8 b)
   204  *
   205  * \brief Set an additional color value used in blit operations
   206  *
   207  * \param surface The surface to update
   208  * \param r The red source color value multiplied into blit operations
   209  * \param g The green source color value multiplied into blit operations
   210  * \param b The blue source color value multiplied into blit operations
   211  *
   212  * \return 0 on success, or -1 if the surface is not valid
   213  *
   214  * \sa SDL_GetSurfaceColorMod()
   215  */
   216 extern DECLSPEC int SDLCALL SDL_SetSurfaceColorMod(SDL_Surface * surface,
   217                                                    Uint8 r, Uint8 g, Uint8 b);
   218 
   219 
   220 /**
   221  * \fn int SDL_GetSurfaceColorMod(SDL_Surface *surface, Uint8 *r, Uint8 *g, Uint8 *b)
   222  *
   223  * \brief Get the additional color value used in blit operations
   224  *
   225  * \param surface The surface to query
   226  * \param r A pointer filled in with the source red color value
   227  * \param g A pointer filled in with the source green color value
   228  * \param b A pointer filled in with the source blue color value
   229  *
   230  * \return 0 on success, or -1 if the surface is not valid
   231  *
   232  * \sa SDL_SetSurfaceColorMod()
   233  */
   234 extern DECLSPEC int SDLCALL SDL_GetSurfaceColorMod(SDL_Surface * surface,
   235                                                    Uint8 * r, Uint8 * g,
   236                                                    Uint8 * b);
   237 
   238 /**
   239  * \fn int SDL_SetSurfaceAlphaMod(SDL_Surface *surface, Uint8 alpha)
   240  *
   241  * \brief Set an additional alpha value used in blit operations
   242  *
   243  * \param surface The surface to update
   244  * \param alpha The source alpha value multiplied into blit operations.
   245  *
   246  * \return 0 on success, or -1 if the surface is not valid
   247  *
   248  * \sa SDL_GetSurfaceAlphaMod()
   249  */
   250 extern DECLSPEC int SDLCALL SDL_SetSurfaceAlphaMod(SDL_Surface * surface,
   251                                                    Uint8 alpha);
   252 
   253 /**
   254  * \fn int SDL_GetSurfaceAlphaMod(SDL_Surface *surface, Uint8 *alpha)
   255  *
   256  * \brief Get the additional alpha value used in blit operations
   257  *
   258  * \param surface The surface to query
   259  * \param alpha A pointer filled in with the source alpha value
   260  *
   261  * \return 0 on success, or -1 if the surface is not valid
   262  *
   263  * \sa SDL_SetSurfaceAlphaMod()
   264  */
   265 extern DECLSPEC int SDLCALL SDL_GetSurfaceAlphaMod(SDL_Surface * surface,
   266                                                    Uint8 * alpha);
   267 
   268 /**
   269  * \fn int SDL_SetSurfaceBlendMode(SDL_Surface *surface, int blendMode)
   270  *
   271  * \brief Set the blend mode used for blit operations
   272  *
   273  * \param surface The surface to update
   274  * \param blendMode SDL_TextureBlendMode to use for blit blending
   275  *
   276  * \return 0 on success, or -1 if the parameters are not valid
   277  *
   278  * \sa SDL_GetSurfaceBlendMode()
   279  */
   280 extern DECLSPEC int SDLCALL SDL_SetSurfaceBlendMode(SDL_Surface * surface,
   281                                                     int blendMode);
   282 
   283 /**
   284  * \fn int SDL_GetSurfaceBlendMode(SDL_Surface *surface, int *blendMode)
   285  *
   286  * \brief Get the blend mode used for blit operations
   287  *
   288  * \param surface The surface to query
   289  * \param blendMode A pointer filled in with the current blend mode
   290  *
   291  * \return 0 on success, or -1 if the surface is not valid
   292  *
   293  * \sa SDL_SetSurfaceBlendMode()
   294  */
   295 extern DECLSPEC int SDLCALL SDL_GetSurfaceBlendMode(SDL_Surface * surface,
   296                                                     int *blendMode);
   297 
   298 /**
   299  * \fn int SDL_SetSurfaceScaleMode(SDL_Surface *surface, int scaleMode)
   300  *
   301  * \brief Set the scale mode used for blit operations
   302  *
   303  * \param surface The surface to update
   304  * \param scaleMode SDL_TextureScaleMode to use for blit scaling
   305  *
   306  * \return 0 on success, or -1 if the surface is not valid or the scale mode is not supported
   307  *
   308  * \note If the scale mode is not supported, the closest supported mode is chosen.  Currently only SDL_TEXTURESCALEMODE_FAST is supported on surfaces.
   309  *
   310  * \sa SDL_GetSurfaceScaleMode()
   311  */
   312 extern DECLSPEC int SDLCALL SDL_SetSurfaceScaleMode(SDL_Surface * surface,
   313                                                     int scaleMode);
   314 
   315 /**
   316  * \fn int SDL_GetSurfaceScaleMode(SDL_Surface *surface, int *scaleMode)
   317  *
   318  * \brief Get the scale mode used for blit operations
   319  *
   320  * \param surface The surface to query
   321  * \param scaleMode A pointer filled in with the current scale mode
   322  *
   323  * \return 0 on success, or -1 if the surface is not valid
   324  *
   325  * \sa SDL_SetSurfaceScaleMode()
   326  */
   327 extern DECLSPEC int SDLCALL SDL_GetSurfaceScaleMode(SDL_Surface * surface,
   328                                                     int *scaleMode);
   329 
   330 /*
   331  * Sets the clipping rectangle for the destination surface in a blit.
   332  *
   333  * If the clip rectangle is NULL, clipping will be disabled.
   334  * If the clip rectangle doesn't intersect the surface, the function will
   335  * return SDL_FALSE and blits will be completely clipped.  Otherwise the
   336  * function returns SDL_TRUE and blits to the surface will be clipped to
   337  * the intersection of the surface area and the clipping rectangle.
   338  *
   339  * Note that blits are automatically clipped to the edges of the source
   340  * and destination surfaces.
   341  */
   342 extern DECLSPEC SDL_bool SDLCALL SDL_SetClipRect(SDL_Surface * surface,
   343                                                  const SDL_Rect * rect);
   344 
   345 /*
   346  * Gets the clipping rectangle for the destination surface in a blit.
   347  * 'rect' must be a pointer to a valid rectangle which will be filled
   348  * with the correct values.
   349  */
   350 extern DECLSPEC void SDLCALL SDL_GetClipRect(SDL_Surface * surface,
   351                                              SDL_Rect * rect);
   352 
   353 /*
   354  * Creates a new surface of the specified format, and then copies and maps 
   355  * the given surface to it so the blit of the converted surface will be as 
   356  * fast as possible.  If this function fails, it returns NULL.
   357  *
   358  * The 'flags' parameter is passed to SDL_CreateRGBSurface() and has those 
   359  * semantics.  You can also pass SDL_RLEACCEL in the flags parameter and
   360  * SDL will try to RLE accelerate colorkey and alpha blits in the resulting
   361  * surface.
   362  *
   363  * This function is used internally by SDL_DisplayFormat().
   364  */
   365 extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurface
   366     (SDL_Surface * src, SDL_PixelFormat * fmt, Uint32 flags);
   367 
   368 /*
   369  * This function performs a fast fill of the given rectangle with 'color'
   370  * The given rectangle is clipped to the destination surface clip area
   371  * and the final fill rectangle is saved in the passed in pointer.
   372  * If 'dstrect' is NULL, the whole surface will be filled with 'color'
   373  * The color should be a pixel of the format used by the surface, and 
   374  * can be generated by the SDL_MapRGB() function.
   375  * This function returns 0 on success, or -1 on error.
   376  */
   377 extern DECLSPEC int SDLCALL SDL_FillRect
   378     (SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color);
   379 
   380 /*
   381  * This performs a fast blit from the source surface to the destination
   382  * surface.  It assumes that the source and destination rectangles are
   383  * the same size.  If either 'srcrect' or 'dstrect' are NULL, the entire
   384  * surface (src or dst) is copied.  The final blit rectangles are saved
   385  * in 'srcrect' and 'dstrect' after all clipping is performed.
   386  * If the blit is successful, it returns 0, otherwise it returns -1.
   387  *
   388  * The blit function should not be called on a locked surface.
   389  *
   390  * The blit semantics for surfaces with and without alpha and colorkey
   391  * are defined as follows:
   392  *
   393  * RGBA->RGB:
   394  *     SDL_SRCALPHA set:
   395  * 	alpha-blend (using alpha-channel).
   396  * 	SDL_SRCCOLORKEY ignored.
   397  *     SDL_SRCALPHA not set:
   398  * 	copy RGB.
   399  * 	if SDL_SRCCOLORKEY set, only copy the pixels matching the
   400  * 	RGB values of the source colour key, ignoring alpha in the
   401  * 	comparison.
   402  * 
   403  * RGB->RGBA:
   404  *     SDL_SRCALPHA set:
   405  * 	alpha-blend (using the source per-surface alpha value);
   406  * 	set destination alpha to opaque.
   407  *     SDL_SRCALPHA not set:
   408  * 	copy RGB, set destination alpha to source per-surface alpha value.
   409  *     both:
   410  * 	if SDL_SRCCOLORKEY set, only copy the pixels matching the
   411  * 	source colour key.
   412  * 
   413  * RGBA->RGBA:
   414  *     SDL_SRCALPHA set:
   415  * 	alpha-blend (using the source alpha channel) the RGB values;
   416  * 	leave destination alpha untouched. [Note: is this correct?]
   417  * 	SDL_SRCCOLORKEY ignored.
   418  *     SDL_SRCALPHA not set:
   419  * 	copy all of RGBA to the destination.
   420  * 	if SDL_SRCCOLORKEY set, only copy the pixels matching the
   421  * 	RGB values of the source colour key, ignoring alpha in the
   422  * 	comparison.
   423  * 
   424  * RGB->RGB: 
   425  *     SDL_SRCALPHA set:
   426  * 	alpha-blend (using the source per-surface alpha value).
   427  *     SDL_SRCALPHA not set:
   428  * 	copy RGB.
   429  *     both:
   430  * 	if SDL_SRCCOLORKEY set, only copy the pixels matching the
   431  * 	source colour key.
   432  *
   433  * If either of the surfaces were in video memory, and the blit returns -2,
   434  * the video memory was lost, so it should be reloaded with artwork and 
   435  * re-blitted:
   436 	while ( SDL_BlitSurface(image, imgrect, screen, dstrect) == -2 ) {
   437 		while ( SDL_LockSurface(image) < 0 )
   438 			Sleep(10);
   439 		-- Write image pixels to image->pixels --
   440 		SDL_UnlockSurface(image);
   441 	}
   442  * This happens under DirectX 5.0 when the system switches away from your
   443  * fullscreen application.  The lock will also fail until you have access
   444  * to the video memory again.
   445  */
   446 /* You should call SDL_BlitSurface() unless you know exactly how SDL
   447    blitting works internally and how to use the other blit functions.
   448 */
   449 #define SDL_BlitSurface SDL_UpperBlit
   450 
   451 /* This is the public blit function, SDL_BlitSurface(), and it performs
   452    rectangle validation and clipping before passing it to SDL_LowerBlit()
   453 */
   454 extern DECLSPEC int SDLCALL SDL_UpperBlit
   455     (SDL_Surface * src, SDL_Rect * srcrect,
   456      SDL_Surface * dst, SDL_Rect * dstrect);
   457 /* This is a semi-private blit function and it performs low-level surface
   458    blitting only.
   459 */
   460 extern DECLSPEC int SDLCALL SDL_LowerBlit
   461     (SDL_Surface * src, SDL_Rect * srcrect,
   462      SDL_Surface * dst, SDL_Rect * dstrect);
   463 
   464 /**
   465  * \fn int SDL_SoftStretch(SDL_Surface * src, const SDL_Rect * srcrect, SDL_Surface * dst, const SDL_Rect * dstrect)
   466  *
   467  * \brief Perform a fast, low quality, stretch blit between two surfaces of the same pixel format.
   468  *
   469  * \note This function uses a static buffer, and is not thread-safe.
   470  */
   471 extern DECLSPEC int SDLCALL SDL_SoftStretch(SDL_Surface * src,
   472                                             const SDL_Rect * srcrect,
   473                                             SDL_Surface * dst,
   474                                             const SDL_Rect * dstrect);
   475 
   476 /* Ends C function definitions when using C++ */
   477 #ifdef __cplusplus
   478 /* *INDENT-OFF* */
   479 }
   480 /* *INDENT-ON* */
   481 #endif
   482 #include "close_code.h"
   483 
   484 #endif /* _SDL_surface_h */
   485 
   486 /* vi: set ts=4 sw=4 expandtab: */