SDL2源代码分析6:复制到渲染器(SDL_RenderCopy())

SDL_RenderCopy()

函数简介

SDL使用SDL_RenderCopy()将纹理数据复制给渲染目标。SDL_RenderCopy()的原型如下。

int SDLCALL SDL_RenderCopy(SDL_Renderer * renderer,
                                           SDL_Texture * texture,
                                           const SDL_Rect * srcrect,
                                           const SDL_Rect * dstrect);

参数的含义如下。
renderer:渲染目标。
texture:输入纹理。
srcrect:选择输入纹理的一块矩形区域作为输入。设置为NULL的时候整个纹理作为输入。
dstrect:选择渲染目标的一块矩形区域作为输出。设置为NULL的时候整个渲染目标作为输出。

成功的话返回0,失败的话返回-1。

源代码分析

SDL_RenderCopy()的源代码位于render\SDL_render.c中,如下所示。

int
SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
               const SDL_Rect * srcrect, const SDL_Rect * dstrect)
{
    SDL_FRect dstfrect;
    SDL_FRect *pdstfrect = NULL;
    if (dstrect) {
        dstfrect.x = (float) dstrect->x;
        dstfrect.y = (float) dstrect->y;
        dstfrect.w = (float) dstrect->w;
        dstfrect.h = (float) dstrect->h;
        pdstfrect = &dstfrect;
    }
    return SDL_RenderCopyF(renderer, texture, srcrect, pdstfrect);
}

从源代码中可以看出,SDL_RenderCopy()返回SDL_RenderCopyF()。

SDL_RenderCopyF()定义如下:

int
SDL_RenderCopyF(SDL_Renderer * renderer, SDL_Texture * texture,
                const SDL_Rect * srcrect, const SDL_FRect * dstrect)
{
    SDL_Rect real_srcrect;
    SDL_FRect real_dstrect;
    int retval;

    CHECK_RENDERER_MAGIC(renderer, -1);
    CHECK_TEXTURE_MAGIC(texture, -1);

    if (renderer != texture->renderer) {
        return SDL_SetError("Texture was not created with this renderer");
    }

#if DONT_DRAW_WHILE_HIDDEN
    /* Don't draw while we're hidden */
    if (renderer->hidden) {
        return 0;
    }
#endif

    real_srcrect.x = 0;
    real_srcrect.y = 0;
    real_srcrect.w = texture->w;
    real_srcrect.h = texture->h;
    if (srcrect) {
        if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
            return 0;
        }
    }

    RenderGetViewportSize(renderer, &real_dstrect);
    if (dstrect) {
        if (!SDL_HasIntersectionF(dstrect, &real_dstrect)) {
            return 0;
        }
        real_dstrect = *dstrect;
    }

    if (texture->native) {
        texture = texture->native;
    }

    real_dstrect.x *= renderer->scale.x;
    real_dstrect.y *= renderer->scale.y;
    real_dstrect.w *= renderer->scale.x;
    real_dstrect.h *= renderer->scale.y;

    texture->last_command_generation = renderer->render_command_generation;

    retval = QueueCmdCopy(renderer, texture, &real_srcrect, &real_dstrect);
    return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
}

从源代码中可以看出,SDL_RenderCopyF()的大致流程如下。

  1. 检查输入参数的合理性。
  2. 调用QueueCmdCopy()方法复制纹理到渲染目标。这一步是整个函数的核心。

QueueCmdCopy()函数定义如下:

static int
QueueCmdCopy(SDL_Renderer *renderer, SDL_Texture * texture, const SDL_Rect * srcrect, const SDL_FRect * dstrect)
{
    SDL_RenderCommand *cmd = PrepQueueCmdDrawTexture(renderer, texture, SDL_RENDERCMD_COPY);
    int retval = -1;
    if (cmd != NULL) {
        retval = renderer->QueueCopy(renderer, cmd, texture, srcrect, dstrect);
        if (retval < 0) {
            cmd->command = SDL_RENDERCMD_NO_OP;
        }
    }
    return retval;
}

调用SDL_Render的RenderCopy ()方法复制纹理到渲染目标。这一步是整个函数的核心。

参考文献

1、https://blog.csdn.net/leixiaohua1020/article/details/40895593

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值