[Solved]Double Buffering in SDL

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
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

[Solved]Double Buffering in SDL

Post by XianForce »

Okay two quick questions about double buffering in SDL.

Firstly:

I just watched LusikkaMage's Game Development tutorial and she said she usually creates two surfaces; the buffer and the screen. So if I did that, it would just be done like:

Code: Select all

//Blit everything to the buffer here...
SDL_Flip(buffer);
SDL_BlitSurface(buffer, NULL, screen, &offsets); //obviously I'd declare/initialize the SDL_Rect offset =p
SDL_Flip(screen);
That's how I imagine it would be done, but I'm a noob =p.

My second question is whether to use this, or the Double Buffer flag when setting up the screen?

And can anyone offer some insight onto how the Double Buffer flag works exactly ?

Thanks for any help/input/advice. I tried Google but I didn't find anything relevant to what I was looking for.


EDIT: Now that I think about it, do I have to do both? Like create both the buffer and screen surfaces enabling the Double Buffer Flag, then use something along the lines of that bit of code I posted?

EDIT2: Actually now that I look at it, it seems the Double Buffer Flag is used when the screen is created as a hardware surface, and the first method I asked about, would be when it's created as a software surface?

If someone could post a small example it would be EXTREMELY helpful =)
Last edited by XianForce on Thu Jun 25, 2009 10:31 am, edited 2 times in total.
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Double Buffering in SDL

Post by Bakkon »

What you're doing in that code snippet is triple buffering. Just use the SDL_DOUBLEBUF flag when you're initializing your SDL_Surface* called screen. Then use SDL_Flip(screen) when you're done blitting everything to screen. The screen surface IS your buffer.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Double Buffering in SDL

Post by XianForce »

Bakkon wrote:What you're doing in that code snippet is triple buffering. Just use the SDL_DOUBLEBUF flag when you're initializing your SDL_Surface* called screen. Then use SDL_Flip(screen) when you're done blitting everything to screen. The screen surface IS your buffer.
but that flag only works when you create it in video memory... What about System Memory =p?
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Double Buffering in SDL

Post by Falco Girgis »

I have never heard of a screenbuffer not existing in VRAM. That's why it's a screenbuffer.

Assuming that you could, why would you want to? It would be slower, because I'm fairly certain that internally you would still have to have a screenbuffer in VRAM to copy it to.
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: Double Buffering in SDL

Post by RyanPridgeon »

Whereas you need to make your own dubble buffering system in Allegro, SDL handles the double-buffering for you, so just blit straight to your screen surface to use as a buffer, then call SDL_Flip() which will then draw the buffer onto the screen.

This is the beauty of SDL :)
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
Moosader
Game Developer
Game Developer
Posts: 1081
Joined: Wed May 07, 2008 12:29 am
Current Project: Find out at: http://www.youtube.com/coderrach
Favorite Gaming Platforms: PC, NES, SNES, PS2, PS1, DS, PSP, X360, WII
Programming Language of Choice: C++
Location: Kansas City
Contact:

Re: Double Buffering in SDL

Post by Moosader »

Yeah. LazyFoo uses "screen", but since I was used to using "buffer" in Allegro, I just renamed screen that.
Basically just draw everything to the buffer surface, then SDL_Flip( buffer );
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Double Buffering in SDL

Post by XianForce »

Oh... I guess I misunderstood that xD.

Thanks, guys =D.
Post Reply