C and C++ Programming Resources

Custom Search

Graphics in C Language

Graphics in C Language

We will restrict our discussion on Graphics in C Language to 16 bit C programming and MS DOS environment. In a C Program first of all you need to initialize the graphics drivers on the computer. This is done using the initgraph method provided in graphics.h library. In the next few pages we will discuss graphics.h library in details. Important functions in graphic.h library will be discuees in details and samples programmes will be provided to show the power of C programming language.

Graphics mode Initialization

First of all we have to call the initgraph function that will intialize the graphics mode on the computer. initigraph have the following prototype.

 void initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver);

Initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver) then putting the system into

graphics mode.Initgraph also resets all graphics settings (color, palette, current position, viewport, etc.) to their defaults, then resets graphresult to 0.

*graphdriver

Integer that specifies the graphics driver to be used. You can give graphdriver a value using a constant of the graphics_drivers enumeration type.

*graphmode

Integer that specifies the initial graphics mode (unless *graphdriver = DETECT). If *graphdriver = DETECT, initgraph sets *graphmode to the highest resolution available for the detected driver. You can give *graphmode a value using a constant of the graphics_modes enumeration type.

*pathtodriver

Specifies the directory path where initgraph looks for graphics drivers (*.BGI) first.

  1. If they’re not there, initgraph looks in the current directory.
  2. If pathtodriver is null, the driver files must be in the current directory.

*graphdriver and *graphmode must be set to valid graphics_drivers and graphics_mode values or you’ll get unpredictable results. (The exception is graphdriver = DETECT.)

After a call to initgraph, *graphdriver is set to the current graphics driver, and *graphmode is set to the current graphics mode. You can tell initgraph to use a particular graphics driver and mode, or to autodetect the attached video adapter at run time and pick the corresponding driver. If you tell initgraph to autodetect, it calls detectgraph to select a graphics driver and mode.

Normally, initgraph loads a graphics driver by allocating memory for the driver (through _graphgetmem), then loading the appropriate .BGI file from disk.As an alternative to this dynamic loading scheme, you can link a graphics driver file (or several of them) directly into your executable program file.

Here is a sample program that initializes the graphics mode in C Language.

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
  int gdriver = DETECT, gmode, errorcode;
/* initialize graphics mode */
  initgraph(&gdriver, &gmode, "");
/* read result of initialization */
  errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
  {
    printf("Graphics error: %s\n", grapherrormsg(errorcode));
    printf("Press any key to halt:");
    getch();
    exit(1); /* return with error code */
  }
/* draw a line */
  line(0, 0, getmaxx(), getmaxy());
/* clean up */
  getch();
  closegraph();
  return 0;
}

The graphics programming in c language is discussed in brief to provide an over view to the beginner.

/* Sample program to draw a circle*/
#include<graphics.h>
#include<conio.h>
main()
{
  int gd=DETECT,gm;
  initgraph(&gd,&gm,""); /* initialization of graphic mode */
  circle(150,150,100);
  getch();
  closegraph(); /* Restore orignal screen mode */
}
/* End of program */

Normally the screen which u view in DOS is in the text mode which means it is meant for text. And for graphics u need to initialize graphics mode. And for this to happen u need to include graphics.h?.

circle(x coordinate ,y coordinate , radius);

The circle command takes a X coordinate which means Vertical axis and Y coordinate which means Horizontal axis. And the last one is the radius of the circle. closegraph();

With out this function the screen mode will still remain in graphic mode and when u come out, to DOS u will see a different screen, which is not in the text mode.

/*A program to draw a space with stars*/
#include<graphics.h>
main()
{
  int gd=DETECT,gm;
  int i,x,y;
  initgraph(&gd,&gm,"");
  line(0,0,640,0);
  line(0,0,0,480);
  line(639,0,639,480);
  line(639,479,0,479);
  for(i=0;i<=1000;i++)
  {
    x=rand()%639;
    y=rand()%480;
    putpixel(x,y,15);
  }
  getch();
  closegraph();
}
/* End of program */
/*Here a sample program to illustrate how to use BARS which are used for visual statistics */
#include<graphics.h>
main() {
  int gd=DETECT,gm,maxx,maxy,x,y,button;
  initgraph(&gd,&gm,"");
  line(80,150,200,150);
  line(80,150,80,50);
  settextstyle(1,HORIZ_DIR,1);
  outtextxy(100,153,"<-X axis");
  settextstyle(1,VERT_DIR,1);
  outtextxy(60,50,"<-Y axis");
  bar(100,100,120,150);
  bar(130,120,150,150);
  getch();
  closegraph();
}

Pages: [Page - 1] [Page - 2] [Page - 3] [Page - 4]

Tags: , ,

There are 145 Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.

  • Junjun says:

    I need to make a programming that works like a voting system…compute votes…and calculates score for finalist and display them in a data text file…using classes and object can anyone help me…

  • Ankit says:

    //This is a game cross and nought( Zero Katta) in c++.

    void show_game_box();
    char m[3][3];
    void main()
    {
    int p,q;
    char ans;
    cout<<"\t\tCROSS & NOUGHT GAME\n";
    do
    {
    for(p=1;p<=3;p++)
    {
    for(q=1;q<=3;q++)
    {
    m[p][q]='';
    }
    }
    int i,j,sum=0;
    while(sum<10)
    {
    if(sum==0)
    show_game_box();
    cout<<"\n\n\nPlayer 1 is'0'\n";
    cout<<"Player 1's turn\n";
    cout<>i;
    cout<>j;
    for(;(i>3)||(j<1)||('X'==m[i][j])||('0'==m[i][j]);)
    {
    cout<<"Sorry you entered wrong choice\n";
    cout<<"Enter your choice again\n";
    cout<>i;
    cout<>j;
    }
    m[i][j]=’0′;
    sum++;
    show_game_box();
    if((m[1][1]==’0′)&&(m[1][1]==m[1][2])&&(m[1][1]==m[1][3]))
    {
    cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    cout<<"player 1 wins the game\n";
    break;
    }
    if((m[2][1]=='0')&&(m[2][1]==m[2][2])&&(m[2][1]==m[2][3]))
    {
    cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    cout<<"player 1 wins the game\n";
    break;
    }
    if((m[3][1]=='0')&&(m[3][1]==m[3][2])&&(m[3][1]==m[3][3]))
    {
    cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    cout<<"player 1 wins the game\n";
    break;
    }
    if((m[1][1]=='0')&&(m[1][1]==m[2][2])&&(m[1][1]==m[3][3]))
    {
    cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    cout<<"player 1 wins the game\n";
    break;
    }
    if((m[1][3]=='0')&&(m[1][3]==m[2][2])&&(m[1][1]==m[3][1]))
    {
    cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    cout<<"player 1 wins the game\n";
    break;
    }
    if((m[1][1]=='0')&&(m[1][1]==m[2][1])&&(m[1][1]==m[3][1]))
    {
    cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    cout<<"player 1 wins the game\n";
    break;
    }
    if((m[1][2]=='0')&&(m[1][2]==m[2][2])&&(m[1][2]==m[3][2]))
    {
    cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    cout<<"player 1 wins the game\n";
    break;
    }
    if((m[1][3]=='0')&&(m[1][3]==m[2][3])&&(m[1][1]==m[3][3]))
    {
    cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    cout<<"player 1 wins the game\n";
    break;
    }
    if(sum==9)
    {
    cout<<"\t\tHURRAY!!!!! The game is over\n";
    cout<<"\t\tNO ONE WINS\n";
    cout<<"The game is draw\n";
    break;
    }
    cout<<"\n\n\nPlayer 2 is'X'\n";
    cout<<"Player 2's turn\n";
    cout<>i;
    cout<>j;
    for(;(i>3)||(j<1)||('X'==m[i][j])||('0'==m[i][j]);)
    {
    cout<<"Sorry you entered wrong choice\n";
    cout<<"Enter your choice again\n";
    cout<>i;
    cout<>j;
    }
    m[i][j]=’X';
    sum++;
    show_game_box();
    if((m[1][1]==’X')&&(m[1][1]==m[1][2])&&(m[1][1]==m[1][3]))
    {
    cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    cout<<"player 2 wins the game\n";
    break;
    }
    if((m[2][1]=='X')&&(m[2][1]==m[2][2])&&(m[2][1]==m[2][3]))
    {
    cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    cout<<"player 2 wins the game\n";
    break;
    }
    if((m[3][1]=='X')&&(m[3][1]==m[3][2])&&(m[3][1]==m[3][3]))
    {
    cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    cout<<"player 2 wins the game\n";
    break;
    }
    if((m[1][1]=='X')&&(m[1][1]==m[2][2])&&(m[1][1]==m[3][3]))
    {
    cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    cout<<"player 2 wins the game\n";
    break;
    }
    if((m[1][3]=='X')&&(m[1][3]==m[2][2])&&(m[1][1]==m[3][1]))
    {
    cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    cout<<"player 2 wins the game\n";
    break;
    }
    if((m[1][1]=='X')&&(m[1][1]==m[2][1])&&(m[1][1]==m[3][1]))
    {
    cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    cout<<"player 2 wins the game\n";
    break;
    }
    if((m[1][2]=='X')&&(m[1][2]==m[2][2])&&(m[1][2]==m[3][2]))
    {
    cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    cout<<"player 2 wins the game\n";
    break;
    }
    if((m[1][3]=='X')&&(m[1][3]==m[2][3])&&(m[1][1]==m[3][3]))
    {
    cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    cout<<"player 2 wins the game\n";
    break;
    }
    if(sum==9)
    {
    cout<<"\t\tHURRAY The game is over\n";
    cout<<"\t\tNO ONE WINS\n";
    cout<<"\t\tThe game is drawn\n";
    break;
    }
    }
    cout<>ans;
    }while((ans==’y')||(ans==’Y'));
    system(“PAUSE”);
    getch();
    }
    void show_game_box()
    {
    cout<<"\n 1 2 3\n"<<endl;
    cout<<" 1 "<<m[1][1]<<"|"<<m[1][2]<<"|"<<m[1][3]<<endl;
    cout<<" -|-|-\n";
    cout<<" 2 "<<m[2][1]<<"|"<<m[2][2]<<"|"<<m[2][3]<<endl;
    cout<<" -|-|-\n";
    cout<<" 3 "<<m[3][1]<<"|"<<m[3][2]<<"|"<<m[3][3]<<"\n\n\n";
    }

  • juhihumera says:

    can i have the list of all the funcions in c graphics……

  • suchit says:

    could anyone help me to get a code for conversion of a digital image file into the matrix containing the pixels values of the image..

  • sadi says:

    I need a program in computer graphics using c language. please write a program for moving a person with flag.thank you.

  • suryana says:

    can u give a tutorial about this.

    Project Name: Implementation of Recursion and Tracing its Stack (Graphically)

    Description: This project will implement and trace recursive functions graphically.

  • suryana says:

    i using a turbo c++ to do a graphical in C..this error be occur what should i do…
    Fatal ..\INCLUDE\GRAPHICS.H 19: Error directive: BGI graphics not supported under Windows

  • smartanu says:

    i using a tc3 to do a graphical in C..this error be occur what should i do…
    #include
    #include
    #include
    #include
    #include
    void draw(int x1,int y1,int x2,int y2);
    void main()
    {

    int x1,y1,x2,y2;
    int gdriver=DETECT,gmode,errorcode;
    initgraph(&gdriver,&gmode,”C:\\TC\\BGI”);
    printf(“Enter the first point \n”);
    scanf(“%d%d”,&x1,&y1);
    printf(“\n\n Enter the second point”);
    scanf(“%d%d”,&x2,&y2);
    printf(“\n\n The line is shown below”);
    draw(x1,y1,x2,y2);
    getch();
    }
    void draw(int x1,int y1,int x2,int y2)
    {
    int x,y,e,i,dx,dy,a;
    x=x1;
    y=y1;
    dx=x2-x1;
    dy=y2-y1;
    a=dx;
    dx=dy;
    dy=a;
    e=2*dy-dx;
    for(i=1;i0)
    {
    x=x+1;
    e=e-2*dx;
    }
    y=y+1;
    e=e+2*dy;
    }
    getch();
    }
    when i run this programme getting error
    Linker error:undefined symbol _putpixel in module myfile name.
    i have executed this programme in lab pc.

  • mimi says:

    hello…
    need ur help guyz..
    i have to add graphics in ma game hangman… plz help me… i dun even knw how to start..:(
    i av to submitt ma project tomorrow… culd anyone help me plz..

    nd how to add a library iostream….??

  • Saqib says:

    amit srivastava:
    You can capture the mouse movement using MFC in VC++ in the following way. Create a standard MFC exe file and in DocumentView calss there will be mouse events i.e. OnMouseMove(), OnLButtonDown(), you can get the position of mouse in any function. Both the function accepts two parameters (UINT nFlags, CPoint point). And in Point object there will be mouse position (x,y) and you can get them easily.

    @raj:
    You can get a calculator progam here.
    http://www.mycplus.com/free-utilities/scientific-calculator/

    @Gcablay_18:
    There are tutorials available here in C++ for creating classes.
    http://www.mycplus.com/tutorials/cplusplus-programming-tutorials/classes-2/

    @vineet:
    This function shutdown the graphics mode and returns to the position it was before the initgraph function was called. Closegraph function releases all the resources occupied by the graphics system like memry, fonts, drivers etc…
    You can learn more about graphics functions here
    http://www.mycplus.com/featured-articles/c-language-graphics-library-reference-part-1/


Leave a Reply

You must be logged in to post a comment.