【发布时间】:2015-12-23 05:42:55
【问题描述】:
这是 SDL_CreateTextureFromSurface 函数的语法:
SDL_Texture* SDL_CreateTextureFromSurface(SDL_Renderer* renderer, SDL_Surface* surface)
但是,我很困惑为什么我们需要传递渲染器*?我以为只有在绘制纹理时才需要渲染器*?
【问题讨论】:
这是 SDL_CreateTextureFromSurface 函数的语法:
SDL_Texture* SDL_CreateTextureFromSurface(SDL_Renderer* renderer, SDL_Surface* surface)
但是,我很困惑为什么我们需要传递渲染器*?我以为只有在绘制纹理时才需要渲染器*?
【问题讨论】:
您需要SDL_Renderer 来获取有关适用约束的信息:
可能还有更多……
【讨论】:
SDL_CreateTextureFromSurface的渲染器中使用吗?
除了plaes的回答..
在底层,SDL_CreateTextureFromSurface 调用 SDL_CreateTexture,它本身也需要一个 Renderer,以创建与传入的表面大小相同的新纹理。
然后在新创建的纹理上调用SDL_UpdateTexture 函数,以将像素数据从您传入的表面加载(复制)到SDL_CreateTextureFromSurface。如果传入的表面之间的格式与渲染器支持的格式不同,则会发生更多的逻辑来确保正确的行为。
SDL_CreateTexture 需要 Renderer 本身,因为它的 GPU 处理和存储纹理(大部分时间),并且 Renderer 应该是 GPU 的抽象。
表面永远不需要渲染器,因为它已加载到 RAM 中并由 CPU 处理。
如果您查看 SDL2 源代码中的 SDL_render.c,您可以了解有关这些调用如何工作的更多信息。
这是SDL_CreateTextureFromSurface中的一些代码:
texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
surface->w, surface->h);
if (!texture) {
return NULL;
}
if (format == surface->format->format) {
if (SDL_MUSTLOCK(surface)) {
SDL_LockSurface(surface);
SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
SDL_UnlockSurface(surface);
} else {
SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
}
}
【讨论】: