Graphics Libraray in C/C++ Programming
Graphics library provided by borland C is most widely used library for graphics programming. Mostly this graphics library is restricted to be used under 16 bit C programming and MS DOS environment. As discussed earlier that first of all you need to initialize the graphics drivers on the computer. This is done using the initgraph() method provided in graphics.h library. graphics.h is used to include the reference to the graphics library, but actual graphics library can be found in lib directory with the name of graphics.lib. In Dev C++ there is no default graphics library, but there are some third party graphics libraries to be used with Dev C++. You can download this library here, after downloading the library you can read through the manual and copy all the files at their desired locations. Also you will have to add reference to this graphics library in your projects or programs.
Adding the graphics.h in your C programs
If you are using borland C/C++ compiler to program, then you will have to follow these simple steps in orderd to get your C or C++ program running in graphics mode.
- First you will need to add #include <graphics.h> reference at the top of you C/C++ program.
- Next step is to initialize the graphics environment. This can be achieved using the function
void far initgraph(int far *driver, int far *mode, char far *path)
- Here path is the actual path to the graphics library. You can give the path to this library by giving the complete path, like “C:\\TC\\BGI”, where BGI is the graphics library directory.
- Afeter initializing the graphics mode you can check for any error which may happen while initializing the graphics mode. The function used to find out any errors is int errorcode = graphresult() which returns the error code for the specific error.
- If you pass this errorcode to grapherrormsg() function the, it will return the complete description of the error message. And if the errorcode==grOk the you are on the way to develop your first graphics program using C/C++.
If you are using Dev C++ compiler for your graphcis programs then you can follow these simple steps to configure your environment for graphics programming.
Sinple graphics functions
I am providing few functions here with some description to make you start with graphcis programming. I have posted the complete description with a demo C/C++ graphics program at my C and C++ Programming Blog. You can find my blog here, there are three parts of the posts about graphics.h library. First part of the post discusses the necesary functions to get the program ready to draw shapes and styles, second post provides description of necessary functions to draw different shapes and fill them with colors or textures. And the last part is a sample program to demonstrate that how you can write programs in graphics mode.
Draws the circle on the screen using a point(x,y) and the radius for the circle.
void far circle(int x, int y, int radius)
This function draws a line in the graphics mode.
void far line(int startx, int starty, int endx, int endy)
Displays a text string on the grpahics screen.
void far outtext(char far *str)
Displays the text at a specified position in graphics mode.
void far outtext(int x, int y, char *str)
Draws a rectangular box on the screen using the points given in the parameters.
void far rectangle(int left, int top, int right, int bottom)
Leave a Reply
You must be logged in to post a comment.