Page 1 of 1

[SDL2] Texture vs Surface ?

Posted: Sun Jan 26, 2014 5:37 am
by heisenberg
Hello everybody,

First question is: What is the difference between Texture and Surface in SDL v2 ?
Second question: Why the texture rendering is stretched ? :x
bool CTexture::OnDraw(SDL_Renderer* Renderer, SDL_Texture* Texture, int XDest, int YDest)
{
    if(Renderer == NULL || Texture == NULL)
    {
        cerr << "Can't render texture [SDL_Error: " << SDL_GetError() << "]" << endl;
        return false;
    }

    SDL_Rect DRect;

    DRect.x = XDest;
    DRect.y = YDest;
    
    // This function stretchs the texture ????!!!
    SDL_RenderCopy(Renderer, Texture, NULL, &DRect);

    return true;
}
PS: Please, explain me when do we use Surface and when do we use Texture in SDL v2.

Thank you in advance.

Re: [SDL2] Texture vs Surface ?

Posted: Sun Jan 26, 2014 1:44 pm
by D-e-X
Well, afaik.. Surfaces are stored in client-side memory, so they're actually just abstractions in that sense, whilst the textures are tied to a context which is the renderer and that means it's likely stored on the GPU or as close to video memory as possible (which means they can be hw accelerated). If that's the case, slow or otherwise heavy computations done can benefit greatly from this.

SDL_RenderCopy clips the destination rect against the viewport and then compensates for, or rather adjusts the source appropriately if I'm not mistaken. You would probably benefit from using SDL_RenderCopyEx instead.

EDIT: Missed the second question.

Re: [SDL2] Texture vs Surface ?

Posted: Mon Jan 27, 2014 1:55 am
by heisenberg
Please, can you explain it to me.
When do we use Surface ? And, when do we use Texture in SDL v2 ?

Re: [SDL2] Texture vs Surface ?

Posted: Wed Jan 29, 2014 9:15 am
by D-e-X
heisenberg wrote:Please, can you explain it to me.
When do we use Surface ? And, when do we use Texture in SDL v2 ?
I thought my initial response explained that sufficiently.

You only want to really use surfaces when there's *need* for processing pixels on the CPU, because why be doing things slower when there're clear ways of doing it more efficiently without added layers of complexity on there.

Re: [SDL2] Texture vs Surface ?

Posted: Wed Jan 29, 2014 12:42 pm
by dandymcgee
As D-e-X said, use Texture whenever possible to keep the data in VRAM. If you need to do software rendering or processing (sllooww), then use the Surface representation in RAM.

Re: [SDL2] Texture vs Surface ?

Posted: Wed Jan 29, 2014 12:57 pm
by heisenberg
Thank you guys for your answers. :)