include/SDL_render.h
author Sam Lantinga <slouken@libsdl.org>
Thu, 03 Feb 2011 10:03:55 -0800
changeset 5164 97423d858a1a
parent 5158 b3ccd1947786
child 5166 4d39eeaad00b
permissions -rw-r--r--
Added an intro to the features and goals of the 2D rendering API.
     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_render.h
    25  *  
    26  *  Header file for SDL 2D rendering functions.
    27  *
    28  *  This API supports the following features:
    29  *      * single pixel points
    30  *      * single pixel lines
    31  *      * filled rectangles
    32  *      * texture images
    33  *
    34  *  The primitives may be drawn in opaque, blended, or additive modes.
    35  *
    36  *  The texture images may be drawn in opaque, blended, or additive modes.
    37  *  They can have an additional color tint or alpha modulation applied to
    38  *  them, and may also be stretched with linear interpolation.
    39  *
    40  *  This API is designed to accelerate simple 2D operations. You may
    41  *  want more functionality such as rotation and particle effects and
    42  *  in that case you should use SDL's OpenGL/Direct3D support or one
    43  *  of the many good 3D engines.
    44  */
    45 
    46 #ifndef _SDL_render_h
    47 #define _SDL_render_h
    48 
    49 #include "SDL_stdinc.h"
    50 #include "SDL_rect.h"
    51 #include "SDL_video.h"
    52 
    53 #include "begin_code.h"
    54 /* Set up for C function definitions, even when using C++ */
    55 #ifdef __cplusplus
    56 /* *INDENT-OFF* */
    57 extern "C" {
    58 /* *INDENT-ON* */
    59 #endif
    60 
    61 /**
    62  *  \brief Flags used when creating a rendering context
    63  */
    64 typedef enum
    65 {
    66     SDL_RENDERER_ACCELERATED = 0x00000001,      /**< The renderer uses hardware 
    67                                                      acceleration */
    68     SDL_RENDERER_PRESENTVSYNC = 0x00000002      /**< Present is synchronized 
    69                                                      with the refresh rate */
    70 } SDL_RendererFlags;
    71 
    72 /**
    73  *  \brief Information on the capabilities of a render driver or context.
    74  */
    75 typedef struct SDL_RendererInfo
    76 {
    77     const char *name;           /**< The name of the renderer */
    78     Uint32 flags;               /**< Supported ::SDL_RendererFlags */
    79     Uint32 num_texture_formats; /**< The number of available texture formats */
    80     Uint32 texture_formats[16]; /**< The available texture formats */
    81     int max_texture_width;      /**< The maximimum texture width */
    82     int max_texture_height;     /**< The maximimum texture height */
    83 } SDL_RendererInfo;
    84 
    85 /**
    86  *  \brief The access pattern allowed for a texture.
    87  */
    88 typedef enum
    89 {
    90     SDL_TEXTUREACCESS_STATIC,    /**< Changes rarely, not lockable */
    91     SDL_TEXTUREACCESS_STREAMING  /**< Changes frequently, lockable */
    92 } SDL_TextureAccess;
    93 
    94 /**
    95  *  \brief The texture channel modulation used in SDL_RenderCopy().
    96  */
    97 typedef enum
    98 {
    99     SDL_TEXTUREMODULATE_NONE = 0x00000000,     /**< No modulation */
   100     SDL_TEXTUREMODULATE_COLOR = 0x00000001,    /**< srcC = srcC * color */
   101     SDL_TEXTUREMODULATE_ALPHA = 0x00000002     /**< srcA = srcA * alpha */
   102 } SDL_TextureModulate;
   103 
   104 /**
   105  *  \brief A structure representing rendering state
   106  */
   107 struct SDL_Renderer;
   108 typedef struct SDL_Renderer SDL_Renderer;
   109 
   110 /**
   111  *  \brief An efficient driver-specific representation of pixel data
   112  */
   113 struct SDL_Texture;
   114 typedef struct SDL_Texture SDL_Texture;
   115 
   116 
   117 /* Function prototypes */
   118 
   119 /**
   120  *  \brief Get the number of 2D rendering drivers available for the current 
   121  *         display.
   122  *  
   123  *  A render driver is a set of code that handles rendering and texture
   124  *  management on a particular display.  Normally there is only one, but
   125  *  some drivers may have several available with different capabilities.
   126  *  
   127  *  \sa SDL_GetRenderDriverInfo()
   128  *  \sa SDL_CreateRenderer()
   129  */
   130 extern DECLSPEC int SDLCALL SDL_GetNumRenderDrivers(void);
   131 
   132 /**
   133  *  \brief Get information about a specific 2D rendering driver for the current 
   134  *         display.
   135  *  
   136  *  \param index The index of the driver to query information about.
   137  *  \param info  A pointer to an SDL_RendererInfo struct to be filled with 
   138  *               information on the rendering driver.
   139  *  
   140  *  \return 0 on success, -1 if the index was out of range.
   141  *  
   142  *  \sa SDL_CreateRenderer()
   143  */
   144 extern DECLSPEC int SDLCALL SDL_GetRenderDriverInfo(int index,
   145                                                     SDL_RendererInfo * info);
   146 
   147 /**
   148  *  \brief Create a 2D rendering context for a window.
   149  *  
   150  *  \param window The window where rendering is displayed.
   151  *  \param index    The index of the rendering driver to initialize, or -1 to 
   152  *                  initialize the first one supporting the requested flags.
   153  *  \param flags    ::SDL_RendererFlags.
   154  *  
   155  *  \return A valid rendering context or NULL if there was an error.
   156  *  
   157  *  \sa SDL_GetRendererInfo()
   158  *  \sa SDL_DestroyRenderer()
   159  */
   160 extern DECLSPEC SDL_Renderer * SDLCALL SDL_CreateRenderer(SDL_Window * window,
   161                                                int index, Uint32 flags);
   162 
   163 /**
   164  *  \brief Get information about a rendering context.
   165  */
   166 extern DECLSPEC int SDLCALL SDL_GetRendererInfo(SDL_Renderer * renderer,
   167                                                 SDL_RendererInfo * info);
   168 
   169 /**
   170  *  \brief Create a texture for a rendering context.
   171  *  
   172  *  \param format The format of the texture.
   173  *  \param access One of the enumerated values in ::SDL_TextureAccess.
   174  *  \param w      The width of the texture in pixels.
   175  *  \param h      The height of the texture in pixels.
   176  *  
   177  *  \return The created texture is returned, or 0 if no rendering context was 
   178  *          active,  the format was unsupported, or the width or height were out
   179  *          of range.
   180  *  
   181  *  \sa SDL_QueryTexture()
   182  *  \sa SDL_DestroyTexture()
   183  */
   184 extern DECLSPEC SDL_Texture * SDLCALL SDL_CreateTexture(SDL_Renderer * renderer,                                                        Uint32 format,
   185                                                         int access, int w,
   186                                                         int h);
   187 
   188 /**
   189  *  \brief Create a texture from an existing surface.
   190  *  
   191  *  \param surface The surface containing pixel data used to fill the texture.
   192  *  
   193  *  \return The created texture is returned, or 0 on error.
   194  *  
   195  *  \note The surface is not modified or freed by this function.
   196  *  
   197  *  \sa SDL_QueryTexture()
   198  *  \sa SDL_DestroyTexture()
   199  */
   200 extern DECLSPEC SDL_Texture * SDLCALL SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface);
   201 
   202 /**
   203  *  \brief Query the attributes of a texture
   204  *  
   205  *  \param texture A texture to be queried.
   206  *  \param format  A pointer filled in with the raw format of the texture.  The 
   207  *                 actual format may differ, but pixel transfers will use this 
   208  *                 format.
   209  *  \param access  A pointer filled in with the actual access to the texture.
   210  *  \param w       A pointer filled in with the width of the texture in pixels.
   211  *  \param h       A pointer filled in with the height of the texture in pixels.
   212  *  
   213  *  \return 0 on success, or -1 if the texture is not valid.
   214  */
   215 extern DECLSPEC int SDLCALL SDL_QueryTexture(SDL_Texture * texture,
   216                                              Uint32 * format, int *access,
   217                                              int *w, int *h);
   218 
   219 /**
   220  *  \brief Set an additional color value used in render copy operations.
   221  *  
   222  *  \param texture The texture to update.
   223  *  \param r       The red color value multiplied into copy operations.
   224  *  \param g       The green color value multiplied into copy operations.
   225  *  \param b       The blue color value multiplied into copy operations.
   226  *  
   227  *  \return 0 on success, or -1 if the texture is not valid or color modulation 
   228  *          is not supported.
   229  *  
   230  *  \sa SDL_GetTextureColorMod()
   231  */
   232 extern DECLSPEC int SDLCALL SDL_SetTextureColorMod(SDL_Texture * texture,
   233                                                    Uint8 r, Uint8 g, Uint8 b);
   234 
   235 
   236 /**
   237  *  \brief Get the additional color value used in render copy operations.
   238  *  
   239  *  \param texture The texture to query.
   240  *  \param r         A pointer filled in with the current red color value.
   241  *  \param g         A pointer filled in with the current green color value.
   242  *  \param b         A pointer filled in with the current blue color value.
   243  *  
   244  *  \return 0 on success, or -1 if the texture is not valid.
   245  *  
   246  *  \sa SDL_SetTextureColorMod()
   247  */
   248 extern DECLSPEC int SDLCALL SDL_GetTextureColorMod(SDL_Texture * texture,
   249                                                    Uint8 * r, Uint8 * g,
   250                                                    Uint8 * b);
   251 
   252 /**
   253  *  \brief Set an additional alpha value used in render copy operations.
   254  *  
   255  *  \param texture The texture to update.
   256  *  \param alpha     The alpha value multiplied into copy operations.
   257  *  
   258  *  \return 0 on success, or -1 if the texture is not valid or alpha modulation 
   259  *          is not supported.
   260  *  
   261  *  \sa SDL_GetTextureAlphaMod()
   262  */
   263 extern DECLSPEC int SDLCALL SDL_SetTextureAlphaMod(SDL_Texture * texture,
   264                                                    Uint8 alpha);
   265 
   266 /**
   267  *  \brief Get the additional alpha value used in render copy operations.
   268  *  
   269  *  \param texture The texture to query.
   270  *  \param alpha     A pointer filled in with the current alpha value.
   271  *  
   272  *  \return 0 on success, or -1 if the texture is not valid.
   273  *  
   274  *  \sa SDL_SetTextureAlphaMod()
   275  */
   276 extern DECLSPEC int SDLCALL SDL_GetTextureAlphaMod(SDL_Texture * texture,
   277                                                    Uint8 * alpha);
   278 
   279 /**
   280  *  \brief Set the blend mode used for texture copy operations.
   281  *  
   282  *  \param texture The texture to update.
   283  *  \param blendMode ::SDL_BlendMode to use for texture blending.
   284  *  
   285  *  \return 0 on success, or -1 if the texture is not valid or the blend mode is
   286  *          not supported.
   287  *  
   288  *  \note If the blend mode is not supported, the closest supported mode is
   289  *        chosen.
   290  *  
   291  *  \sa SDL_GetTextureBlendMode()
   292  */
   293 extern DECLSPEC int SDLCALL SDL_SetTextureBlendMode(SDL_Texture * texture,
   294                                                     SDL_BlendMode blendMode);
   295 
   296 /**
   297  *  \brief Get the blend mode used for texture copy operations.
   298  *  
   299  *  \param texture   The texture to query.
   300  *  \param blendMode A pointer filled in with the current blend mode.
   301  *  
   302  *  \return 0 on success, or -1 if the texture is not valid.
   303  *  
   304  *  \sa SDL_SetTextureBlendMode()
   305  */
   306 extern DECLSPEC int SDLCALL SDL_GetTextureBlendMode(SDL_Texture * texture,
   307                                                     SDL_BlendMode *blendMode);
   308 
   309 /**
   310  *  \brief Update the given texture rectangle with new pixel data.
   311  *  
   312  *  \param texture   The texture to update
   313  *  \param rect      A pointer to the rectangle of pixels to update, or NULL to 
   314  *                   update the entire texture.
   315  *  \param pixels    The raw pixel data.
   316  *  \param pitch     The number of bytes between rows of pixel data.
   317  *  
   318  *  \return 0 on success, or -1 if the texture is not valid.
   319  *  
   320  *  \note This is a fairly slow function.
   321  */
   322 extern DECLSPEC int SDLCALL SDL_UpdateTexture(SDL_Texture * texture,
   323                                               const SDL_Rect * rect,
   324                                               const void *pixels, int pitch);
   325 
   326 /**
   327  *  \brief Lock a portion of the texture for pixel access.
   328  *  
   329  *  \param texture   The texture to lock for access, which was created with 
   330  *                   ::SDL_TEXTUREACCESS_STREAMING.
   331  *  \param rect      A pointer to the rectangle to lock for access. If the rect 
   332  *                   is NULL, the entire texture will be locked.
   333  *  \param pixels    This is filled in with a pointer to the locked pixels, 
   334  *                   appropriately offset by the locked area.
   335  *  \param pitch     This is filled in with the pitch of the locked pixels.
   336  *  
   337  *  \return 0 on success, or -1 if the texture is not valid or was not created with ::SDL_TEXTUREACCESS_STREAMING.
   338  *  
   339  *  \sa SDL_UnlockTexture()
   340  */
   341 extern DECLSPEC int SDLCALL SDL_LockTexture(SDL_Texture * texture,
   342                                             const SDL_Rect * rect,
   343                                             void **pixels, int *pitch);
   344 
   345 /**
   346  *  \brief Unlock a texture, uploading the changes to video memory, if needed.
   347  *  
   348  *  \sa SDL_LockTexture()
   349  */
   350 extern DECLSPEC void SDLCALL SDL_UnlockTexture(SDL_Texture * texture);
   351 
   352 /**
   353  *  \brief Set the color used for drawing operations (Fill and Line).
   354  *  
   355  *  \param r The red value used to draw on the rendering target.
   356  *  \param g The green value used to draw on the rendering target.
   357  *  \param b The blue value used to draw on the rendering target.
   358  *  \param a The alpha value used to draw on the rendering target, usually 
   359  *           ::SDL_ALPHA_OPAQUE (255).
   360  *  
   361  *  \return 0 on success, or -1 on error
   362  */
   363 extern DECLSPEC int SDL_SetRenderDrawColor(SDL_Renderer * renderer,
   364                                            Uint8 r, Uint8 g, Uint8 b,
   365                                            Uint8 a);
   366 
   367 /**
   368  *  \brief Get the color used for drawing operations (Fill and Line).
   369  *  
   370  *  \param r A pointer to the red value used to draw on the rendering target.
   371  *  \param g A pointer to the green value used to draw on the rendering target.
   372  *  \param b A pointer to the blue value used to draw on the rendering target.
   373  *  \param a A pointer to the alpha value used to draw on the rendering target, 
   374  *           usually ::SDL_ALPHA_OPAQUE (255).
   375  *  
   376  *  \return 0 on success, or -1 on error
   377  */
   378 extern DECLSPEC int SDL_GetRenderDrawColor(SDL_Renderer * renderer,
   379                                            Uint8 * r, Uint8 * g, Uint8 * b,
   380                                            Uint8 * a);
   381 
   382 /**
   383  *  \brief Set the blend mode used for drawing operations (Fill and Line).
   384  *  
   385  *  \param blendMode ::SDL_BlendMode to use for blending.
   386  *  
   387  *  \return 0 on success, or -1 on error
   388  *  
   389  *  \note If the blend mode is not supported, the closest supported mode is 
   390  *        chosen.
   391  *  
   392  *  \sa SDL_GetRenderDrawBlendMode()
   393  */
   394 extern DECLSPEC int SDLCALL SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer,
   395                                                        SDL_BlendMode blendMode);
   396 
   397 /**
   398  *  \brief Get the blend mode used for drawing operations.
   399  *  
   400  *  \param blendMode A pointer filled in with the current blend mode.
   401  *  
   402  *  \return 0 on success, or -1 on error
   403  *  
   404  *  \sa SDL_SetRenderDrawBlendMode()
   405  */
   406 extern DECLSPEC int SDLCALL SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer,
   407                                                        SDL_BlendMode *blendMode);
   408 
   409 /**
   410  *  \brief Clear the current rendering target with the drawing color
   411  */
   412 extern DECLSPEC int SDLCALL SDL_RenderClear(SDL_Renderer * renderer);
   413 
   414 /**
   415  *  \brief Draw a point on the current rendering target.
   416  *  
   417  *  \param x The x coordinate of the point.
   418  *  \param y The y coordinate of the point.
   419  *  
   420  *  \return 0 on success, or -1 on error
   421  */
   422 extern DECLSPEC int SDLCALL SDL_RenderDrawPoint(SDL_Renderer * renderer,
   423                                                 int x, int y);
   424 
   425 /**
   426  *  \brief Draw multiple points on the current rendering target.
   427  *  
   428  *  \param points The points to draw
   429  *  \param count The number of points to draw
   430  *  
   431  *  \return 0 on success, or -1 on error
   432  */
   433 extern DECLSPEC int SDLCALL SDL_RenderDrawPoints(SDL_Renderer * renderer,
   434                                                  const SDL_Point * points,
   435                                                  int count);
   436 
   437 /**
   438  *  \brief Draw a line on the current rendering target.
   439  *  
   440  *  \param x1 The x coordinate of the start point.
   441  *  \param y1 The y coordinate of the start point.
   442  *  \param x2 The x coordinate of the end point.
   443  *  \param y2 The y coordinate of the end point.
   444  *  
   445  *  \return 0 on success, or -1 on error
   446  */
   447 extern DECLSPEC int SDLCALL SDL_RenderDrawLine(SDL_Renderer * renderer,
   448                                                int x1, int y1, int x2, int y2);
   449 
   450 /**
   451  *  \brief Draw a series of connected lines on the current rendering target.
   452  *  
   453  *  \param points The points along the lines
   454  *  \param count The number of points, drawing count-1 lines
   455  *  
   456  *  \return 0 on success, or -1 on error
   457  */
   458 extern DECLSPEC int SDLCALL SDL_RenderDrawLines(SDL_Renderer * renderer,
   459                                                 const SDL_Point * points,
   460                                                 int count);
   461 
   462 /**
   463  *  \brief Draw a rectangle on the current rendering target.
   464  *  
   465  *  \param rect A pointer to the destination rectangle, or NULL to outline the entire rendering target.
   466  *  
   467  *  \return 0 on success, or -1 on error
   468  */
   469 extern DECLSPEC int SDLCALL SDL_RenderDrawRect(SDL_Renderer * renderer,
   470                                                const SDL_Rect * rect);
   471 
   472 /**
   473  *  \brief Draw some number of rectangles on the current rendering target.
   474  *  
   475  *  \param rects A pointer to an array of destination rectangles.
   476  *  \param count The number of rectangles.
   477  *  
   478  *  \return 0 on success, or -1 on error
   479  */
   480 extern DECLSPEC int SDLCALL SDL_RenderDrawRects(SDL_Renderer * renderer,
   481                                                 const SDL_Rect ** rects,
   482                                                 int count);
   483 
   484 /**
   485  *  \brief Fill a rectangle on the current rendering target with the drawing color.
   486  *  
   487  *  \param rect A pointer to the destination rectangle, or NULL for the entire 
   488  *              rendering target.
   489  *  
   490  *  \return 0 on success, or -1 on error
   491  */
   492 extern DECLSPEC int SDLCALL SDL_RenderFillRect(SDL_Renderer * renderer,
   493                                                const SDL_Rect * rect);
   494 
   495 /**
   496  *  \brief Fill some number of rectangles on the current rendering target with the drawing color.
   497  *  
   498  *  \param rects A pointer to an array of destination rectangles.
   499  *  \param count The number of rectangles.
   500  *  
   501  *  \return 0 on success, or -1 on error
   502  */
   503 extern DECLSPEC int SDLCALL SDL_RenderFillRects(SDL_Renderer * renderer,
   504                                                 const SDL_Rect ** rect,
   505                                                 int count);
   506 
   507 /**
   508  *  \brief Copy a portion of the texture to the current rendering target.
   509  *  
   510  *  \param texture The source texture.
   511  *  \param srcrect   A pointer to the source rectangle, or NULL for the entire 
   512  *                   texture.
   513  *  \param dstrect   A pointer to the destination rectangle, or NULL for the 
   514  *                   entire rendering target.
   515  *  
   516  *  \return 0 on success, or -1 on error
   517  */
   518 extern DECLSPEC int SDLCALL SDL_RenderCopy(SDL_Renderer * renderer,
   519                                            SDL_Texture * texture,
   520                                            const SDL_Rect * srcrect,
   521                                            const SDL_Rect * dstrect);
   522 
   523 /**
   524  *  \brief Read pixels from the current rendering target.
   525  *  
   526  *  \param rect   A pointer to the rectangle to read, or NULL for the entire 
   527  *                render target.
   528  *  \param format The desired format of the pixel data, or 0 to use the format
   529  *                of the rendering target
   530  *  \param pixels A pointer to be filled in with the pixel data
   531  *  \param pitch  The pitch of the pixels parameter.
   532  *  
   533  *  \return 0 on success, or -1 if pixel reading is not supported.
   534  *  
   535  *  \warning This is a very slow operation, and should not be used frequently.
   536  */
   537 extern DECLSPEC int SDLCALL SDL_RenderReadPixels(SDL_Renderer * renderer,
   538                                                  const SDL_Rect * rect,
   539                                                  Uint32 format,
   540                                                  void *pixels, int pitch);
   541 
   542 /**
   543  *  \brief Update the screen with rendering performed.
   544  */
   545 extern DECLSPEC void SDLCALL SDL_RenderPresent(SDL_Renderer * renderer);
   546 
   547 /**
   548  *  \brief Destroy the specified texture.
   549  *  
   550  *  \sa SDL_CreateTexture()
   551  *  \sa SDL_CreateTextureFromSurface()
   552  */
   553 extern DECLSPEC void SDLCALL SDL_DestroyTexture(SDL_Texture * texture);
   554 
   555 /**
   556  *  \brief Destroy the rendering context for a window and free associated
   557  *         textures.
   558  *  
   559  *  \sa SDL_CreateRenderer()
   560  */
   561 extern DECLSPEC void SDLCALL SDL_DestroyRenderer(SDL_Renderer * renderer);
   562 
   563 
   564 /* Ends C function definitions when using C++ */
   565 #ifdef __cplusplus
   566 /* *INDENT-OFF* */
   567 }
   568 /* *INDENT-ON* */
   569 #endif
   570 #include "close_code.h"
   571 
   572 #endif /* _SDL_render_h */
   573 
   574 /* vi: set ts=4 sw=4 expandtab: */