Tuesday, August 28, 2012

Graphics in GNU - C (gcc)

Here is an article which teaches you to install graphics packages in ubuntu or in any other linux operating system. Most of the coders would try their graphic codes in windows because MS DOS comes with a default graphics package called graphics.h. However the results of execution of graphics programs in linux operating system is far more better than in windows. and I've tried that in my ubuntu laptop. So the upcoming part of this article will contain the steps required to install graphics packages in ubuntu or in any other linux operating system.



Step 1: open terminal by pressing Ctrl+Alt+T. Type this command.

sudo apt-get install build-essential

and execute it.

Step 2: Now we need to install some packages which are required for installing graphics.h. Those packages are libsdl-image1.2,libsdl-image1.2-dev,guile-1.8 and guile-1.8-dev. Type this command.

sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-1.8 guile-1.8-dev libsdl1.2debian-all libart-2.0-dev libaudiofile-dev libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev libxext-dev x11proto-xext-dev libfreetype6 libaa1 libaa1-dev libslang2-dev libasound2 libasound2-dev

Execute the above command and all those packages will be installed.



Step 3: Now we need to install a package called libgraph. There are 2 download links.

  1. download.savannah.gnu.org/releases/libgraph/libgraph-1.0.2.tar.gz
  2. http://mirror.veriportal.com/savannah/libgraph/

You can use any one of above download links and can download the libgraph package. It'll be in compressed form, i.e., in tar.gz format. You'll have to extract those files to your home folder.


Step 4: a) Now you should create a path to the extracted folder. For that use this command.

CMD 1 :  mkdir libgraph-1.0.2 (if needed)
CMD 2 : cd libgraph-1.0.2

b) There will be some configuration files in that libgraph-1.0.2 file. You will have to configure those files. There is a simple command for this.

CMD 3 : ./configure

c) Now you'll have to prepare the package libgraph for installation. So you should execute these two commands.

sudo make install

d) Now you should copy the executed and installed libgraph file to the usr folder. Use the cp command as shown below.

sudo cp /usr/local/lib/libgraph.* /usr/lib

Step 5: Now you've successfully installed libgraph package in ubuntu or in any linux operating systems. So you can successfully execute the graphic programs in GNU/Linux operating systems. However there are some modifications required. In the Turbo C graphics program you'll see some lines like these.

int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");

Replace the c:\\tc\\bgi with NULL. And finally when you are compiling the graphic program in ubuntu, be sure to add -lgraph parameter, i.e.,

gcc prgname.c -0 prgname -lgraph

Here is an graphic program and you can try out this program in ubuntu after installing the libgraph package in the method which I've stated above.


#include<stdio.h>
#include"myconio.h" //Refer previous article for this header
#include<graphics.h>

int main() 
{
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
int i,x=10;
char str[3]="0";
setbkcolor(WHITE);

setcolor(12); //outline heart
circle(50,50,40);
circle(110,50,40);
line(22,80,80,140);
line(80,140,138,80);

floodfill(50,50,12); //fill heart
floodfill(110,50,12);
floodfill(80,50,12);
floodfill(80,100,12);

getch();
closegraph();
return 0;
}

Output will be :





I hope that you enjoyed this article.

No comments:

Post a Comment