Graphics in C language (graphics.h header file functions and examples)

In this article, we will learn the use of 'graphics.h' in language C and will also make some programs based on our learning.
Submitted by Sneha Dujaniya, on June 28, 2018

Color Description in C

setbkcolor sets the background to the color specified by the color or the number. The argument color may be a name or a number as given in the table below. (These symbolic names are defined in graphics.h). These colors can also be used to set textcolor (color of the text) or filling inside various shapes that you make in your program. We shall first learn about the color and their values and then we will learn it via the programs.

Color 				Numeric Value
BLACK				  0
BLUE				  1
GREEN				  2
CYAN				  3
RED				  4
MAGENTA			   	  5
BROWN				  6
LIGHTGRAY			  7
DARKGRAY			  8
LIGHTBLUE			  9
LIGHTGREEN			  10
LIGHTCYAN			  11
LIGHTRED			  12
LIGHTMAGENTA			  13
YELLOW				  14
WHITE				  15

Sample Graphics programs in C

1. Background color

#include<graphics.h> /* header file */
#include<conio.h>
 
main()
{
 /* the following two lines are the syntax for writing a particular
 program in graphics. It's explanation is given after the program.*/

   int gd = DETECT, gm;
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   
   setbkcolor (GREEN);
 
   getch();
   closegraph();
   return 0;
}

Output

Graphics.h Header function example 1


What are initgraph, gd and gm?

  • gd = graphdriver;
  • gm = graphmode;

Syntax for initgraph:

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

Description for initgraph:

initgraph

initgraph is used to initialize the graphics system by loading a graphics driver from disk and thereby putting the system into graphics mode.

To start the graphics system, we first call the initgraph function. initgraph may use a particular graphics driver and mode, or it may auto-detect and pick the corresponding driver at runtime, according to our needs.

If we tell initgraph to autodetect, it calls detectgraph to select a graphics driver and mode. It also resets all graphics settings to their defaults values like current position, color, viewport and so on and also resets graphresult to 0.

Normally, memory is allocated by initgraph to load a particular graphics driver through _graphgetmem, then it loads the appropriate BGI file from disk.

pathtodriver

pathtodriver denotes the directory path where initgraph must look for graphic drivers. initgraph first goes through the directed path to look for the files and if they are not found there, it goes to the current directory. The graphic driver must files must be present in the current directory if the pathtodriver is null.

graphdriver

*graphdriver is the integer that specifies which graphics driver is to be used. We can give it a value using a constant of the graphics_drivers enum type, which is defined in graphics.h and listed below.

graphics_drivers constant    	Numeric value
DETECT	                        0 (requests autodetect)
CGA	                        1
MCGA	                        2
EGA	                        3
EGA64	                        4
EGAMONO	                        5
IBM8514	                        6
HERCMONO	                7
ATT400	                        8
VGA	                        9
PC3270	                        10

graphmode

*graphmode is also an integer that specifies the initial graphics mode. The table for the values of *graphmode are given in the tlink below and its values are assigned in the same way as for *graphdriver.

graphdriver and graphmode must be given proper values from the tables or we will get absurd and unexpected results. The exception here is when graphdriver = DETECT. In this case, initgraph sets *graphmode to the highest resolution available for the detected driver.

Reference: https://www.cs.colorado.edu/~main/bgi/doc/initgraph.html

What is BGI?

Borland Graphics Interface (BGI) is a graphics library that is bundled with several Borland compilers for the DOS operating systems since 1987. The library loads graphic drivers (*.BGI) and vector fonts (*.CHR) from disk so to provide device independent graphics support to the programmers.

BGI is accessible in C/C++ with graphics.lib/graphics.h.

Reference: Borland Graphics Interface

What is closegraph()?

Syntax for closegraph() :

void closegraph (int wid= ALL_WINDOWS);

Description:

closegraph deallocates the memory allocated by the graphics system and then restores the screen to the mode it was in before calling initgraph.

Return Value: Return value is none.

Windows Version of closegraph() :

In windows version of closegraph, there is an optional parameter called the 'wid' which is the window id (returned by initwindow) of the window that is supposed to be closed.

The wid parameter can also take one of the two constant values given below:

  1. CURRENT_WINDOW which closes only the current window or
  2. ALL_WINDOWS which by default closes all the open graphic windows.

By closing the current window, current window will no longer exist and we will not be able to give any further drawing commands until a new window is created or a current window is set by calling setcurrentwindow.

Reference: https://www.cs.colorado.edu/~main/bgi/doc/closegraph.html


2. Text color

#include<stdio.h>
#include<conio.h>
int main()
{
	textcolor(RED);
	clrcsr();
	cprintf("HELLO WORLD\N");
	getch();
	return 0;
}

Output

Graphics.h Header function example 2


3. Circle Graphics

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>

void main()
{
	int gd=DETECT, gm; int i;
	initgraph (&gd,&gm,"c:\tc\\bgi");

	clrscr();

	for(i=0;i<500;i++)
	{
		setcolor(i);
		//coordinates of center from x axis, y axis, radius
		circle(100,100,30+i); 
		delay(30);
		//cleardevice(); //try with and without cleardevice();
	}
	
	getch();
	
	closegraph();
}

Output

The circles will keep on growing till the assigned number.

Graphics.h Header function example 3


4. Hut Graphics

#include<graphics.h>
#include<conio.h>
 
int main(){
	int gd = DETECT,gm;
	initgraph(&gd, &gm, "X:\\TC\\BGI");
	/* Draw Hut */
	setcolor(WHITE);
	rectangle(150,180,250,300);
	rectangle(250,180,420,300);
	rectangle(180,250,220,300);

	line(200,100,150,180);
	line(200,100,250,180);
	line(200,100,370,100);
	line(370,100,420,180);

	/* Fill colours */
	setfillstyle(SOLID_FILL, BROWN);
	floodfill(152, 182, WHITE);
	floodfill(252, 182, WHITE);
	setfillstyle(SLASH_FILL, BLUE);
	floodfill(182, 252, WHITE);
	setfillstyle(HATCH_FILL, GREEN);
	floodfill(200, 105, WHITE);
	floodfill(210, 105, WHITE);

	getch();
	closegraph();
	return 0;
}

Output

Graphics.h Header function example 4

Author's Note:

These programs are made and tested in TURBO C7. This is my request to all the readers to please run the program for better understanding of the code. I'm always there to help in case of any query. Happy Coding!




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.