# Basic question on textures

**URL:** <https://discourse.libsdl.org/t/basic-question-on-textures/20579>\
**Category:** SDL Development\
**Created:** [April 4, 2014, 4:29pm UTC](https://discourse.libsdl.org/t/basic-question-on-textures/20579 "2014-04-04T16:29:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![catslap](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/c/a88e4f/32.png) [@catslap](https://discourse.libsdl.org/u/catslap)\
**Post date:** [April 4, 2014, 4:29pm UTC](https://discourse.libsdl.org/t/basic-question-on-textures/20579/1 "2014-04-04T16:29:51Z")

</div>

I’m new to SDL, and finding it a little hard finding examples and on SDL2 rather than the previous version, so thanks in advance for the help.

I’m trying to get my head around using the hardware acceleration and so trying to recode a game I have in Flash in SDL using C++, while using SDL 1.2 examples updating them to SDL2. I’m in a bit of a tangle.

It’s an overhead view RPG type thing, nothing new, but have a map of tiles and a camera view that shows part of it, centred over he player character.

I got it working by manipulating all the parts of surfaces in local memory then doing an SDL\_UpdateTexture() and SDL\_Present(). I want to be using textures I think, it’s very SDL 1 rather than SDL2 (if that makes sense)

Question 1)

I thought that by adding SDL\_RENDERER\_PRESENTVSYNC to the SDL\_CreateRenderer flags I’d be able to remove some code from my main loop (the call to UpdateTexture I was previously calling 65 times a second, or the call the RenderPresent()). I can’t - what does the flag actually do if I still have to update and present manually?

Question 2)

This one is more important! I’m trying to change it so that it’s working from textures, but is this more correct than building surfaces then passing a texture over? Currently I’m changing it so as it loads each graphic it converts it straight to a texture, then I’m trying to build the map from those as the camera moves around. Is the idea to get everything as textures then manipulate them in GPU memory? Have I made a silly mistake in understanding what needs to be done?

All I can find are simple examples to put an image on the screen, which use textures (but without any worry of performance as simply 4 tiles, not the few 10 - 1000 that I need) and lots of old SDL 1.2 code which prepares images as surfaces then ‘Flips’ (?) them to the screen.

Thanks, help much appreciated.

---

<div class="post-metadata">

**Author:** ![Jonas\_Kulla](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@Jonas\_Kulla](https://discourse.libsdl.org/u/Jonas_Kulla)\
**Post date:** [April 4, 2014, 10:39pm UTC](https://discourse.libsdl.org/t/basic-question-on-textures/20579/2 "2014-04-04T22:39:15Z")

</div>

2014-04-04 18:29 GMT+02:00 catslap \<sanchezlopez.scott at [gmail.com](http://gmail.com)\>:

> I’m new to SDL, and finding it a little hard finding examples and on  
> SDL2 rather than the previous version, so thanks in advance for the help.
> 
> I’m trying to get my head around using the hardware acceleration and so  
> trying to recode a game I have in Flash in SDL using C++, while using SDL  
> 1.2 examples updating them to SDL2. I’m in a bit of a tangle.
> 
> It’s an overhead view RPG type thing, nothing new, but have a map of tiles  
> and a camera view that shows part of it, centred over he player character.
> 
> I got it working by manipulating all the parts of surfaces in local memory  
> then doing an SDL\_UpdateTexture() and SDL\_Present(). I want to be using  
> textures I think, it’s very SDL 1 rather than SDL2 (if that makes sense)
> 
> Question 1)
> 
> I thought that by adding SDL\_RENDERER\_PRESENTVSYNC to the  
> SDL\_CreateRenderer flags I’d be able to remove some code from my main loop  
> (the call to UpdateTexture I was previously calling 65 times a second, or  
> the call the RenderPresent()). I can’t - what does the flag actually do if  
> I still have to update and present manually?
> 
> Question 2)
> 
> This one is more important! I’m trying to change it so that it’s working  
> from textures, but is this more correct than building surfaces then passing  
> a texture over? Currently I’m changing it so as it loads each graphic it  
> converts it straight to a texture, then I’m trying to build the map from  
> those as the camera moves around. Is the idea to get everything as textures  
> then manipulate them in GPU memory? Have I made a silly mistake in  
> understanding what needs to be done?
> 
> All I can find are simple examples to put an image on the screen, which  
> use textures (but without any worry of performance as simply 4 tiles, not  
> the few 10 - 1000 that I need) and lots of old SDL 1.2 code which prepares  
> images as surfaces then ‘Flips’ (?) them to the screen.
> 
> Thanks, help much appreciated.

Regarding 1)  
This is a hint to the graphics driver to limit and sync your redraw rate to  
your monitor’s  
refresh rate (in most cases 60Hz). RenderPresent will delay the execution  
of your  
program until the next monitor refresh (thus creating the frame limit  
without you having  
to do any manual timer shenanigans). You should _not_ change any part in  
your code  
when requesting vsync. Just keep on drawing and calling RenderPresent.  
Using vsync prevents screen tearing, and also stops your application from  
wasting  
cycles drawing stuff that your user won’t see anyway; on the other hand,  
your game  
logic might be very slightly delayed.  
Note that this is only a hint. The driver is always free to override it and  
force vsync on  
or off depending on other factors. Some platforms don’t support it at all.

---

<div class="post-metadata">

**Author:** ![Jeffrey\_Carpenter](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@Jeffrey\_Carpenter](https://discourse.libsdl.org/u/Jeffrey_Carpenter)\
**Post date:** [April 5, 2014, 5:13am UTC](https://discourse.libsdl.org/t/basic-question-on-textures/20579/3 "2014-04-05T05:13:58Z")

</div>

In regards to question two,

As I understand it, the general idea is basically best – in terms of efficiency – is to use surfaces for anytime you need pixel-level manipulation (think: post processing some effect, collision detection, rescaling and so on). So, if after loading the texture map, if all that you need to do is render to display, then yes, straight to texture is best. Otherwise, do your processing effects while it is still a surface, and then render as a texture – you can think of textures as a “ready to render” data type.

Surfaces are CPU bound, whereas textures are GPU bound. The biggest expense with textures tends to be the moment of uploading said texture to the GPU, so anytime you can minimize this, the better off you are (think: sprite sheets / atlas pages).

Lastly, do especially note on the SDL2 wiki docs page for SDL\_LockTexture under remarks – “As an optimization, the pixels made available for editing don’t necessarily contain the old texture data. This is a write-only operation, and if you need to keep a copy of the texture data you should do that at the application level.” AKA you should never count on the pixels read from a texture to necessarily be accurate (sorry, I don’t know the details as to why, when and what not).

As per the migration docs, “SDL\_TEXTUREACCESS\_STREAMING” type textures can be used for uploading fully-rendered frames onto the screen, which sounds like it may be the type you are interested in.

I hope that my detail helped somewhat, I’m still getting the hang of SDL2 and the concept of GPU textures, too.

Cheers,  
Jeffrey Carpenter  
\<@Jeffrey\_Carpenter\>On 2014/04/ 04, at 11:29, catslap \<sanchezlopez.scott at [gmail.com](http://gmail.com)\> wrote:

> I’m new to SDL, and finding it a little hard finding examples and on SDL2 rather than the previous version, so thanks in advance for the help.
> 
> I’m trying to get my head around using the hardware acceleration and so trying to recode a game I have in Flash in SDL using C++, while using SDL 1.2 examples updating them to SDL2. I’m in a bit of a tangle.
> 
> It’s an overhead view RPG type thing, nothing new, but have a map of tiles and a camera view that shows part of it, centred over he player character.
> 
> I got it working by manipulating all the parts of surfaces in local memory then doing an SDL\_UpdateTexture() and SDL\_Present(). I want to be using textures I think, it’s very SDL 1 rather than SDL2 (if that makes sense)
> 
> Question 1)
> 
> I thought that by adding SDL\_RENDERER\_PRESENTVSYNC to the SDL\_CreateRenderer flags I’d be able to remove some code from my main loop (the call to UpdateTexture I was previously calling 65 times a second, or the call the RenderPresent()). I can’t - what does the flag actually do if I still have to update and present manually?
> 
> Question 2)
> 
> This one is more important! I’m trying to change it so that it’s working from textures, but is this more correct than building surfaces then passing a texture over? Currently I’m changing it so as it loads each graphic it converts it straight to a texture, then I’m trying to build the map from those as the camera moves around. Is the idea to get everything as textures then manipulate them in GPU memory? Have I made a silly mistake in understanding what needs to be done?
> 
> All I can find are simple examples to put an image on the screen, which use textures (but without any worry of performance as simply 4 tiles, not the few 10 - 1000 that I need) and lots of old SDL 1.2 code which prepares images as surfaces then ‘Flips’ (?) them to the screen.
> 
> Thanks, help much appreciated.
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![catslap](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/c/a88e4f/32.png) [@catslap](https://discourse.libsdl.org/u/catslap)\
**Post date:** [April 5, 2014, 10:43am UTC](https://discourse.libsdl.org/t/basic-question-on-textures/20579/4 "2014-04-05T10:43:27Z")

</div>

Thank you!
