Blog

How To Use Graphics Turbo C++

How To Use Graphics Turbo C++

How To Use Graphics Turbo C++

Turbo C++ was a popular integrated development environment (IDE) for C and C++ programming in the late 1980s and 1990s. It included the Borland Graphics Interface (BGI) which provided functions for 2D graphics operations. Though Turbo C++ is now considered outdated, especially for modern application development, it still holds nostalgic value for many, and learning to use its graphics capabilities can be fun.

Here’s a simple example of how to use graphics in Turbo C++:

  1. Setup:
    Before you can use BGI, you have to set the path for the graphics driver and the BGI graphics library.
  2. Sample Program:
#include <graphics.h>
#include <conio.h>

void main() {
    int gd = DETECT, gm; // gd is the Graphics driver, gm is the Graphics mode.

    // initgraph initializes the graphics system
    // by loading a graphics driver from disk.
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

    // Check for error
    if (graphresult() != grOk) {
        printf("Graphics error: %s\n", grapherrormsg(graphresult()));
        return;
    }

    // Draw a line
    line(100, 100, 500, 500);

    // Draw a circle
    circle(320, 240, 50);

    // Wait for user input to close the graphics window.
    getch();
    closegraph();
}
  1. Compilation and Execution:
    You would typically compile and execute this within the Turbo C++ IDE. Once you’ve written the code in the editor, you can compile it (usually with the Alt+F9 key combination) and run it (using Ctrl+F9).
  2. Important Points:
  • Always include the graphics.h header for BGI-related functions.
  • You need to call initgraph() to initialize the graphics mode. Specify the path to the BGI and driver directories. The path "C:\\TURBOC3\\BGI" is just an example. Update it according to your Turbo C++ installation directory.
  • After initgraph(), always check for errors using graphresult().
  • Use BGI functions to draw shapes and perform other graphical operations.
  • Use closegraph() to exit from the graphics mode and return to text mode.
  • Remember that BGI supports only 16 colors in its palette, and you may need to set colors using the setcolor() function.