LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Double Buffering (https://www.linuxquestions.org/questions/programming-9/double-buffering-704252/)

Ace Blackwell 02-12-2009 02:46 PM

Double Buffering
 
Hey all,

I'm playing around with Allegro and I'm testing the double buffering. I have written a few simple Simon Says type games in Quick C years ago. I didn't use double buffering though I did use various graphics during the game.

I'm a novice by far , but just from what I'm seeing, can anyone explain the benefits to double buffering? I mean it seems your still going to have to draw or aquire a file, whether memory or screen. So what is the benefit to the extra step of blitting the memory to screen?

The only thing I can think of would be to draw numerous sprites to a memory bitmap and then transfer only the onces you want to the screen as you need them or using them repeatedly without redrawing.

Anyways, just trying to learn

Thanks in advance for your time.

Ace

ErV 02-12-2009 02:55 PM

Quote:

Originally Posted by Ace Blackwell (Post 3441418)
I'm a novice by far , but just from what I'm seeing, can anyone explain the benefits to double buffering? I mean it seems your still going to have to draw or aquire a file, whether memory or screen. So what is the benefit to the extra step of blitting the memory to screen?

If you'll draw on screen directly, without second buffer, everything that moves will flicker terribly. Image will get displayed (on monitor) when you haven't drawn anything (just cleared screen), rendered only half of the picture, etc. and each time different portion of scene will be displayed. The only way to avoid this is to use double-buffering.

Another thing is that second buffer doesn't necessarily blit onto screen. There might be actually two buffers being swapped every frame, which will be faster than blitting. One of them will serve as screen (i.e. "primary surface"), while another will serve as backbuffer. Not sure if allegro works this way, though, but such option was widely used in DirectDraw, so if Allegro is hardware-accelerated, it should have similar functionality.

Quote:

Originally Posted by Ace Blackwell (Post 3441418)
The only thing I can think of would be to draw numerous sprites to a memory bitmap and then transfer only the onces you want to the screen as you need them or using them repeatedly without redrawing.

This won't be possible if screen has serious amount of movement (entire screen scrolls, etc.)
Anyway, everything that changes (animated/moving) should appear on screen at once, otherwise it might flicker and some spirtes might even disappear (if you accidentally "synchronize" redrawing with display refresh rate). That's why double-buffering is needed.

Ace Blackwell 02-13-2009 08:06 AM

Thanks, This is a bit over my head at this point, but it makes sense and should help me to catch on a little quicker.

Along the Double Buffering lines. I seen where I can download templates for sprites (humanoid) and alter them in a paint shop editor. My question is how do you use them in program. Do you pull the template into a memory screen and them pull off what you need as you need them or is there another way to get them in a program?

Thanks
Ace

ErV 02-13-2009 09:19 AM

Quote:

Originally Posted by Ace Blackwell (Post 3442275)
Thanks, This is a bit over my head at this point, but it makes sense and should help me to catch on a little quicker.

Along the Double Buffering lines. I seen where I can download templates for sprites (humanoid) and alter them in a paint shop editor. My question is how do you use them in program. Do you pull the template into a memory screen and them pull off what you need as you need them or is there another way to get them in a program?

Thanks
Ace

Look, I don't work with Allegro (but flickering and reasons to use double-buffering are common across all apis/libraries - it even applies to complicated gui widgets), I prefer SDl+OpenGL(even for 2D) and I have experience with DirectDraw, so I can't give detail on how to do it in allegro.

In general, to draw sprite on screen, you need to load it onto surface(in 2D) or texture(in 3D), blit surface onto backbuffer (or draw textured polygon onto backbuffer), then swap(or "flip") buffers or blit backbuffer onto screen. I'd recommend to check allegro documentation.

In SDL you would use SDL_image library (supports most common formats) to load pictures onto surfaces, surface blitting functions(SDL_BlitSurface) for blits (or OpenGL functions to draw polygons) and SDL_Flip to swap buffers(or SDL_GL_SwapBuffers if used with OpenGL).


All times are GMT -5. The time now is 11:15 AM.