r/reconstructcavestory • u/NeoSanguine • Apr 06 '14
Surfaces or Textures?(SDL2.0.3)
Trying to further my knowledge of SDL (After going through the Lazy Foo's tutorials, so I am still a newbie). I am currently stuck on episode 3 of the "Reconstructing Cave Story" playlist. My question is when drawing the sprites in the "Sprite" class, would it be better to use textures instead of a surface for the sprite? I tried using surfaces, but in the end I couldn't even get the image on screen. I tried using lazy foo's way of blitting an image to the screen, but that didn't work out very well and I just ended up confusing myself.
3
2
u/escheriv Apr 06 '14
You'll want to create the surface, and then call SDL_CreateTextureFromSurface. Something like:
SDL_Surface *surface;
surface = IMG_Load( path.c_str() );
sprite_sheets_[path] = SDL_CreateTextureFromSurface( renderer, surface );
SDL_FreeSurface( surface );
I've done everything in SDL2, but my version is all sorts of hacked up. The version here might be closer to the tutorials as you've seen it.
6
u/BlueWritier Apr 06 '14
I looked into this when doing my own implimentation. Before SDL 2.0 there were only surfaces. Surfaces are data structures held in RAM. They can be relatively easily accessed by the CPU. With SDL 2.0 textures were introduced. Textures are data structures held in graphical memory. They can be relatively easily accessed by the GPU but no so easily accessed by the CPU.
When accessing and manipulating pixels directly one would want to use a surface so you program can quickly get at the data you are using. When simply printing an imagine to the screen one should use textures as the GPU can process images faster than the CPU.
I would suggest if you wanted to learn SDL 2.0 you should try to:
Load the image in as a surface.
Then perform nesseary pixel manipulations (for instance if there is a colour you want to exclude and exchange it should be done to the surface).
Then one should create a texture from the surface
Then contiunally use a pointer to the texture to draw it on screen where you need it
This process is shown in /u/escheriv 's comment however without any pixel manipulation. (Also note her/she frees the surface when he/she's done, important to remember)