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