• No results found

LEC_01_Graphics in c.pdf

N/A
N/A
Protected

Academic year: 2020

Share "LEC_01_Graphics in c.pdf"

Copied!
26
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Nihar Ranjan Roy

2

“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

(3)

Nihar Ranjan Roy

3

80x25 640x480

Cursor appears n(by default) Cursor Disappears (by default)

(4)

Nihar Ranjan Roy

4

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.

(5)

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(C

x,Cy,radius)

getch();

}

(6)

Nihar Ranjan Roy

6

Graphics Driver?

EGAMONO 5 IBM8514 6 HERCMONO 7 ATT400 8 VGA 9 PC3270 10

*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

constant Numeric value

DETECT 0 (requests autodetect)

CGA 1

MCGA 2

EGA 3

EGA64 4

graphics_driver

(7)

Nihar Ranjan Roy

7

Graphics Mode

*graphmode is an integer that specifies the initial graphics mode

Driver graphics_mode Value x Rows Palette Pages

CGA CGAC0 0 320 x 200 C0 1

CGAC1 1 320 x 200 C1 1

CGAC2 2 320 x 200 C2 1

CGAC3 3 320 x 200 C3 1

(8)

CGA color palettes

Nihar Ranjan Roy

8

Palette listings C0, C1, C2, and C3 refer to the four predefined

four-color 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

0 LIGHTGREEN LIGHTRED YELLOW

1 LIGHTCYAN LIGHTMAGENTA WHITE

2 GREEN RED BROWN

(9)

Nihar Ranjan Roy

9

MCGA MCGAC0 0 320 x 200 C0 1

MCGAC1 1 320 x 200 C1 1

MCGAC2 2 320 x 200 C2 1

MCGAC3 3 320 x 200 C3 1

MCGAMED 4 640 x 200 2 color 1 MCGAHI 5 640 x 480 2 color 1

EGA EGALO 0 640 x 200 16 color 4 EGAHI 1 640 x 350 16 color 2

EGA64 EGA64LO 0 640 x 200 16 color 1 EGA64HI 1 640 x 350 4 color 1

EGA-MONO EGAMONOHI 3 640 x 350 2 color 1 w/64K EGAMONOHI 3 640 x 350 2 color 2 w/256K

HERC HERCMONOHI 0 720 x 348 2 color 2

(10)

Nihar Ranjan Roy

10

Driver graphics_mode Value x Rows Palette Pages

ATT400 ATT400C0 0 320 x 200 C0 1

ATT400C1 1 320 x 200 C1 1

ATT400C2 2 320 x 200 C2 1

ATT400C3 3 320 x 200 C3 1

ATT400MED 4 640 x 200 2 color 1 ATT400HI 5 640 x 400 2 color 1

VGA VGALO 0 640 x 200 16 color 2

VGAMED 1 640 x 350 16 color 2

VGAHI 2 640 x 480 16 color 1

(11)

Nihar Ranjan Roy

11

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

(12)

Nihar Ranjan Roy

12

Colors in VGA

BLACK 0

BLUE: 1

GREEN 2

CYAN: 3

RED 4

MAGENTA: 5

BROWN: 6

LIGHTGRAY 7

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

DARKGRAY: 8

LIGHTBLUE: 9

LIGHTGREEN: 10

LIGHTCYAN: 11

LIGHTRED: 12

LIGHTMAGENTA 13

YELLOW: 14

(13)

Nihar Ranjan Roy

13

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

(14)

Nihar Ranjan Roy

14

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

(15)

Nihar Ranjan Roy

15

The parameter pattern in setfillstyle is as follows:

Names Value Means Fill With...

EMPTY_FILL 0 Background color

SOLID_FILL 1 Solid fill

LINE_FILL 2

---LTSLASH_FILL 3 ///

SLASH_FILL 4 ///, thick lines

BKSLASH_FILL 5 \\\, thick lines

LTBKSLASH_FILL 6 \\\

HATCH_FILL 7 Light hatch

XHATCH_FILL 8 Heavy crosshatch

INTERLEAVE_FILL 9 Interleaving lines WIDE_DOT_FILL 10 Widely spaced dots CLOSE_DOT_FILL 11 Closely spaced dots

(16)

Nihar Ranjan Roy

16

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

(17)
(18)

Nihar Ranjan Roy

18

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

(19)

Nihar Ranjan Roy

19

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

(20)

Nihar Ranjan Roy

20

Current Position (CP)

void far moveto(x1,y1)

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

void far moverel(dx,dy)

(21)

Nihar Ranjan Roy

21

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 Service Purpose

51 0

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

51 1

Show mouse pointer Call with AX = 1

Returns: Nothing

51 2

Hide mouse pointer Call with AX = 2 Returns: Nothing

(22)

Nihar Ranjan Roy

22

51 3

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

51 4

Set mouse pointer position

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

51 7

Set horizontal limits for pointer

Call with AX = 7

CX = minimum x coordinate DX = maximum x coordinate Returns: Nothing

51 8

Set vertical limits for pointer

Call with AX = 8

(23)

Nihar Ranjan Roy

23

Problem

(24)

Nihar Ranjan Roy

24

int callmouse()

{

in.x.ax=1;

//show mouse

int86(51,&in,&out);

return 1;

}

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

}

#include<graphics.h> #include<stdio.h> #include<conio.h> #include<dos.h>
(25)

Nihar Ranjan Roy

25

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

(26)

Nihar Ranjan Roy

26

Few more functions for mouse

handling

int callmouse() { in.x.ax=1; int86(51,&in,&out); return 1; }

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

References

Related documents

Video Processor Intel® HD Graphics Family Driver Provider Intel Corporation. Driver

ANDY: Yeah, you're right, Will's still a lying sack of shit!. WILL: At least I have

and mountain biking Mountain climbing, Mount..

Just as ideation worked upon the ground of scientific competence to create prototypes, so more general supervening social necessities now work on these prototypes to move them out

I have taken my knowledge and training in education and technology, some twenty years of full-time college and university teaching experience wherein I gained experience teaching at

The present study entailed the development (Phase I), pilot (Phase II) and randomised controlled trial (RCT) (Phase III) of a psychosexual information booklet for women

This paper suggests four strategies for doing this: using Community Development Block Grant funds to support programs; joining with private sector partners to

In diagnosing conditional VaR estimates the dynamic quantile test (Engle and Manganelli 2004) and a Portmanteau approach (Hurlin and Tokpavi 2006) are shown to suffer from marked