include/SDL_surface.h
author Sam Lantinga <slouken@libsdl.org>
Thu, 03 Feb 2011 02:45:29 -0800
changeset 5163 d72793305335
parent 5138 da10636e5eca
child 5241 189b1bdb7baf
permissions -rw-r--r--
Making the API simpler, moved the surface drawing functions to the software renderer.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 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_blendmode.h"
    36 #include "SDL_rwops.h"
    37 
    38 #include "begin_code.h"
    39 /* Set up for C function definitions, even when using C++ */
    40 #ifdef __cplusplus
    41 /* *INDENT-OFF* */
    42 extern "C" {
    43 /* *INDENT-ON* */
    44 #endif
    45 
    46 /**
    47  *  \name Surface flags
    48  *  
    49  *  These are the currently supported flags for the ::SDL_surface.
    50  *  
    51  *  \internal
    52  *  Used internally (read-only).
    53  */
    54 /*@{*/
    55 #define SDL_PREALLOC        0x00000001  /**< Surface uses preallocated memory */
    56 #define SDL_RLEACCEL        0x00000002  /**< Surface is RLE encoded */
    57 /*@}*//*Surface flags*/
    58 
    59 /**
    60  *  Evaluates to true if the surface needs to be locked before access.
    61  */
    62 #define SDL_MUSTLOCK(S)	(((S)->flags & SDL_RLEACCEL) != 0)
    63 
    64 /**
    65  * \brief A collection of pixels used in software blitting.
    66  *
    67  * \note  This structure should be treated as read-only, except for \c pixels,
    68  *        which, if not NULL, contains the raw pixel data for the surface.
    69  */
    70 typedef struct SDL_Surface
    71 {
    72     Uint32 flags;               /**< Read-only */
    73     SDL_PixelFormat *format;    /**< Read-only */
    74     int w, h;                   /**< Read-only */
    75     int pitch;                  /**< Read-only */
    76     void *pixels;               /**< Read-write */
    77 
    78     /** Application data associated with the surface */
    79     void *userdata;             /**< Read-write */
    80 
    81     /** information needed for surfaces requiring locks */
    82     int locked;                 /**< Read-only */
    83     void *lock_data;            /**< Read-only */
    84 
    85     /** clipping information */
    86     SDL_Rect clip_rect;         /**< Read-only */
    87 
    88     /** info for fast blit mapping to other surfaces */
    89     struct SDL_BlitMap *map;    /**< Private */
    90 
    91     /** format version, bumped at every change to invalidate blit maps */
    92     int format_version;         /**< Private */
    93 
    94     /** Reference count -- used when freeing surface */
    95     int refcount;               /**< Read-mostly */
    96 } SDL_Surface;
    97 
    98 /**
    99  * \brief The type of function used for surface blitting functions.
   100  */
   101 typedef int (*SDL_blit) (struct SDL_Surface * src, SDL_Rect * srcrect,
   102                          struct SDL_Surface * dst, SDL_Rect * dstrect);
   103 
   104 /**
   105  *  Allocate and free an RGB surface.
   106  *  
   107  *  If the depth is 4 or 8 bits, an empty palette is allocated for the surface.
   108  *  If the depth is greater than 8 bits, the pixel format is set using the
   109  *  flags '[RGB]mask'.
   110  *  
   111  *  If the function runs out of memory, it will return NULL.
   112  *  
   113  *  \param flags The \c flags are obsolete and should be set to 0.
   114  */
   115 extern DECLSPEC SDL_Surface *SDLCALL SDL_CreateRGBSurface
   116     (Uint32 flags, int width, int height, int depth,
   117      Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
   118 extern DECLSPEC SDL_Surface *SDLCALL SDL_CreateRGBSurfaceFrom(void *pixels,
   119                                                               int width,
   120                                                               int height,
   121                                                               int depth,
   122                                                               int pitch,
   123                                                               Uint32 Rmask,
   124                                                               Uint32 Gmask,
   125                                                               Uint32 Bmask,
   126                                                               Uint32 Amask);
   127 extern DECLSPEC void SDLCALL SDL_FreeSurface(SDL_Surface * surface);
   128 
   129 /**
   130  *  \brief Set the palette used by a surface.
   131  *  
   132  *  \return 0, or -1 if the surface format doesn't use a palette.
   133  *  
   134  *  \note A single palette can be shared with many surfaces.
   135  */
   136 extern DECLSPEC int SDLCALL SDL_SetSurfacePalette(SDL_Surface * surface,
   137                                                   SDL_Palette * palette);
   138 
   139 /**
   140  *  \brief Sets up a surface for directly accessing the pixels.
   141  *  
   142  *  Between calls to SDL_LockSurface() / SDL_UnlockSurface(), you can write
   143  *  to and read from \c surface->pixels, using the pixel format stored in 
   144  *  \c surface->format.  Once you are done accessing the surface, you should 
   145  *  use SDL_UnlockSurface() to release it.
   146  *  
   147  *  Not all surfaces require locking.  If SDL_MUSTLOCK(surface) evaluates
   148  *  to 0, then you can read and write to the surface at any time, and the
   149  *  pixel format of the surface will not change.
   150  *  
   151  *  No operating system or library calls should be made between lock/unlock
   152  *  pairs, as critical system locks may be held during this time.
   153  *  
   154  *  SDL_LockSurface() returns 0, or -1 if the surface couldn't be locked.
   155  *  
   156  *  \sa SDL_UnlockSurface()
   157  */
   158 extern DECLSPEC int SDLCALL SDL_LockSurface(SDL_Surface * surface);
   159 /** \sa SDL_LockSurface() */
   160 extern DECLSPEC void SDLCALL SDL_UnlockSurface(SDL_Surface * surface);
   161 
   162 /**
   163  *  Load a surface from a seekable SDL data stream (memory or file).
   164  *  
   165  *  If \c freesrc is non-zero, the stream will be closed after being read.
   166  *  
   167  *  The new surface should be freed with SDL_FreeSurface().
   168  *  
   169  *  \return the new surface, or NULL if there was an error.
   170  */
   171 extern DECLSPEC SDL_Surface *SDLCALL SDL_LoadBMP_RW(SDL_RWops * src,
   172                                                     int freesrc);
   173 
   174 /**
   175  *  Load a surface from a file.
   176  *  
   177  *  Convenience macro.
   178  */
   179 #define SDL_LoadBMP(file)	SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)
   180 
   181 /**
   182  *  Save a surface to a seekable SDL data stream (memory or file).
   183  *  
   184  *  If \c freedst is non-zero, the stream will be closed after being written.
   185  *  
   186  *  \return 0 if successful or -1 if there was an error.
   187  */
   188 extern DECLSPEC int SDLCALL SDL_SaveBMP_RW
   189     (SDL_Surface * surface, SDL_RWops * dst, int freedst);
   190 
   191 /** 
   192  *  Save a surface to a file.
   193  *  
   194  *  Convenience macro.
   195  */
   196 #define SDL_SaveBMP(surface, file) \
   197 		SDL_SaveBMP_RW(surface, SDL_RWFromFile(file, "wb"), 1)
   198 
   199 /**
   200  *  \brief Sets the RLE acceleration hint for a surface.
   201  *  
   202  *  \return 0 on success, or -1 if the surface is not valid
   203  *  
   204  *  \note If RLE is enabled, colorkey and alpha blending blits are much faster,
   205  *        but the surface must be locked before directly accessing the pixels.
   206  */
   207 extern DECLSPEC int SDLCALL SDL_SetSurfaceRLE(SDL_Surface * surface,
   208                                               int flag);
   209 
   210 /**
   211  *  \brief Sets the color key (transparent pixel) in a blittable surface.
   212  *  
   213  *  \param surface The surface to update
   214  *  \param flag Non-zero to enable colorkey and 0 to disable colorkey 
   215  *  \param key The transparent pixel in the native surface format
   216  *  
   217  *  \return 0 on success, or -1 if the surface is not valid
   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 
   357 /**
   358  * \brief Copy a block of pixels of one format to another format
   359  */
   360 extern DECLSPEC int SDLCALL SDL_ConvertPixels(int width, int height,
   361                                               Uint32 src_format,
   362                                               const void * src, int src_pitch,
   363                                               Uint32 dst_format,
   364                                               void * dst, int dst_pitch);
   365 
   366 /**
   367  *  Performs a fast fill of the given rectangle with \c color.
   368  *  
   369  *  If \c rect is NULL, the whole surface will be filled with \c color.
   370  *  
   371  *  The color should be a pixel of the format used by the surface, and 
   372  *  can be generated by the SDL_MapRGB() function.
   373  *  
   374  *  \return 0 on success, or -1 on error.
   375  */
   376 extern DECLSPEC int SDLCALL SDL_FillRect
   377     (SDL_Surface * dst, const SDL_Rect * rect, Uint32 color);
   378 extern DECLSPEC int SDLCALL SDL_FillRects
   379     (SDL_Surface * dst, const SDL_Rect ** rects, int count, Uint32 color);
   380 
   381 /**
   382  *  Performs a fast blit from the source surface to the destination surface.
   383  *  
   384  *  This assumes that the source and destination rectangles are
   385  *  the same size.  If either \c srcrect or \c dstrect are NULL, the entire
   386  *  surface (\c src or \c dst) is copied.  The final blit rectangles are saved
   387  *  in \c srcrect and \c dstrect after all clipping is performed.
   388  *  
   389  *  \return If the blit is successful, it returns 0, otherwise it returns -1.
   390  *
   391  *  The blit function should not be called on a locked surface.
   392  *
   393  *  The blit semantics for surfaces with and without alpha and colorkey
   394  *  are defined as follows:
   395  *  \verbatim
   396     RGBA->RGB:
   397       SDL_SRCALPHA set:
   398         alpha-blend (using alpha-channel).
   399         SDL_SRCCOLORKEY ignored.
   400       SDL_SRCALPHA not set:
   401         copy RGB.
   402         if SDL_SRCCOLORKEY set, only copy the pixels matching the
   403         RGB values of the source colour key, ignoring alpha in the
   404         comparison.
   405    
   406     RGB->RGBA:
   407       SDL_SRCALPHA set:
   408         alpha-blend (using the source per-surface alpha value);
   409         set destination alpha to opaque.
   410       SDL_SRCALPHA not set:
   411         copy RGB, set destination alpha to source per-surface alpha value.
   412       both:
   413         if SDL_SRCCOLORKEY set, only copy the pixels matching the
   414         source colour key.
   415    
   416     RGBA->RGBA:
   417       SDL_SRCALPHA set:
   418         alpha-blend (using the source alpha channel) the RGB values;
   419         leave destination alpha untouched. [Note: is this correct?]
   420         SDL_SRCCOLORKEY ignored.
   421       SDL_SRCALPHA not set:
   422         copy all of RGBA to the destination.
   423         if SDL_SRCCOLORKEY set, only copy the pixels matching the
   424         RGB values of the source colour key, ignoring alpha in the
   425        comparison.
   426    
   427     RGB->RGB: 
   428       SDL_SRCALPHA set:
   429         alpha-blend (using the source per-surface alpha value).
   430       SDL_SRCALPHA not set:
   431         copy RGB.
   432       both:
   433         if SDL_SRCCOLORKEY set, only copy the pixels matching the
   434         source colour key.
   435     \endverbatim
   436  *  
   437  *  If either of the surfaces were in video memory, and the blit returns -2,
   438  *  the video memory was lost, so it should be reloaded with artwork and 
   439  *  re-blitted:
   440  *  @code
   441  *  while ( SDL_BlitSurface(image, imgrect, screen, dstrect) == -2 ) {
   442  *      while ( SDL_LockSurface(image) < 0 )
   443  *          Sleep(10);
   444  *      -- Write image pixels to image->pixels --
   445  *      SDL_UnlockSurface(image);
   446  *  }
   447  *  @endcode
   448  *  
   449  *  This happens under DirectX 5.0 when the system switches away from your
   450  *  fullscreen application.  The lock will also fail until you have access
   451  *  to the video memory again.
   452  *  
   453  *  You should call SDL_BlitSurface() unless you know exactly how SDL
   454  *  blitting works internally and how to use the other blit functions.
   455  */
   456 #define SDL_BlitSurface SDL_UpperBlit
   457 
   458 /**
   459  *  This is the public blit function, SDL_BlitSurface(), and it performs
   460  *  rectangle validation and clipping before passing it to SDL_LowerBlit()
   461  */
   462 extern DECLSPEC int SDLCALL SDL_UpperBlit
   463     (SDL_Surface * src, const SDL_Rect * srcrect,
   464      SDL_Surface * dst, SDL_Rect * dstrect);
   465 
   466 /**
   467  *  This is a semi-private blit function and it performs low-level surface
   468  *  blitting only.
   469  */
   470 extern DECLSPEC int SDLCALL SDL_LowerBlit
   471     (SDL_Surface * src, SDL_Rect * srcrect,
   472      SDL_Surface * dst, SDL_Rect * dstrect);
   473 
   474 /**
   475  *  \brief Perform a fast, low quality, stretch blit between two surfaces of the
   476  *         same pixel format.
   477  *  
   478  *  \note This function uses a static buffer, and is not thread-safe.
   479  */
   480 extern DECLSPEC int SDLCALL SDL_SoftStretch(SDL_Surface * src,
   481                                             const SDL_Rect * srcrect,
   482                                             SDL_Surface * dst,
   483                                             const SDL_Rect * dstrect);
   484 
   485 /* Ends C function definitions when using C++ */
   486 #ifdef __cplusplus
   487 /* *INDENT-OFF* */
   488 }
   489 /* *INDENT-ON* */
   490 #endif
   491 #include "close_code.h"
   492 
   493 #endif /* _SDL_surface_h */
   494 
   495 /* vi: set ts=4 sw=4 expandtab: */