Turbo C graphics programming

Thursday, November 20th, 2008

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);

Pages: 1 2

Avatar Image

Author Name :
Ranjith

Total : 62 Comments


62 Responses to Turbo C graphics programming

  1. gokul

    please send image compression program using C.

  2. shabeeb ahamed

    when i run the followed program, i have got error msg.

    program
    /* simple.c
    example 1.0
    */
    #include
    #include

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

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

    getch();
    closegraph();
    }

    error
    1.undefined symbol _closegraph
    2.undefined symbol _circle
    3.undefined symbol _initgraph.

    and also i cant run any graphic program successfully. these types of errors are showing.

    plz help me to solve this problem.

  3. shabeeb ahamed

    sry, idnt know about image compression program.

  4. bhuvan

    i wanna program which detects circle which is actually read from paint

  5. chaitanya kuamar

    shabeeb ahamed ,just you try this one once.

    #include
    #include

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

    initgraph(&gd, &gm, “” );
    circle(200,100,150);

    getch();
    closegraph();
    }

  6. shahan

    please tell me about the turbo c graphics text explicitly

  7. Sim

    dis was pretty useful… but i have this project to implement the dijkstra’s algorithm for directed graphs and we were required to draw the graph in c. i have this problem in making the arrows for the directed edges as well as placing the edges witout criss crossing

  8. Saranya

    Can any one tell me how to draw rounded rectangle and sphere?

  9. rachit

    i need a program to draw all d figures present in car digital display… like indicators, petrol sign,etc.. can u help me in dis..

  10. Ronnel S. Lim

    Can anyone of you that could help me for the fulfilment of my requirement? I need the program that output like this..

    describing figure
    *it is any polygon figure that contain with string on it.
    *it is combination color with shadow…

    I highly expected your kindness……

  11. vennila.p

    how to correct the error -undefined symbol initgraph(),closegraph()?

  12. mj

    hi!! can i have a sample program for SWITCH CASE..thankS!!!

  13. mj

    you can send it to my email..michaeljordanl@yahoo.com!! thanks..

  14. megha

    I need coding of visual bubble sort in C using graphics

  15. Naresh Kumar

    Can any please give me a program of graph with respect to x and y

    in which we can take the input of x and y and the graph is plotted

    please send me as soon as possible

    nareshkumar326@hotmail.com

    thanking u in anticipation.

  16. maze

    can you help us…?????
    what is the reason why when i run the program that have a graphics…
    the message is only “watch”

  17. maze

    is there a connection in the monitor why turbo c that have graphic cant run….?????

    please badly needed……

    just send 2 my mail

    mazieamolo@yahoo.com

    tnx

  18. nandu

    need some other graphical codes like marquee

  19. Ranjeet Singh

    those who are receiving the message
    linking error
    1.undefined symbol _closegraph
    2.undefined symbol _circle
    3.undefined symbol _initgraph.

    they are needed change some turobo c ide settings as
    1 goto “option” menu of the menu bar
    2 next to linker sub menu
    3 and then to Libraries
    3 put a [X] on graphics library

    and the program would work perfectly fine

  20. shray

    could any 1 tell me how to get to know wat address is to be specified in initgraph declaration i.e. like”c:\\tc\\bgi” my compiler cant include graphics.h. wher as i have graphics .h installed.

  21. vinay

    @shray
    just try this
    in the bgi folder in tc ,copy a file named egavga.bgi
    and paste it in the bin folder ( from where we start c++)
    then u no longer need to put any path in the initgraph function.

  22. aswathy sarath

    pleasa include syntax of each functions

  23. Aakash

    hi 2 all…
    i have some doubt…

    m working in graphical mode…

    in my program i need to clear screen again and again in graphical mode only thats y i cant use closegraph() again and again

    and if i use clrscr() then the page become white..

    so can any1 tell me that how can i clear my screen….

    most probably there is some function for this can any1 tell me that function name or can help me in any way…

    m very thankful 2 u;;

    with regardsss

  24. preethi

    thanks vinay ur post was useful

  25. Ajith

    i need a image compresion program in c…….

    if you having that program means please sent me that program to this email

    e-id: ajithpp1989@gmail.com

  26. corporate signs

    I’m definitely with you on this one.
    at the moment i am studying multimedia design at a little college in Melbourne and am constantly on the pursuit for new concepts and fresh perspectives on this topic. I thank you for putting up your “Turbo C graphics programming | electrofriends.com” page! it delivered some really top shelf information.

  27. Anonymous

    [...] [...]

  28. nxcanhitec

    how do draw sphere in tubo c using wireframe?.help me ! thanhs

  29. Homewood

    Sehr interessant. Kommt hier noch ein weiterer Beitrag? Würde gern einiges mehr darüber erfahren. Kannst du mir per E-Mail weiterhelfen?

  30. carla

    hello
    In turbo c how can i run a message around the edges of the screen???
    please help me..
    may you can give the code plzzz.,
    thank you…
    Morepower!!!!

  31. ashok

    please tell whether turbo c graphics helps to darw a sphere.
    thanking you

  32. satheesh

    please help me to create a drawing in c program. thank you.

  33. niraj

    hey after running this code i m getting error UNABLE TO OPEN FILE “TCLASSS.LIB” plzzzz help me out thisss……

  34. suman phuyal

    i want to make the shape of square,,but i did not know how to make it??

  35. mallikarjun

    those who are receiving the message
    linking error
    1.undefined symbol _closegraph
    2.undefined symbol _circle
    3.undefined symbol _initgraph.

    they are needed change some turobo c ide settings as
    1 goto “option” menu of the menu bar
    2 next to linker sub menu
    3 and then to Libraries
    3 put a [X] on graphics library

    and the program would work perfectly fine

  36. Leogem

    Please help me with my project!.. We are going to draw this image “http://www.google.com.ph/imglanding?q=kurochan&um=1&hl=tl&tbm=isch&tbnid=EJSypCPY8Bpw8M:&imgrefurl=http://www.pinoyexchange.com/forums/showthread.php%253Ft%253D112473%2526page%253D71&imgurl=http://img243.imageshack.us/img243/3356/digimiikun.png&w=579&h=586&ei=1dbCTa_CJJKmvQOI4ajLAQ&zoom=1&iact=rc&page=1&tbnh=178&tbnw=176&start=0&ndsp=18&ved=1t:429,r:11,s:0&biw=1280&bih=641″ to Turbo C++ or Dos Box.. Anyone can do it for me?? please

  37. Zaheer Abbas

    use the path “c:\\tc\\bgi” at the path of “c:\\turboc\\bgi”;

  38. alireza

    the problem won’t be solved with your solution,I have made everything ok,but i think the problem is from graphic card,non of the graphic cards in the bgi file now are not used and are disabled,ofcourse if some1 can help me,i’ll be pleased so much,when i run a graphical program like drawing circle as an easy one,my screen gets black,what is the problem,can you help me???

  39. Jarvis Clark

    How can i create a table in c language?

  40. manoj

    Any problems with linking errors in windows 7 dos box or in xp please contact me 4 solution !!
    passionatecapri@gmail.com

    happy to help..:)

  41. manipriya challa

    we can easily implement programs in c,c graphics is so easy we can also run events also….

  42. varun nayyar

    when i run the followed program, i have got error msg.
    program
    /* simple.c
    example 1.0
    */
    #include
    #include
    void main()
    {
    int gd=DETECT, gm;
    initgraph(&gd, &gm, “c:\\turboc3\\bgi ” );
    circle(200,100,150);
    getch();
    closegraph();
    }
    error
    1.undefined symbol _closegraph
    2.undefined symbol _circle
    3.undefined symbol _initgraph.
    and also i cant run any graphic program successfully. these types of errors are showing.
    plz help me to solve this problem.

  43. sumanta

    to all of them, who r having probs. dude, do u actually know how to write programs in turbo c ?? 1st of all include header files for proper output. eg. #include then define int s gd=0, gm; then use initgraph(&gd,&gm,”..//bgi”);

  44. Sumanta

    include header files graphics.h inside two angular brackets. because of this page it does not support angular brackets.

  45. kepPer

    wanna share my own guys….

    try this code:

    #include
    #include
    main()
    {
    int gd = DETECT,gm;
    initgraph(&gd,&gm,”C:\\TC\BGI”); or initgraph(&gd,&gm,”D:\\TC|BGI”); -because it depends were u save your TC folder..
    cirlce(100,100,50);
    getch();
    closegraph();
    return 0;
    {

  46. Abhishek

    It just a simple linker error goto option->linker->librarian-> and x sign on the graphics
    and enjoy ur program

  47. Bhuvaneshwari

    how to take those screen shots…I could not take that screen shots as a print outs..

  48. raj bharath

    hai in tat progrm ur header file is wrong

  49. ANJU

    i want the program code for fill colours in circle, rectangle& ellipse.

  50. ANJU

    c graphics code for fill colours in circle, ellipse &rectangle

  51. Gaurav

    hi… this gaurav kabra
    those wo havong error for not defined _closegraph
    thi file graphics.h must be included in your program
    you should use the statement as #include”graphics.h”
    also for the error after execution like use ‘initgraph’
    the path of bgi folder is required in the initgraph()
    the folder can be found in tc folder in c: & use’/’ in the path insted ‘\’

  52. amit sarvaiya

    i have tried all the things which are specified above for graphics programming
    m a beginner but after all correction i have one error constantly coming on the execution screen
    after pressing alt f5 it shows
    BGI error:couldnt initialize graphics (use initgraph)

    plz help me………..:’(

    this is the code i have written

    #include
    #include
    void main()
    {
    int gd=DETECT, gm;
    initgraph(&gd, &gm,”E:\TC\BGI” );
    circle(200,100,150);
    getch();
    closegraph();
    }

    help plz………….

  53. Zuni

    Hi,

    I want to draw a graph for the binary value of lenght 1byte in turbo C.
    Kindly help me with the code as I am new to turbo C graphics.

  54. pawan pandey

    @shabeeb ahamed,
    its a linker error buddy. go to option and then linker, and then put [X] to graphics mode…

  55. Sahib SIngh

    Hello , Can anyone tell me how to toggle between Graphics mode and textual mode in c please reply fast.

  56. pavi

    ppl help me out na..
    i need to use bck grnd color n font color so juz tell me hw to use under

  57. pavi

    ssry how to use under

  58. pavi
  59. jins

    pls help me out..

    i need to copy graphics output to word file.. how to take the screen shot..
    prtscr_Sysrq is not working.. pls help me out guys..

  60. Sankumarsingh

    Hi Jins, The print screen key is not working in case you want to take the screenshot. you need to use Dos box, which will be useful for this. Using this you can mount any directory (folder) and run the program in dos box window, then the printscreen key will work.

  61. Purva Hattekar

    I’m using Win7, & I’m unable to run graphic programs under small screen & Win7 doesnt support fullscreen for Turbo C.
    Can U solve my problem… plzzz….!

  62. vinayak wagharalkar

    when i run the following program, i got an error

    program

    #include
    #include
    #include

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

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

    getch();
    closegraph();
    }

    error

    BGI error:graphics not initialized(use”initgraph”)

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup