You are on page 1of 26

Introduction to graphics

programming in c
Nihar Ranjan Roy

What is computer graphics?


The pictorial representation and manipulation of data by a
compute
The creation, display, and storage of pictures, with a
computer

Computer graphics is a sub-field of computer science


which studies methods for digitally synthesizing and
manipulating visual content. Although the term often refers
to the study of three-dimensional computer graphics, it also
encompasses
two-dimensional
graphics
and
image
processing
Nihar Ranjan Roy
https://sites.google.com/site/niharran

Graphics Mode and

Text Mode

80x25

640x480

Cursor appears n(by default)

Cursor Disappears (by default)


Precise access to screen locations

Nihar Ranjan Roy


https://sites.google.com/site/niharran

How to change the mode?


first call the initgraph() function.

void initgraph ( int *graphdriver,


int *graphmode,char *pathtodriver);
initgraph loads the graphics driver and puts the system into
graphics mode.
You can tell initgraph to use a particular graphics driver and
mode, or to autodetect the attached video adapter at run time
and pick the corresponding driver.

Nihar Ranjan Roy


https://sites.google.com/site/niharran

Simple graphics program?


#include<graphics.h> //contains graphics
//functions

void main()
{
int gd=DETECT,gm; //graphics driver & mode
initgraph(&gd,&gm,"c:\\tc\\bgi"); //initialise
circle(320,240,100); //draw circle(Cx,Cy,radius)
getch();
}

Nihar Ranjan Roy


https://sites.google.com/site/niharran

Graphics Driver?
*graphdriver is an integer that specifies the graphics driver to be
used. You can give it a value using a constant of the
graphics_drivers enumeration type, which is defined in
graphics.h and listed below.
graphics_drivers
Numeric value
constant

graphics_driver
s constant

Numeric value

DETECT

0 (requests
autodetect)

EGAMONO

IBM8514

CGA

HERCMONO

MCGA

ATT400

EGA

VGA

EGA64

PC3270

10

Nihar Ranjan Roy


https://sites.google.com/site/niharran

Graphics Mode
*graphmode is an integer that specifies the initial graphics mode

Driver

graphics_mode

Value

x Rows

CGA

CGAC0

320 x 200

C0

CGAC1

320 x 200

C1

CGAC2

320 x 200

C2

CGAC3

320 x 200

C3

CGAHI

640 x 200

2 color

Nihar Ranjan Roy


https://sites.google.com/site/niharran

Palette

Pages

CGA color palettes


Palette listings C0, C1, C2, and C3 refer to the four predefined fourcolor palettes available on CGA (and compatible) systems. You can
select the background color (entry #0) in each of these palettes, but
the other colors are fixed.
Palette
Number

Three Colors

LIGHTGREEN

LIGHTRED

YELLOW

LIGHTCYAN

LIGHTMAGENTA

WHITE

GREEN

RED

BROWN

CYAN

MAGENTA

LIGHTGRAY

Nihar Ranjan Roy


https://sites.google.com/site/niharran

Driver

graphics_mode

Value

x Rows

Palette

Pages

MCGAC0

320 x 200

C0

MCGAC1

320 x 200

C1

MCGAC2

320 x 200

C2

MCGAC3

320 x 200

C3

MCGAMED

640 x 200

2 color

MCGAHI

640 x 480

2 color

EGALO

640 x 200

16 color

EGAHI

640 x 350

16 color

EGA64LO

640 x 200

16 color

EGA64HI

640 x 350

4 color

EGA-MONO EGAMONOHI

640 x 350

2 color

1 w/64K

EGAMONOHI

640 x 350

2 color

2 w/256K

HERCMONOHI

720 x 348

2 color

MCGA

EGA
EGA64

HERC

Nihar Ranjan Roy


https://sites.google.com/site/niharran

Driver
ATT400

graphics_mode

Value

x Rows

Palette

Pages

ATT400C0

320 x 200

C0

ATT400C1

320 x 200

C1

ATT400C2

320 x 200

C2

ATT400C3

320 x 200

C3

ATT400MED

640 x 200

2 color

ATT400HI

640 x 400

2 color

VGALO

640 x 200

16 color

VGAMED

640 x 350

16 color

VGAHI

640 x 480

16 color

PC3270

PC3270HI

720 x 350

2 color

IBM8514

IBM8514HI

640 x 480

256 color

IBM8514LO

1024 x 768

256 color

VGA

Nihar Ranjan Roy


https://sites.google.com/site/niharran

Return Value Of Initialization Process


initgraph always sets the internal error code; on success, it sets the
code to 0. If an error occurred, *graphdriver is set to -2, -3, -4, or -5,
and graphresult returns the same value as listed below:

Constant Name

Number

Meaning

grNotDetected

-2

Cannot detect a graphics card

grFileNotFound

-3

Cannot find driver file

grInvalidDriver

-4

Invalid driver

grNoLoadMem

-5

Insufficient memory to load


driver

Nihar Ranjan Roy


https://sites.google.com/site/niharran

Colors in VGA
Here are 16 colors declared in graphics.h as listed bellow.

BLACK

DARKGRAY:

BLUE:

LIGHTBLUE:

GREEN

LIGHTGREEN:

10

CYAN:

LIGHTCYAN:

11

RED

LIGHTRED:

12

MAGENTA:

LIGHTMAGENTA

13

BROWN:

YELLOW:

14

LIGHTGRAY

WHITE:

15

Nihar Ranjan Roy


https://sites.google.com/site/niharran

initgraph(&gdriver, &gmode, ""); /* read result of initialization */


errorcode = graphresult();
if (errorcode != grOk)
/* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}

Nihar Ranjan Roy


https://sites.google.com/site/niharran

Fill Colors
void far setcolor(int color);
//setcolor sets the current drawing color to color, which
can range from 0 to getmaxcolor.

void far setbkcolor(int color);


//setbkcolor sets the background to the color specified by
color.
void far setfillstyle(int pattern, int color);
//setfillstyle sets the current fill pattern and fill color.

Nihar Ranjan Roy


https://sites.google.com/site/niharran

The parameter pattern in setfillstyle is as follows:


Names

Value

Means Fill With...

EMPTY_FILL

Background color

SOLID_FILL

Solid fill

LINE_FILL

---

LTSLASH_FILL

///

SLASH_FILL

///, thick lines

BKSLASH_FILL

\\\, thick lines

LTBKSLASH_FILL

HATCH_FILL

Light hatch

XHATCH_FILL

Heavy crosshatch

INTERLEAVE_FILL

Interleaving lines

\\\

WIDE_DOT_FILL

10

Widely spaced dots

CLOSE_DOT_FILL

11

Closely spaced dots

USER_FILL

Nihar12
Ranjan Roy User-defined fill pattern
https://sites.google.com/site/niharran

Few more Functions


int poly[12]={350,450, 350,410, 430,400, 350,
350, 300,430, 350,450 };
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");
Nihar Ranjan Roy
https://sites.google.com/site/niharran

Nihar Ranjan Roy


https://sites.google.com/site/niharran

Pixel
void far

putpixel(int x,int u,int color);

//set the color of the pixel at location (x,y) with the specified color

Unsigned far getpixel(int x, int y);


//Retreive the color value fo the pixel at location (x,y)

Nihar Ranjan Roy


https://sites.google.com/site/niharran

LINES
line(x1,y1,x2,y2);
//draw a line from (x1,y1) and (x2,y2)
lineto(x2,y2);
//draw a line from current position to (X2,y2)
linerel(x1,y1,x2,y2);

//draw a line from current position to a relative distance of


(X2,y2)

Nihar Ranjan Roy


https://sites.google.com/site/niharran

Current Position (CP)


void far moveto(x1,y1)
//Move the cursor to the new position (x1,y1)

void far moverel(dx,dy)


//Move the cursor to the new position whose relative distance
from current position is (dx,dy)

Nihar Ranjan Roy


https://sites.google.com/site/niharran

Mouse Handling in C
The various mouse functions can be accessed by setting
up the AX register with different values (service number) and
issuing interrupt number 51. The functions are listed bellow
Interrupt
51

51

51

Service
0

Purpose
Reset mouse and get status
Call with AX = 0
Returns: AX = FFFFh If mouse support is available
Ax = 0 If mouse support is not available

Show mouse pointer


Call with AX = 1
Returns: Nothing

Hide mouse pointer


Call with AX = 2
Returns: Nothing
Nihar Ranjan Roy
https://sites.google.com/site/niharran

Get mouse position and button status


Call with AX = 3
Returns: BX = mouse button status
Bit Significance
0
button not pressed
1
left button is pressed
2
right button is pressed
3
center button is pressed
CX = x coordinate
DX = y coordinate

Set mouse pointer position


Call with AX = 4
CX = x coordinate
DX = y coordinate
Returns: Nothing

51

Set horizontal limits for pointer


Call with AX = 7
CX = minimum x coordinate
DX = maximum x coordinate
Returns: Nothing

51

Set vertical limits for pointer


Call with AX = 8
CX = minimum y coordinate
8
DX = maximum y coordinate
Nihar
Ranjan Roy
Returns:
Nothing
https://sites.google.com/site/niharran

51

51

Problem
Write a program in which the
mouse cursor is visible and its limit
is set to (x1,y1) to (x2,y2).

Nihar Ranjan Roy


https://sites.google.com/site/niharran

int callmouse()
{

in.x.ax=1; //show mouse


int86(51,&in,&out);
return 1;

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
union REGS in, out;

}
void restrictmouseptr(int x1,int y1,int x2,int y2)
{
in.x.ax=7; //set horizontal limit for mouse
in.x.cx=x1; //Min X
in.x.dx=x2; //Max X
int86(51,&in,&out);
in.x.ax=8; //set Vertical limit for the mouse
in.x.cx=y1; //min Y
in.x.dx=y2; //max Y
int86(51,&in,&out);
}
Nihar Ranjan Roy
https://sites.google.com/site/niharran

void main()
{
int x,y,cl,a,b;
int g=DETECT,m;
clrscr();
initgraph(&g,&m,"c:\\tc\\bgi");
rectangle(100,100,550,400);
callmouse();
restrictmouseptr(100,100,550,400);
getch();
}

Nihar Ranjan Roy


https://sites.google.com/site/niharran

Few more functions for mouse


handling
int mousehide()
int callmouse()
{
{
in.x.ax=2;
in.x.ax=1;
int86(51,&in,&out);
int86(51,&in,&out);
return 1;
return 1;
}
}
void mouseposi(int &xpos,int &ypos,int &click)
{
void setposi(int &xpos,int &ypos)
in.x.ax=3;
{
int86(51,&in,&out);
in.x.ax=4;
click=out.x.bx;
in.x.cx=xpos;
xpos=out.x.cx;
in.x.dx=ypos;
ypos=out.x.dx;
int86(51,&in,&out);
}
}

Nihar Ranjan Roy


https://sites.google.com/site/niharran

You might also like