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:
Generally, when should I use surface and when should I use texture?
Thank you for your time and all help and suggestions are welcome :)
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.
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.
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.
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_Surface
s as source or destination, SDL_Texture
s 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:
SDL_Renderer
, and is always better to pre-load surfaces on textures.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With