Easy Graphics: A Beginner's Guide to SVGAlib

ArticleCategory: [Choose a category, translators: do not translate this, see list below for available categories]

SoftwareDevelopment

AuthorImage:[Here we need a little image from you]

[Photo of the Author]

TranslationInfo:[Author + translation history. mailto: or http://homepage]

original in en Jay Link

AboutTheAuthor:[A small biography about the author]

Jay Link is twentysomething and lives in Springfield, Illinois. Aside from Linux, his interests include mountain climbing and flying. He administrates InterLink BBS (an unintentionally not-for-profit Internet provider) in his fleeting spare moments, as well as working various odd jobs to pay the rent.

Abstract:[Here you write a little summary]

Are you looking for a simple graphics package for your Linux system? If so, look no further. SVGAlib provides an easy way to create graphical applications and eliminates the rigmarole of the X Window System. If you have even the most rudimentary grasp of programming in C, then you can use SVGAlib.

ArticleIllustration:[One image that will end up at the top of the article]

[Ilustration]

ArticleBody:[The main part of the article]

What is SVGAlib?

SVGAlib is a low-level graphics library for Linux. It augments the C programming language, which doesn't provide support for graphics.

But there are lots of graphics programs written in C!

Yes, but they all rely on external library functions. C itself can only give you text. That's because all graphic functions are system dependent and are non-portable. Unfortunately, graphic routines coded for one operating system will not work under another unless they are completely rewritten. For example, graphics originally written for DOS or Windows are useless under Linux.

To code graphics in C under Linux, it is necessary to use an external set of functions which are native to Linux. SVGAlib is one such set.

SVGAlib is simply a binary C library which is called by C programs

How is SVGAlib different than the X Window System?

The X Window System (XFree86) is actually a server. This server must be started prior to using any applications which require X. Furthermore, the X server is unabashedly system-intensive (i.e. it's piggy with your resources) and it will prohibit you from using your virtual terminals (Alt 1-6).

SVGAlib, on the other hand, requires no such preparation. You do not "run" SVGAlib like you do X Windows. SVGAlib is simply a binary C library which is called by C programs, just like all the other libraries in /lib and /usr/lib. If SVGAlib is properly installed, the average user shouldn't even be aware of its existence. Finally, SVGAlib will not affect your virtual terminals, leaving you free to run multiple applications like always. You can have normal text on one terminal and graphics on another.

Drawbacks

There are many more applications available for X Windows than there are for SVGAlib, due to the fact that the X Window System is cross-platform (it runs on a variety of UNIXes). Only Linux uses SVGAlib. Also, poorly-written SVGAlib applications can mung-up your console, requiring a reboot. Finally, you shouldn't switch back and forth quickly between two consoles using SVGAlib graphics, or you risk screen corruption (forcing another reboot).

However, it is a myth that SVGAlib is a security risk. While SVGAlib apps must be setuid root, that privilege is given up immediately after execution. There is no need to be concerned.

In summary, despite the aforementioned problems, SVGAlib's speed and ease of use make it attractive in many situations. Especially if you just want to doodle on the screen.

Examples

To use SVGAlib, you must reference it in your C program. Simply #include <vga.h> . Here's about the easiest SVGAlib program there is: Download

#include  <stdio.h>
#include  <vga.h>

int main(void) {
   vga_init();
   vga_setmode(5);
   vga_setcolor(4);
   vga_drawpixel(10,10);

   sleep(5);
   vga_setmode(0);
   exit(0);
}
gradient

This will paint a single red pixel on your screen. After five seconds, it will reset your console to text mode and will exit.

Note our first statement, vga_init(). This relinquishes root status and initializes the SVGAlib library. The second line, vga_setmode(5) , sets the screen to mode 5, which is 320x200x256. That is to say, your screen becomes a grid which is 320 pixels wide, 200 pixels high, and which supports 256 colors. Alternatively, you could write vga_setmode(G320x200x256). Either statement is acceptable. Our next command, vga_setcolor(4) , makes red the current color. We can choose any value from 0 to 255. More colors are available with other commands, but we'll stick with these basic colors for this example. Finally, we paint our pixel at coordinate 10, 10 . This is eleven spaces right of the screen's left border, and eleven spaces down. It's eleven, not ten, because the coordinate grid starts at 0. Coordinate 0,0 is in the upper left-hand corner. vga_setmode(0) returns the screen to text mode. vga_setmode(text) is identical to vga_setmode(0). It's always nice to do this at the end of your program. Otherwise, you'll make life difficult for your users.

To compile this code, use the regular gcc compiler. You'll also need to link to SVGAlib with the -lvga command. Lastly, I suggest using -O3 , the best level of optimization. So here's our command:

gcc -O3 -o sample sample.c -lvga

Then, to make it usable by non-root accounts, type:

chmod u+s

To execute, just type:

sample     <or whatever you named it>

The complete set of SVGAlib commands is documented in the SVGAlib man page. We won't go into all of them here, though. Instead, we'll write our second sample program using a faster set of SVGAlib functions: vgagl.

Type "man vgagl", and you'll see that vgagl is "a fast, framebuffer-level graphics library based on SVGAlib." Basically, it gives you advanced graphics functions, such as the ability to draw shapes with one statement.

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: Download

#include <stdio.h>
#include <stdlib.h>
#include <vga.h>
#include <vgagl.h>

GraphicsContext *physicalscreen;
GraphicsContext *virtualscreen;

int main(void)
{
   int i,
       j,
       b,
       y,
       c;

   vga_init();
   vga_setmode(5);
   gl_setcontextvga(5);
   physicalscreen = gl_allocatecontext();
   gl_getcontext(physicalscreen);
   gl_setcontextvgavirtual(5);
   virtualscreen = gl_allocatecontext();
   gl_getcontext(virtualscreen);

   gl_setcontext(virtualscreen);
   y = 0;
   c = 0;
   gl_setpalettecolor(c, 0, 0, 0);
   c++;
   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);
         y++;
      }
      c++;
   }

   gl_copyscreen(physicalscreen);

   getchar();
   gl_clearscreen(0);
   vga_setmode(0);
   exit(0);
}


     

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:

gl_setcontextvga(5)
gl_setcontextvgavirtual(5)
physicalscreen = gl_allocatecontext()
gl_getcontext(physicalscreen)
virtualscreen = gl_allocatecontext()
gl_getcontext(virtualscreen)

We now announce that we'll be working with the virtual screen: gl_setcontext(virtualscreen).

gl_setpalettecolor() gives us 64 shades of blue. We'll draw 3 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, you can find the latest version of SVGAlib at sunsite.unc.edu in pub/Linux/libs/graphics or at tsx-11.mit.edu in pub/linux/sources/libs . At the time of this writing, the current version is 1.3.0.

Finally, the Slackware distribution, while a good one, has a tendancy to install SVGAlib to the wrong directories. Getting and installing a new SVGAlib distribution from Sunsite or MIT will fix that.