Graphics programming in C Language is discussed rarely. We will discuss some important functions of graphics.h in C Language, at the end we will make a simple program to demonstrate the use of graphics functions. So let’s start with the description of important functions and their use.
Graphics Drivers
Graphics drivers are a subset of device drivers and are applicable only in the graphics mode. They work in the above fashion to execute whatever task we have assigned them. Turbo C offers certain graphics drivers. These are the files with a BGI extension. Depending on what adapter is used, one of these drivers gets selected. Our programs have been developed on the VGA adapter. Thus we need the EGAVGA.BGI file as our graphics driver. In our example program given at the end of this chapter, gd has been assigned the value DETECT, thereby asking initgraph( ) to figure out which BGI file is needed. This file is then loaded into memory. If we do not initiate gd with DETECT then it is our responsibility to set up gd and gm with appropriate values. Since most of the times we don’t want to be bothered with this responsibility we use the DETECT macro.
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 discussed 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.
Integer that specifies the graphics driver to be used. You can give graphdriver a value using a constant of the graphics_drivers enumeration type.
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.
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.
/* Sample program to draw a circle*/
int gd=DETECT,gm;
initgraph(&gd,&gm,””); /* initialization of graphic mode */
closegraph(); /* Restore orignal screen mode */
/* End of program */
The graphics programming in c language is discussed in brief to provide an over view to the beginner.
Normally the screen which you view in DOS is in the text mode which means it is meant for text. And for graphics you need to initialize graphics mode. And for this to happen you 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 you come out, to DOS you will see a different screen, which is not in the text mode.
/*A program to draw a space with stars*/



int gd=DETECT,gm;

int i,x,y;















/* End of program */

Scroll to Top