Turbo C++ Graphics Programming

Intention of this tutorial is to make you comfortable with the basic concepts in graphics. If you want to learn graphics programming then Turbo C++ 3.0 is good choice. Under this tutorial i will cover some standard library functions, so you can probably figure out the remaining graphics functions on your own. Basically there are two diffrent modes,namely text mode and graphics mode. In text mode it is possibe to display or capture only text in terms of ASCII. But in graphics any type of figure can be displayed,captured and animated.

O First Impressions

To start with graphics programming, you have to set out some of the things on your own like compiler or it’s installation & configuration etc. Example in tutorial wil work no matter which compiler you use, of course some of the library functions have minor variations. I recommend using Turbo C++ 3.0, as it will be readily availbale and it is much easier to use.

O Requirement

If you want to start graphics programming then you need two files which are GRAPHICS.H and GRAPHICS.LIB. These files are provided as part of TURBO C++. Check these files. The graphic mode functions require a graphics monitor and adapter card such as CGA,EGA and VGA. I assume you have complete working compiler so here’s how to create circle on the screen.


/* Simple example to draw circle */

#include"graphics.h"

#include"conio.h"

void main()

{

int gd=DETECT,gm;

initgraph(&gd, &gm, "c:/tc/bgi ");

circle(330,180,100);

getch();

closegraph();

restorecrtmode();

}

O Code explaination

The first line to look at is: GRAPHICS.H ,this file contains definitions and explaination of all the graphic functions and constants. While GRAPHICS.LIB file contains standard graphic functions.

Turbo C++ graphic functions have two categaries :Text mode graphic functions and graphic mode functions. Here we are dealing with graphic mode function.so just forget about text mode function right now. To switch from text mode to graphic mode,we have function called as ” initgraph ” .

initgraph : This function initialises the graphic mode. It selects the best resolution and direct that value to mode in variable gm.The two int variables gd, gm are graphic driver and graphic mode respectively. The gm handles value that tells us which resolution and monitor we are using. The gd specifies the graphic driver to be used. In our program we have gd=DETECT means we have passed the highest possible value available for the detected driver. If you don’t want that value then you have to assign the constant value for gd,gm.The ” &” symbol is used for initgraph to pass address of the constants.

Path ( ” C:\\tc\\bgi”) : It specifies the directory path where initgraph looks for graphics drivers (*.BGI) first. If files are not there then initgraph will look for the current directory of your program. If it unable to find wihtin current working directory then it will parse an error. You can leave it blank ( ” ” ) if the *.BGI files are within the working directory.

Circle( ) : Circle function takes X and Y values with respect to top left corner of the screen and third co-ordinate is nothing but radius of circle. In our example we have passed X=330,Y=180 and radius equal to 100 in terms of pixels as arguments.

Closegraph( ) : The closegraph() swithces back the screen from grpahics mode to text mode. If you don’t use this function then you may have undesirable effects.Here this function is called afer the getch() function as screen shouldn’t switch to text mode till user hits any key.

Restorcrtmode( ) : This mode will restore the original video mode detected by initgraph function.

getch( ) : getch( ) function gets a character from console but does not echo it on screen. This is used to pause the screen till user hits any key.

Note:

1) Make sure you have entered the correct path for the include & library directories. You can change the path by pointing your mouse to : Options > Directories. Enter the valid path for the include directory and libraries,and output directories.

2) After installation of Turbo C,you have to adjust the settings of linker. Go to Options>Linker > Libraries> and then check the ” Graphics Library“. This will help to solve the linker errors for the graphics programs. Please do not uncheck any other option already selected by compiler.

3) Graphic initialisation depends on the path mentioned in initgraph path. Be sure to enter slash between c,tc,bgi. The path C & TC depends on user if he installed TC in d: drive then it will be d,tc. Read the above code’s path carefully.

4) If you want help on specific function then point your mouse to “Help> Contents“,and then browse the content for the function you want. If you want fast-help then put the cursor on the first letter of the function or term and press CTRL+F1,it will point you to the help file of that term/function.

Errors and Solutions

1. Screen Goes Blank : This error is related to the BGI configuration. In case of windows XP and 98, 2000 OS you can resolve this by checking all the files in bgi directory. Copy it from someone else if your bgi directory has missing files. Windows Vista and 7 seems to be not supporting old compilers so you have to run them under simulator like Dosbox.

2. Unable to find BGI directory or Linker error: The path given in this tutorial is “C://tc//bgi”. Check it with your own installation. If your installation is on “D://tc://bgi” then update it in your code. Don’t just copy and paste code from here and dream about running the program. Also do check the linker folder path or else it could throw name_of_program.exe error every time you run graphics program.

3. Windows 7 and Turbo C++ 3.0 and 4.5 : As you know DOS based programs have less compatibility under windows 7, your programs are running under XP mode inside windows 7. So don’t expect this or any other old compiler graphics programs to run in one attempt. Besides that DOS programs are not supported under full screen resolution somehow unless you’re running some emulator. You have to run these programs under DosBox or other DOS emulator.

4. DOSBox, Windows 7 and Turbo C++ 3.0 : Dosbox will throw error related to missing CD rom. This is because you’re showing path via command line and Dosbox is searching for installation disk while executing the program. As Turbo C++ 3.0 installation files are circulated in Zip file you have to extract in C folder or else it could throw error related to unknown drive like D: or C: drive “insert disk” error.

If you have any problems related to the Turbo C compiler or if you have queries,suggestion regarding the information provided in this post then please comment it over this thread.

I hope above information helped.If you have any questions then please post them here.Again, any suggestions are welcome.

  • sumit rawat

    hello,
    I am sumit rawat from indore,india. I have Turbo c++ compiler but there is no BGI files present in that folder of TC3.So where that files access by the compiler. I am using windows xp
    thank you.

  • http://onecore.net mahesh

    Usually *bgi files are in the directory that are seperate BGI folder.e.g.”c/tc/bgi”.
    If you dont have the bgi directory in that case chances are may be those are in the working directory of TC.And if you leave the field blank it will look for the working directory.Try this.Still if it doesn’t work ask me i’ll send you another installer of the Turbo C.

  • Mahesh

    Post is updated.Have a look at the notes at the end if it solves your problem.Otherwise post your questions here.