这是SDL_CreateTextureFromSurface函数的语法:
SDL_Texture* SDL_CreateTextureFromSurface(SDL_Renderer* renderer, SDL_Surface* surface)
然而,我不明白为什么我们需要传递renderer*?我认为只有在绘制纹理时才需要renderer*吗?
SDL_Renderer
来获取有关适用约束的信息:
可能还有其他东西..
SDL_CreateTextureFromSurface
在底层调用SDL_CreateTexture
,而SDL_CreateTexture
本身也需要一个渲染器来创建一个与传入表面相同大小的新纹理。然后,在新创建的纹理上调用SDL_UpdateTexture
函数,将从传入到SDL_CreateTextureFromSurface
的表面中复制像素数据。如果传入表面的格式与渲染器支持的格式不同,则会发生更多逻辑以确保正确的行为。需要Renderer本身来进行SDL_CreateTexture
,因为GPU处理和存储纹理(大部分时间),而Renderer应该是GPU的抽象层。表面永远不需要Renderer,因为它在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);
}
}
SDL_CreateTextureFromSurface
的渲染器中使用吗? - HolyBlackCat