Sunday, October 18, 2009

C/C++ Graphics

Hey guys,

            I'm going to give a outline on DOS based graphics in C/C++ with graphics.h which can be implemented in Turbo C/CPP.DOS screen can be of 2 types. Command based or Graphics based. When we are using printf() in C  or cout in CPP , we are mainly using the command console available to us. To use graphics ,we need to call a few functions.we'll now see how to use basic graphics in Turbo C/CPP.

             Any device in computer requires a driver ( a program ) to have communication between device and us ( operating system/ other programs ) . In this case to initiate graphics, we need a graphics driver ( just a file ) called EGAVGA.BGI . BGI stands for Borland Graphics Interface which provided this file to be used in Turbo/Borland editions of C/C++. Let's take an example...

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

setbkcolor(1);
line(0,0,639,479);
getch();
closegraph();
}



Like any in-built (library ) functions require a header file, this also requires one "graphics.h".This file have 
           initgraph()
           setbkcolor()

           line()




initgraph() - It takes 3 parameters.

           1) gdriver = DETECT

              Driver to be used.In this case we are using DETECT ( or 0 ) so that the program takes care of the best available driver. TCPP will use VGA graphics driver.( For this to work we need EGAVGA.BGI )


           2) gmode
               When the driver gets detected, it selects the the graphics mode. This is nothing but the number of colors used and the screen resolution. By default , it'll have  16 colors and 640X480 px. This is maximum possible with VGA graphics.



            Both the variable's addresses are passed as the address fetches the necessary data.


            3) ""
            This argument is the location of the driver file ( in our case EGAVGA.BGI ). Empty quotes is used if the file is located in the current directory. This is best because even when the program( EXE ) is transferred , the program will work as far as the BGI file is present in the same directory of the program.



Now the graphics is initialized and is ready to be used.


setbkcolor(). It takes color as the parameter ranging from 0 to 15.This sets the background color.
Note : If setcolor(0) is used , its set the color to back color (set in setbkcolor ) and NOT BLACK.


            Now the screen is set and background color is also changed.The screen is in 4th quadrant i.e. (0,0) is on the left top of the screen (unlike windows ) and (639,479) is the right bottom of the screen.



line() - It takes 2 sets of coordinates as the parmeter and draw a line between them i.e. line (x1,y1,x2,y2)


Similarly there are other functions to draw various shapes which is available in Turbo C/CPP help inside "graphics.h" 


closegraph()- This is used to close the graphics connection.This frees the allocated memory for graphics and graphics functions cannot be used after this unless initgraph() is called again.
        




Sunday, September 20, 2009

Hello Web!!!

I am G. R . Karthik Prashanth , II yr CSE Sastra University,Tanjore . I was asked to create a blog since I came to this college,but I was too lazy :-) .
But , now I'm starting it at 20:09 20/09/2009...
I'm going to keep this blog updated as much as possible.

P.S.

I'm not so good at writing ( in English ). Plz do correct me if I go wrong....