include/SDL_surface.h
author Ryan C. Gordon <icculus@icculus.org>
Wed, 21 Nov 2012 23:00:06 -0500
changeset 6680 9d3df1f5c542
parent 6257 4b6e961362fb
child 6885 700f1b25f77f
permissions -rw-r--r--
Removed some tab chars.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 /**
    23  *  \file SDL_surface.h
    24  *  
    25  *  Header file for ::SDL_surface definition and management functions.
    26  */
    27 
    28 #ifndef _SDL_surface_h
    29 #define _SDL_surface_h
    30 
    31 #include "SDL_stdinc.h"
    32 #include "SDL_pixels.h"
    33 #include "SDL_rect.h"
    34 #include "SDL_blendmode.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 /**
    46  *  \name Surface flags
    47  *  
    48  *  These are the currently supported flags for the ::SDL_surface.
    49  *  
    50  *  \internal
    51  *  Used internally (read-only).
    52  */
    53 /*@{*/
    54 #define SDL_SWSURFACE       0           /**< Just here for compatibility */
    55 #define SDL_PREALLOC        0x00000001  /**< Surface uses preallocated memory */
    56 #define SDL_RLEACCEL        0x00000002  /**< Surface is RLE encoded */
    57 #define SDL_DONTFREE        0x00000004  /**< Surface is referenced internally */
    58 /*@}*//*Surface flags*/
    59 
    60 /**
    61  *  Evaluates to true if the surface needs to be locked before access.
    62  */
    63 #define SDL_MUSTLOCK(S)	(((S)->flags & SDL_RLEACCEL) != 0)
    64 
    65 /**
    66  * \brief A collection of pixels used in software blitting.
    67  *
    68  * \note  This structure should be treated as read-only, except for \c pixels,
    69  *        which, if not NULL, contains the raw pixel data for the surface.
    70  */
    71 typedef struct SDL_Surface
    72 {
    73     Uint32 flags;               /**< Read-only */
    74     SDL_PixelFormat *format;    /**< Read-only */
    75     int w, h;                   /**< Read-only */
    76     int pitch;                  /**< Read-only */
    77     void *pixels;               /**< Read-write */
    78 
    79     /** Application data associated with the surface */
    80     void *userdata;             /**< Read-write */
    81 
    82     /** information needed for surfaces requiring locks */
    83     int locked;                 /**< Read-only */
    84     void *lock_data;            /**< Read-only */
    85 
    86     /** clipping information */
    87     SDL_Rect clip_rect;         /**< Read-only */
    88 
    89     /** info for fast blit mapping to other surfaces */
    90     struct SDL_BlitMap *map;    /**< Private */
    91 
    92     /** Reference count -- used when freeing surface */
    93     int refcount;               /**< Read-mostly */
    94 } SDL_Surface;
    95 
    96 /**
    97  * \brief The type of function used for surface blitting functions.
    98  */
    99 typedef int (*SDL_blit) (struct SDL_Surface * src, SDL_Rect * srcrect,
   100                          struct SDL_Surface * dst, SDL_Rect * dstrect);
   101 
   102 /**
   103  *  Allocate and free an RGB surface.
   104  *  
   105  *  If the depth is 4 or 8 bits, an empty palette is allocated for the surface.
   106  *  If the depth is greater than 8 bits, the pixel format is set using the
   107  *  flags '[RGB]mask'.
   108  *  
   109  *  If the function runs out of memory, it will return NULL.
   110  *  
   111  *  \param flags The \c flags are obsolete and should be set to 0.
   112  */
   113 extern DECLSPEC SDL_Surface *SDLCALL SDL_CreateRGBSurface
   114     (Uint32 flags, int width, int height, int depth,
   115      Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
   116 extern DECLSPEC SDL_Surface *SDLCALL SDL_CreateRGBSurfaceFrom(void *pixels,
   117                                                               int width,
   118                                                               int height,
   119                                                               int depth,
   120                                                               int pitch,
   121                                                               Uint32 Rmask,
   122                                                               Uint32 Gmask,
   123                                                               Uint32 Bmask,
   124                                                               Uint32 Amask);
   125 extern DECLSPEC void SDLCALL SDL_FreeSurface(SDL_Surface * surface);
   126 
   127 /**
   128  *  \brief Set the palette used by a surface.
   129  *  
   130  *  \return 0, or -1 if the surface format doesn't use a palette.
   131  *  
   132  *  \note A single palette can be shared with many surfaces.
   133  */
   134 extern DECLSPEC int SDLCALL SDL_SetSurfacePalette(SDL_Surface * surface,
   135                                                   SDL_Palette * palette);
   136 
   137 /**
   138  *  \brief Sets up a surface for directly accessing the pixels.
   139  *  
   140  *  Between calls to SDL_LockSurface() / SDL_UnlockSurface(), you can write
   141  *  to and read from \c surface->pixels, using the pixel format stored in 
   142  *  \c surface->format.  Once you are done accessing the surface, you should 
   143  *  use SDL_UnlockSurface() to release it.
   144  *  
   145  *  Not all surfaces require locking.  If SDL_MUSTLOCK(surface) evaluates
   146  *  to 0, then you can read and write to the surface at any time, and the
   147  *  pixel format of the surface will not change.
   148  *  
   149  *  No operating system or library calls should be made between lock/unlock
   150  *  pairs, as critical system locks may be held during this time.
   151  *  
   152  *  SDL_LockSurface() returns 0, or -1 if the surface couldn't be locked.
   153  *  
   154  *  \sa SDL_UnlockSurface()
   155  */
   156 extern DECLSPEC int SDLCALL SDL_LockSurface(SDL_Surface * surface);
   157 /** \sa SDL_LockSurface() */
   158 extern DECLSPEC void SDLCALL SDL_UnlockSurface(SDL_Surface * surface);
   159 
   160 /**
   161  *  Load a surface from a seekable SDL data stream (memory or file).
   162  *  
   163  *  If \c freesrc is non-zero, the stream will be closed after being read.
   164  *  
   165  *  The new surface should be freed with SDL_FreeSurface().
   166  *  
   167  *  \return the new surface, or NULL if there was an error.
   168  */
   169 extern DECLSPEC SDL_Surface *SDLCALL SDL_LoadBMP_RW(SDL_RWops * src,
   170                                                     int freesrc);
   171 
   172 /**
   173  *  Load a surface from a file.
   174  *  
   175  *  Convenience macro.
   176  */
   177 #define SDL_LoadBMP(file)	SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)
   178 
   179 /**
   180  *  Save a surface to a seekable SDL data stream (memory or file).
   181  *  
   182  *  If \c freedst is non-zero, the stream will be closed after being written.
   183  *  
   184  *  \return 0 if successful or -1 if there was an error.
   185  */
   186 extern DECLSPEC int SDLCALL SDL_SaveBMP_RW
   187     (SDL_Surface * surface, SDL_RWops * dst, int freedst);
   188 
   189 /** 
   190  *  Save a surface to a file.
   191  *  
   192  *  Convenience macro.
   193  */
   194 #define SDL_SaveBMP(surface, file) \
   195 		SDL_SaveBMP_RW(surface, SDL_RWFromFile(file, "wb"), 1)
   196 
   197 /**
   198  *  \brief Sets the RLE acceleration hint for a surface.
   199  *  
   200  *  \return 0 on success, or -1 if the surface is not valid
   201  *  
   202  *  \note If RLE is enabled, colorkey and alpha blending blits are much faster,
   203  *        but the surface must be locked before directly accessing the pixels.
   204  */
   205 extern DECLSPEC int SDLCALL SDL_SetSurfaceRLE(SDL_Surface * surface,
   206                                               int flag);
   207 
   208 /**
   209  *  \brief Sets the color key (transparent pixel) in a blittable surface.
   210  *  
   211  *  \param surface The surface to update
   212  *  \param flag Non-zero to enable colorkey and 0 to disable colorkey
   213  *  \param key The transparent pixel in the native surface format
   214  *  
   215  *  \return 0 on success, or -1 if the surface is not valid
   216  *
   217  *  You can pass SDL_RLEACCEL to enable RLE accelerated blits.
   218  */
   219 extern DECLSPEC int SDLCALL SDL_SetColorKey(SDL_Surface * surface,
   220                                             int flag, Uint32 key);
   221 
   222 /**
   223  *  \brief Gets the color key (transparent pixel) in a blittable surface.
   224  *  
   225  *  \param surface The surface to update
   226  *  \param key A pointer filled in with the transparent pixel in the native 
   227  *             surface format
   228  *  
   229  *  \return 0 on success, or -1 if the surface is not valid or colorkey is not 
   230  *          enabled.
   231  */
   232 extern DECLSPEC int SDLCALL SDL_GetColorKey(SDL_Surface * surface,
   233                                             Uint32 * key);
   234 
   235 /**
   236  *  \brief Set an additional color value used in blit operations.
   237  *  
   238  *  \param surface The surface to update.
   239  *  \param r The red color value multiplied into blit operations.
   240  *  \param g The green color value multiplied into blit operations.
   241  *  \param b The blue color value multiplied into blit operations.
   242  *  
   243  *  \return 0 on success, or -1 if the surface is not valid.
   244  *  
   245  *  \sa SDL_GetSurfaceColorMod()
   246  */
   247 extern DECLSPEC int SDLCALL SDL_SetSurfaceColorMod(SDL_Surface * surface,
   248                                                    Uint8 r, Uint8 g, Uint8 b);
   249 
   250 
   251 /**
   252  *  \brief Get the additional color value used in blit operations.
   253  *  
   254  *  \param surface The surface to query.
   255  *  \param r A pointer filled in with the current red color value.
   256  *  \param g A pointer filled in with the current green color value.
   257  *  \param b A pointer filled in with the current blue color value.
   258  *  
   259  *  \return 0 on success, or -1 if the surface is not valid.
   260  *  
   261  *  \sa SDL_SetSurfaceColorMod()
   262  */
   263 extern DECLSPEC int SDLCALL SDL_GetSurfaceColorMod(SDL_Surface * surface,
   264                                                    Uint8 * r, Uint8 * g,
   265                                                    Uint8 * b);
   266 
   267 /**
   268  *  \brief Set an additional alpha value used in blit operations.
   269  *  
   270  *  \param surface The surface to update.
   271  *  \param alpha The alpha value multiplied into blit operations.
   272  *  
   273  *  \return 0 on success, or -1 if the surface is not valid.
   274  *  
   275  *  \sa SDL_GetSurfaceAlphaMod()
   276  */
   277 extern DECLSPEC int SDLCALL SDL_SetSurfaceAlphaMod(SDL_Surface * surface,
   278                                                    Uint8 alpha);
   279 
   280 /**
   281  *  \brief Get the additional alpha value used in blit operations.
   282  *  
   283  *  \param surface The surface to query.
   284  *  \param alpha A pointer filled in with the current alpha value.
   285  *  
   286  *  \return 0 on success, or -1 if the surface is not valid.
   287  *  
   288  *  \sa SDL_SetSurfaceAlphaMod()
   289  */
   290 extern DECLSPEC int SDLCALL SDL_GetSurfaceAlphaMod(SDL_Surface * surface,
   291                                                    Uint8 * alpha);
   292 
   293 /**
   294  *  \brief Set the blend mode used for blit operations.
   295  *  
   296  *  \param surface The surface to update.
   297  *  \param blendMode ::SDL_BlendMode to use for blit blending.
   298  *  
   299  *  \return 0 on success, or -1 if the parameters are not valid.
   300  *  
   301  *  \sa SDL_GetSurfaceBlendMode()
   302  */
   303 extern DECLSPEC int SDLCALL SDL_SetSurfaceBlendMode(SDL_Surface * surface,
   304                                                     SDL_BlendMode blendMode);
   305 
   306 /**
   307  *  \brief Get the blend mode used for blit operations.
   308  *  
   309  *  \param surface   The surface to query.
   310  *  \param blendMode A pointer filled in with the current blend mode.
   311  *  
   312  *  \return 0 on success, or -1 if the surface is not valid.
   313  *  
   314  *  \sa SDL_SetSurfaceBlendMode()
   315  */
   316 extern DECLSPEC int SDLCALL SDL_GetSurfaceBlendMode(SDL_Surface * surface,
   317                                                     SDL_BlendMode *blendMode);
   318 
   319 /**
   320  *  Sets the clipping rectangle for the destination surface in a blit.
   321  *  
   322  *  If the clip rectangle is NULL, clipping will be disabled.
   323  *  
   324  *  If the clip rectangle doesn't intersect the surface, the function will
   325  *  return SDL_FALSE and blits will be completely clipped.  Otherwise the
   326  *  function returns SDL_TRUE and blits to the surface will be clipped to
   327  *  the intersection of the surface area and the clipping rectangle.
   328  *  
   329  *  Note that blits are automatically clipped to the edges of the source
   330  *  and destination surfaces.
   331  */
   332 extern DECLSPEC SDL_bool SDLCALL SDL_SetClipRect(SDL_Surface * surface,
   333                                                  const SDL_Rect * rect);
   334 
   335 /**
   336  *  Gets the clipping rectangle for the destination surface in a blit.
   337  *  
   338  *  \c rect must be a pointer to a valid rectangle which will be filled
   339  *  with the correct values.
   340  */
   341 extern DECLSPEC void SDLCALL SDL_GetClipRect(SDL_Surface * surface,
   342                                              SDL_Rect * rect);
   343 
   344 /**
   345  *  Creates a new surface of the specified format, and then copies and maps 
   346  *  the given surface to it so the blit of the converted surface will be as 
   347  *  fast as possible.  If this function fails, it returns NULL.
   348  *  
   349  *  The \c flags parameter is passed to SDL_CreateRGBSurface() and has those 
   350  *  semantics.  You can also pass ::SDL_RLEACCEL in the flags parameter and
   351  *  SDL will try to RLE accelerate colorkey and alpha blits in the resulting
   352  *  surface.
   353  */
   354 extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurface
   355     (SDL_Surface * src, SDL_PixelFormat * fmt, Uint32 flags);
   356 extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurfaceFormat
   357     (SDL_Surface * src, Uint32 pixel_format, Uint32 flags);
   358 
   359 /**
   360  * \brief Copy a block of pixels of one format to another format
   361  *  
   362  *  \return 0 on success, or -1 if there was an error
   363  */
   364 extern DECLSPEC int SDLCALL SDL_ConvertPixels(int width, int height,
   365                                               Uint32 src_format,
   366                                               const void * src, int src_pitch,
   367                                               Uint32 dst_format,
   368                                               void * dst, int dst_pitch);
   369 
   370 /**
   371  *  Performs a fast fill of the given rectangle with \c color.
   372  *  
   373  *  If \c rect is NULL, the whole surface will be filled with \c color.
   374  *  
   375  *  The color should be a pixel of the format used by the surface, and 
   376  *  can be generated by the SDL_MapRGB() function.
   377  *  
   378  *  \return 0 on success, or -1 on error.
   379  */
   380 extern DECLSPEC int SDLCALL SDL_FillRect
   381     (SDL_Surface * dst, const SDL_Rect * rect, Uint32 color);
   382 extern DECLSPEC int SDLCALL SDL_FillRects
   383     (SDL_Surface * dst, const SDL_Rect * rects, int count, Uint32 color);
   384 
   385 /**
   386  *  Performs a fast blit from the source surface to the destination surface.
   387  *  
   388  *  This assumes that the source and destination rectangles are
   389  *  the same size.  If either \c srcrect or \c dstrect are NULL, the entire
   390  *  surface (\c src or \c dst) is copied.  The final blit rectangles are saved
   391  *  in \c srcrect and \c dstrect after all clipping is performed.
   392  *  
   393  *  \return If the blit is successful, it returns 0, otherwise it returns -1.
   394  *
   395  *  The blit function should not be called on a locked surface.
   396  *
   397  *  The blit semantics for surfaces with and without alpha and colorkey
   398  *  are defined as follows:
   399  *  \verbatim
   400     RGBA->RGB:
   401       SDL_SRCALPHA set:
   402         alpha-blend (using alpha-channel).
   403         SDL_SRCCOLORKEY ignored.
   404       SDL_SRCALPHA not set:
   405         copy RGB.
   406         if SDL_SRCCOLORKEY set, only copy the pixels matching the
   407         RGB values of the source colour key, ignoring alpha in the
   408         comparison.
   409    
   410     RGB->RGBA:
   411       SDL_SRCALPHA set:
   412         alpha-blend (using the source per-surface alpha value);
   413         set destination alpha to opaque.
   414       SDL_SRCALPHA not set:
   415         copy RGB, set destination alpha to source per-surface alpha value.
   416       both:
   417         if SDL_SRCCOLORKEY set, only copy the pixels matching the
   418         source colour key.
   419    
   420     RGBA->RGBA:
   421       SDL_SRCALPHA set:
   422         alpha-blend (using the source alpha channel) the RGB values;
   423         leave destination alpha untouched. [Note: is this correct?]
   424         SDL_SRCCOLORKEY ignored.
   425       SDL_SRCALPHA not set:
   426         copy all of RGBA to the destination.
   427         if SDL_SRCCOLORKEY set, only copy the pixels matching the
   428         RGB values of the source colour key, ignoring alpha in the
   429        comparison.
   430    
   431     RGB->RGB: 
   432       SDL_SRCALPHA set:
   433         alpha-blend (using the source per-surface alpha value).
   434       SDL_SRCALPHA not set:
   435         copy RGB.
   436       both:
   437         if SDL_SRCCOLORKEY set, only copy the pixels matching the
   438         source colour key.
   439     \endverbatim
   440  *  
   441  *  You should call SDL_BlitSurface() unless you know exactly how SDL
   442  *  blitting works internally and how to use the other blit functions.
   443  */
   444 #define SDL_BlitSurface SDL_UpperBlit
   445 
   446 /**
   447  *  This is the public blit function, SDL_BlitSurface(), and it performs
   448  *  rectangle validation and clipping before passing it to SDL_LowerBlit()
   449  */
   450 extern DECLSPEC int SDLCALL SDL_UpperBlit
   451     (SDL_Surface * src, const SDL_Rect * srcrect,
   452      SDL_Surface * dst, SDL_Rect * dstrect);
   453 
   454 /**
   455  *  This is a semi-private blit function and it performs low-level surface
   456  *  blitting only.
   457  */
   458 extern DECLSPEC int SDLCALL SDL_LowerBlit
   459     (SDL_Surface * src, SDL_Rect * srcrect,
   460      SDL_Surface * dst, SDL_Rect * dstrect);
   461 
   462 /**
   463  *  \brief Perform a fast, low quality, stretch blit between two surfaces of the
   464  *         same pixel format.
   465  *  
   466  *  \note This function uses a static buffer, and is not thread-safe.
   467  */
   468 extern DECLSPEC int SDLCALL SDL_SoftStretch(SDL_Surface * src,
   469                                             const SDL_Rect * srcrect,
   470                                             SDL_Surface * dst,
   471                                             const SDL_Rect * dstrect);
   472 
   473 #define SDL_BlitScaled SDL_UpperBlitScaled
   474 
   475 /**
   476  *  This is the public scaled blit function, SDL_BlitScaled(), and it performs
   477  *  rectangle validation and clipping before passing it to SDL_LowerBlitScaled()
   478  */
   479 extern DECLSPEC int SDLCALL SDL_UpperBlitScaled
   480     (SDL_Surface * src, const SDL_Rect * srcrect,
   481     SDL_Surface * dst, SDL_Rect * dstrect);
   482 
   483 /**
   484  *  This is a semi-private blit function and it performs low-level surface
   485  *  scaled blitting only.
   486  */
   487 extern DECLSPEC int SDLCALL SDL_LowerBlitScaled
   488     (SDL_Surface * src, SDL_Rect * srcrect,
   489     SDL_Surface * dst, SDL_Rect * dstrect);
   490 
   491 
   492 /* Ends C function definitions when using C++ */
   493 #ifdef __cplusplus
   494 /* *INDENT-OFF* */
   495 }
   496 /* *INDENT-ON* */
   497 #endif
   498 #include "close_code.h"
   499 
   500 #endif /* _SDL_surface_h */
   501 
   502 /* vi: set ts=4 sw=4 expandtab: */