Coupon Accepted Successfully!


Initilisation Of Graphics

The header file graphics.h is to be included at the beginning of the C++ program. This header file comprises the definitions and explanations of all the graphics functions. This file controls many functions and constants that are supported by the display adapters. The programmer can see the details of the graphics mode by writing graphics_mode in turbo c++ editor. After writing graphics_mode, bring the cursor at the start of the graphics_mode and press ctrl+f1 and the enter key to see the details. On the screen, we get the table describing various graphic drivers, their graphic modes, resolutions, palettes, and so on.

The functions described in this chapter are defined in the graphics.h file, which should be included in every graphics program.

To switch from text mode to graphics mode, the function initgraph()should be invoked. It selects the best resolution and puts the corresponding number in gm.

The initgraph()

This function initializes the graphics mode. Its prototype is defined in “graphics.h.” It is declared as per the following format:

void far initgraph (int far *gd, int far *gm, char far *pathtodriver);

where gd is the graphics driver, and gm is the graph mode.

Before starting any drawing action, we need to initialize the graphics mode. The initgraph() function finds out the best graphics mode and sets the corresponding number in the variable gm. Using the value of gm, we can find out the details of the monitor, such as its type, resolution, number of video pages, and supporting colors.

The variable gd is used for graphics drivers. Graphics drivers are a sub-set of device drivers that are used in only the graphics mode. The graphics drivers files have an extension .bgi. Depending on the type of adapter, one of the supporting device driver files is selected. In the graphics mode, the cursor disappears, and the top left corner (0,0) of the console is considered the beginning.

The third parameter in initgraph is path driver (“c:\\tc\\bgi”). It specifies the graphics driver, that is, initgraph(); looks for graphics driver (*.BGI).

22.1 Write a program to switch over from text mode to graphics mode.





int main()


int gd=DETECT,gm;

char x;

cout<<“\n By default display is text mode:”;

cout<<“\n Press character ‘g’ to switch over to graphics mode:”;





outtextxy(40,60,“This is graphics mode”);





cout<<“\n Sorry You are in text mode:”;



return 0;



By default display is text mode:

Press character ‘g’ to switch over to graphics mode: g

This is graphics mode

Explanation: On compiling and running the program, the screen will be in the text mode. The screen will show the first two lines as shown in the o/p of the program. After pressing the character ‘g,’ the screen will move from the text to the graphics mode. The message displayed on the screen is “This is graphics mode” at (40,60) x,y co-ordinates, respectively. The output is in white with a black background. This effect can be observed after running the program.

Test Your Skills Now!
Take a Quiz now
Reviewer Name