[SDL2] Texture vs Surface ?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
heisenberg
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Sat May 04, 2013 3:53 pm
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++

[SDL2] Texture vs Surface ?

Post 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.
D-e-X
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Tue Dec 28, 2010 6:49 pm

Re: [SDL2] Texture vs Surface ?

Post 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.
I remember when I used to be into nostalgia.
User avatar
heisenberg
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Sat May 04, 2013 3:53 pm
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++

Re: [SDL2] Texture vs Surface ?

Post by heisenberg »

Please, can you explain it to me.
When do we use Surface ? And, when do we use Texture in SDL v2 ?
D-e-X
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Tue Dec 28, 2010 6:49 pm

Re: [SDL2] Texture vs Surface ?

Post 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.
I remember when I used to be into nostalgia.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: [SDL2] Texture vs Surface ?

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
heisenberg
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Sat May 04, 2013 3:53 pm
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++

Re: [SDL2] Texture vs Surface ?

Post by heisenberg »

Thank you guys for your answers. :)
Post Reply