VGA Mode 10h Sprite


This sample got a head start from Michael Abrash's excellent book "The Zen of Graphics Programming" and the source owes a lot to Michael's sample code. It was assembled with MASM 6.15 and linked with LINK 5.60.339 (to get a DOS image).
 Download sprite.exe (5KB).

 Download the Sprite assembly language source code (30KB).
   

The sample demonstrates an MS-DOS application manipulating Video Graphics Array (VGA) hardware directly. It simply animates an 80x80 pixel sprite, moving it around the screen until a key is pressed. Since a VGA lies at the heart of all graphics adaptors, this application will run on any PC. It is an MS-DOS application (it calls BIOS functions) but it will also run on any 32-bit Windows platform.

VGA Mode 10h (decimal 16) is used, which is a medium-resolution 640x350 pixel mode with 16 colours. The application divides the VGA display memory into two framebuffers so that one may be on display whilst the other is being written into. This technique for avoiding 'shearing' and other unpleasant visual artifacts is called double-buffering and is achieved by alternately assigning the locations of the framebuffers to the VGA's video data start address and then waiting for the vertical sync pulse (when the CRT's electron beam returns to the top of the screen ready to begin scanning lines again) before writing into the off-screen framebuffer.

Another method used here is to copy the sprite's four bitmaps into the VGA's four colour planes in an undisplayed part of the VGA display memory in program initialisation. This involves four different writing operations as well as several OUT instructions, all of which takes time. If the same code was used every time the sprite neeed to be drawn then performance would suffer (in theory at least; it wouldn't be noticed in a simple sample such as this). Instead, VGA write mode 1 is used to copy data in all four planes from one part of display memory to another in a single read and write. Although only one address is accessed each time a byte is read and written, the VGA can access a byte in up to four of the colour planes which actually reside at that same address in write mode 1.

last updated: 4-oct-01