Coders Packet

Using Graphics in C++ to create Birthday Cake

By Pratiti Prashant Pande

Today we are going to see how we can use Turbo C++ in Graphics mode by learning some Graphic functions

To use TurboC in Graphics mode we first have to include the header file. This header file provides access to the graphics library that makes it possible to draw some standard shapes and also to make use of colors in our program
The next thing is to initialize the graphics driver on the system which is done using the initgraph() function. This function initializes the graphic mode on the system
The initgraph function has 3 arguments:
void initgraph(int *graphicDriver, int *graphicMode, char *driverDirectoryPath);
GraphicDriver tells the compiler which graphics driver to use or to automatically detect the driver. For auto-detection of the graphicDriver, we use DETECT macro of graphics.h library
GraphicMode specifies the graphics mode to be used. If *gdriver is set to DETECT, then initgraph sets *gmode to the highest resolution available for the detected driver.
driverDirectoryPath specifies the location where the BGI files are installed. This location will vary from system to system depending on where the user has stored it.
For ex the location can be "C:\\turboc3\\bgi"
(BGI is Borland Graphics Interface)
If location is not specified then it will search for driver files in the current working directory 

Closegraph(): This is used to come out of the graphics mode.

This is the basic structure that we have to follow for using TurboC++ in Graphics Mode.

Now let us see how we can draw some basic shapes in graphics mode
1) line()
The line function is used to draw a line
Syntax is line(int x1,int y1,int x2,int y2);
where x1,y1: are coordinates of first point
where x2,y2: are coordinates of second point.
On joining these points we get a line. The coordinate system is similar to what we have learned in our school time

2) circle()
The circle function is used to draw a circle
Syntax is circle(int x,int y,int r);
where x and y: coordinates of the center
r: radius of the circle

The rectangle function is used to draw a rectangle
Syntax is rectangle(int left,int top,int right, int bottom);
where left: x coordinate of the top left corner, top: the y coordinate of the top left corner, right :x coordinate of the right bottom corner,
bottom:y coordinate of the right bottom corner.

4) ellipse()
The ellipse function is used to draw an ellipse
Syntax is ellipse (int x,int y,int stangle,int endangle,int x radius,int y radius);
where x,y: are coordinates of the center
stangle,endangle: starting and ending of the angles (0,90,180,270,360)
x radius,y radius: the radius decides the shape of the ellipse. Whether is it going to be horizontal or vertical or equally spread
This function can be used to draw a full ellipse (by varying angle from 0 to 360) or we can also draw just an arc (by varying stangle and endangle between 0 to 360)

5) fillellipse()
The fillellipse function is used to draw a complete ellipse and fill it with colors/patterns
Syntax is fillellipse(int x,int y,int ,int x radius, int y radius);


This was about drawing patterns in Graphics mode. Now, if we want to print some message we use the following two functions
1) outtext()
This function is used to print a message on the current position
Syntax: outtext("inside this, it could be string or variable);

This function is used to print a message on the specified coordinates
Syntax: (x,y,"String/variable");

3) We have one more function to set the font, direction, and size of the text which is the settextsyle()
Syntax: settextsyle( int font, int direction, int charsize);
Note: This function needs to be called before the outtextxy() function, otherwise there will be no effect on text and output will be the same.


Colors in the graphics mode:
We know that the color of the text or shape by default appears in white and the background color is black. We can set colors by using some functions
The total number of colors available depends on the current graphics driver and mode. 

1) setcolor ()
This function is used to assign a color to text or shape
Syntax: setcolor(Name of color in all capital); or setcolor(value of color);

This function is used to set the background color
Syntax: setbkcolor(Name of color in all capital); or setcolor(value of color);

3)int getcolor()
This function returns the current drawing color that is set in integer type
Eg. int a;

4)int getbkcolor()
This function returns the current background color that is set in integer type

5)int getmaxcolor()
This function returns the maximum color value in integer type

This function sets different patterns in a shape of a particular color

It depends which colors are supported depending on the graphic driver that is installed.
To know this open TurboC++ --> Help--> Header files-->conio.h-->scroll a little bit down-->Colors and we can see a list of colors that we can use

To know more about functions used in  Graphics mode open TurboC++ --> Help-->Click on Contents-->Functions -->Graphic routines
In this, we have a list of functions and their description.

So, now we have learned some basic functions used in the Graphics Mode. Based on this let us develop a C++ program for Graphics Mode

Nowadays, online greetings are trending!!
So, let us write a program to wish happy birthday to our family members, friends, relatives, colleagues, using C++ graphics
The explanation of the code is given in the source code file

Download project

Reviews Report

Submitted by Pratiti Prashant Pande (PratitiPande10)

Download packets of source code on Coders Packet