Home > Tutorials > Graphics

 

Graphics in C/C++

 

Graphics in C/C++

As most of us are aware, C++ unleashes the power of creativity to the maximum at the hands of the programmer. Graphics in C++ are just another example. Graphics are a very useful and important part of C++.

  • Many useful text based softwares can be given a better interface to work upon. 

  • Using graphics, many dos-based games can be built.

  • An attractive introduction & a more user friendly look can be given to all kinds of programs. 

  • All analytical softwares can include graphical representation of data, in a more organized manner.

It is first necessary to know a little bit more about C++ graphics - just as much a beginner should know. I will try to make things as simple as possible. C++ graphics libraries provide us with the Borland Graphical Interface or BGI. This provides us a working environment of 640x480 resolution in 16 colours. This is a totally different environment from the 80x25 text screen resolution. In this graphical environment each point on the screen is occupied by a pixel. A pixel is somewhat the size of the tip of your pen. To give you a better idea, your screen is horizontally divided into 640 vertical columns, each column is of the width of a pixel. You have the control to give one of the 16 colours to each of the 307200 pixels individually. The screen is divided into two axial lines - from the top left to the bottom left of the screen extend the Y-axis & from the top left to the top right extend the X-axis. As we move towards the right or bottom the X-axis & Y-axis coordinates increase respectively. Each pixel is represented by a X & Y coordinate (X,Y). The topmost & leftmost pixel is (0,0) & the bottommost & rightmost pixel is (640,480). Now you might be tempted to ask that how many kangaroos do actually live in Australia, but that's besides the point! You can also initiate non BGI environments in C++ (like svga.h in header files section), but none of what I said here is necessary to be applicable to them.

You are now ready for a step by step instructional guide to initiate graphics in your program. Make sure you have the *.bgi & *.chr files (found in the BGI folder) in your working directory (the BIN folder). You will have to initialize the graphic libraries by going to options>>linker>>libraries & mark the graphic libraries.

You will have to insert the following piece of code to initiate graphics:

int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"");
errorcode=graphresult();
if(errorcode!=grOk) 
{
cout<<"Graphics error : "<<grapherrormsg(errorcode); 
getch(); exit(1);
}

It is necessary to close the graphics screen when its use is over by inserting: closegraph();

All function calls of graphics.h must be placed after the initgraph() & before the closegraph() calls in your code.
To view the sample code of a graphics program, click here & observe it.
You have now learnt how to use graphics in C/C++! You will now need to learn the use of the functions provided in graphics.h. As the number of functions are very large, I will illustrate an example for you. The remaining functions can be learnt from the C++ help (by pressing Shift+F1 & typing the name of the function) by understanding the use of the required parameters.

line(int x1,int y1,int x2,int y2) : the line function, as you will read in the C++ help, is used to draw a line from the coordinates (x1,y1) to (x2,y2). Its that simple! A function call line(0,0,640,480) draws a diagonal from top left to bottom right. You must always check out the titles int the "see also" at the end of a function's help, as it will help you learn & absorb more knowledge on graphics.

Here are some other important C/C++ Graphics functions: 

circle(); 
ellipse();
rectangle(); 
bar();
floodfill();
cleardevice();
setcolor();
outtextxy();
putpixel();

If you have any further queries, feel free to contact me at turbocpp@email.com.

- Abhishek Sheopory

(Abhishek Sheopory is partner, RAS)

 

[ Back to Home ]


Copyright © 2000  RAS Website Creations, All rights reserved.