SDL2 - 为什么 SDL_CreateTextureFromSurface() 需要渲染器*?
SDL2 - Why does SDL_CreateTextureFromSurface() need a renderer*?
这是 SDL_CreateTextureFromSurface 函数的语法:
SDL_Texture* SDL_CreateTextureFromSurface(SDL_Renderer* renderer, SDL_Surface* surface)
但是,我很困惑为什么我们需要传递渲染器*?我以为我们只在绘制纹理时才需要渲染器*?
您需要 SDL_Renderer
来获取有关适用约束的信息:
- 支持的最大尺寸
- 像素格式
可能还有更多..
除了plaes的回答..
在幕后,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);
}
}
这是 SDL_CreateTextureFromSurface 函数的语法:
SDL_Texture* SDL_CreateTextureFromSurface(SDL_Renderer* renderer, SDL_Surface* surface)
但是,我很困惑为什么我们需要传递渲染器*?我以为我们只在绘制纹理时才需要渲染器*?
您需要 SDL_Renderer
来获取有关适用约束的信息:
- 支持的最大尺寸
- 像素格式
可能还有更多..
除了plaes的回答..
在幕后,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);
}
}