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