graphics.h working in Linux - Ubuntu

Working on Linux distribution is a fun way, tricky and ....... (rest you know, if you r a linux user).

Although Ubuntu come pre-added programming language support for The Coders like you. But, it miss some dependencies which is required to add manualy.

Lets see how to get graphics.h header file working in Ubuntu.



There are several option available to do graphics programming using Ubuntu.

Using SDL

If you want to use graphics.h on Ubuntu platform you need to compile and install libgraph. It is the implementation of turbo c graphics API on Linux using SDL.

It is not very powerful and suitable for production quality application, but it is simple and easy to use for learning purpose.

You can download it from here.


  • Open terminal window by pressing Ctrl+Alt+T
  • First install build-essential by typing in terminal
sudo apt-get install build-essential

  •  Install some additional packages by typing

sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-1.8 \
  guile-1.8-dev libsdl1.2debian 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

  • Now extract the downloaded libgraph-1.0.2.tar.gz file.
  • Goto extracted folder and run following command
 ./configure
make
sudo make install
sudo cp /usr/local/lib/libgraph.* /usr/lib

  • Now you can use #include<graphics.h> on ubuntu platform
  • Use following line in your program
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
Here is a sample program using graphics.h

/*  demo.c*/
#include<graphics.h>
int main()
{
   int gd = DETECT,gm,left=100,top=100,right=200,bottom=200,x= 300,y=150,radius=50;
   initgraph(&gd,&gm,NULL);
   rectangle(left, top, right, bottom);
   circle(x, y, radius);
   bar(left + 300, top, right + 300, bottom);
   line(left - 10, top + 150, left + 410, top + 150);
   ellipse(x, y + 200, 0, 360, 100, 50);
   outtextxy(left + 100, top + 325, "C Graphics Program");
   delay(5000);
   closegraph();
   return 0;
}

  • To compile it use
gcc demo.c -o demo -lgraph

  • To run type
./demo

Using OpenGL (via GLUT)

Although OpenGL is basically made for 3D programming, drawwing 2D shapes gives the basic outline and introduction to OpenGL and gives the idea about how to start drawing objects in OpenGL.

  • To install GLUT, open terminal and type sudo apt-get install freeglut3-dev
  • Here is a simple graphics program using GLUT
/*  demo.c*/
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glu.h>
void setup() {   glClearColor(1.0f, 1.0f, 1.0f, 1.0f); }
void display()
   {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glColor3f(0.0f, 0.0f, 0.0f);
      glRectf(-0.75f,0.75f, 0.75f, -0.75f);
      glutSwapBuffers();
   }
int main(int argc, char *argv[])
  {
     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
     glutInitWindowSize(800,600);
     glutCreateWindow("Hello World");
     setup();
     glutDisplayFunc(display);
     glutMainLoop();
     return 0;
  }

  • Compile it using 
gcc demo.c -o demo -lglut -lGL 

  • Run is using 
 ./demo


Share on Google Plus

1 comment:

  1. I’d say that SDL_bgi is much better than libgraph: it’s SDL2-based, more portable, and *much* faster than libgraph. http://libxbgi.sourceforge.net/

    ReplyDelete