include/SDL_render.h
author Sam Lantinga <slouken@libsdl.org>
Thu, 03 Feb 2011 00:54:29 -0800
changeset 5158 b3ccd1947786
parent 5157 657543cc92f9
child 5164 97423d858a1a
permissions -rw-r--r--
Simplified and improved the process of creating a texture from a surface.
     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 surface The surface containing pixel data used to fill the texture.
   176  *  
   177  *  \return The created texture is returned, or 0 on error.
   178  *  
   179  *  \note The surface is not modified or freed by this function.
   180  *  
   181  *  \sa SDL_QueryTexture()
   182  *  \sa SDL_DestroyTexture()
   183  */
   184 extern DECLSPEC SDL_Texture * SDLCALL SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface);
   185 
   186 /**
   187  *  \brief Query the attributes of a texture
   188  *  
   189  *  \param texture A texture to be queried.
   190  *  \param format  A pointer filled in with the raw format of the texture.  The 
   191  *                 actual format may differ, but pixel transfers will use this 
   192  *                 format.
   193  *  \param access  A pointer filled in with the actual access to the texture.
   194  *  \param w       A pointer filled in with the width of the texture in pixels.
   195  *  \param h       A pointer filled in with the height of the texture in pixels.
   196  *  
   197  *  \return 0 on success, or -1 if the texture is not valid.
   198  */
   199 extern DECLSPEC int SDLCALL SDL_QueryTexture(SDL_Texture * texture,
   200                                              Uint32 * format, int *access,
   201                                              int *w, int *h);
   202 
   203 /**
   204  *  \brief Set an additional color value used in render copy operations.
   205  *  
   206  *  \param texture The texture to update.
   207  *  \param r       The red color value multiplied into copy operations.
   208  *  \param g       The green color value multiplied into copy operations.
   209  *  \param b       The blue color value multiplied into copy operations.
   210  *  
   211  *  \return 0 on success, or -1 if the texture is not valid or color modulation 
   212  *          is not supported.
   213  *  
   214  *  \sa SDL_GetTextureColorMod()
   215  */
   216 extern DECLSPEC int SDLCALL SDL_SetTextureColorMod(SDL_Texture * texture,
   217                                                    Uint8 r, Uint8 g, Uint8 b);
   218 
   219 
   220 /**
   221  *  \brief Get the additional color value used in render copy operations.
   222  *  
   223  *  \param texture The texture to query.
   224  *  \param r         A pointer filled in with the current red color value.
   225  *  \param g         A pointer filled in with the current green color value.
   226  *  \param b         A pointer filled in with the current blue color value.
   227  *  
   228  *  \return 0 on success, or -1 if the texture is not valid.
   229  *  
   230  *  \sa SDL_SetTextureColorMod()
   231  */
   232 extern DECLSPEC int SDLCALL SDL_GetTextureColorMod(SDL_Texture * texture,
   233                                                    Uint8 * r, Uint8 * g,
   234                                                    Uint8 * b);
   235 
   236 /**
   237  *  \brief Set an additional alpha value used in render copy operations.
   238  *  
   239  *  \param texture The texture to update.
   240  *  \param alpha     The alpha value multiplied into copy operations.
   241  *  
   242  *  \return 0 on success, or -1 if the texture is not valid or alpha modulation 
   243  *          is not supported.
   244  *  
   245  *  \sa SDL_GetTextureAlphaMod()
   246  */
   247 extern DECLSPEC int SDLCALL SDL_SetTextureAlphaMod(SDL_Texture * texture,
   248                                                    Uint8 alpha);
   249 
   250 /**
   251  *  \brief Get the additional alpha value used in render copy operations.
   252  *  
   253  *  \param texture The texture to query.
   254  *  \param alpha     A pointer filled in with the current alpha value.
   255  *  
   256  *  \return 0 on success, or -1 if the texture is not valid.
   257  *  
   258  *  \sa SDL_SetTextureAlphaMod()
   259  */
   260 extern DECLSPEC int SDLCALL SDL_GetTextureAlphaMod(SDL_Texture * texture,
   261                                                    Uint8 * alpha);
   262 
   263 /**
   264  *  \brief Set the blend mode used for texture copy operations.
   265  *  
   266  *  \param texture The texture to update.
   267  *  \param blendMode ::SDL_BlendMode to use for texture blending.
   268  *  
   269  *  \return 0 on success, or -1 if the texture is not valid or the blend mode is
   270  *          not supported.
   271  *  
   272  *  \note If the blend mode is not supported, the closest supported mode is
   273  *        chosen.
   274  *  
   275  *  \sa SDL_GetTextureBlendMode()
   276  */
   277 extern DECLSPEC int SDLCALL SDL_SetTextureBlendMode(SDL_Texture * texture,
   278                                                     SDL_BlendMode blendMode);
   279 
   280 /**
   281  *  \brief Get the blend mode used for texture copy operations.
   282  *  
   283  *  \param texture   The texture to query.
   284  *  \param blendMode A pointer filled in with the current blend mode.
   285  *  
   286  *  \return 0 on success, or -1 if the texture is not valid.
   287  *  
   288  *  \sa SDL_SetTextureBlendMode()
   289  */
   290 extern DECLSPEC int SDLCALL SDL_GetTextureBlendMode(SDL_Texture * texture,
   291                                                     SDL_BlendMode *blendMode);
   292 
   293 /**
   294  *  \brief Update the given texture rectangle with new pixel data.
   295  *  
   296  *  \param texture   The texture to update
   297  *  \param rect      A pointer to the rectangle of pixels to update, or NULL to 
   298  *                   update the entire texture.
   299  *  \param pixels    The raw pixel data.
   300  *  \param pitch     The number of bytes between rows of pixel data.
   301  *  
   302  *  \return 0 on success, or -1 if the texture is not valid.
   303  *  
   304  *  \note This is a fairly slow function.
   305  */
   306 extern DECLSPEC int SDLCALL SDL_UpdateTexture(SDL_Texture * texture,
   307                                               const SDL_Rect * rect,
   308                                               const void *pixels, int pitch);
   309 
   310 /**
   311  *  \brief Lock a portion of the texture for pixel access.
   312  *  
   313  *  \param texture   The texture to lock for access, which was created with 
   314  *                   ::SDL_TEXTUREACCESS_STREAMING.
   315  *  \param rect      A pointer to the rectangle to lock for access. If the rect 
   316  *                   is NULL, the entire texture will be locked.
   317  *  \param pixels    This is filled in with a pointer to the locked pixels, 
   318  *                   appropriately offset by the locked area.
   319  *  \param pitch     This is filled in with the pitch of the locked pixels.
   320  *  
   321  *  \return 0 on success, or -1 if the texture is not valid or was not created with ::SDL_TEXTUREACCESS_STREAMING.
   322  *  
   323  *  \sa SDL_UnlockTexture()
   324  */
   325 extern DECLSPEC int SDLCALL SDL_LockTexture(SDL_Texture * texture,
   326                                             const SDL_Rect * rect,
   327                                             void **pixels, int *pitch);
   328 
   329 /**
   330  *  \brief Unlock a texture, uploading the changes to video memory, if needed.
   331  *  
   332  *  \sa SDL_LockTexture()
   333  */
   334 extern DECLSPEC void SDLCALL SDL_UnlockTexture(SDL_Texture * texture);
   335 
   336 /**
   337  *  \brief Set the color used for drawing operations (Fill and Line).
   338  *  
   339  *  \param r The red value used to draw on the rendering target.
   340  *  \param g The green value used to draw on the rendering target.
   341  *  \param b The blue value used to draw on the rendering target.
   342  *  \param a The alpha value used to draw on the rendering target, usually 
   343  *           ::SDL_ALPHA_OPAQUE (255).
   344  *  
   345  *  \return 0 on success, or -1 on error
   346  */
   347 extern DECLSPEC int SDL_SetRenderDrawColor(SDL_Renderer * renderer,
   348                                            Uint8 r, Uint8 g, Uint8 b,
   349                                            Uint8 a);
   350 
   351 /**
   352  *  \brief Get the color used for drawing operations (Fill and Line).
   353  *  
   354  *  \param r A pointer to the red value used to draw on the rendering target.
   355  *  \param g A pointer to the green value used to draw on the rendering target.
   356  *  \param b A pointer to the blue value used to draw on the rendering target.
   357  *  \param a A pointer to the alpha value used to draw on the rendering target, 
   358  *           usually ::SDL_ALPHA_OPAQUE (255).
   359  *  
   360  *  \return 0 on success, or -1 on error
   361  */
   362 extern DECLSPEC int SDL_GetRenderDrawColor(SDL_Renderer * renderer,
   363                                            Uint8 * r, Uint8 * g, Uint8 * b,
   364                                            Uint8 * a);
   365 
   366 /**
   367  *  \brief Set the blend mode used for drawing operations (Fill and Line).
   368  *  
   369  *  \param blendMode ::SDL_BlendMode to use for blending.
   370  *  
   371  *  \return 0 on success, or -1 on error
   372  *  
   373  *  \note If the blend mode is not supported, the closest supported mode is 
   374  *        chosen.
   375  *  
   376  *  \sa SDL_GetRenderDrawBlendMode()
   377  */
   378 extern DECLSPEC int SDLCALL SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer,
   379                                                        SDL_BlendMode blendMode);
   380 
   381 /**
   382  *  \brief Get the blend mode used for drawing operations.
   383  *  
   384  *  \param blendMode A pointer filled in with the current blend mode.
   385  *  
   386  *  \return 0 on success, or -1 on error
   387  *  
   388  *  \sa SDL_SetRenderDrawBlendMode()
   389  */
   390 extern DECLSPEC int SDLCALL SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer,
   391                                                        SDL_BlendMode *blendMode);
   392 
   393 /**
   394  *  \brief Clear the current rendering target with the drawing color
   395  */
   396 extern DECLSPEC int SDLCALL SDL_RenderClear(SDL_Renderer * renderer);
   397 
   398 /**
   399  *  \brief Draw a point on the current rendering target.
   400  *  
   401  *  \param x The x coordinate of the point.
   402  *  \param y The y coordinate of the point.
   403  *  
   404  *  \return 0 on success, or -1 on error
   405  */
   406 extern DECLSPEC int SDLCALL SDL_RenderDrawPoint(SDL_Renderer * renderer,
   407                                                 int x, int y);
   408 
   409 /**
   410  *  \brief Draw multiple points on the current rendering target.
   411  *  
   412  *  \param points The points to draw
   413  *  \param count The number of points to draw
   414  *  
   415  *  \return 0 on success, or -1 on error
   416  */
   417 extern DECLSPEC int SDLCALL SDL_RenderDrawPoints(SDL_Renderer * renderer,
   418                                                  const SDL_Point * points,
   419                                                  int count);
   420 
   421 /**
   422  *  \brief Draw a line on the current rendering target.
   423  *  
   424  *  \param x1 The x coordinate of the start point.
   425  *  \param y1 The y coordinate of the start point.
   426  *  \param x2 The x coordinate of the end point.
   427  *  \param y2 The y coordinate of the end point.
   428  *  
   429  *  \return 0 on success, or -1 on error
   430  */
   431 extern DECLSPEC int SDLCALL SDL_RenderDrawLine(SDL_Renderer * renderer,
   432                                                int x1, int y1, int x2, int y2);
   433 
   434 /**
   435  *  \brief Draw a series of connected lines on the current rendering target.
   436  *  
   437  *  \param points The points along the lines
   438  *  \param count The number of points, drawing count-1 lines
   439  *  
   440  *  \return 0 on success, or -1 on error
   441  */
   442 extern DECLSPEC int SDLCALL SDL_RenderDrawLines(SDL_Renderer * renderer,
   443                                                 const SDL_Point * points,
   444                                                 int count);
   445 
   446 /**
   447  *  \brief Draw a rectangle on the current rendering target.
   448  *  
   449  *  \param rect A pointer to the destination rectangle, or NULL to outline the entire rendering target.
   450  *  
   451  *  \return 0 on success, or -1 on error
   452  */
   453 extern DECLSPEC int SDLCALL SDL_RenderDrawRect(SDL_Renderer * renderer,
   454                                                const SDL_Rect * rect);
   455 
   456 /**
   457  *  \brief Draw some number of rectangles on the current rendering target.
   458  *  
   459  *  \param rects A pointer to an array of destination rectangles.
   460  *  \param count The number of rectangles.
   461  *  
   462  *  \return 0 on success, or -1 on error
   463  */
   464 extern DECLSPEC int SDLCALL SDL_RenderDrawRects(SDL_Renderer * renderer,
   465                                                 const SDL_Rect ** rects,
   466                                                 int count);
   467 
   468 /**
   469  *  \brief Fill a rectangle on the current rendering target with the drawing color.
   470  *  
   471  *  \param rect A pointer to the destination rectangle, or NULL for the entire 
   472  *              rendering target.
   473  *  
   474  *  \return 0 on success, or -1 on error
   475  */
   476 extern DECLSPEC int SDLCALL SDL_RenderFillRect(SDL_Renderer * renderer,
   477                                                const SDL_Rect * rect);
   478 
   479 /**
   480  *  \brief Fill some number of rectangles on the current rendering target with the drawing color.
   481  *  
   482  *  \param rects A pointer to an array of destination rectangles.
   483  *  \param count The number of rectangles.
   484  *  
   485  *  \return 0 on success, or -1 on error
   486  */
   487 extern DECLSPEC int SDLCALL SDL_RenderFillRects(SDL_Renderer * renderer,
   488                                                 const SDL_Rect ** rect,
   489                                                 int count);
   490 
   491 /**
   492  *  \brief Copy a portion of the texture to the current rendering target.
   493  *  
   494  *  \param texture The source texture.
   495  *  \param srcrect   A pointer to the source rectangle, or NULL for the entire 
   496  *                   texture.
   497  *  \param dstrect   A pointer to the destination rectangle, or NULL for the 
   498  *                   entire rendering target.
   499  *  
   500  *  \return 0 on success, or -1 on error
   501  */
   502 extern DECLSPEC int SDLCALL SDL_RenderCopy(SDL_Renderer * renderer,
   503                                            SDL_Texture * texture,
   504                                            const SDL_Rect * srcrect,
   505                                            const SDL_Rect * dstrect);
   506 
   507 /**
   508  *  \brief Read pixels from the current rendering target.
   509  *  
   510  *  \param rect   A pointer to the rectangle to read, or NULL for the entire 
   511  *                render target.
   512  *  \param format The desired format of the pixel data, or 0 to use the format
   513  *                of the rendering target
   514  *  \param pixels A pointer to be filled in with the pixel data
   515  *  \param pitch  The pitch of the pixels parameter.
   516  *  
   517  *  \return 0 on success, or -1 if pixel reading is not supported.
   518  *  
   519  *  \warning This is a very slow operation, and should not be used frequently.
   520  */
   521 extern DECLSPEC int SDLCALL SDL_RenderReadPixels(SDL_Renderer * renderer,
   522                                                  const SDL_Rect * rect,
   523                                                  Uint32 format,
   524                                                  void *pixels, int pitch);
   525 
   526 /**
   527  *  \brief Update the screen with rendering performed.
   528  */
   529 extern DECLSPEC void SDLCALL SDL_RenderPresent(SDL_Renderer * renderer);
   530 
   531 /**
   532  *  \brief Destroy the specified texture.
   533  *  
   534  *  \sa SDL_CreateTexture()
   535  *  \sa SDL_CreateTextureFromSurface()
   536  */
   537 extern DECLSPEC void SDLCALL SDL_DestroyTexture(SDL_Texture * texture);
   538 
   539 /**
   540  *  \brief Destroy the rendering context for a window and free associated
   541  *         textures.
   542  *  
   543  *  \sa SDL_CreateRenderer()
   544  */
   545 extern DECLSPEC void SDLCALL SDL_DestroyRenderer(SDL_Renderer * renderer);
   546 
   547 
   548 /* Ends C function definitions when using C++ */
   549 #ifdef __cplusplus
   550 /* *INDENT-OFF* */
   551 }
   552 /* *INDENT-ON* */
   553 #endif
   554 #include "close_code.h"
   555 
   556 #endif /* _SDL_render_h */
   557 
   558 /* vi: set ts=4 sw=4 expandtab: */