这是SDL_CreateTextureFromSurface函数的语法:
SDL_Texture* SDL_CreateTextureFromSurface(SDL_Renderer* renderer, SDL_Surface* surface)
Run Code Online (Sandbox Code Playgroud)
但是,我很困惑为什么我们需要传递一个渲染器*?我认为只有在绘制纹理时才需要渲染器*?
小智 5
除了plas的回答..
在底层,SDL_CreateTextureFromSurface调用SDL_CreateTexture,它本身也需要一个渲染器,来创建一个与传入表面大小相同的新纹理。
然后,在新创建的纹理上调用该SDL_UpdateTexture函数,以从您传入的表面加载(复制)像素数据SDL_CreateTextureFromSurface。如果传入表面之间的格式与渲染器支持的格式不同,则会发生更多逻辑来确保正确的行为。
渲染器本身是必需的,SDL_CreateTexture因为 GPU 负责处理和存储纹理(大多数时候),并且渲染器应该是 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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2338 次 |
| 最近记录: |