/*_______________________________________________________________ tc-003.c Function: This program demonstrates how to draw on the screen using dynamic coordinates. Compatibility: Supports all graphics adapters and monitors. The software uses the 640x480 16-color mode if a VGA is present, the 640x350 16-color mode if an EGA and enhanced monitor are present, the 640x200 16-color mode if an EGA and standard monitor are present, and the 320x200 4-color mode if a CGA or MCGA is present. Remarks: Refer to the book for guidance in preparing a template and for instruction in design techniques. Copyright 1988 Lee Adams and TAB BOOKS Inc. _________________________________________________________________ I N C L U D E F I L E S */ #include /* supports the BIOS call */ #include /* supports the printf function */ #include /* supports the graphics functions */ #include /* supports the exit function */ /*_______________________________________________________________ D E C L A R A T I O N S */ char fill_3[]={0,32,0,0,0,2,0,0}; /* 3% fill */ char fill_6[]={32,0,2,0,128,0,8,0}; /* 6.25% fill */ char fill_12[]={32,2,128,8,32,2,128,8}; /* 12.5% fill */ char fill_25[]={68,17,68,17,68,17,68,17}; /* 25% fill */ char fill_37[]={146,41,148,73,164,73,146,73}; /* 37.5% fill */ char fill_50[]={85,170,85,170,85,170,85,170}; /* 50% fill */ char fill_62[]={109,214,107,182,91,182,109,182}; /* 62.5% fill */ char fill_75[]={187,238,187,238,187,238,187,238}; /* 75% fill */ char fill_87[]={223,253,127,247,223,253,127,247}; /* 87.5% fill */ void keyboard(void);void quit_pgm(void); /* global subroutines */ void notice(float x, float y); void graphics_setup(void);void coords(void); float sx=0,sy=0; /* screen display coordinates */ float x_res=0,y_res=0; /* dimensions of screen */ int C0=0,C1=1,C2=2,C3=3,C4=4,C5=5,C6=6,C7=7,C8=8,C9=9,C10=10, C11=11,C12=12,C13=13,C14=14,C15=15,mode_flag=0; float sx1=0,sy1=0,sx2=0,sy2=0; /*_______________________________________________________________ M A I N R O U T I N E */ main(){ graphics_setup(); /* establish graphics mode */ setfillpattern(fill_25,C7); sx=0;sy=24;coords();sx1=sx;sy1=sy; sx=639;sy=454;coords();sx2=sx;sy2=sy; bar(sx1,sy1,sx2,sy2); /* draw background */ setfillstyle(SOLID_FILL,C0); sx=200;sy=108;coords();sx1=sx;sy1=sy; sx=500;sy=420;coords();sx2=sx;sy2=sy; bar(sx1,sy1,sx2,sy2); /* dropshadow */ setfillstyle(SOLID_FILL,C9); sx=170;sy=72;coords();sx1=sx;sy1=sy; sx=470;sy=384;coords();sx2=sx;sy2=sy; bar(sx1,sy1,sx2,sy2); /* the block graphic */ /* add halftoning to the block graphic */ sx=170;sy=72;coords();sx1=sx;sy1=sy; sx=470;sy=98;coords();sx2=sx;sy2=sy; setfillpattern(fill_6,C1);bar(sx1,sy1,sx2,sy2); sx=170;sy=98;coords();sx1=sx;sy1=sy; sx=470;sy=132;coords();sx2=sx;sy2=sy; setfillpattern(fill_12,C1);bar(sx1,sy1,sx2,sy2); sx=170;sy=132;coords();sx1=sx;sy1=sy; sx=470;sy=168;coords();sx2=sx;sy2=sy; setfillpattern(fill_25,C1);bar(sx1,sy1,sx2,sy2); sx=170;sy=168;coords();sx1=sx;sy1=sy; sx=470;sy=204;coords();sx2=sx;sy2=sy; setfillpattern(fill_37,C1);bar(sx1,sy1,sx2,sy2); sx=170;sy=204;coords();sx1=sx;sy1=sy; sx=470;sy=240;coords();sx2=sx;sy2=sy; setfillpattern(fill_50,C1);bar(sx1,sy1,sx2,sy2); sx=170;sy=240;coords();sx1=sx;sy1=sy; sx=470;sy=276;coords();sx2=sx;sy2=sy; setfillpattern(fill_62,C1);bar(sx1,sy1,sx2,sy2); sx=170;sy=276;coords();sx1=sx;sy1=sy; sx=470;sy=312;coords();sx2=sx;sy2=sy; setfillpattern(fill_75,C1);bar(sx1,sy1,sx2,sy2); sx=170;sy=312;coords();sx1=sx;sy1=sy; sx=470;sy=348;coords();sx2=sx;sy2=sy; setfillpattern(fill_87,C1);bar(sx1,sy1,sx2,sy2); sx=170;sy=348;coords();sx1=sx;sy1=sy; sx=470;sy=384;coords();sx2=sx;sy2=sy; setfillstyle(SOLID_FILL,C1);bar(sx1,sy1,sx2,sy2); setcolor(C7); /* draw the star */ sx=319;sy=96;coords();moveto(sx,sy); sx=350;sy=192;coords();lineto(sx,sy); sx=445;sy=192;coords();lineto(sx,sy); sx=370;sy=254;coords();lineto(sx,sy); sx=398;sy=350;coords();lineto(sx,sy); sx=319;sy=293;coords();lineto(sx,sy); sx=240;sy=350;coords();lineto(sx,sy); sx=270;sy=254;coords();lineto(sx,sy); sx=192;sy=192;coords();lineto(sx,sy); sx=290;sy=192;coords();lineto(sx,sy); sx=319;sy=96;coords();lineto(sx,sy); sx=319;sy=238;coords();setfillstyle(SOLID_FILL,C7); floodfill(sx,sy,C7); sx=0;sy=0;coords();notice(sx,sy); /* notice */ keyboard(); /* wait for user to press */ quit_pgm(); /* end the program gracefully */ } /*_______________________________________________________________ SUBROUTINE: CHECK THE KEYBOARD BUFFER */ void keyboard(void){ union u_type {int a;char b[3];} keystroke; int get_keystroke(void); /* declare a local subroutine */ do keystroke.a=get_keystroke(); while (keystroke.b[0]!=27); /* return if is pressed */ } /* LOCAL SUBROUTINE: RETRIEVE ONE KEYSTROKE */ int get_keystroke(void){ union REGS regs;regs.h.ah=0;return int86(0x16,®s,®s);} /*_______________________________________________________________ SUBROUTINE: GRACEFUL EXIT FROM THE PROGRAM */ void quit_pgm(void){ cleardevice();restorecrtmode();exit(0);} /*______________________________________________________________ SUBROUTINE: VGA/EGA/CGA/MCGA compatibility module This subroutine invokes the highest-resolution graphics mode which is permitted by the hardware. The 640x480 16-color mode is established if a VGA is present. The 640x350 16-color mode is established if an EGA is being used with an enhanced color display monitor or a multiscanning monitor. The 640x200 16-color mode is established if an EGA is being used with a standard color monitor. The 320x200 4-color mode is invoked if a CGA or MCGA is present. */ void graphics_setup(void){ int graphics_adapter,graphics_mode; detectgraph(&graphics_adapter,&graphics_mode); if (graphics_adapter==VGA) goto VGA_mode; /* if VGA */ if (graphics_mode==EGAHI) goto EGA_ECD_mode; /* if EGA and ECD */ if (graphics_mode==EGALO) goto EGA_SCD_mode; /* if EGA and SCD */ if (graphics_adapter==CGA) goto CGA_mode; /* if CGA */ if (graphics_adapter==MCGA) goto MCGA_mode; /* if MCGA */ goto abort_message; /* if no VGA, EGA, CGA, or MCGA */ VGA_mode: /* establish 640x480 16-color mode */ graphics_adapter=VGA;graphics_mode=VGAHI; initgraph(&graphics_adapter,&graphics_mode,""); x_res=640;y_res=480;C0=0;C1=1;C2=2;C3=3;C4=4;C5=5,C6=6;C7=7; C8=8;C9=9;C10=10;C11=11;C12=12;C13=13;C14=14;C15=15; mode_flag=1; setcolor(C7);outtextxy(0,472,"640x480 16-color VGA mode."); return; EGA_ECD_mode: /* establish 640x350 16-color mode */ graphics_adapter=EGA;graphics_mode=EGAHI; initgraph(&graphics_adapter,&graphics_mode,""); x_res=640;y_res=350;C0=0;C1=1;C2=2;C3=3;C4=4;C5=5,C6=6;C7=7; C8=8;C9=9;C10=10;C11=11;C12=12;C13=13;C14=14;C15=15; mode_flag=2; setcolor(C7);outtextxy(0,342,"640x350 16-color EGA mode."); return; EGA_SCD_mode: /* establish 640x200 16-color mode */ graphics_adapter=EGA;graphics_mode=EGALO; initgraph(&graphics_adapter,&graphics_mode,""); x_res=640;y_res=200;C0=0;C1=1;C2=2;C3=3;C4=4;C5=5,C6=6;C7=7; C8=8;C9=9;C10=10;C11=11;C12=12;C13=13;C14=14;C15=15; mode_flag=3; setcolor(C7);outtextxy(0,192,"640x200 16-color EGA mode."); return; CGA_mode: /* establish 320x200 4-color mode */ graphics_adapter=CGA;graphics_mode=CGAC3; initgraph(&graphics_adapter,&graphics_mode,""); x_res=320;y_res=200;C0=0;C1=1;C2=1;C3=1;C4=1;C5=1,C6=1;C7=3; C8=1;C9=0;C10=1;C11=1;C12=1;C13=2;C14=1;C15=3; mode_flag=4; setcolor(C7);outtextxy(0,192,"320x200 4-color CGA mode."); return; MCGA_mode: /* establish 320x200 4-color mode */ graphics_adapter=MCGA;graphics_mode=MCGAC3; initgraph(&graphics_adapter,&graphics_mode,""); x_res=320;y_res=200;C0=0;C1=1;C2=1;C3=1;C4=1;C5=1;C6=1;C7=3; C8=1;C9=0;C10=1;C11=1;C12=1;C13=2;C14=1;C15=3; mode_flag=4; setcolor(C7);outtextxy(0,192,"320x200 4-color MCGA mode."); return; abort_message: printf("\n\nUnable to proceed.\n"); printf("Requires VGA, EGA, MCGA, or CGA adapter\n"); printf(" with appropriate monitor.\n"); printf("Please refer to the book.\n\n"); exit(0); } /*_______________________________________________________________ SUBROUTINE: coords() This subroutine accepts sx,sy device-independent display coordinates and returns sx,sy device-dependent screen coordinates scaled to fit the 640x480, 640x350, 640x200, or 320x200 screen, depending upon the graphics mode being used. */ void coords(void){ sx=sx*(x_res/640);sy=sy*(y_res/480);return;} /*_______________________________________________________________ SUBROUTINE: Copyright Notice This subroutine displays the standard copyright notice. If you are typing in this program from the book you can safely omit this subroutine, provided that you also remove the instruction "notice()" from the main routine. */ int copyright[][3]={0x7c00,0x0000,0x0000,0x8231, 0x819c,0x645e,0xba4a,0x4252,0x96d0,0xa231,0x8252,0x955e,0xba4a, 0x43d2,0xf442,0x8231,0x825c,0x945e,0x7c00,0x0000,0x0000}; void notice(float x, float y){ int a,b,c; int t1=0; for (t1=0;t1<=6;t1++){a=copyright[t1][0];b=copyright[t1][1]; c=copyright[t1][2]; setlinestyle(USERBIT_LINE,a,NORM_WIDTH); moveto(x,y);lineto(x+15,y); setlinestyle(USERBIT_LINE,b,NORM_WIDTH); moveto(x+16,y);lineto(x+31,y); setlinestyle(USERBIT_LINE,c,NORM_WIDTH); moveto(x+32,y);lineto(x+47,y);y=y+1;}; setlinestyle(USERBIT_LINE,0xFFFF,NORM_WIDTH); return;} /*_______________________________________________________________ End of source code */