Double Buffering in Screen 13


Today many games use doublebuffering to decrease flickering on the screen.
Such a routine stores the whole data which would actually be stored in the Video Segment (&HA000). After the data has been written to the buffer, the whole buffer is put onto the screen at one time, so that it doesn't flicker and it's very fast.
The major part of these routines is written in ASM, but I'll explain now how you can do doublebuffering in pure QB.

Therefor we need a buffer which can contain 64.004 Bytes (320x200 Bytes for the screen data + 4 Bytes to save the screen size). So we have to DIM a array within 32.001 Integers (not 32.002 because the index 0 is used, too):


DIM SHARED Buffer%(32001)
Buffer%(0) = 320 * 8
'X-Width of the buffer in Bits (therefor the multiplication with 8
Buffer%(1) = 200
'Y-Width of the buffer in Pixels


Now there's a buffer, but at the moment we can't write into it. This problem is solved with a little routine. ( If you have questions about this routine, have a look at this article http://faq.qbasicnews.com/?blast=PlottingInScreenThirteen )


SUB Drawpixel (x%,y%,col%)
DEF SEG = VARSEG(Buffer%(32001))
POKE 320& * y% + x% + 4, col%
'There is 4 added because of the 4 bytes which included the screen size at the beginning of the array
DEF SEG
END SUB


There's only one routine missing, but you don't have to code it. It's already included in QB. The PUT-Routine. To draw this buffer to the screen you only have to call PUT with the right parameters:


PUT (0, 0), Buffer%, PSET


This is easy, isn't it?

Here's a complete example:


DIM SHARED Buffer%(32001)
Buffer%(0) = 320 * 8
Buffer%(1) = 200
SCREEN 13
FOR x% = 0 TO 320
FOR y% = 0 TO 200
Drawpixel x%, y%, INT(RND * 256)
NEXT y%
NEXT x%
SLEEP
PUT (0, 0), Buffer%, PSET

SUB Drawpixel (x%,y%,col%)
DEF SEG = VARSEG(Buffer%(32001))
POKE 320& * y% + x% + 4, col%
DEF SEG
END SUB


Hope I could help ya
...
akOOma


DoubleBufferingInScreenThirteen - page last edited 2004-02-10 12:32:29 by 172.167.146.22 (home) (edit)
Blast WIKI - by RoboticBoy - edited and tweaked for our evil purposes by Hexadecimal Disaster