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