★ wanayoo — archive 1999 http://www.opensourceit.com/tutorials/990930_svglib.htmlNouvelle recherche | Portail wanayoo

Home Click Here!
Click Here!

Find:
 
Expert search
SEARCH THE IT WEB

languages
  Tcl
  PHP
  Python

operating systems
  Linux
  BSD

servers
  Apache
  Sendmail

applications
  GIMP
  Mozilla

general resources
  Open sources

Subscribe to our Weekly!

Related Newsletters
buy books and software!
link to us and earn money!
shop at EarthWeb Direct
b2b auctions
IT jobs
IT info

EarthWeb sites:
IT Management
Networking & Telecommunications
IT Jobs
Software Development
Hardware & Systems
Commerce
Earthweb Discussions
EarthWeb International

free and open source software programming
| | |
|
linux open source operating system
ADVERTISING

Searching the Web for tech topics just got better.
Search TechCrawler where all results come only from popular technical sites.

check it out!

SupportSource
Which Drivers
Are IT Pros Looking For?

See top ranking, real-time searches
at Driver Guide.Com Access a huge vault of drivers.

check it out!

User support problems on the rise?
This free tool nips 'em in the bud... automatically.
Download this great pre-emptive alert system.

check it out!

HAVE A WEBSITE?
Join the EarthWeb Allies Affiliate Network and earn $$ by linking your site with ours.

check it out!

Sign-up for YESMAIL
and get exclusive deals from leading brands of software,
hardware,
online magazines
,
and much more. No kidding! And it's FREE.

check it out!


linux graphics

Easy graphics:
A beginner's guide to SVGAlib

By 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.


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!

Wanna get notified when we post new tutorials? Sign up for our newsletter:
open it!
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 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.

How is SVGAlib different from the X Windowing System?

The X Windowing System (XFree86) is actually a server. This server must be started prior to using any applications that require X. Furthermore, the X server is unabashedly system-intensive (i.e., it's piggy with your resources) and it might (depending on your graphics card) 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 run the X server. SVGAlib is simply a binary C library 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 Windowing System is cross-platform (it runs on a variety of UNIXs). 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:

#include <stdio.h> #include <vga.h> int main(void) { vga_init(); vga_setmode(5); /* G320x200x256 */ vga_setcolor(4); vga_drawpixel(10, 10); sleep(5); vga_setmode(0); /* TEXT */ return(0); }

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 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 -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.

continued



Click Here!
Click Here!
Use of this site is subject to certain Terms & Conditions.
Earthweb's statement regarding our users' right to privacy.
Copyright ©1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited.