Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » double buffer, triple buffer and z buffer stuff

Credits go to Evert, HoHo, jamal, and Tobias Dammers for helping out!
This thread is locked; no one can reply to it. rss feed Print
double buffer, triple buffer and z buffer stuff
lucaz
Member #4,194
January 2004

I always use the double buffer method.
What are the other methods?, when is ok to use one or another?

HoHo
Member #4,534
April 2004
avatar

zbuffer is only useful in 3d, triplebuffer might make some things run a bit faster because program dont need to wait for vsync. I thik doublebuffering is good enoughf for everything that can't be done easily with dirty rectangles

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

lucaz
Member #4,194
January 2004

what are those dirty rectangles?.

By the way, I'm not sure if I'm using double buffer :-/. This is my code:

1#include <allegro.h>
2int main {
3 allegro_init();
4 install_keyboard();
5 set_color_depth(16);
6 set_gfx_mode(GFX_AUTODETECT,640,400,0,0);
7 BITMAP* buffer = create_bitmap(640,400);
8 
9 while(!key[KEY_ESC]) {
10 circlefill(buffer,rand()%640,rand()%400,rand()%40,rand()%15)
11 draw_sprite(screen,buffer,0,0);
12 clear_bitmap(buffer);
13 }
14 allegro_exit();
15}
16END_OF_MAIN()

jamal
Member #3,326
March 2003
avatar

Yes you do :).

Tobias Dammers
Member #2,604
August 2002
avatar

...but with two flaws:
1) You should clear the back buffer right before you start drawing, not after blitting (which in your loop is the same, except for the very first frame)
2) You shouldn't be using draw_sprite() to do double buffering. It is slower, and if the back buffer contains transparent pixels (color #0 or magic pink), the previous frame will show through. Just try moving the circle and you'll know what I mean.

Then:
Double buffering: What you do, essentially. Draw to a memory buffer, then copy to screen. This approach is easy to code and works well on pretty much all systems.
Dirty rectangles: A variant of double buffering. Instead of drawing and blitting the whole screen, you keep track of the areas that have changed since the last frame ('dirty'). You only redraw objects that are inside these areas, and you only blit the areas, not the whole screen. In applications that change only small portions of the screen at a time, this is usually way faster than double buffering, especially if you're low on video bus bandwidth. It's not suitable for 3D games and most scrolling 2D, though.
Page flipping: You allocate 2 pages of vram. While page 1 is visible, you draw to page 2. When you're done, you tell the hardware to display page 2 ('flip'), and start drawing on page 1. The benefit of this method is that you draw directly to vram, and you can skip the additional copy. When vram is approximately as fast as system ram, this can give a noticable performance boost. However, if there is a lot of layered, transparent or blended graphics, and vram speed or video bus bandwidth are a bottleneck, it may also be slower than double buffering.
Triple buffering: The same as page flipping, but with 3 buffers. In a page flipping system, you can't do anything between the flip request and the next vsync(). A triple buffering system uses the extra buffer to solve this: Page 1 is visible, page 2 is requested to be drawn, and page 3 is available for drawing. As soon as the video card has flipped, page 2 is visible, and page 3 can be requested. Page 1 is now available for drawing. Sounds complicated, but isn't really.
z-buffering: Has nothing to do with the above screen buffering methods. It is an algorithm used to determine which parts of overlapping faces in a 3D scene are visible and which are behind other faces. To achieve this, an extra buffer is implemented that holds the Z coordinate of each pixel (or rather, the fragment occupying that pixel). Then when you draw a polygon, you compare the z value in the buffer to the one you want to display, and only display if the new value is smaller. Other algorithms to solve this problem are scanline buffer (implemented by the allegro scene_XXXX() routines) and painter's algorithm (basically a back-to-front polygon sort).
Z-buffering gives the best results for arbitrary polygon models with "solid" faces, and all modern hardware supports it, so virtually all 3D games use it.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Evert
Member #794
November 2000
avatar

Also, do have a look at the exupdate.c example that comes with Allegro. It doesn't do dirty rectangles, but it does double/triple buffering and page flipping.

Tobias Dammers
Member #2,604
August 2002
avatar

Or the demo game, which actually does all of them.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

lucaz
Member #4,194
January 2004

Thanks a lot Tobias!

HoHo
Member #4,534
April 2004
avatar

Actually dirty rectangles can be used with 3d graphics. I think that little big adventure 2 used it. How else could p100 run it in full software with no problems? The image quality seemed better than in q2 for me(it had colors other than brownish grey). Come to think of it, it was a very advanced game. It even had a nice vehile physics in it.

http://www.gamershell.com/pc/little_big_adventure_2/screenshots.html
http://www.geocities.com/lba_pictures/

Quote:

System Requirements

Required for Windows® 95
Pentium™ 100MHz
16MB RAM
4X speed CD-ROM
DirectX2 supported
1MB SVGA video card
Sound Card

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Oscar Giner
Member #2,207
April 2002
avatar

Quote:

How else could p100 run it in full software with no problems?

Because the background image was still. It used the same technique that all other "prerendered 3D backgrounds with 3D realtime characters" games. The only difference is that the backgrounds were prerendered by the game engine itself.

HoHo
Member #4,534
April 2004
avatar

Hmm, seems I might have messed up two very different technologies. Guess I should get some sleep.

Still, if I would have made the game I would have used somekind of DR system. Moving things cover at most 1/4 area of the screen. Blitting the background every frame would make it slower than bliting smaller areas of it.

I wonder how hard it would be to make hl2 graphical quality game running with fixed viewpoint(like LBA) in software;D

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Oscar Giner
Member #2,207
April 2002
avatar

Quote:

Still, if I would have made the game I would have used somekind of DR system. Moving things cover at most 1/4 area of the screen. Blitting the background every frame would make it slower than bliting smaller areas of it.

Yeah, they may have used DR there.

Go to: