Skip to content

C Tutorial #3:- C Graphics

March 23, 2012

C Graphics

Introducing Graphics:-

Normally, using printf() , scanf() functions, we see outputs on the screen as normal characters. That is called Textmode. In this textmode, we can’t draw the circles,rectangles and cant do animations. So, In order to draw the paints and do other stuff, our immediate job is to shift from textmode to Graphics mode.

How to shift to Graphics mode??

It is very easy to shift to Graphics mode in C. The only task  to do is Initializing Graphics mode in Our C Program.  The initialization of Graphics in our program is done by only one function

initgraph(int *graphic driver, int *graphic mode, char* path);

We have to pass the Graphic Driver, Graphics Mode and path to the initgraph() function.

Let us see one sample program.

#include<graphics.h>  // for using initgraph()

void main()

{

int gd = 3 , gm = 4;

initgraph(&gd,&gm,”c:\\tc\\cpp”);

}

The above program shows the use of initgraph().

What the values 3 and 4 indicate??

Let us discuss what the first parameter in initgraph() indicate?

To initialise the graphics mode, we need to select the appropriate graphics driver and then install it!!

It is as simple as installing SONY LAN DRIVERS on SONY LAN CARD. If we install SAMSUNG drivers on SONY CARD, then it wont work.

So, our first task becomes selecting the best driver. Here, in TURBOC C, all drivers are given inbuilt.

They are of .BGI extension. Just check in your TC or TURBOC folder. All Graphics Drivers (.BGI Files ) are numbers from 0 to N-1. So, after selecting  our graphics driver, we have to pass  appropriate value to initgraph().

So, in our example, we have passed gd = 3 to initgraph()

i.e 4th Graphic Driver.(numbered from 0 to n-1)

What about 2nd Parameter ??

Here, we have to pass appropriate Graphics Mode number.

What is Graphics mode?

Its just a resolution of monitor. Resolution is total number of PIXELS can be displayed on Monitor.

If a monitor can display, 4 Pixels, then resolution is 4.

Normally, we specify resolution as 800×600 pixels. (Total Pixels are 480000). Here 800 specify length and 600 specify width).

So, A Monitor can display a number of Modes on it! We have to select Appropriate mode.

VARIOUS DISPLAY DRIVER NAMES and appropriate numbers.

CGA     — 1

MCGA     —   2

EGA      —   3

EGA64    —  4

EGAMONO  —   5

IBM8514  —   6

HERCMONO —   7

ATT400   —  8

VGA      —   9

PC3270   —  10

 What is the third parameter?

Here, we have to pass the PATH of BGI files located in our system. So, that initgraph() function loads the specified driver specified by first parameter.

Finally, initgraph() loads the Specified Graphic driver specified in first parameter from third parameter and sets the resolution specified in 2nd parameter.

But, how to get the values of Graphic driver and Graphic mode values??

User detectgraph() function.

Lets us draw the circle as a output.

#include<graphics.h>

#include<conio.h>

main()

{

Int gd,gm;

detectgraph(&gd,&gm);

initgraph(&gd,&gm,” “); // Path is left blank, indicating all .BGI files are in TURBOC folder.

circle(200,200,20);

getch();

}

Here, detectgraph() automatically detects the hardware and inserts correct values into gd, and gm. So, we can directly use in initgraph() function.

Advertisements

From → C Graphics

One Comment
  1. anna plz post the program and files topic also…… and also font changing….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: