Turbo C graphics programming

To start with graphics programming, Turbo C is a good choice. Even though DOS has its own limitations,  it is having a large number of  useful functions and is easy to program. To implement graphics algorithms,  To give graphical display of statistics, To view signals from any source, we can use C graphics. Here  is  a article to start programming  with  Turbo  C. ‘Run and Learn’ is  our   method.   We  have  used source codes throughout the explanations. Just  execute  them to understand what is happening.

Turbo C has a good collection of graphics libraries. If you know the basics of C, you can easily learn graphics programming. To start programming, let us write a small program that displays a circle on the screen.

/* simple.c
example 1.0
*/
#include<graphics.h>
#include<conio.h>

void main()
{
int gd=DETECT, gm;

initgraph(&gd, &gm, “c:\\turboc3\\bgi ” );
circle(200,100,150);

getch();
closegraph();
}

To run this program, you need graphics.h header file, graphics.lib library file and Graphics driver (BGI file) in the program folder. These files are part of Turbo C package. In all our programs we used 640×480 VGA monitor. So all the programs are according to that specification. You need to make necessary changes to your programs according to your screen resolution. For VGA monitor, graphics driver used is EGAVGA.BGI.

Here, initgraph() function initializes the graphics mode and clears the screen. We will study the difference between text mode and graphics mode in detail latter.

InitGraph: Initializes the graphics system.

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

Remarks: To start the graphics system, you must first call initgraph.

initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver) then putting the system into graphics mode.

initgraph also resets all graphics settings (color, palette, current position, viewport, etc.) to their defaults, then resets graphresult to 0.
Arguments:

*graphdriver: Integer that specifies the graphics driver to be used. You can give graphdriver a value using a constant of the graphics drivers enumeration type.

*graphmode : Integer that specifies the initial graphics mode (unless *graphdriver = DETECT). If *graphdriver = DETECT, initgraph sets *graphmode to the highest resolution available for the detected driver. You can give *graphmode a value using a constant of the graphics_modes enumeration type.

pathtodriver : Specifies the directory path where initgraph looks for graphics drivers (*.BGI) first.  If they’re not there, initgraph looks in the current directory.  If pathtodriver is null, the driver files must be in the current directory.  This is also the path settextstyle searches for the stroked character font files (*.CHR).

closegraph() function switches back the screen from graphcs mode to text mode. It clears the screen also. A graphics program should have a closegraph function at the end of graphics. Otherwise DOS screen will not go to text mode after running the program. Here, closegraph() is called after getch() since screen should not clear until user hits a key.

If you have the BGI file in the same folder of your program, you can just leave it as “” only. you need not mention *graphmode if you give *graphdriver as DETECT.

In graphics mode, all the screen co-ordinates are mentioned in terms of pixels. Number of pixels in the screen decides resolution of the screen. In the example 1.0,  circle is drawn with x-coordinate of the center 200, y-coordinate 100 and radius 150 pixels. All the coordinates are mentioned with respect to top-left corner of the screen.

Basic Shapes and Colors:

Now let us write a program to draw some basic shapes.

/*
shapes.c
example 1.1
*/

#include<graphics.h>
#include<conio.h>

void main()
{
int gd=DETECT, gm;
int poly[12]={350,450, 350,410, 430,400, 350,350, 300,430, 350,450 };
initgraph(&gd, &gm, “”);

circle(100,100,50);
outtextxy(75,170, “Circle”);
rectangle(200,50,350,150);
outtextxy(240, 170, “Rectangle”);
ellipse(500, 100,0,360, 100,50);
outtextxy(480, 170, “Ellipse”);
line(100,250,540,250);
outtextxy(300,260,”Line”);

sector(150, 400, 30, 300, 100,50);
outtextxy(120, 460, “Sector”);
drawpoly(6, poly);
outtextxy(340, 460, “Polygon”);
getch();
closegraph();
}

Here is the screenshot of output:

Output of above program
Output of above program

Here, circle() function takes x, y coordinates of the circle with respect to left top of the screen and radius of the circle in terms of pixels as arguments. Not that, in graphics, almost all the screen parameters are measured in terms of pixels.

Function outtextxy() displays a string in graphical mode. You can use different fonts, text sizes, alignments, colors and directions of the text that we will study later. Parameters passed are x and y coordinates of the position on the screen where text is to be displayed. There is another function outtext() that displays a text in the current position. Current position is the place where last drawing is ended. These functions are declared as follows:

void far outtextxy(int x, int y, char *text);
void far outtext(char *text);

89 Responses to “Turbo C graphics programming”

  1. satheesh

    For run time error of “bgi not initialized ‘use initgraph’ ”

    Ans: simple, just change ‘\’ to ‘/’ that’s it..
    Ex: initgraph( &gd, &gm, “c:/tc/bgi”);
    thats it friends…

    Reply
    • Just read this latest entry and ALL the comments and am marveling at how this journey your on has taken on a &#f2&6;li8e1#8217; of it’s own. I would prefer that this wasn’t happening to you, but beyond all the beautiful art you have already created, you have created something beautiful here. Blessings.Lindy

      Reply
  2. I installed turbo c on an i5 processor-DDR3 RAM-Intel H51 chipset(OEM-model:acer veriton M200-H61)computer.No errors while compiling,but, when i execute the program i get a scrambled screen with different colours, whereas in the old CPU(acer-veriton M200) no such error occurs. I enabled Graphics [email protected]>>Linker>>Library menu. Still not working. I have not installed Video & Chipset driver for this new computer. Does that has anything to do with this ?. On another computer(Intel 945GVSR) also i have not installed VGA driver, but still, i get the desired output. What am i doing wrong ? kindly help me. Any tangible help is appreciated.

    Reply
    • Checking my &#9a3Muric;n card's corners, I think one corner is the Garand and one is the 1911. Not too sure about the others, perhaps '03 Springfield, Winchester M12 or 94, S&W Model 10 (bonus points for 3 inch, RB), Remington 870…

      Reply
  3. sivakumar

    i want print screen in initgraph.bgi graphics programming.pls help me

    Reply
    • “And Aaron, wer39#;&e always working on the balance of this stuff. But we could tell y'all loved the Q&A, which is why we went about an hour overtime :).”You got me pinned Adam! ;)Thanks man!

      Reply
  4. DragonHeart335

    Hi, thanks for the examples above. I have been working for the past month getting VirtualBox running on my Win 8.1 machine and then installed DOS 6.22, Ubuntu Linux and openSUSE Linux. DOS was the most challenging, but it was fun. Little by little I got things working. I found and installed a copy of Turbo C 2.01, but IDE is too dif. to use. Then I found Borland C++ 3.1 (I actually bought that new years ago) and installed that. It is truly the best environment for doing old DOS graphics programming! Like others said, make sure you #include and make sure you write your: initgraph( &gd, &gm, “c:\\tc\\bgi”); like so. Notice the THIRD PARAMETER! It must point to the directory that has the BGI files and it MUST have two slashes (backslashes for DOS, forward slashes for Linux). Have fun!

    Reply
    • I found your blog on google and read a few of your other posts. I just added you to my Google News Reared. Keep up the good work. Look forward to reading more from you in the future.

      Reply
  5. yogesh dange+

    this code gives me linker error undefined _closegraph,circle,initgraph in modual graph.c

    Reply

Leave a Reply