January 26, 2021
Hot Topics:

Easy graphics: A beginner's guide to SVGAlib

  • By Jay Link
  • Send Email »
  • More Articles »

Below is a program which uses vgagl. It should display a blue gradient screen, just like the ones that some installation programs use. It starts out as light blue on top of the screen, and gradually fades to black as it moves down. One thing that it does differently than the first program is that it does the drawing on an invisible "virtual" screen. When all the lines have been drawn, it copies the finished picture to the visible "physical" screen in one fell swoop. This eliminates screen flicker, and makes your application look more professional. I like to think of the virtual screen as being "backstage", a place where "props" can be assembled between "acts" in the show.

To compile, I suggest typing:

gcc -O3 -o gradient gradient.c -lvgagl -lvga

Note that -lvgagl comes before -lvga. It is mandatory that you refer to vgagl first when using that library. Did you remember to chmod u+s ?

Here's the code:


GraphicsContext *physicalscreen;
GraphicsContext *virtualscreen;

int main(void)
   int i,

   physicalscreen = gl_allocatecontext();

   virtualscreen = gl_allocatecontext();

   gl_setpalettecolor(c, 0, 0, 0);
   y = 0;
   c = 1;
   for (i = 0; i < 64; i++)
      b = 63 - i;
      gl_setpalettecolor(c, 0, 0, b);
      for (j = 0; j < 3; j++)
         gl_hline(0, y, 319, c);



Note that we now need to #include <vgagl.h>.

We start our code by setting the graphics context:

GraphicsContext *physicalscreen
GraphicsContext *virtualscreen

Then we'll declare our variables and use vga_setmode() to set the screen mode. We'll use mode 5 (G320x200x256) like before.

We need to initialize both the visible "physical" screen and the invisible "virtual" screen. We then save the contexts into variables:

physicalscreen = gl_allocatecontext()
virtualscreen = gl_allocatecontext()

We now announce that we'll be working with the virtual screen:


Gl_setpalettecolor() gives us 64 shades of blue. We'll draw three lines per shade for a total of 192 lines. The remaining eight lines will be black anyway so they won't stand out.

When we are finished, we copy the contents of the virtual screen (our current screen) to the physical screen with gl_copyscreen(physicalscreen).

This time, we'll let the user decide how long to leave the picture visible. Getchar() waits for user input. After receiving a keystroke (any key, it doesn't matter which), we gl_clearscreen(0) and vga_setmode(0) to return to text.

The entire set of functions for SVGAlib can be viewed by typing "man svgalib" and "man vgagl". Then, each function has its own man page. After reading this introduction, you should be able to insert new commands into your own programs with relative ease. You also have the demo programs that came with SVGAlib from which to learn.

In the event that your distribution was incomplete (the Slackware distribution, for example, while a good one, has a tendancy to install SVGAlib to the wrong directories), you can find the latest version of SVGAlib at the MetaLab in pub/Linux/libs/graphics. At the time of this writing, the current version is 1.4.0.ø

Related resources

1. SVGAlib home page Web-based resource to download the most recent version of SVGAlib, ask for help and read tutorials.
2. Brion Vibber's FAQ More help for SVGAlib developers.
3. SVGAlib mailing list The Rutgers mailing list. Very helpful.

Jay Link is twenty-something and lives in Springfield, IL. He administrates InterLink BBS - an unintentionally nonprofit Internet service provider - in his fleeting spare moments as well as working various odd jobs to pay the rent.

Page 2 of 2

This article was originally published on September 30, 1999

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date