Convert rectangle portion of a surface to a texture

SDL_CreateTextureFromSurface() can be used to convert an entire surface to a texture, but is it possible to convert a rectangle portion of the surface without first creating a copy of the surface pixels?
At worst, I could hand code a routine to copy the pixels to a static texture, but I was wondering if there was a way to do this with the SDL2 API.

As a related question, it is preferable to use a single large (128x2048) sprite sheet texture or many (16x16 or so) textures for “copying” to an SDL_Renderer?

Vortico wrote:

SDL_CreateTextureFromSurface() can be used to convert an entire surface to a texture, but is it possible to convert a rectangle portion of the surface without first creating a copy of the surface pixels?
At worst, I could hand code a routine to copy the pixels to a static texture, but I was wondering if there was a way to do this with the SDL2 API.

As a related question, it is preferable to use a single large (128x2048) sprite sheet texture or many (16x16 or so) textures for “copying” to an SDL_Renderer?

For the first question, you’d need to use SDL_UpdateTexture() to do the work. But I have no clue as to what gymnastics you’d have to go through to get the required pointer to the pixel data for just a portion of the surface. Take a look at the source for SDL_CreateTextureFromSurface() for a start on it - it basicallly just creates a new texture, and then uses SDL_UpdateTexture() to copy the pixel data from the surface into it (after some work to make sure the pixel formats are the same).

As to the second, if your hardware can handle a single texture that size, then it’s better performance-wise to use it instead of a bunch of small textures. There is a price to be paid when switching contexts between different source textures, though depending on what you’re doing it may not be all that great of a price. You will need to verify that the hardware can handle a texture that size - use SDL_GetRendererInfo() to see what the limits are.

Thanks. I suspected I would implement it similarly to SDL_CreateTextureFromSurface() if it wasn’t implemented in some other function. Currently I’m creating a temporary surface, blitting the portion of the source surface to it, and converting it to a texture. The CPU usage is doing fine, but I will write a proper “SDL_CreateTextureFromSurfaceRect()” function to replace that nasty hack.

Maybe name it SDL_CreateTextureFromSurfaceEx()?On Sun, Dec 29, 2013 at 2:53 AM, Vortico wrote:

Thanks. I suspected I would implement it similarly to
SDL_CreateTextureFromSurface() if it wasn’t implemented in some other
function. Currently I’m creating a temporary surface, blitting the portion
of the source surface to it, and converting it to a texture. The CPU usage
is doing fine, but I will write a proper
"SDL_CreateTextureFromSurfaceRect()" function to replace that nasty hack.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

You can use SDL_CreateRGBSurfaceFrom(), passing it the correct offset into
the pixels and the pitch of the original surface and then use
SDL_CreateTextureFromSurface(). It’s very efficient, just a little
cumbersome to write.On Sat, Dec 28, 2013 at 4:53 PM, Vortico wrote:

Thanks. I suspected I would implement it similarly to
SDL_CreateTextureFromSurface() if it wasn’t implemented in some other
function. Currently I’m creating a temporary surface, blitting the portion
of the source surface to it, and converting it to a texture. The CPU usage
is doing fine, but I will write a proper
"SDL_CreateTextureFromSurfaceRect()" function to replace that nasty hack.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org