SDL_RenderCopy

Use this function to copy a portion of the texture to the current rendering target.
使用该函数复制材质图像的一部分至当前的渲染目标。

Syntax 语法

  1. int SDL_RenderCopyEx(SDL_Renderer* renderer,
  2. SDL_Texture* texture,
  3. const SDL_Rect* srcrect,
  4. const SDL_Rect* dstrect)

Function Parameters 函数参数

rendererthe rendering context
渲染器
texturethe source texture; see Remarks for details
源材质,详细请看注释
srcrectthe source SDL_Rect structure or NULL for the entire texture
SDL_Rect 结构体指定的要复制的源材质的矩形区域或者 NULL 为整个材质图像。
dstrectthe destination SDL_Rect structure or NULL for the entire rendering target
SDL_Rect 结构体指定的目标矩形区域或者 NULL 为渲染器的整个区域。

Return Value 返回值

Returns 0 on success or a negative error code on failure; call SDL_GetError() for more information.
成功返回 0, 否则返回一个负数的错误码;调用 SDL_GetError() 获取更多信息。

Code Examples 代码示例

  1. #include "SDL.h"
  2. #define SHAPE_SIZE 16
  3. int main(int argc, char *argv[])
  4. {
  5. SDL_Window* Main_Window;
  6. SDL_Renderer* Main_Renderer;
  7. SDL_Surface* Loading_Surf;
  8. SDL_Texture* Background_Tx;
  9. SDL_Texture* BlueShapes;
  10. /* 指定要渲染的材质矩形区域和目标渲染区域 */
  11. SDL_Rect SrcR;
  12. SDL_Rect DestR;
  13. SrcR.x = 0;
  14. SrcR.y = 0;
  15. SrcR.w = SHAPE_SIZE;
  16. SrcR.h = SHAPE_SIZE;
  17. DestR.x = 640 / 2 - SHAPE_SIZE / 2;
  18. DestR.y = 580 / 2 - SHAPE_SIZE / 2;
  19. DestR.w = SHAPE_SIZE;
  20. DestR.h = SHAPE_SIZE;
  21. /* 渲染前创建一个窗口和渲染器 */
  22. Main_Window = SDL_CreateWindow("SDL_RenderCopy Example",
  23. SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 580, 0);
  24. Main_Renderer = SDL_CreateRenderer(Main_Window, -1, SDL_RENDERER_ACCELERATED);
  25. /* 加载一幅图像,为了使用硬件加速渲染,把得到的 Surface 格式图像转换成材质图像 */
  26. Loading_Surf = SDL_LoadBMP("Background.bmp");
  27. Background_Tx = SDL_CreateTextureFromSurface(Main_Renderer, Loading_Surf);
  28. SDL_FreeSurface(Loading_Surf); /* we got the texture now -> free surface */
  29. /* 加载其他图像 */
  30. Loading_Surf = SDL_LoadBMP("Blueshapes.bmp");
  31. BlueShapes = SDL_CreateTextureFromSurface(Main_Renderer, Loading_Surf);
  32. SDL_FreeSurface(Loading_Surf);
  33. /* 这里是重点。该操作在屏幕中间渲染一个旋转的蓝色图形选中区域 */
  34. int i;
  35. int n;
  36. for (i = 0; i < 2; ++i) {
  37. for(n = 0; n < 4; ++n) {
  38. SrcR.x = SHAPE_SIZE * (n % 2);
  39. if (n > 1) {
  40. SrcR.y = SHAPE_SIZE;
  41. } else {
  42. SrcR.y = 0;
  43. }
  44. /* 渲染背景,NULL 为使用默认的值 */
  45. SDL_RenderCopy(Main_Renderer, Background_Tx, NULL, NULL);
  46. /* 渲染这个图形的动画 */
  47. SDL_RenderCopy(Main_Renderer, BlueShapes, &SrcR, &DestR);
  48. SDL_RenderPresent(Main_Renderer);
  49. SDL_Delay(500);
  50. }
  51. }
  52. /* 渲染器工作起来跟一个画布很像:
  53. 当你执行 RenderCopy() 的时候你在往上面添加图像,每次绘制在最上方;
  54. 你可以指定新图像的混合模式;
  55. 渲染完成后使用 SDL_RenderPresent() 将画面显示到程序窗口。*/
  56. /* SDL 1.2 的提示:如果你习惯了 1.2 中的表面和 Blit 而对渲染器不是很明白的话,
  57. 你可以把它当成你的主表面,SDL_RenderCopy() 就相当于把材质图像 Blit 到主表面,
  58. SDL_RenderPresent() 就相当于以前的 SDL_Flip() 函数。*/
  59. SDL_DestroyTexture(BlueShapes);
  60. SDL_DestroyTexture(Background_Tx);
  61. SDL_DestroyRenderer(Main_Renderer);
  62. SDL_DestroyWindow(Main_Window);
  63. SDL_Quit();
  64. return 0;
  65. }

Remarks 注释

The texture is blended with the destination based on its blend mode set with SDL_SetTextureBlendMode().
渲染后的材质根据 SDL_SetTextureBlendMode() 的设置会带有混合效果。

The texture color is affected based on its color modulation set by SDL_SetTextureColorMod().
材质的颜色是受 SDL_SetTextureColorMod() 影响的。

The texture alpha is affected based on its alpha modulation set by SDL_SetTextureAlphaMod().
材质的透明度是受 SDL_SetTextureAlphaMod() 影响的。

Related Functions 相关函数

SDL_RenderCopyEx
SDL_SetTextureAlphaMod
SDL_SetTextureBlendMode
SDL_SetTextureColorMod


@lxfly2000