Home
Tutorials
Code Snippets
Code Samples
Downloads
Links

The Blog
Our Projects
About
Contact

::Add RageStorm to Favorites!::

The Blog | Our Projects | Guest Book | About | Contact

 
Tutorial - Tripple-Buffering
Author:Arkon
Category:DOS Graphics
Uploaded at:28-May-03
Views:7230
Well well well, did you guess what Triple-Buffering stands for already and why we use it?
Hmmm one offscreen (buffer) for background, another buffer for foreground and one is the Video Memory as usual.
If you have no $^@$ing idea what I'm talking about then read this.

OK you smart ass, you ask why you need another offscreen huh?
Well ofcourse you can handle well without it, but it makes life easier and your program faster. Think that you make a game that has a static background image (or one which rarely changes), then instead of loading the image every frame and draw it, you will just load it once and draw it once on the background offscreen. Then every frame you copy the background onto the foreground, draw the foreground stuff (such as: Players, Bullets, Effects.. bla bla). And then draw the foreground buffer onto the screen. That it is.

In the source code of this tutorial you will find a nice example of using Triple-Buffering.. Check it out... It took me around 30 mins to write it... but the damn background image took more time to draw :)

// Pointers to the buffers.
unsigned char *scr = NULL, *bkg = NULL;

// Allocate memory for the buffers
int init_virscr()
{ 
 scr = (unsigned char *)malloc(320 * 200);
 if (src == NULL) return(0);
 bkg = (unsigned char *)malloc(320 * 200);
 if (bkg == NULL)
 {
  free(scr);
  return(0);
 }
 return(1);
}

// Draw a pixel on a given buffer
void plotpixel_db(int x, int y, unsigned char co, 
                  unsigned char *buf)
{ 
 buf[(y << 6) + (y << 8) + x] = col;
}

void copy_screen(unsigned char *dst, unsigned char *src)
{
 memcpy(dst, src, 320 * 200);
}

void drawscreen_db()
{
 memcpy(VIDEO_ADDRESS, scr, 320 * 200);
}

// Free the offscreen buffers
void free_virscr()
{
 free(bkg);
 free(scr);
}

// Usage of the Triple-Buffering:
// (Hey the source is really really cool!! You'll like it)
init_virscr();
// Draw a pixel as the background screen :) yak
putpixel_db(0, 0, 10, bkg);
while(!GameOver())
{
 // Copy the background onto our foreground offscreen.
 // Notice we don't need to clear the screen.
 copy_screen(scr, bkg);
 put_pixel_db(1, 1, 14, scr);
 drawscreen_db();
}
free_virscr();

Source Code:
Turbo C - tribuf.zip

User Contributed Comments(1)
 [1] Gaebu rider | 2013-08-29 02:49:08

Great tutorial. This was the only one I found on the internet about tripple buffering. ;)
NOTE:
Comments that will hurt anyone in any way will be deleted.
Don't ask for features, advertise or curse.
If you want to leave a message to the author use the contacts,
if you have any question in relation to your comments please use the forum.
Comments which violate any of these requests will be deleted without further
notice. Use the comment system decently.

Post your comment:
Name:
email:
Comment:
::Top 5 Tutorials::
Embedded Python[106602]
2D Rotated Rectangles Collision Detection[79757]
Keyboard Hook[68953]
UDP[52984]
HTTP Proxy[36218]

::Top 5 Samples::
2D ColDet Rotated Rectangles[10624]
PS2 Mouse Driver[6110]
Wave Format Player[4994]
Reading FAT12[4880]
CodeGuru[4570]


All rights reserved to RageStorm © 2009