How to Create Graphical Applications using Turbo C++ 3.0 IDE (BGI library)

By | July 9, 2015
  BGI (Borland Graphics Interface) is a graphics library which provides several functions which can be used to create graphical applications. It is a 16-bit library and the programs based on this library will work on 16-bit and 32-bit systems.

How to Create Graphical Applications using Turbo C++ 3.0 IDE

Here, we’ll show you how to create graphical applications using Turbo C++ 3.0 IDE and the BGI library. The header file “graphics.h” will be used to access the functions of this library.
Before we discuss more, you can see some of our applications created using BGI.

Structure of a typical graphics program using BGI


#include <graphics.h>
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

// Other header files (if any required)

int main()
  int gd=DETECT, gm, errorcode; // gd=DETECT means we're requesting auto-detection of graphics drivers
  initgraph(&gd, &gm, "c://turboc3//bgi"); // initgraph() initializes Graphics mode
  errorcode = graphresult();

/* graphresult() tells about the result of initialization i.e. whether it was successful or any error(s) occurred. */

  if (errorcode != grOk)
// An error has occurred, therefore the following statements will be executed
    cout<<"Graphics error :: "<<grapherrormsg(errorcode);
    cout<<"n press any key to halt: ";
// Insert the code to perform the actual tasks here
  return 0;

A brief description of the above program

In the above program, the function initgraph() initializes the graphics system.

The first parameter gd specifies the graphics driver to be used. The second parameter i.e. gm specifies the initial graphics mode. If gd = DETECT, initgraph() sets the graphics mode to the highest resolution available for the detected driver.

The last parameter i.e. “c://turboc3//bgi”  specifies the directory path where initgraph() looks for graphics drivers (files with .bgi extension). This path may differ, depending upon your Turbo C++ installation. 

For example, it may be “c://tc//bgi” or “d://tc//bgi” depending upon the location of installation and the name of the Turbo C++ folder. You should find the appropriate path of this folder manually before using it in the initgraph() function.

The function graphresult() returns the error code for the last graphics operation that reported an error, then resets the error value to grOk. Therefore we must store the value returned by graphresult() in a variable and then test it to know whether an error was returned (and if there was an error, to know more about it).

The function grapherrormsg() takes errorcode as its argument and returns a pointer to the error message string associated with that value of errorcode.

For example, a sample error message is displayed below :

An error occurred while trying to initialize graphics mode

The function closegraph() closes the graphics mode and deallocates all the memory space allocated by this mode.
The function restorecrtmode() restores the screen mode to the initial setting i.e. the one before using initgraph().
Following table lists all the graphics drivers available :
Available Graphics drivers

The functions present in this library have been discussed in subsequent articles.

BGI library functions – Part 1 (Introduction)

Did you like this article? Tell us in your comments below.
(Visited 1,729 times, 1 visits today)

2 thoughts on “How to Create Graphical Applications using Turbo C++ 3.0 IDE (BGI library)

Leave a Reply

Your email address will not be published. Required fields are marked *