How to Run First Graphics Program in C or C++?
Here you will learn that, How to run first graphics program in C or C++ language by using two different (Turbo C++ Compiler and Dev C++ Compiler) compilers. I am trying to explain this topic through step by step with images. Hope it will prove helpful to you. Please comment for encouragement to me. Read complete article and learn new things.
How to Run First Graphics Program in Turbo C++
Download and Install Turbo C++ Compiler
For working with Computer Graphics, first you must have Turbo C++ Compiler installed in your computer system.
If you have not then click => Download Turbo C++ Compiler and install the compiler in your system.
Instructions to Run First Program
● Type the following first C program in Turbo C++ Compiler and then save program by
● File-> Save-> Type file name as “cgfirst.c” or “cgfirst.cpp” Then press “Enter” to save the program.
Program Code to draw line of graphics program in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //Draw a Line program in C Graphics #include<graphics.h> #include<stdio.h> #include<conio.h> void main() { int gdriver = DETECT, gmode; int x1 = 100, y1 = 100; int x2 = 300, y2 = 300; clrscr(); initgraph(&gdriver, &gmode, "c:\turboc3\bgi"); line(x1, y1, x2, y2); getch(); closegraph(); } |
● To run this program Click Run => Run [or press CTRL+F9 key]
Output Window
Program Explanation
⮞ First step : graphics program is use to include the graphics.h header file. This header file provides the access of graphics library functions for drawing the lines, circle, rectangles, ovals, polygons and images etc.
⮞ Second step : It is to initialize the graphics system using the initgraph method of graphics.h library.
Declaration :
void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);
graphdriver :- It is pointer to integer that specifies the graphics driver to be used.
graphmode:- It is pointer to integer that specifies the initial graphics mode. When we set graphicsdriver = DETECT then initgraph set graphmode to the highest resolution available for the detected driver.
pathtodriver :- It specifies the directory path where all graphics driver(*.bgi) files are located.
⮞ Third step is used to set the coordinates values
int x1 = 100, y1 = 100;
int x2 = 300, y2 = 300;
⮞ Forth step is used to call parameters to the line function.
line(x1,y1,x2,y2);
Parameter Explanation
x1 – X Coordinate of First Point
y1 – Y Coordinate of First Point
x2 – X Coordinate of Second Point
y2 – Y Coordinate of Second Point
⮞ Final step is used to unload the graphics drivers by calling closegraph function.
How to Run First Graphics Program in Dev C++
Download and Install Dev C++
Add Graphics File in Dev C++
- Download supported files from => Download Link
- unzip compress files.
- Copy “graphics.h” and “winbgim.h” files to “include” folder of Dev C++ directory.
- Copy “libbgi.a” file to “lib” folder of Dev C++ directory.
Instructions to Run First Program in Dev C++ Compiler
➤ Open Dev C++, Click File => New => Project => Press Enter
➤ Select Console Application, C++ Project, Type project name(Which name you want to give), Click OK button
➤ Select File menu, click Save-> Type program name(with .cpp Extention), Click Save
➤ Select Project, click Project Options [or press CTRL+ H]
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32
Program Code to Draw circle in Dev C++
1 2 3 4 5 6 7 8 | //Draw a circle in Dev C++ #include <graphics.h> int main( ) { initwindow(400, 300, "First Sample"); circle(100, 50, 40); getch(); return 0; } |
Output Window
Program Explanation
● First step in any graphics program is use to include the graphics.h header file. This header file provides the access of graphics library functions for drawing the lines, circle, rectangles, ovals, polygons and images etc.
● Second step is to initialize the graphics window using the initwindows() function of graphics.h library.
Declaration:
initwindow(x, y, “Title”);
Example:
initwindow(400, 300, “First Sample”);
In that first parameter (400) is the x coordinates (width), second parameter(300) is the y coordinates(height) of graphics window and third parameter (“First Sample”) is the title of graphics window.
● Third step is used to call the circle function, in that
Declaration:
circle(x, y, r);
Example:
circle(100, 50, 40);
● Final step here getch() function is used to hold the output screen till any key is pressed.