Introduction to Graphics with graphics.h and initgraph() Function in C Language

Graphics programming in C language allows programmers to create different graphical objects and shapes such as lines, rectangles, circles, ellipses and arcs. We can also fill colours in graphical objects and create simple animation programs such as a jumping ball, rainbow, moving fan and moving car. In traditional C graphics programming, the graphics.h header file is used to access graphics library functions. It provides functions to draw lines, rectangles, circles, ellipses, arcs and display text in a graphics window. The graphics.h library and BGI graphics system are associated with traditional Turbo C/C++ environments. They are not part of the standard C language or its standard library. Modern compilers such as GCC do not normally provide graphics.h by default. A compatible graphics library or an appropriate graphics environment is required.

C लैंग्वेज में Graphics Programming के द्वारा Programmer विभिन्न Graphical Objects और Shapes जैसे Lines, Rectangles, Circles, Ellipses तथा Arcs तैयार कर सकते हैं। हम Graphical Objects में Colours भी भर सकते हैं तथा Jumping Ball, Rainbow, Moving Fan और Moving Car जैसे सामान्य Animation Programs बना सकते हैं। Traditional C Graphics Programming में Graphics Library के Functions का उपयोग करने के लिए graphics.h Header File को Include किया जाता है। इसमें Lines, Rectangles, Circles, Ellipses, Arcs बनाने तथा Graphics Window में Text Display करने के लिए Functions उपलब्ध होते हैं। graphics.h Library तथा BGI Graphics System का उपयोग मुख्यतः Traditional Turbo C/C++ Environment में किया जाता है। ये Standard C Language या इसकी Standard Library का हिस्सा नहीं हैं। GCC जैसे Modern Compilers में सामान्यतः graphics.h पहले से उपलब्ध नहीं होती। इसके लिए Compatible Graphics Library या उपयुक्त Graphics Environment की आवश्यकता होती है।

Graphics Header File in C

The graphics.h header file contains declarations of graphics-related functions, constants and other definitions used by the traditional BGI graphics library. To use these functions, we include the header file at the beginning of a graphics program.

Traditional BGI Graphics Library में Graphics से संबंधित Functions, Constants तथा अन्य Definitions उपलब्ध होते हैं। इन Functions का उपयोग करने के लिए Graphics Program की शुरुआत में graphics.h Header File को Include किया जाता है।

Syntax:

#include <graphics.h>

initgraph() Function in C Graphics

The initgraph() function is used to initialize the graphics system in traditional BGI graphics programming. It loads the required graphics driver, initializes the graphics mode and switches the program from text mode to graphics mode. It also initializes graphics settings such as the background colour, drawing colour and current graphics position.

Traditional BGI Graphics Programming में initgraph() Function का उपयोग Graphics System को Initialize करने के लिए किया जाता है। यह आवश्यक Graphics Driver को Load करता है, Graphics Mode को Initialize करता है तथा Program को Text Mode से Graphics Mode में परिवर्तित करता है। यह Background Colour, Drawing Colour तथा Current Graphics Position जैसी Settings को भी Initialize करता है।

Syntax of initgraph()

The syntax of the initgraph() function is:

void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);

initgraph() Function का Syntax निम्नलिखित है:

void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);

Parameters of initgraph()

The initgraph() function accepts three parameters:

initgraph() Function में तीन Parameters का उपयोग किया जाता है:

1. graphdriver

It is a pointer to an integer that specifies the graphics driver to be used. In traditional BGI programming, the constant DETECT is used to request automatic detection of the available graphics driver.

यह एक Integer Pointer होता है जो उपयोग किए जाने वाले Graphics Driver को निर्धारित करता है। Traditional BGI Programming में उपलब्ध Graphics Driver का Automatic Detection करने के लिए DETECT Constant का उपयोग किया जाता है।

2. graphmode

It is a pointer to an integer that specifies the graphics mode. When DETECT is used for automatic driver detection, the graphics system selects a suitable graphics mode and stores the detected mode through this parameter.

यह एक Integer Pointer होता है जो Graphics Mode को निर्धारित करता है। जब Automatic Driver Detection के लिए DETECT का उपयोग किया जाता है, तब Graphics System उपयुक्त Graphics Mode का चयन करके इस Parameter के माध्यम से उस Mode को निर्धारित करता है।

3. pathtodriver

It specifies the directory path where the BGI graphics driver files are located. In traditional Turbo C installations, this path usually points to the BGI directory.

यह BGI Graphics Driver Files के संग्रहित होने वाले Directory Path को दर्शाता है। Traditional Turbo C Installation में यह Path सामान्यतः BGI Directory को दर्शाता है।

Example of initgraph() Function

The following statement initializes the graphics system using automatic driver detection:

निम्नलिखित Statement Automatic Driver Detection का उपयोग करके Graphics System को Initialize करता है:

int gd = DETECT;
int gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

Explanation:

  • gd is the graphics driver variable.

  • gm is the graphics mode variable.

  • DETECT requests automatic graphics driver detection.

  • &gd and &gm pass the addresses of these variables.

  • "C:\\TURBOC3\\BGI" specifies the BGI driver directory. This path must be changed according to the actual installation directory.

व्याख्या:

  • gd Graphics Driver Variable है।

  • gm Graphics Mode Variable है।

  • DETECT Automatic Graphics Driver Detection का निर्देश देता है।

  • &gd तथा &gm इन Variables के Addresses Pass करते हैं।

  • "C:\\TURBOC3\\BGI" BGI Driver Directory का Path दर्शाता है। इस Path को अपने Computer में उपलब्ध वास्तविक Installation Directory के अनुसार बदलना चाहिए।

C Program to Initialize Graphics Mode Using initgraph()

The following program demonstrates how to initialize graphics mode and draw some basic graphical shapes using traditional BGI graphics functions.

निम्नलिखित Program में Traditional BGI Graphics Functions का उपयोग करके Graphics Mode Initialize करना तथा कुछ Basic Graphical Shapes बनाना प्रदर्शित किया गया है।

Program:

#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main(void)
{
int gd = DETECT, gm;
int errorcode;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics initialization failed: %s\n",
grapherrormsg(errorcode));
return 1;
}
line(50, 50, 250, 50);
rectangle(50, 80, 200, 160);
circle(300, 120, 50);
ellipse(450, 120, 0, 360, 80, 40);
outtextxy(150, 220, "Welcome to C Graphics");
getch();
closegraph();
return 0;
}

Output: A graphics window displays a line, rectangle, circle, ellipse and a text message.

आउटपुट: Graphics Window में एक Line, Rectangle, Circle, Ellipse तथा एक Text Message दिखाई देता है।

Explanation:

  1. #include <graphics.h> includes the graphics library header file.

  2. #include <conio.h> is used for the traditional getch() function.

  3. gd and gm are variables used for graphics driver and graphics mode.

  4. initgraph() initializes the graphics system.

  5. graphresult() checks whether graphics initialization was successful.

  6. line() draws a line.

  7. rectangle() draws a rectangle.

  8. circle() draws a circle.

  9. ellipse() draws an ellipse.

  10. outtextxy() displays text at the specified coordinates.

  11. getch() waits for a key press.

  12. closegraph() closes the graphics system and returns to text mode.

व्याख्या:

  1. #include <graphics.h> Graphics Library की Header File को Include करता है।

  2. #include <conio.h> का उपयोग Traditional getch() Function के लिए किया जाता है।

  3. gd तथा gm Graphics Driver और Graphics Mode के Variables हैं।

  4. initgraph() Graphics System को Initialize करता है।

  5. graphresult() यह जाँचता है कि Graphics Initialization सफल हुआ है या नहीं।

  6. line() एक Line बनाता है।

  7. rectangle() एक Rectangle बनाता है।

  8. circle() एक Circle बनाता है।

  9. ellipse() एक Ellipse बनाता है।

  10. outtextxy() निर्धारित Coordinates पर Text Display करता है।

  11. getch() किसी Key के Press होने की प्रतीक्षा करता है।

  12. closegraph() Graphics System को बंद करके Text Mode में वापस आता है।

Important Points About initgraph()

  • initgraph() is a function of the traditional BGI graphics library.

  • It initializes the graphics driver and graphics mode.

  • DETECT is used for automatic graphics driver detection.

  • The address operator (&) is used to pass the addresses of the driver and mode variables.

  • The BGI directory path must be correct for the selected graphics environment.

  • graphresult() can be used to check graphics initialization errors.

  • closegraph() is used to close the graphics system.

  • graphics.h is not a standard C header file.

  • initgraph() Traditional BGI Graphics Library का Function है।

  • यह Graphics Driver तथा Graphics Mode को Initialize करता है।

  • Graphics Driver का Automatic Detection करने के लिए DETECT का उपयोग किया जाता है।

  • Driver तथा Mode Variables के Addresses Pass करने के लिए Address Operator (&) का उपयोग किया जाता है।

  • चुने गए Graphics Environment के अनुसार BGI Directory का Path सही होना चाहिए।

  • Graphics Initialization में Errors जाँचने के लिए graphresult() का उपयोग किया जा सकता है।

  • Graphics System को बंद करने के लिए closegraph() का उपयोग किया जाता है।

  • graphics.h Standard C Header File नहीं है।

Computer graphics in C allows programmers to create graphical shapes, display text and develop simple animations. The traditional BGI graphics library provides functions such as initgraph(), line(), rectangle(), circle() and ellipse() to perform graphics operations.

C Language में Computer Graphics के द्वारा Programmer Graphical Shapes बना सकते हैं, Text Display कर सकते हैं तथा Simple Animations तैयार कर सकते हैं। Traditional BGI Graphics Library में Graphics Operations के लिए initgraph(), line(), rectangle(), circle() तथा ellipse() जैसे Functions उपलब्ध होते हैं.

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025