Borland C Graphics.h Tutorials | How Computer graphics using C graphics library in Programming

Borland C Graphics

If 70% of C programming is inheritance,structure,classes and other theoretical subjects the rest 40% is related with C graphics programming.It is the fun side of programming actually.I was Ignorant about graphics programming in c ,c++ since the day I learned #include<iostream.h> the portion is not usually covered in usual C programming tutorials , there are special books for C graphics programming alone.Nobody will speak about graphics if you go and search for 'learn C' in google.I still believe its necessary to teach about graphics programming with the basic lesson itself.There was nobody there to teach me about about this topic , finally I dug up some c graphics programming books from my college library and mastered the concepts in few days.Its simple than the rest of the OOPS concepts actually.So let's move into work.

Your Screen For C graphics

Borland C graphics.h tutorial

The most basic thing you have to learn before stepping in the C shore of graphics is about the monitor.

The monitor you are staring at right now contains lots of small dots called pixels , when each pixels glow in different colours you see an image.You can imagine it as minute bricks which can change to any colour used in building up the screen you see.

When we initialize a programming for using C graphics.h it converts the whole screen into a matrix, if you don’t know what a matrix in mathematics means ,just understand that your screens top left corner is taken as origin the x-axis increases by width from left to right and the y-axis increases by length from top to bottom.The co-ordinates we enter in each function is mapped according to this order.Now I don’t want to confuse you with further complicated theory.If you need to get indepth technichal knowledge about this topic you can go for C grapics book from amazon i've linked somewhere in the bottom of this page.We will hover straight into the application now.For developing graphics using C we need to do it in three simple steps.

  1. Loading the header file of C graphics.h
  2. Initializing 'graphics driver' and 'grahpics mode'
  3. Using the functions where ever necessary

Loading The Header File

#include<stdio.h>
#include<graphics.h>
void main()
{
}

Loading the header file of C graphics.h

The C graphics.h is the header file which should be included to initalize your computer to start using grahipcs methods and also to initialize the monitor.These kinds of statements before the main program are called preprocessor dierctives.Its very simple as coded below.

Initializing Graphics Form Borland C graphics library

This initialization is done inside the main program.The method initgraph()  is used for this purpose there are three parameters for this , the first two are of integer type and the next is the path which you can leave blank in quoutes to take the default path.The integer parameters are initalized in this method for all the C graphics programs so you just need to memorize it and apply it for all C graphics programming from now on.

Initializing 'Graphics Driver' and 'Graphics Mode' from Borland C graphics library

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode," ");
}

Methods In Borland C Graphics programming


cleardevice()











gotoxy(x,y)










putpixel(x,y,WHITE)








outtextxy(x,y,"HELLO")







rectangle(x,y,int x_width,int y_height)




circle(x,y,radius)




line(x1,y1,x2,y2)




moveto(dx,dy)



lineto(x,y)





ellipse(x-centre,y-center,starting_angle,ending_angle,x_radius,y_radius)



drawpoly(num_of_points + 1, points)




settextstyle(DEFAULT_FONT,HORIZ_DIR,1)





setfillstyle(SOLID_FILL,RED)



setcolor(CYAN)




floodfill(x,y,getmaxcolor())




setviewport(int left,int top,int right,int bottom, int clip)
setviewport(10,10,630,470,1)



itoa(x1,st1,10)



delay(100)




closegraph()

Some Common C Graphics Programming Methods

Well, these are some of the most popular C Graphics Methods used in while graphics programming soon after you learn these you can think of developing small games , puzzles and so on.I'll just give only a small overview on each methods.x,y,x1,y1,x2,y2 etc are all variables you'll have to substitute with numeric values to get working programs.Further clear-cut reference can be obtained from any of the C graphics book available from amazon or your local library(those books would be pretty old and dusty by now). If you find it difficult to view the long C graphics library functions code in the window because the code is too lengthy, then click on the 'view plain' written in small fonts at the top left of the codes box on the left side of this text.

Cleardevice()

Clears all previous graphical outputs generated by the previous programs.It's a good practice to include this method at the starting of each program.

gotoxy()

This will initialize the graphics cusor to the specified co-ordiante.In C gotoxy function is used very frequently to locate the cursor at different locations whenever as necessary.

putpixex()

It will colour the pixel specified by the co-ordinates.

outtextxy()

This method is used to display a text in any position on the screen.The numeric coordinates are substituted for x and y.

rectangle()

Draws a rectangle according to the given parameter x and y are the top-left corner co-ordinates.

circle()

Draws a circle with x,y as the center .

line()

Draws a line as per the given co-ordinates.

moveto()

Cursor is moved from the current location to the specified location dx,dy.These parameters can also be used as incremental values.

lineto()

Draws a line from its current location to the co-ordinate(x,y)

ellipse()

Draws the ellipse with the specified angles and coordinates.

drawpoly()

Draws a polygon with (num_of_points +1) edges.The array 'points'

int points[ ]=(x1,y1,x2,y2,x3,y3...)

settextstyle()

The fonts available are :TRIPLEX_FONT, SMALL_FONT, SANS_SERIE_FONT, GOTHIC_FONT

The direction can be changed as HORIZ_DIR or VERT_DIR

The character size increases from 1 to 10

setfillstyle()

The fill styles available are SOLID_FILL, LINE_FILL, HATCH_FILL, SLASH_FILL etc.

setcolor()

Sets the color

floodfill()

The function fills the enclosed area about x,y with the highest value of the color returned by the getmaxcolor().

setviewport()

This marks a rectangle starting from (10,10) having widht 630 pixels height 470 pixels.The integer specifies this identified area may be clipped. clearviewport() reverse this function.

itoa()

Converts the integer x1into an alphabet, and puts it in st1, which is an array to hold 10 characters.10 is the buffer size.

delay()

Cause a pause in execution of the program 1000ms= 1 second

closegraph()

Terminates all graphics operations and revert the hardware back to the normal mode.

C Graphics Book

If you are really into mastering the C/C++ Graphics I would suggest you to go for only two books:

  • Graphics Programming in C++: Writing Graphics Applications for Windows 98

This is a very well written book on designing your own graphics applications for Windows, no matter what type of application you are wanting to do. Mark Walmsley goes into good detail about designing many different types of applications from simple display program to advanced subjects such as 3D modeling. Mr. Walmsley address many common errors and pitfalls with the included examples.I would recommend this book to anyone who is interested in writing a graphics application.

  • Advanced Graphics Programming Using C/C++

A book is a good source for advanced PC graphics topics currently being used in a wide variety of fields. Stresses a hands-on approach, providing numerous program examples written in C and applicable to any C compiler with correct, ready-to-use and well-described code. Covers ray tracing, used to create realistic 3-D graphics. Includes information on graphical file formats and manipulating digital images. Also focuses on printing screens and images.

Bonus Three Debugging C Tips

This is a small bonus tip from me about debugging C.When you start programming , first few times you would find that debugging is a nasty job but once you learn understand the heart of C everything will be so easy.These are the three most common error which a programmer (mostly novice) come across but yet the get confused in correcting it.

  • I always have a confusion while using the special characters for new line(\n) , tab space(\t) etc whether its /n or \n.To easily remember that keep " %"- this symbol in mind.Just type the slash in the opposite direction.
  • The most common errors while for a novice programmer is that he forgets to put the semicolon ";" at the end of a statement.
  • Another tricky error which frequently occurs is that you forget to put the "&" symbol while taking an input with scanf in 'C'.This wont generate any error messages but the you will get strange results as output. This is because all the arguments pointers pointing to the memory.The scanf function will not perform correctly because it will be attempting to overwrite the wrong sections of memory, rather than pointing to the memory location of the variable you are attempting to get input for.

Was This Article Helpful?

If this article was helpful for you, vote it up by clicking the beautiful green oval a few paragraph below. Your experience may be a lesson for others also. Do share your incredible insights about More Graphics Tutorials, Tips & Tricks For C on the comment box below.

More by this Author


Comments 21 comments

NoRR4Me profile image

NoRR4Me 6 years ago

Nanospeck, nice hub. In the first sentence you said 40% is graphic, but should it be 30% if the other is 70%? That would total 100%.

I don't know anything about C programming, but you should get good traffic to this hub if no one else is helping on this subject and lots of people are searching for it. That is if the search engines rank your hub well. You have everything nicely written and illustrated.

Best wishes.


nanospeck profile image

nanospeck 6 years ago Author

Hi NoRR4Me, I just meant to apply a kind of figure of speech there ;).Graphics grow into Games, OS etc and forms too interactive when programmed perfectly.It exceeds what is 100% of C.Got Me??? Guess So.. Your comment is very encouraging NoRR. I wrote his hub with the main aim to help others coz i had to refer a bit to find about this myself.

Thanks A LoT!!! :)


NoRR4Me profile image

NoRR4Me 6 years ago

Oh ok. You wanted it to say 70% and 40% I got it now. lol.

It's nice to share. Others will appreciate it.

You're welcome. :-)


sparkle 6 years ago

Love your effort!

It has really encouraged me to add more graphics in to my c project.

Cheers!


nanospeck profile image

nanospeck 6 years ago Author

cheeeers sparkle!


vikas 6 years ago

is there any site for best tutorial for computer graphics programming in C++


Peter neck 6 years ago

The source code for programs should be supplied in the manner that they could easily b understood by a dummy man.


nanospeck profile image

nanospeck 6 years ago Author

@vikas: i suggest you buy some books about it vikas, its difficult to find topic about graphcs over the internet. but still i would search over and let u know soon.


nanospeck profile image

nanospeck 6 years ago Author

@peter : Thanks for the suggestion peter. actually, this tutorial is an 'intermeddiate' level. I belive a person who is somewhat familiar with the programming language would find it easy to follow.. I also didn't want to reproduce things already available on the internet. still i'll try to improve. Thanks again.


sushanta 6 years ago

hi this help me a lot like i was waiting for this knowlage for last few months this site help me a lot i cant express my happynes through these few words


razor 6 years ago

how u display an image??


banu 5 years ago

its very useful to me .thank u


chiste 5 years ago

"you will get strange results as output with no reason."

if you don't understand how reference[&] works, you shouldn't call them "strange results"


nanospeck profile image

nanospeck 5 years ago Author

Hi chiste.. i've mentioned that it's for the novice programmers. I didn't want to confuse them with pointers as this article was not meant only to be a graphics tutorial. However since you found it less explaining i've added the necessary details. Hope you find this article helpful!

Thanks.


Karan 5 years ago

Hi,

How to get rid of BGI error. I tried running a program which shows a circle but Borland C++ 5.02 gives an error which says "BGI graphics not supported under Windows"

Plz help


Abhishek 5 years ago

nice job dude


brumboy profile image

brumboy 5 years ago from UK

It is also possible to link in the graphics library so that it is contained within the program itself. This can be found in the Borland C++ users guide.

Saves the program from looking for a separate graphics.lib file!


bss 4 years ago

how to change the origin frm topleft corner???????

plzzzzzzz reply soon


sem jen 4 years ago

Really, appreciable efforts and a very handy material for the beginners for CG....

Thnaks!


Java Programs profile image

Java Programs 4 years ago from India

Highly impressed by the effort for writing this hub .... excellent tutorials ..... thanks for such a wonderful hub .... keep the good work up ..... thanks .....


ss005 4 years ago

If I am correct, bcc5.5 does not have "graphics.h" header file unlike from tc3.0, then what is the alternative of "graphic.h" in bcc5.*?

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working