Home »
C programming language
Graphics in C/C++: Graphics Modes in Turbo C Compiler
Here, we are going to learn about the Graphics mode in Turbo C Compiler and how to use/initialize the graphics mode using C/C++?
Submitted by Mahima Rao, on October 22, 2018
In C / C ++ Advance learning Tutorial, today we are introducing Graphics Mode.
Graphics in C/C++
To create a program in Graphics Mode, the first step would be to include the header file graphics.h. This file is required for Graphics programming. After this, the graphics have to be initialized.
initgraph() Function
C Language supports 16 Bit's MS-DOS environment. Initializing the Graphics mode is to call various functions, one such is called initgraph. Here, we will explain how to use the initgraph function. Have a look the prototype of the function.
void initgraph (int *graphdriver, int *graphmode, char *pathtodriver);
This function takes 3 parameters,
- graphdriver: This is an integer that indicates that the graphics driver has been used.
- graphmode: It is also an integer value that detects the available graphics driver and initializes the graphics mode according to its highest resolution.
- pathtodriver: This is the path of the directory that first searches the initgraph function graphics driver. If the graphics driver is not available then the system searches it in the current directory.
It is necessary to pass the correct value of the three parameters in the initgraph function or else an unpredictable output is obtained.
intgd = DETECT, gm;
initgraph (&gd, &gm, " ");
Initialize Graphics mode
To initialize Graphics mode, you only have to write two lines. Here, we have taken two integer variables 'd' and 'm'.
Here, DETECT is an enumeration type that identifies and identifies the proper graphics driver. The initgraph function has to pass the address of both the variables.
You can see in the example that we have given a space at the position of the third variable. This means that if you do not know the driver's path then you can leave it blank. The compiler will auto-detect the path.
Example
Here is the complete program for the same...
#include <graphics.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
getch();
closegraph();
return 0;
}