The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
basic question on textures
catslap


Joined: 04 Apr 2014
Posts: 4
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.
basic question on textures
Jonas Kulla
Guest

2014-04-04 18:29 GMT+02:00 catslap:
Quote:
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.
basic question on textures
Jeffrey Carpenter
Guest

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


On 2014/04/ 04, at 11:29, catslap wrote:

Quote:
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

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
catslap


Joined: 04 Apr 2014
Posts: 4
Thank you!