SDL_UpdateTexture()
函数简介
SDL使用SDL_UpdateTexture()设置纹理的像素数据。SDL_UpdateTexture()的原型如下。
int SDLCALL SDL_UpdateTexture(SDL_Texture * texture,
const SDL_Rect * rect,
const void *pixels, int pitch);
参数的含义如下。
texture:目标纹理。
rect:更新像素的矩形区域。设置为NULL的时候更新整个区域。
pixels:像素数据。
pitch:一行像素数据的字节数。
成功的话返回0,失败的话返回-1。
源代码分析
SDL_UpdateTexture()的源代码位于render\SDL_render.c中。如下所示。
int
SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
const void *pixels, int pitch)
{
SDL_Rect real_rect;
CHECK_TEXTURE_MAGIC(texture, -1);
if (!pixels) {
return SDL_InvalidParamError("pixels");
}
if (!pitch) {
return SDL_InvalidParamError("pitch");
}
real_rect.x = 0;
real_rect.y = 0;
real_rect.w = texture->w;
real_rect.h = texture->h;
if (rect) {
if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
return 0;
}
}
if (real_rect.w == 0 || real_rect.h == 0) {
return 0; /* nothing to do. */
#if SDL_HAVE_YUV
} else if (texture->yuv) {
return SDL_UpdateTextureYUV(texture, &real_rect, pixels, pitch);
#endif
} else if (texture->native) {
return SDL_UpdateTextureNative(texture, &real_rect, pixels, pitch);
} else {
SDL_Renderer *renderer = texture->renderer;
if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
return -1;
}
return renderer->UpdateTexture(renderer, texture, &real_rect, pixels, pitch);
}
}
从源代码中可以看出,SDL_UpdateTexture()的大致流程如下。
- 检查输入参数的合理性。例如像素格式是否支持,宽和高是否小于等于0等等。
- 如果是一些特殊的格式,进行一定的处理:
a) 如果输入的像素数据是YUV格式的,则会调用SDL_UpdateTextureYUV()进行处理。
b) 如果输入的像素数据的像素格式不是渲染器支持的格式,则会调用SDL_UpdateTextureNative()进行处理。 - 调用SDL_Render的UpdateTexture()方法更新纹理。这一步是整个函数的核心。
下面我们详细看一下几种不同的渲染器的UpdateTexture ()的方法。
参考文献
1、https://blog.csdn.net/leixiaohua1020/article/details/40876089