Click here if you are stuck in someone else's frames.
Clearing (blanking) the screen

To clear the screen to any color, all you would need to do is load every pixel with the appropriate color value.  In VGA mode 13h, every pixel is one byte long and every byte of video memory is contiguous.  With that in mind, we can write a function that will quickly change all bytes of video memory to whatever color value we want.  This coding example does just that:

     /* Define constants for screen dimensions */
     #define ScreenWidth  (unsigned)320
     #define ScreenHeight (unsigned)200

     void FillScreen(int color)
     {
          unsigned int i;             /* Used for counting */

          for (i = 0; i < ScreenWidth * ScreenHeight; i++)
               videoMem[i] = color;   /* Set each pixel to color */
     }

This method works and is certainly one of the most "portable" methods to use.  Of course, it isn't the most efficient as far as speed goes.  This is because each time through the loop, the processor must add the index to the pointer to determine the address with which to load the value of the color variable into.   This can be sped up a bit by using another pointer that always points to the current byte address that is being changed.  For example:

     void FillScreen(int color)
     {
          unsigned int i;               /* Count variable */
          unsigned char far *tempPtr;   /* Temporary pointer */

          /* Set temporary pointer to video memory */
          tempPtr = videoMem;

          /* Assign each byte the color attribute */
          for (i = 0; i < ScreenHeight * ScreenWidth; i++) {
               *tempPtr = color;
               tempPtr++;
          }
     }

This method is also very portable, most all DOS based C compilers will compile and run this function without a hitch.  One last example makes use of a function that is already defined in the C library.  This function is called memset.

     #include <mem.h>     /* For memset & _fmemset functions */

     /* Define constants for screen dimensions */
     #define ScreenWidth  (unsigned)320
     #define ScreenHeight (unsigned)200

     void FillScreen(int color)
     {
          /* Set all video memory to appropriate color */
          _fmemset(videoMem, color, ScreenWidth * ScreenHeight);
     }

With a single line, we set every byte of video memory to the appropriate color attribute.  This is certainly the smallest and perhaps the fastest method.   However, since we used _fmemset instead of the ANSI standard memset function, we loose some portability.  The _fmemset is used because the video memory requires a far (segment & offset) pointer while memset can work only with near (offset only) pointers.  Since this requirement is a DOS / Intel 80x86 requirement, all DOS compilers should support _fmemset in some form.  (You may need to substitute fmemset, without a leading underscore, or farmemset, with or without a leading underscore, for _fmemset.)  You can also use memset and compile the program using the large memory model (far pointers used for everything).  If all else fails, you can simply use one of the previous examples, they're not as compact or fast, but they work.  Besides, speed is normally not an issue when blanking the screen (how often do you think you'll do that in any game?).  In some cases you may even want to slow it down some for a more visual effect.

Previous Page | Main Page | Next page