How to use graphics.h in turbo C++ by C or C++ Language

graphics library use in turbo C++ by C or C++

Introduction :

Using functions of graphics.h library you can create interactive and visually appealing graphical applications such as games, animations, and other graphical user interfaces in Turbo C++. This library allows programmers to draw various shapes and figures on the screen, such as lines, circles, rectangles, and more. In this article, I shall discuss you how to use graphics.h in Turbo C++ to create graphics-based applications. Here I show you a step-by-step guide to help beginners get started with graphics programming in Turbo C++.

What is graphics.h :

Graphics.h is a simple and easy-to-learn header file used in C or C++ programming language to create graphical applications. In graphics.h library, there are set of functions that allow us to draw lines, circles, rectangles, and more shapes. Graphics.h is a specific library for the Turbo C++ compiler and may not be available in other C or C++ compilers.
Graphics.h is mainly used in the education sector to teach students the basics of graphics programming. It is also used to create graphical applications for hobby and fun.

Step-by-step guide for use graphics.h in Turbo C++ :

To use graphics.h in Turbo C++, you first need to open the turbo C++ ide. If you not install turbo C++ in your pc, You can install turbo C++ from here. After opening turbo C++, create a new file by select “File” from menu bar then select “New” for create a new file.

new file for for graphics program in turbo C++

After that select “Save as” for save the new file with name like “graphicpac.c”.

save new file for graphics program in turbo C++

To use graphics.h, you need to include its header file at the beginning of your source code. To do this, select “Options” and then select “Linker” in the menu bar. In the “Linker” window, select “Libraries”.

set graphic library for graphics program in turbo C++

Then check “Graphics library” in the list of libraries and click “OK” to close the “Linker” window.

save graphic library for graphics program in turbo C++

Now, you can use the graphics.h header file in your program by including it at the beginning of your source code. After that, type the following line of code in the beginning of your code.

#include <graphics.h>

Initialize graphics.h by initgraph() function :

First you need to initialize graphics to draw any graphics on the screen. For this, you need to call the initgraph() function. The initgraph() function takes three arguments the graphics driver, graphics mode, and path to the bgi file.

int graphic_driver = DETECT, graphic_mode;
initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");

Here, the DETECT parameter automatically detects the graphics driver, graphic_mode sets the graphics mode to default and the last parameter for the path to the bgi file tells the compiler to use the bgi path of pc.

How set bgi path of initgraph() :

In turbo C++, the initgraph() function is used to initialize graphics and set up the graphics window. One of the parameters of this function is the path to the bgi file, which is necessary for turbo C++ to show graphics program on the screen.
To set the bgi path of initgraph(), you need to provide the path to the bgi driver file in the last parameter of the initgraph() function. Here’s an example:

initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");

In this example, the path to the bgi driver file is “//turboc3/bgi”. You need to replace this with the path where the bgi driver file is located on your pc.

set bgi path for graphics program in turbo C++

Note that the backslashes in the path need to be escaped with another backslash, so that Turbo C++ can correctly interpret the path.

After initializing graphics, you can use functions such as line(), circle(), rectangle() to draw various shapes and figures on the screen.
After you have finished drawing graphics by different functions of graphics.h library, you need to close graphics by calling the closegraph() function. Here getch() use for take a character before close the graphics.

getch();
closegraph();

Once you have finished your code, you can compile and run it to see the output. For compile your program, first select “Compile” from the menu bar and also select “Compile”.

compile graphics program in turbo C++

If you not get any error after compile, then you can run the program. Now for run your program, select “Run” from the menu bar and then select “Run”.

run graphics program in turbo C++

You can copy the below code and paste in your file of turbo C++ to see the output.

/*Develop by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
int main(void)
{
    int graphic_driver = DETECT, graphic_mode;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    setbkcolor(15);
    setcolor(4);
    line(50, 200, 180, 200);
    circle(300, 200, 100);
    rectangle(450, 100, 550, 300);
    getch();
    closegraph();
    return 0;
}

Output :

You can see the below image as output of above code.

output of graphics program in turbo C++ by c language

Conclusion :

In this article, I have discussed how to use graphics.h in turbo C++ to create graphics-based applications. By following the step-by-step guide, you will have a basic understanding of graphics programming and you can easily draw lines, circles, rectangles, and other shapes on the screen. With some practice, you can create more complex graphical applications using graphics.h in turbo C++. Thank for visit my site.

You can see my following project :

You can see my following program :

Scroll to Top