SDL2 / Surface / Texture / Render

Viktor picture Viktor · Dec 26, 2013 · Viewed 10.1k times · Source

I'm trying to learn SDL2. The main difference (as I can see) between the old SDL and SDL2 is that old SDL had window represented by it's surface, all pictures were surfaces and all image operations and blits were surface to surface. In SDL2 we have surfaces and textures. If I got it right, surfaces are in RAM, and textures are in graphics memory. Is that right?

My goal is to make object-oriented wrapper for SDL2 because I had a similar thing for SDL. I want to have class window and class picture (has private texture and surface). Window will have it's contents represented by an instance of the picture class, and all blits will be picture to picture object blits. How to organize these picture operations:

  1. Pixel manipulation should be on surface level?
  2. If I want to copy part of one picture to another without rendering it, it should be on surface level?
  3. Should I blit surface to texture only when I want to render it on the screen?
  4. Is it better to render it all to one surface and then render it to the window texture or to render each picture to the window texture separately?

Generally, when should I use surface and when should I use texture?

Thank you for your time and all help and suggestions are welcome :)

Answer

TalesM picture TalesM · Jan 2, 2014

First I need to clarify some misconceptions: The texture based rendering does not work as the old surface rendering did. While you can use SDL_Surfaces as source or destination, SDL_Textures are meant to be used as source for rendering and the complimentary SDL_Renderer is used as destination. Generally you will have to choose between the old rendering framework that is done entirely on CPU and the new one that goes for GPU, but mixing is possible.

So for you questions:

  1. Textures do not provide direct access to pixels, so it is better to do on surfaces.
  2. Depends. It does not hurt to copy on textures if it is not very often and you want to render it accelerated later.
  3. When talking about textures you will always render to SDL_Renderer, and is always better to pre-load surfaces on textures.
  4. As I explained in first paragraph, there is no window texture. You can either use entirely surface based rendering or entirely texture based rendering. If you really need both (if you want to have direct pixel access and accelerated rendering) is better do as you said: blit everything to one surface and then upload to a texture.

Lastly you should use textures whenever you can. The surface use is a exception, use it when you either have to use intensive pixel manipulation or have to deal with legacy code.