Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDL2 / Surface / Texture / Render

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

like image 699
Viktor Avatar asked Dec 26 '13 21:12

Viktor


People also ask

What is texture in SDL2?

In SDL2 it is possible to render off-screen / render directly to a texture. The function to use is: int SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture); This only works if the renderer enables SDL_RENDERER_TARGETTEXTURE.

What is an SDL surface?

An SDL surface is just an image data type that contains the pixels of an image along with all data needed to render it. SDL surfaces use software rendering which means it uses the CPU to render. It is possible to render hardware images but it's a bit more difficult so we're going to learn it the easy way first.

What is SDL renderer?

SDL_Renderer is a struct that handles all rendering. It is tied to a SDL_Window so it can only render within that SDL_Window . It also keeps track the settings related to the rendering. There are several important functions tied to the SDL_Renderer.


1 Answers

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.

like image 141
TalesM Avatar answered Sep 23 '22 18:09

TalesM