How to Run First Graphics Program in C or C++?

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.

 

graphics program in c
 

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.

download button
Turbo C++ Download

 

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

draw line simple graphics program in c

 

● To run this program  Click Run => Run [or press CTRL+F9 key]

 

draw line simple graphics program in c1

 

 Output Window

computer graphics program in c

 

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++

Download Dev C++ from  its Dev C++ Official Website or SourceForge
Install Dev C++ in your system.
 
 

Add Graphics File in Dev C++

  1. Download supported files from =>  Download Link 
  2. unzip compress files.
  3. Copy “graphics.h” and “winbgim.h” files to “include” folder of Dev C++ directory.
  4. 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

New Project window will be displayed
 
dev cpp new project
 

➤ 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]

 

draw circle in dev cpp

 

➤ Select Parameters Tab, from Project Options, In Linker add the following code (without any spelling error)
 
-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32
 
➤ Click Ok
 
dev cpp project option

 

➤ Select TDM-GCC 4.9.2 32-bit Release From Drop Down.
dev cpp tdm gcc 4.9
 
 ➤All setting done. Now copy the bellow sample program code in window, save it and then compile by Click Execute => Compile and Run [or press F11 key]
 
 

Program Code to Draw circle in Dev C++

 Output Window

dev cpp circle program

 

 

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.

 

Conclusion

Here, we learned that how to run graphics programs using Turbo C++ Compiler and Dev C++ Compiler in C Programming. This was an initial program of Graphics Programming. Hope you would love this code. Please find our other interesting topics and codes on our website.
 
 
 
 
 
 

Leave a Comment

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

Scroll to Top