Easy graphics: A beginner's guide to SVGAlib
Easy graphics: A beginner's guide to SVGAlibBy Jay Link
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 Windowing System. If you have even the most rudimentary grasp of programming in C, then you can use SVGAlib. ExamplesTo use SVGAlib, you must reference it in your C program. Simply #include <vga.h> . Here's about the easiest SVGAlib program there is:
#includeThis 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 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 11 spaces down. It's 11, not 10, 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 -lvgaThen, to make it usable by non-root accounts, type: chmod u+sTo 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. |
Page 1 of 2
This article was originally published on September 30, 1999