In a C program, first step is 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 more details. Important functions in

graphic.h

library will be discussed in details and samples programs will be provided to show the power of C programming language especially for graphic programming developing graphical user interfaces.

We will restrict our discussion on Graphics in C to 16 bit C programming, MS DOS environment and 640×480 VGA monitor. Complete reference of graphics.h library and explanation of each method in that library can be found in the following articles.

  1. Graphics Library (graphics.h) Reference (part 1)
  2. Graphics Library (graphics.h) Reference (part 2)
  3. Graphics Library (graphics.h) Reference (part 3)

Graphics mode Initialization – initgraph() function

First of all we call the

initgraph()

function that will initialize the graphics mode on the computer. The method

initigraph()

has the following prototype.

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

The method

initgraph()

initializes the graphics system by loading the graphics driver from disk (or validating a registered driver) then putting the system into graphics mode. The method

initgraph()

also resets all graphics settings (color, palette, current position, viewport, etc.) to their defaults. The initialization result is set to 0 which can be retrieved by calling

graphresult()

.

The

initgraph()

method has the following parameters.

*graphdriver

This is an integer value that specifies the graphics driver to be used. You can give

graphdriver

a value using a constant of the graphics_drivers enumeration type which is listed in

graphics.h

. Normally we use value as “0” (requests auto-detect). Other values are 1 to 10 and description of each enumeration type is listed here.

*graphmode

This is an integer value that specifies the initial graphics mode (unless

*graphdriver = DETECT

). If

*graphdriver = DETECT

, then

initgraph()

method sets *graphmode to the highest resolution available for the detected graphics driver. You can give *graphmode a value using a constant of the graphics_modes enumeration type and description of each enumeration type is listed here.

*pathtodriver

Specifies the directory path where

initgraph()

looks for graphics drivers (*.BGI) first.

  1. If they’re not there,
    initgraph()

    method looks in the current directory.

  2. If pathtodriver is null, the driver files must be in the current directory.

Both *graphdriver and *graphmode parameters 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 auto detect the attached video adapter at run time and pick the corresponding driver. If you tell initgraph to auto detect, it calls detectgraph to select a graphics driver and mode.

The

initgraph()

method loads a graphics driver by allocating memory for the driver (through

_graphgetmem()

method call), 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 simple program that initializes the graphics mode in C programming language and print the line in graphics mode.

#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 below program draws a circle in the current drawing color with its center at (150,150) and the radius (100) given by radius.

/* 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 we see in DOS/Command Mode is in the text mode which means it is meant for text only. And for graphics we need to initialize graphics mode using initgraph() method defined in 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();

This function unloads the graphics drivers and returns the screen back to text mode.

/*A program to draw a space with stars*/
#include<graphics.h>
#include<stdio.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. The bar is filled using the current fill pattern and fill color. Bar method accepts parameters i.e. left, top, right and bottom. The setfillstyle() method can be used to fill the bar with a different color or pattern.

#include <graphics.h>
#include <conio.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();
}