/*_______________________________________________________________ tc-002.c Function: This program displays the versatile range of colors available on VGA, EGA, and CGA graphics adapters. 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. 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 */ /* declare global subroutines */ void keyboard(void);void quit_pgm(void);void halftone_swatch(void); void notice(float x,float y);void graphics_setup(void); /* declare global variables */ float x,y,h,v,x1,v1;float sx1,sy1,sx2,sy2; 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,CLR,mode_flag=0; float x_res,y_res; /*_______________________________________________________________ M A I N R O U T I N E */ main(){ graphics_setup(); /* establish graphics mode */ CLR=C8;halftone_swatch(); x=x1;y=y+v1;CLR=C7;halftone_swatch(); x=x1;y=y+v1;CLR=C15;halftone_swatch(); x=x1;y=y+v1;CLR=C1;halftone_swatch(); x=x1;y=y+v1;CLR=C9;halftone_swatch(); x=x1;y=y+v1;CLR=C6;halftone_swatch(); x=x1;y=y+v1;CLR=C14;halftone_swatch(); x=x1;y=y+v1;CLR=C4;halftone_swatch(); x=x1;y=y+v1;CLR=C12;halftone_swatch(); x=x1;y=y+v1;CLR=C5;halftone_swatch(); x=x1;y=y+v1;CLR=C13;halftone_swatch(); x=x1;y=y+v1;CLR=C3;halftone_swatch(); x=x1;y=y+v1;CLR=C11;halftone_swatch(); x=x1;y=y+v1;CLR=C2;halftone_swatch(); x=x1;y=y+v1;CLR=C10;halftone_swatch(); setcolor(C7);rectangle(sx1,sy1,sx2,sy2); notice(sx1+1,sy2+2); /* notice */ keyboard(); /* wait for user to press */ quit_pgm(); /* end the program gracefully */ } /*_______________________________________________________________ SUBROUTINE: DRAW HALFTONE SWATCHES */ void halftone_swatch(void){ setfillpattern(fill_3,CLR);bar(x,y,x+h,y+v); x=x+h;setfillpattern(fill_6,CLR);bar(x,y,x+h,y+v); x=x+h;setfillpattern(fill_12,CLR);bar(x,y,x+h,y+v); x=x+h;setfillpattern(fill_25,CLR);bar(x,y,x+h,y+v); x=x+h;setfillpattern(fill_37,CLR);bar(x,y,x+h,y+v); x=x+h;setfillpattern(fill_50,CLR);bar(x,y,x+h,y+v); x=x+h;setfillpattern(fill_62,CLR);bar(x,y,x+h,y+v); x=x+h;setfillpattern(fill_75,CLR);bar(x,y,x+h,y+v); x=x+h;setfillpattern(fill_87,CLR);bar(x,y,x+h,y+v); x=x+h;setfillstyle(SOLID_FILL,CLR);bar(x,y,x+h,y+v); return;} /*_______________________________________________________________ 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;mode_flag=1;x=19;y=39;h=60;v=24;x1=19; v1=25;sx1=18;sy1=38;sx2=620;sy2=414; setcolor(C7);outtextxy(216,472,"640x480 16-color VGA mode"); outtextxy(128,0,"U S I N G C T O D I S P L A Y H U E S"); 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;mode_flag=2;x=19;y=28;h=60;v=18;x1=19; v1=19;sx1=18;sy1=27;sx2=620;sy2=313; setcolor(C7);outtextxy(216,342,"640x350 16-color EGA mode"); outtextxy(128,0,"U S I N G C T O D I S P L A Y H U E S"); 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;mode_flag=3;x=19;y=16;h=60;v=10;x1=19; v1=11;sx1=18;sy1=15;sx2=620;sy2=181; setcolor(C7);outtextxy(216,192,"640x200 16-color EGA mode"); outtextxy(128,0,"U S I N G C T O D I S P L A Y H U E S"); 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=3;C2=1;C3=2;C4=1;C5=3;C6=2;C7=1; C8=3;C9=1;C10=2;C11=3;C12=2;C13=1;C14=3;C15=2; mode_flag=4;x=10;y=16;h=30;v=10;x1=10; v1=11;sx1=9;sy1=15;sx2=310;sy2=181; setcolor(C7);outtextxy(64,342,"320x200 4-color CGA mode"); outtextxy(64,0,"USING C TO DISPLAY HUES"); 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=3;C2=1;C3=2;C4=1;C5=3;C6=2;C7=1; C8=3;C9=1;C10=2;C11=3;C12=2;C13=1;C14=3;C15=2; mode_flag=4;x=10;y=16;h=30;v=10;x1=10; v1=11;sx1=9;sy1=15;sx2=310;sy2=181; setcolor(C7);outtextxy(64,342,"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: 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 */