[SDL] Proper way to duplicate a SDL_Texture

lloyd_b lloydbaz at msn.com
Thu Oct 17 01:11:59 PDT 2013


FabioF wrote:
> Hello all,
> I've recently been porting my stuff to SDL2 and I still have doubts about Textures (but I will leave this to another post).
> 
> My question is: how do you "properly" copy a SDL_Texture?
> With Sourfaces you just blit one into another, clipping the way you want. With Textures (from what I have understood) you have to define your target Texture with SDL_TEXTUREACCESS_TARGET, then bind it to a new SDL_Renderer(which has to be bound to a window) and the you can user SDL_RenderCopy() to copy the contents. It seems to me an incredible amount of work.
> 
> For instance, in an "oldstyle" 2D game. Previously in 1.2 I had a struct form my animated sprites that had a Surface as sprite sheet and another as sprite "face". At every animation tick I would blit the current frame from the sheet into the face, modify it as needed (color mods, alpha mods, etc) and then redender it to the screen. Now I have to change my approach (or, I have to call SDL_CreateTextureFromSurface() every frame, which is bad).
> 
> What am I missing?
> 
> Thanks!


Why do you need to create a copy to apply the color/alpha mods to?  

1.  Load your spritesheet into a texture.
2.  Set alpha and color mods using the SetTextureAlphaMod() and SetTextureColorMod() functions.
3.  Do a RenderCopy()  to the display.
4.  Do the RenderPresent() to actually show it.

The mods will be applied during the render operation.  If some other tweakage is required, you should be able to do it via a second RenderCopy() (for instance, rendering a light fog over your sprite(s)) before you do the RenderPresent().

This way, all of the work will be done in the video card's memory, and by the GPU, rather than in main memory and by the CPU.  Unless you're using a seriously flaky video driver (*cough* open source ati driver *cough*) this will be *much* faster than doing things the old way...




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20131017/6710b5e4/attachment-0009.htm>


More information about the SDL mailing list