include/SDL_surface.h
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Feb 2011 13:59:59 -0800
changeset 5297 1800dc39b74c
parent 5296 48067bfc300c
child 5375 16877f74123c
permissions -rw-r--r--
Changed the concept of a render clip rect to a render viewport.
The render viewport is automatically re-centered when the window changes size, so applications that don't care will not have to handle recalculating their rendering coordinates.

Fixed API for drawing and filling multiple rectangles - the parameter should be an array of rects, not an array of pointers to rects.

Fixed API for updating window rects for consistency with other APIs - the order is pointer to array followed by count in array.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2011 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 #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 extern DECLSPEC int SDLCALL SDL_SetColorKey(SDL_Surface * surface,
   218                                             int flag, Uint32 key);
   219 
   220 /**
   221  *  \brief Gets the color key (transparent pixel) in a blittable surface.
   222  *  
   223  *  \param surface The surface to update
   224  *  \param key A pointer filled in with the transparent pixel in the native 
   225  *             surface format
   226  *  
   227  *  \return 0 on success, or -1 if the surface is not valid or colorkey is not 
   228  *          enabled.
   229  */
   230 extern DECLSPEC int SDLCALL SDL_GetColorKey(SDL_Surface * surface,
   231                                             Uint32 * key);
   232 
   233 /**
   234  *  \brief Set an additional color value used in blit operations.
   235  *  
   236  *  \param surface The surface to update.
   237  *  \param r The red color value multiplied into blit operations.
   238  *  \param g The green color value multiplied into blit operations.
   239  *  \param b The blue color value multiplied into blit operations.
   240  *  
   241  *  \return 0 on success, or -1 if the surface is not valid.
   242  *  
   243  *  \sa SDL_GetSurfaceColorMod()
   244  */
   245 extern DECLSPEC int SDLCALL SDL_SetSurfaceColorMod(SDL_Surface * surface,
   246                                                    Uint8 r, Uint8 g, Uint8 b);
   247 
   248 
   249 /**
   250  *  \brief Get the additional color value used in blit operations.
   251  *  
   252  *  \param surface The surface to query.
   253  *  \param r A pointer filled in with the current red color value.
   254  *  \param g A pointer filled in with the current green color value.
   255  *  \param b A pointer filled in with the current blue color value.
   256  *  
   257  *  \return 0 on success, or -1 if the surface is not valid.
   258  *  
   259  *  \sa SDL_SetSurfaceColorMod()
   260  */
   261 extern DECLSPEC int SDLCALL SDL_GetSurfaceColorMod(SDL_Surface * surface,
   262                                                    Uint8 * r, Uint8 * g,
   263                                                    Uint8 * b);
   264 
   265 /**
   266  *  \brief Set an additional alpha value used in blit operations.
   267  *  
   268  *  \param surface The surface to update.
   269  *  \param alpha The alpha value multiplied into blit operations.
   270  *  
   271  *  \return 0 on success, or -1 if the surface is not valid.
   272  *  
   273  *  \sa SDL_GetSurfaceAlphaMod()
   274  */
   275 extern DECLSPEC int SDLCALL SDL_SetSurfaceAlphaMod(SDL_Surface * surface,
   276                                                    Uint8 alpha);
   277 
   278 /**
   279  *  \brief Get the additional alpha value used in blit operations.
   280  *  
   281  *  \param surface The surface to query.
   282  *  \param alpha A pointer filled in with the current alpha value.
   283  *  
   284  *  \return 0 on success, or -1 if the surface is not valid.
   285  *  
   286  *  \sa SDL_SetSurfaceAlphaMod()
   287  */
   288 extern DECLSPEC int SDLCALL SDL_GetSurfaceAlphaMod(SDL_Surface * surface,
   289                                                    Uint8 * alpha);
   290 
   291 /**
   292  *  \brief Set the blend mode used for blit operations.
   293  *  
   294  *  \param surface The surface to update.
   295  *  \param blendMode ::SDL_BlendMode to use for blit blending.
   296  *  
   297  *  \return 0 on success, or -1 if the parameters are not valid.
   298  *  
   299  *  \sa SDL_GetSurfaceBlendMode()
   300  */
   301 extern DECLSPEC int SDLCALL SDL_SetSurfaceBlendMode(SDL_Surface * surface,
   302                                                     SDL_BlendMode blendMode);
   303 
   304 /**
   305  *  \brief Get the blend mode used for blit operations.
   306  *  
   307  *  \param surface   The surface to query.
   308  *  \param blendMode A pointer filled in with the current blend mode.
   309  *  
   310  *  \return 0 on success, or -1 if the surface is not valid.
   311  *  
   312  *  \sa SDL_SetSurfaceBlendMode()
   313  */
   314 extern DECLSPEC int SDLCALL SDL_GetSurfaceBlendMode(SDL_Surface * surface,
   315                                                     SDL_BlendMode *blendMode);
   316 
   317 /**
   318  *  Sets the clipping rectangle for the destination surface in a blit.
   319  *  
   320  *  If the clip rectangle is NULL, clipping will be disabled.
   321  *  
   322  *  If the clip rectangle doesn't intersect the surface, the function will
   323  *  return SDL_FALSE and blits will be completely clipped.  Otherwise the
   324  *  function returns SDL_TRUE and blits to the surface will be clipped to
   325  *  the intersection of the surface area and the clipping rectangle.
   326  *  
   327  *  Note that blits are automatically clipped to the edges of the source
   328  *  and destination surfaces.
   329  */
   330 extern DECLSPEC SDL_bool SDLCALL SDL_SetClipRect(SDL_Surface * surface,
   331                                                  const SDL_Rect * rect);
   332 
   333 /**
   334  *  Gets the clipping rectangle for the destination surface in a blit.
   335  *  
   336  *  \c rect must be a pointer to a valid rectangle which will be filled
   337  *  with the correct values.
   338  */
   339 extern DECLSPEC void SDLCALL SDL_GetClipRect(SDL_Surface * surface,
   340                                              SDL_Rect * rect);
   341 
   342 /**
   343  *  Creates a new surface of the specified format, and then copies and maps 
   344  *  the given surface to it so the blit of the converted surface will be as 
   345  *  fast as possible.  If this function fails, it returns NULL.
   346  *  
   347  *  The \c flags parameter is passed to SDL_CreateRGBSurface() and has those 
   348  *  semantics.  You can also pass ::SDL_RLEACCEL in the flags parameter and
   349  *  SDL will try to RLE accelerate colorkey and alpha blits in the resulting
   350  *  surface.
   351  */
   352 extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurface
   353     (SDL_Surface * src, SDL_PixelFormat * fmt, Uint32 flags);
   354 
   355 /**
   356  * \brief Copy a block of pixels of one format to another format
   357  */
   358 extern DECLSPEC int SDLCALL SDL_ConvertPixels(int width, int height,
   359                                               Uint32 src_format,
   360                                               const void * src, int src_pitch,
   361                                               Uint32 dst_format,
   362                                               void * dst, int dst_pitch);
   363 
   364 /**
   365  *  Performs a fast fill of the given rectangle with \c color.
   366  *  
   367  *  If \c rect is NULL, the whole surface will be filled with \c color.
   368  *  
   369  *  The color should be a pixel of the format used by the surface, and 
   370  *  can be generated by the SDL_MapRGB() function.
   371  *  
   372  *  \return 0 on success, or -1 on error.
   373  */
   374 extern DECLSPEC int SDLCALL SDL_FillRect
   375     (SDL_Surface * dst, const SDL_Rect * rect, Uint32 color);
   376 extern DECLSPEC int SDLCALL SDL_FillRects
   377     (SDL_Surface * dst, const SDL_Rect * rects, int count, Uint32 color);
   378 
   379 /**
   380  *  Performs a fast blit from the source surface to the destination surface.
   381  *  
   382  *  This assumes that the source and destination rectangles are
   383  *  the same size.  If either \c srcrect or \c dstrect are NULL, the entire
   384  *  surface (\c src or \c dst) is copied.  The final blit rectangles are saved
   385  *  in \c srcrect and \c dstrect after all clipping is performed.
   386  *  
   387  *  \return If the blit is successful, it returns 0, otherwise it returns -1.
   388  *
   389  *  The blit function should not be called on a locked surface.
   390  *
   391  *  The blit semantics for surfaces with and without alpha and colorkey
   392  *  are defined as follows:
   393  *  \verbatim
   394     RGBA->RGB:
   395       SDL_SRCALPHA set:
   396         alpha-blend (using alpha-channel).
   397         SDL_SRCCOLORKEY ignored.
   398       SDL_SRCALPHA not set:
   399         copy RGB.
   400         if SDL_SRCCOLORKEY set, only copy the pixels matching the
   401         RGB values of the source colour key, ignoring alpha in the
   402         comparison.
   403    
   404     RGB->RGBA:
   405       SDL_SRCALPHA set:
   406         alpha-blend (using the source per-surface alpha value);
   407         set destination alpha to opaque.
   408       SDL_SRCALPHA not set:
   409         copy RGB, set destination alpha to source per-surface alpha value.
   410       both:
   411         if SDL_SRCCOLORKEY set, only copy the pixels matching the
   412         source colour key.
   413    
   414     RGBA->RGBA:
   415       SDL_SRCALPHA set:
   416         alpha-blend (using the source alpha channel) the RGB values;
   417         leave destination alpha untouched. [Note: is this correct?]
   418         SDL_SRCCOLORKEY ignored.
   419       SDL_SRCALPHA not set:
   420         copy all of RGBA to the destination.
   421         if SDL_SRCCOLORKEY set, only copy the pixels matching the
   422         RGB values of the source colour key, ignoring alpha in the
   423        comparison.
   424    
   425     RGB->RGB: 
   426       SDL_SRCALPHA set:
   427         alpha-blend (using the source per-surface alpha value).
   428       SDL_SRCALPHA not set:
   429         copy RGB.
   430       both:
   431         if SDL_SRCCOLORKEY set, only copy the pixels matching the
   432         source colour key.
   433     \endverbatim
   434  *  
   435  *  You should call SDL_BlitSurface() unless you know exactly how SDL
   436  *  blitting works internally and how to use the other blit functions.
   437  */
   438 #define SDL_BlitSurface SDL_UpperBlit
   439 
   440 /**
   441  *  This is the public blit function, SDL_BlitSurface(), and it performs
   442  *  rectangle validation and clipping before passing it to SDL_LowerBlit()
   443  */
   444 extern DECLSPEC int SDLCALL SDL_UpperBlit
   445     (SDL_Surface * src, const SDL_Rect * srcrect,
   446      SDL_Surface * dst, SDL_Rect * dstrect);
   447 
   448 /**
   449  *  This is a semi-private blit function and it performs low-level surface
   450  *  blitting only.
   451  */
   452 extern DECLSPEC int SDLCALL SDL_LowerBlit
   453     (SDL_Surface * src, SDL_Rect * srcrect,
   454      SDL_Surface * dst, SDL_Rect * dstrect);
   455 
   456 /**
   457  *  \brief Perform a fast, low quality, stretch blit between two surfaces of the
   458  *         same pixel format.
   459  *  
   460  *  \note This function uses a static buffer, and is not thread-safe.
   461  */
   462 extern DECLSPEC int SDLCALL SDL_SoftStretch(SDL_Surface * src,
   463                                             const SDL_Rect * srcrect,
   464                                             SDL_Surface * dst,
   465                                             const SDL_Rect * dstrect);
   466 
   467 /**
   468  *  \brief Perform a fast, low quality, stretch blit between two surfaces of the
   469  *         different pixel formats.
   470  *  
   471  *  \note This function calls SDL_SoftStretch or SDL_LowerBlit.
   472  */
   473 extern DECLSPEC int SDLCALL SDL_BlitScaled
   474     (SDL_Surface * src, const SDL_Rect * srcrect,
   475     SDL_Surface * dst, const SDL_Rect * dstrect);
   476 
   477 
   478 /* Ends C function definitions when using C++ */
   479 #ifdef __cplusplus
   480 /* *INDENT-OFF* */
   481 }
   482 /* *INDENT-ON* */
   483 #endif
   484 #include "close_code.h"
   485 
   486 #endif /* _SDL_surface_h */
   487 
   488 /* vi: set ts=4 sw=4 expandtab: */