home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / MISC / BGIHERC.ZIP / BGIDEMO.C next >
Encoding:
C/C++ Source or Header  |  1989-05-31  |  39.4 KB  |  1,402 lines

  1. /*
  2.    GRAPHICS DEMO FOR TURBO C 2.0
  3.  
  4.    Copyright (c) 1987,88 Borland International. All rights reserved.
  5.  
  6.    Altered (modestly) to support the Hercules InColor Card using the
  7.    "unified" HERC.BGI driver.
  8.  
  9. */
  10.  
  11. #ifdef __TINY__
  12. #error BGIDEMO will not run in the tiny model.
  13. #endif
  14.  
  15. #include <dos.h>
  16. #include <math.h>
  17. #include <conio.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <stdarg.h>
  21.  
  22. #include <graphics.h>
  23.  
  24. #define ESC    0x1b            /* Define the escape key    */
  25. #define TRUE    1            /* Define some handy constants    */
  26. #define FALSE    0            /* Define some handy constants    */
  27. #define PI    3.14159         /* Define a value for PI    */
  28. #define ON    1            /* Define some handy constants    */
  29. #define OFF    0            /* Define some handy constants    */
  30.  
  31. char *Fonts[] = {
  32.   "DefaultFont",   "TriplexFont",   "SmallFont",
  33.   "SansSerifFont", "GothicFont"
  34. };
  35.  
  36. char *LineStyles[] = {
  37.   "SolidLn",  "DottedLn",  "CenterLn",  "DashedLn",  "UserBitLn"
  38. };
  39.  
  40. char *FillStyles[] = {
  41.   "EmptyFill",  "SolidFill",      "LineFill",      "LtSlashFill",
  42.   "SlashFill",  "BkSlashFill",    "LtBkSlashFill", "HatchFill",
  43.   "XHatchFill", "InterleaveFill", "WideDotFill",   "CloseDotFill"
  44. };
  45.  
  46. char *TextDirect[] = {
  47.   "HorizDir",  "VertDir"
  48. };
  49.  
  50. char *HorizJust[] = {
  51.   "LeftText",   "CenterText",   "RightText"
  52. };
  53.  
  54. char *VertJust[] = {
  55.   "BottomText",  "CenterText",  "TopText"
  56. };
  57.  
  58. struct PTS {
  59.   int x, y;
  60. };    /* Structure to hold vertex points    */
  61.  
  62. int    GraphDriver;        /* The Graphics device driver        */
  63. int    GraphMode;        /* The Graphics mode value        */
  64. double AspectRatio;        /* Aspect ratio of a pixel on the screen*/
  65. int    MaxX, MaxY;        /* The maximum resolution of the screen */
  66. int    MaxColors;        /* The maximum # of colors available    */
  67. int    ErrorCode;        /* Reports any graphics errors        */
  68. struct palettetype palette;    /* Used to read palette info    */
  69.  
  70. /*                                    */
  71. /*    Function prototypes                        */
  72. /*                                    */
  73.  
  74. void Initialize(void);
  75. void ReportStatus(void);
  76. void TextDump(void);
  77. void Bar3DDemo(void);
  78. void RandomBars(void);
  79. void TextDemo(void);
  80. void ColorDemo(void);
  81. void ArcDemo(void);
  82. void CircleDemo(void);
  83. void PieDemo(void);
  84. void BarDemo(void);
  85. void LineRelDemo(void);
  86. void PutPixelDemo(void);
  87. void PutImageDemo(void);
  88. void LineToDemo(void);
  89. void LineStyleDemo(void);
  90. void CRTModeDemo(void);
  91. void UserLineStyleDemo(void);
  92. void FillStyleDemo(void);
  93. void FillPatternDemo(void);
  94. void PaletteDemo(void);
  95. void PolyDemo(void);
  96. void SayGoodbye(void);
  97. void Pause(void);
  98. void MainWindow(char *header);
  99. void StatusLine(char *msg);
  100. void DrawBorder(void);
  101. void changetextstyle(int font, int direction, int charsize);
  102. int  gprintf(int *xloc, int *yloc, char *fmt, ... );
  103.  
  104. /*                                    */
  105. /*    Begin main function                        */
  106. /*                                    */
  107.  
  108.  
  109. int main()
  110. {
  111.   Initialize();         /* Set system into Graphics mode    */
  112.   ReportStatus();        /* Report results of the initialization */
  113.  
  114.   ColorDemo();            /* Begin actual demonstration        */
  115.   if( GraphDriver==EGA || GraphDriver==EGALO || GraphDriver==VGA )
  116.     PaletteDemo();
  117.   if( GraphDriver==HERCMONO && (getmaxcolor() == 15)) /* Test for InColor */
  118.     PaletteDemo();
  119.   PutPixelDemo();
  120.   PutImageDemo();
  121.   Bar3DDemo();
  122.   BarDemo();
  123.   RandomBars();
  124.   ArcDemo();
  125.   CircleDemo();
  126.   PieDemo();
  127.   LineRelDemo();
  128.   LineToDemo();
  129.   LineStyleDemo();
  130.   UserLineStyleDemo();
  131.   TextDump();
  132.   TextDemo();
  133.   CRTModeDemo();
  134.   FillStyleDemo();
  135.   FillPatternDemo();
  136.   PolyDemo();
  137.   SayGoodbye();         /* Give user the closing screen     */
  138.  
  139.   closegraph();         /* Return the system to text mode    */
  140.   return(0);
  141. }
  142.  
  143. /*                                    */
  144. /*    INITIALIZE: Initializes the graphics system and reports     */
  145. /*    any errors which occured.                    */
  146. /*                                    */
  147.  
  148. void Initialize(void)
  149. {
  150.   int xasp, yasp;            /* Used to read the aspect ratio*/
  151.  
  152.   GraphDriver = DETECT;
  153.   initgraph( &GraphDriver, &GraphMode, "" );
  154.   ErrorCode = graphresult();        /* Read result of initialization*/
  155.   if( ErrorCode != grOk ){        /* Error occured during init    */
  156.     printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) );
  157.     exit( 1 );
  158.   }
  159.  
  160.   getpalette( &palette );        /* Read the palette from board    */
  161.   MaxColors = getmaxcolor() + 1;    /* Read maximum number of colors*/
  162.  
  163.   MaxX = getmaxx();
  164.   MaxY = getmaxy();            /* Read size of screen        */
  165.  
  166.   getaspectratio( &xasp, &yasp );    /* read the hardware aspect    */
  167.   AspectRatio = (double)xasp / (double)yasp; /* Get correction factor    */
  168.  
  169. }
  170.  
  171. /*                                    */
  172. /*    REPORTSTATUS: Report the current configuration of the system    */
  173. /*    after the auto-detect initialization.                */
  174. /*                                    */
  175.  
  176. void ReportStatus(void)
  177. {
  178.   struct viewporttype      viewinfo;    /* Params for inquiry procedures*/
  179.   struct linesettingstype lineinfo;
  180.   struct fillsettingstype fillinfo;
  181.   struct textsettingstype textinfo;
  182.   struct palettetype      palette;
  183.  
  184.   char *driver, *mode;            /* Strings for driver and mode    */
  185.   int x, y;
  186.  
  187.   getviewsettings( &viewinfo );
  188.   getlinesettings( &lineinfo );
  189.   getfillsettings( &fillinfo );
  190.   gettextsettings( &textinfo );
  191.   getpalette( &palette );
  192.  
  193.   x = 10;
  194.   y = 4;
  195.  
  196.   MainWindow( "Status report after InitGraph" );
  197.   settextjustify( LEFT_TEXT, TOP_TEXT );
  198.  
  199.   driver = getdrivername();
  200.   mode = getmodename(GraphMode);    /* get current setting        */
  201.  
  202.   gprintf( &x, &y, "Graphics device    : %-20s (%d)", driver, GraphDriver );
  203.   gprintf( &x, &y, "Graphics mode      : %-20s (%d)", mode, GraphMode );
  204.   gprintf( &x, &y, "Screen resolution  : ( 0, 0, %d, %d )", getmaxx(), getmaxy() );
  205.  
  206.   gprintf( &x, &y, "Current view port  : ( %d, %d, %d, %d )",
  207.   viewinfo.left, viewinfo.top, viewinfo.right, viewinfo.bottom );
  208.   gprintf( &x, &y, "Clipping           : %s", viewinfo.clip ? "ON" : "OFF" );
  209.  
  210.   gprintf( &x, &y, "Current position   : ( %d, %d )", getx(), gety() );
  211.   gprintf( &x, &y, "Colors available   : %d", MaxColors );
  212.   gprintf( &x, &y, "Current color      : %d", getcolor() );
  213.  
  214.   gprintf( &x, &y, "Line style         : %s", LineStyles[ lineinfo.linestyle ] );
  215.   gprintf( &x, &y, "Line thickness     : %d", lineinfo.thickness );
  216.  
  217.   gprintf( &x, &y, "Current fill style : %s", FillStyles[ fillinfo.pattern ] );
  218.   gprintf( &x, &y, "Current fill color : %d", fillinfo.color );
  219.  
  220.   gprintf( &x, &y, "Current font       : %s", Fonts[ textinfo.font ] );
  221.   gprintf( &x, &y, "Text direction     : %s", TextDirect[ textinfo.direction ] );
  222.   gprintf( &x, &y, "Character size     : %d", textinfo.charsize );
  223.   gprintf( &x, &y, "Horizontal justify : %s", HorizJust[ textinfo.horiz ] );
  224.   gprintf( &x, &y, "Vertical justify   : %s", VertJust[ textinfo.vert ] );
  225.  
  226.   Pause();                /* Pause for user to read screen*/
  227.  
  228. }
  229.  
  230. /*                                    */
  231. /*    TEXTDUMP: Display the all the characters in each of the     */
  232. /*    available fonts.                        */
  233. /*                                    */
  234.  
  235. void TextDump()
  236. {
  237.   static int CGASizes[]  = {
  238.     1, 3, 7, 3, 3   };
  239.   static int NormSizes[] = {
  240.     1, 4, 7, 4, 4   };
  241.  
  242.   char buffer[80];
  243.   int font, ch, wwidth, lwidth, size;
  244.   struct viewporttype vp;
  245.  
  246.   for( font=0 ; font<5 ; ++font ){    /* For each available font    */
  247.     sprintf( buffer, "%s Character Set", Fonts[font] );
  248.     MainWindow( buffer );        /* Display fontname as banner    */
  249.     getviewsettings( &vp );        /* read current viewport    */
  250.  
  251.     settextjustify( LEFT_TEXT, TOP_TEXT );
  252.     moveto( 2, 3 );
  253.  
  254.     buffer[1] = '\0';                   /* Terminate string             */
  255.     wwidth = vp.right - vp.left;    /* Determine the window width    */
  256.     lwidth = textwidth( "H" );          /* Get average letter width     */
  257.  
  258.     if( font == DEFAULT_FONT ){
  259.       changetextstyle( font, HORIZ_DIR, 1 );
  260.       ch = 0;
  261.       while( ch < 256 ){        /* For each possible character    */
  262.     buffer[0] = ch;         /* Put character into a string    */
  263.     outtext( buffer );        /* send string to screen    */
  264.     if( (getx() + lwidth) > wwidth )
  265.       moveto( 2, gety() + textheight("H") + 3 );
  266.     ++ch;                /* Goto the next character    */
  267.       }
  268.     }
  269.     else{
  270.  
  271.       size = (MaxY < 200) ? CGASizes[font] : NormSizes[font];
  272.       changetextstyle( font, HORIZ_DIR, size );
  273.  
  274.       ch = '!';                         /* Begin at 1st printable       */
  275.       while( ch < 127 ){        /* For each printable character */
  276.     buffer[0] = ch;         /* Put character into a string    */
  277.     outtext( buffer );        /* send string to screen    */
  278.     if( (lwidth+getx()) > wwidth )    /* Are we still in window?    */
  279.       moveto( 2, gety()+textheight("H")+3 );
  280.     ++ch;                /* Goto the next character    */
  281.       }
  282.  
  283.     }
  284.  
  285.     Pause();                /* Pause until user acks    */
  286.  
  287.   }                    /* End of FONT loop        */
  288.  
  289. }
  290.  
  291. /*                                    */
  292. /*    BAR3DDEMO: Display a 3-D bar chart on the screen.        */
  293. /*                                    */
  294.  
  295. void Bar3DDemo(void)
  296. {
  297.   static int barheight[] = {
  298.     1, 3, 5, 4, 3, 2, 1, 5, 4, 2, 3   };
  299.   struct viewporttype vp;
  300.   int xstep, ystep;
  301.   int i, j, h, color, bheight;
  302.   char buffer[10];
  303.  
  304.   MainWindow( "Bar 3-D / Rectangle Demonstration" );
  305.  
  306.   h = 3 * textheight( "H" );
  307.   getviewsettings( &vp );
  308.   settextjustify( CENTER_TEXT, TOP_TEXT );
  309.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  310.   outtextxy( MaxX/2, 6, "These are 3-D Bars" );
  311.   changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  312.   setviewport( vp.left+50, vp.top+40, vp.right-50, vp.bottom-10, 1 );
  313.   getviewsettings( &vp );
  314.  
  315.   line( h, h, h, vp.bottom-vp.top-h );
  316.   line( h, (vp.bottom-vp.top)-h, (vp.right-vp.left)-h, (vp.bottom-vp.top)-h );
  317.   xstep = ((vp.right-vp.left) - (2*h)) / 10;
  318.   ystep = ((vp.bottom-vp.top) - (2*h)) / 5;
  319.   j = (vp.bottom-vp.top) - h;
  320.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  321.  
  322.   for( i=0 ; i<6 ; ++i ){
  323.     line( h/2, j, h, j );
  324.     itoa( i, buffer, 10 );
  325.     outtextxy( 0, j, buffer );
  326.     j -= ystep;
  327.   }
  328.  
  329.   j = h;
  330.   settextjustify( CENTER_TEXT, TOP_TEXT );
  331.  
  332.   for( i=0 ; i<11 ; ++i ){
  333.     color = random( MaxColors );
  334.     setfillstyle( i+1, color );
  335.     line( j, (vp.bottom-vp.top)-h, j, (vp.bottom-vp.top-3)-(h/2) );
  336.     itoa( i, buffer, 10 );
  337.     outtextxy( j, (vp.bottom-vp.top)-(h/2), buffer );
  338.     if( i != 10 ){
  339.       bheight = (vp.bottom-vp.top) - h - 1;
  340.       bar3d( j, (vp.bottom-vp.top-h)-(barheight[i]*ystep), j+xstep, bheight, 15, 1 );
  341.     }
  342.     j += xstep;
  343.   }
  344.  
  345.   Pause();                /* Pause for user's response    */
  346.  
  347. }
  348.  
  349. /*                                    */
  350. /*    RANDOMBARS: Display random bars                 */
  351. /*                                    */
  352.  
  353. void RandomBars(void)
  354. {
  355.   int color;
  356.  
  357.   MainWindow( "Random Bars" );
  358.   StatusLine( "Esc aborts or press a key..." ); /* Put msg at bottom of screen   */
  359.   while( !kbhit() ){            /* Until user enters a key...    */
  360.     color = random( MaxColors-1 )+1;
  361.     setcolor( color );
  362.     setfillstyle( random(11)+1, color );
  363.     bar3d( random( getmaxx() ), random( getmaxy() ),
  364.        random( getmaxx() ), random( getmaxy() ), 0, OFF);
  365.   }
  366.  
  367.   Pause();                /* Pause for user's response    */
  368.  
  369. }
  370.  
  371.  
  372. /*                                    */
  373. /*    TEXTDEMO: Show each font in several sizes to the user.        */
  374. /*                                    */
  375.  
  376. void TextDemo(void)
  377. {
  378.   int charsize[] = {
  379.     1, 3, 7, 3, 4   };
  380.   int font, size;
  381.   int h, x, y, i;
  382.   struct viewporttype vp;
  383.   char buffer[80];
  384.  
  385.   for( font=0 ; font<5 ; ++font ){    /* For each of the four fonts    */
  386.  
  387.     sprintf( buffer, "%s Demonstration", Fonts[font] );
  388.     MainWindow( buffer );
  389.     getviewsettings( &vp );
  390.  
  391.     changetextstyle( font, VERT_DIR, charsize[font] );
  392.     settextjustify( CENTER_TEXT, BOTTOM_TEXT );
  393.     outtextxy( 2*textwidth("M"), vp.bottom - 2*textheight("M"), "Vertical" );
  394.  
  395.     changetextstyle( font, HORIZ_DIR, charsize[font] );
  396.     settextjustify( LEFT_TEXT, TOP_TEXT );
  397.     outtextxy( 2*textwidth("M"), 2, "Horizontal" );
  398.  
  399.     settextjustify( CENTER_TEXT, CENTER_TEXT );
  400.     x = (vp.right - vp.left) / 2;
  401.     y = textheight( "H" );
  402.  
  403.     for( i=1 ; i<5 ; ++i ){        /* For each of the sizes */
  404.       size = (font == SMALL_FONT) ? i+3 : i;
  405.       changetextstyle( font, HORIZ_DIR, size );
  406.       h = textheight( "H" );
  407.       y += h;
  408.       sprintf( buffer, "Size %d", size );
  409.       outtextxy( x, y, buffer );
  410.  
  411.     }
  412.  
  413.     if( font != DEFAULT_FONT ){     /* Show user declared font size */
  414.       y += h / 2;            /* Move down the screen     */
  415.       settextjustify( CENTER_TEXT, TOP_TEXT );
  416.       setusercharsize( 5, 6, 3, 2 );
  417.       changetextstyle( font, HORIZ_DIR, USER_CHAR_SIZE );
  418.       outtextxy( (vp.right-vp.left)/2, y, "User Defined Size" );
  419.     }
  420.  
  421.     Pause();                /* Pause to let user look    */
  422.  
  423.   }                    /* End of FONT loop        */
  424.  
  425. }
  426.  
  427. /*                                    */
  428. /*    COLORDEMO: Display the current color palette on the screen.    */
  429. /*                                    */
  430.  
  431. void ColorDemo(void)
  432. {
  433.   struct viewporttype vp;
  434.   int color, height, width;
  435.   int x, y, i, j;
  436.   char cnum[5];
  437.  
  438.   MainWindow( "Color Demonstration" );  /* Show demonstration name      */
  439.  
  440.   color = 1;
  441.   getviewsettings( &vp );        /* Get the current window size    */
  442.   width  = 2 * ( (vp.right+1) / 16 );       /* Get box dimensions       */
  443.   height = 2 * ( (vp.bottom-10) / 10 );
  444.  
  445.   x = width / 2;
  446.   y = height / 2;    /* Leave 1/2 box border     */
  447.  
  448.   for( j=0 ; j<3 ; ++j ){        /* Row loop            */
  449.  
  450.     for( i=0 ; i<5 ; ++i ){        /* Column loop            */
  451.  
  452.       setfillstyle(SOLID_FILL, color);    /* Set to solid fill in color    */
  453.       setcolor( color );        /* Set the same border color    */
  454.  
  455.       bar( x, y, x+width, y+height );    /* Draw the rectangle        */
  456.       rectangle( x, y, x+width, y+height );  /* outline the rectangle    */
  457.  
  458.       if( color == BLACK ){        /* If box was black...        */
  459.     setcolor( WHITE );        /* Set drawing color to white    */
  460.     rectangle( x, y, x+width, y+height );  /* Outline black in white*/
  461.       }
  462.  
  463.       itoa( color, cnum, 10 );        /* Convert # to ASCII        */
  464.       outtextxy( x+(width/2), y+height+4, cnum );  /* Show color #    */
  465.  
  466.       color = ++color % MaxColors;    /* Advance to the next color    */
  467.       x += (width / 2) * 3;        /* move the column base     */
  468.     }                /* End of Column loop        */
  469.  
  470.     y += (height / 2) * 3;        /* move the row base        */
  471.     x = width / 2;            /* reset column base        */
  472.   }                    /* End of Row loop        */
  473.  
  474.   Pause();                /* Pause for user's response    */
  475.  
  476. }
  477.  
  478. /*                                    */
  479. /*    ARCDEMO: Display a random pattern of arcs on the screen */
  480. /*    until the user says enough.                    */
  481. /*                                    */
  482.  
  483. void ArcDemo(void)
  484. {
  485.   int mradius;                /* Maximum radius allowed    */
  486.   int eangle;                /* Random end angle of Arc    */
  487.   struct arccoordstype ai;        /* Used to read Arc Cord info    */
  488.  
  489.   MainWindow( "Arc Demonstration" );
  490.   StatusLine( "ESC Aborts - Press a Key to stop" );
  491.  
  492.   mradius = MaxY / 10;            /* Determine the maximum radius */
  493.  
  494.   while( !kbhit() ){            /* Repeat until a key is hit    */
  495.     setcolor( random( MaxColors - 1 ) + 1 );    /* Randomly select a color    */
  496.     eangle = random( 358 ) + 1;     /* Select an end angle        */
  497.     arc( random(MaxX), random(MaxY), random(eangle), eangle, mradius );
  498.     getarccoords( &ai );        /* Read Cord data        */
  499.     line( ai.x, ai.y, ai.xstart, ai.ystart ); /* line from start to center */
  500.     line( ai.x, ai.y,    ai.xend,   ai.yend ); /* line from end to center   */
  501.   }                    /* End of WHILE not KBHIT    */
  502.  
  503.   Pause();                /* Wait for user's response     */
  504.  
  505. }
  506.  
  507. /*                                    */
  508. /*    CIRCLEDEMO: Display a random pattern of circles on the screen    */
  509. /*    until the user says enough.                    */
  510. /*                                    */
  511.  
  512. void CircleDemo(void)
  513. {
  514.   int mradius;                /* Maximum radius allowed    */
  515.  
  516.   MainWindow( "Circle Demonstration" );
  517.   StatusLine( "ESC Aborts - Press a Key to stop" );
  518.  
  519.   mradius = MaxY / 10;            /* Determine the maximum radius */
  520.  
  521.   while( !kbhit() ){            /* Repeat until a key is hit    */
  522.     setcolor( random( MaxColors - 1 ) + 1 );    /* Randomly select a color    */
  523.     circle( random(MaxX), random(MaxY), random(mradius) );
  524.   }                    /* End of WHILE not KBHIT    */
  525.  
  526.   Pause();                /* Wait for user's response     */
  527.  
  528. }
  529.  
  530. /*                                    */
  531. /*    PIEDEMO: Display a pie chart on the screen.            */
  532. /*                                    */
  533.  
  534. #define adjasp( y )    ((int)(AspectRatio * (double)(y)))
  535. #define torad( d )    (( (double)(d) * PI ) / 180.0 )
  536.  
  537. void PieDemo(void)
  538. {
  539.   struct viewporttype vp;
  540.   int xcenter, ycenter, radius, lradius;
  541.   int x, y;
  542.   double radians, piesize;
  543.  
  544.   MainWindow( "Pie Chart Demonstration" );
  545.  
  546.   getviewsettings( &vp );        /* Get the current viewport    */
  547.   xcenter = (vp.right - vp.left) / 2;    /* Center the Pie horizontally    */
  548.   ycenter = (vp.bottom - vp.top) / 2+20;/* Center the Pie vertically    */
  549.   radius  = (vp.bottom - vp.top) / 3;    /* It will cover 2/3rds screen    */
  550.   piesize = (vp.bottom - vp.top) / 4.0; /* Optimum height ratio of pie    */
  551.  
  552.   while( (AspectRatio*radius) < piesize ) ++radius;
  553.  
  554.   lradius = radius + ( radius / 5 );    /* Labels placed 20% farther    */
  555.  
  556.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  557.   settextjustify( CENTER_TEXT, TOP_TEXT );
  558.   outtextxy( MaxX/2, 6, "This is a Pie Chart" );
  559.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  560.   settextjustify( CENTER_TEXT, TOP_TEXT );
  561.  
  562.   setfillstyle( SOLID_FILL, RED );
  563.   pieslice( xcenter+10, ycenter-adjasp(10), 0, 90, radius );
  564.   radians = torad( 45 );
  565.   x = xcenter + (int)( cos( radians ) * (double)lradius );
  566.   y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
  567.   settextjustify( LEFT_TEXT, BOTTOM_TEXT );
  568.   outtextxy( x, y, "25 %" );
  569.  
  570.   setfillstyle( WIDE_DOT_FILL, GREEN );
  571.   pieslice( xcenter, ycenter, 90, 135, radius );
  572.   radians = torad( 113 );
  573.   x = xcenter + (int)( cos( radians ) * (double)lradius );
  574.   y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
  575.   settextjustify( RIGHT_TEXT, BOTTOM_TEXT );
  576.   outtextxy( x, y, "12.5 %" );
  577.  
  578.   setfillstyle( INTERLEAVE_FILL, YELLOW );
  579.   settextjustify( RIGHT_TEXT, CENTER_TEXT );
  580.   pieslice( xcenter-10, ycenter, 135, 225, radius );
  581.   radians = torad( 180 );
  582.   x = xcenter + (int)( cos( radians ) * (double)lradius );
  583.   y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
  584.   settextjustify( RIGHT_TEXT, CENTER_TEXT );
  585.   outtextxy( x, y, "25 %" );
  586.  
  587.   setfillstyle( HATCH_FILL, BLUE );
  588.   pieslice( xcenter, ycenter, 225, 360, radius );
  589.   radians = torad( 293 );
  590.   x = xcenter + (int)( cos( radians ) * (double)lradius );
  591.   y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
  592.   settextjustify( LEFT_TEXT, TOP_TEXT );
  593.   outtextxy( x, y, "37.5 %" );
  594.  
  595.   Pause();                /* Pause for user's response    */
  596.  
  597. }
  598.  
  599. /*                                    */
  600. /*    BARDEMO: Draw a 2-D bar chart using Bar and Rectangle.        */
  601. /*                                    */
  602.  
  603. void BarDemo(void)
  604. {
  605.   int barheight[] = {
  606.     1, 3, 5, 2, 4   };
  607.   int styles[]      = {
  608.     1, 3, 10, 5, 9, 1    };
  609.   int xstep, ystep;
  610.   int sheight, swidth;
  611.   int i, j, h;
  612.   struct viewporttype vp;
  613.   char buffer[40];
  614.  
  615.   MainWindow( "Bar / Rectangle demonstration" );
  616.   h = 3 * textheight( "H" );
  617.   getviewsettings( &vp );
  618.   settextjustify( CENTER_TEXT, TOP_TEXT );
  619.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  620.   outtextxy( MaxX /2, 6, "These are 2-D Bars" );
  621.   changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  622.   setviewport( vp.left+50, vp.top+30, vp.right-50, vp.bottom-10, 1 );
  623.  
  624.   getviewsettings( &vp );
  625.   sheight = vp.bottom - vp.top;
  626.   swidth  = vp.right  - vp.left;
  627.  
  628.   line( h, h, h, sheight-h );
  629.   line( h, sheight-h, sheight-h, sheight-h );
  630.   ystep = (sheight - (2*h) ) / 5;
  631.   xstep = (swidth  - (2*h) ) / 5;
  632.   j = sheight - h;
  633.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  634.  
  635.   for( i=0 ; i<6 ; ++i ){
  636.     line( h/2, j, h, j );
  637.     itoa( i, buffer, 10 );
  638.     outtextxy( 0, j, buffer );
  639.     j -= ystep;
  640.   }
  641.  
  642.   j = h;
  643.   settextjustify( CENTER_TEXT, TOP_TEXT );
  644.   for( i=0 ; i<6 ; ++i ){
  645.     setfillstyle( styles[i], random(MaxColors) );
  646.     line( j, sheight - h, j, sheight- 3 - (h/2) );
  647.     itoa( i, buffer, 10 );
  648.     outtextxy( j, sheight - (h/2), buffer );
  649.     if( i != 5 ){
  650.       bar( j, (sheight-h)-(barheight[i] * ystep), j+xstep, sheight-h-1 );
  651.       rectangle( j, (sheight-h)-(barheight[i] * ystep), j+xstep, sheight-h);
  652.     }
  653.     j += xstep;
  654.   }
  655.  
  656.   Pause();
  657.  
  658. }
  659.  
  660. /*                                    */
  661. /*    LINERELDEMO: Display pattern using moverel and linerel cmds.    */
  662. /*                                    */
  663.  
  664. void LineRelDemo(void)
  665. {
  666.   struct viewporttype vp;
  667.   int h, w, dx, dy, cx, cy;
  668.   struct PTS outs[7];
  669.  
  670.  
  671.   MainWindow( "MoveRel / LineRel Demonstration" );
  672.   StatusLine( "Press any key to continue, ESC to Abort" );
  673.  
  674.   getviewsettings( &vp );
  675.   cx = (vp.right  - vp.left) / 2;    /* Center of the screen coords    */
  676.   cy = (vp.bottom - vp.top ) / 2;
  677.  
  678.   h  = (vp.bottom - vp.top ) / 8;
  679.   w  = (vp.right  - vp.left) / 9;
  680.  
  681.   dx = 2 * w;
  682.   dy = 2 * h;
  683.  
  684.   setcolor( BLACK );
  685.  
  686.   setfillstyle( SOLID_FILL, BLUE );
  687.   bar( 0, 0, vp.right-vp.left, vp.bottom-vp.top );    /* Draw backgnd */
  688.  
  689.   outs[0].x = cx -  dx;
  690.   outs[0].y = cy -  dy;
  691.   outs[1].x = cx - (dx-w);
  692.   outs[1].y = cy - (dy+h);
  693.   outs[2].x = cx +  dx;
  694.   outs[2].y = cy - (dy+h);
  695.   outs[3].x = cx +  dx;
  696.   outs[3].y = cy +  dy;
  697.   outs[4].x = cx + (dx-w);
  698.   outs[4].y = cy + (dy+h);
  699.   outs[5].x = cx -  dx;
  700.   outs[5].y = cy + (dy+h);
  701.   outs[6].x = cx -  dx;
  702.   outs[6].y = cy -  dy;
  703.  
  704.   setfillstyle( SOLID_FILL, WHITE );
  705.   fillpoly( 7, (int far *)outs );
  706.  
  707.   outs[0].x = cx - (w/2);
  708.   outs[0].y = cy + h;
  709.   outs[1].x = cx + (w/2);
  710.   outs[1].y = cy + h;
  711.   outs[2].x = cx + (w/2);
  712.   outs[2].y = cy - h;
  713.   outs[3].x = cx - (w/2);
  714.   outs[3].y = cy - h;
  715.   outs[4].x = cx - (w/2);
  716.   outs[4].y = cy + h;
  717.  
  718.   setfillstyle( SOLID_FILL, BLUE );
  719.   fillpoly( 5, (int far *)outs );
  720.  
  721.   /*    Draw a Tesseract object on the screen using the LineRel and    */
  722.   /*    MoveRel drawing commands.                    */
  723.  
  724.   moveto( cx-dx, cy-dy );
  725.   linerel(  w, -h );
  726.   linerel(  3*w,    0 );
  727.   linerel(   0,  5*h );
  728.   linerel( -w,    h );
  729.   linerel( -3*w,    0 );
  730.   linerel(   0, -5*h );
  731.  
  732.   moverel( w, -h );
  733.   linerel(   0,  5*h );
  734.   linerel( w+(w/2), 0 );
  735.   linerel(   0, -3*h );
  736.   linerel( w/2,   -h );
  737.   linerel( 0, 5*h );
  738.  
  739.   moverel(  0, -5*h );
  740.   linerel( -(w+(w/2)), 0 );
  741.   linerel( 0, 3*h );
  742.   linerel( -w/2, h );
  743.  
  744.   moverel( w/2, -h );
  745.   linerel( w, 0 );
  746.  
  747.   moverel( 0, -2*h );
  748.   linerel( -w, 0 );
  749.  
  750.   Pause();                /* Wait for user's response     */
  751.  
  752. }
  753.  
  754. /*                                    */
  755. /*    PUTPIXELDEMO: Display a pattern of random dots on the screen    */
  756. /*    and pick them back up again.                    */
  757. /*                                    */
  758.  
  759. void PutPixelDemo(void)
  760. {
  761.   int seed = 1958;
  762.   int i, x, y, h, w, color;
  763.   struct viewporttype vp;
  764.  
  765.   MainWindow( "PutPixel / GetPixel Demonstration" );
  766.  
  767.   getviewsettings( &vp );
  768.   h = vp.bottom - vp.top;
  769.   w = vp.right    - vp.left;
  770.  
  771.   srand( seed );            /* Restart random # function    */
  772.  
  773.   for( i=0 ; i<5000 ; ++i ){        /* Put 5000 pixels on screen    */
  774.     x = 1 + random( w - 1 );        /* Generate a random location    */
  775.     y = 1 + random( h - 1 );
  776.     color = random( MaxColors );
  777.     putpixel( x, y, color );
  778.   }
  779.  
  780.   srand( seed );            /* Restart Random # at same #    */
  781.  
  782.   for( i=0 ; i<5000 ; ++i ){        /* Take the 5000 pixels off    */
  783.     x = 1 + random( w - 1 );        /* Generate a random location    */
  784.     y = 1 + random( h - 1 );
  785.     color = getpixel( x, y );        /* Read the color pixel     */
  786.     if( color == random( MaxColors ) )    /* Used to keep RANDOM in sync    */
  787.       putpixel( x, y, 0 );        /* Write pixel to BLACK     */
  788.   }
  789.  
  790.   Pause();                /* Wait for user's response     */
  791.  
  792. }
  793.  
  794. /*                                    */
  795. /*   PUTIMAGEDEMO                            */
  796. /*                                    */
  797. void PutImageDemo(void)
  798. {
  799.   static int r        = 20;
  800.   static int StartX = 100;
  801.   static int StartY = 50;
  802.  
  803.   struct viewporttype vp;
  804.   int PauseTime, x, y, ulx, uly, lrx, lry, size, i, width, height, step;
  805.   void *Saucer;
  806.  
  807.   MainWindow("GetImage / PutImage Demonstration");
  808.   getviewsettings( &vp );
  809.  
  810.   /* Draw Saucer */
  811.   setfillstyle( SOLID_FILL, getmaxcolor() );
  812.   fillellipse(StartX, StartY, r, (r/3)+2);
  813.   ellipse(StartX, StartY-4, 190, 357, r, r/3);
  814.  
  815.   line(StartX+7, StartY-6, StartX+10, StartY-12);
  816.   circle(StartX+10, StartY-12, 2);
  817.   line(StartX-7, StartY-6, StartX-10, StartY-12);
  818.   circle(StartX-10, StartY-12, 2);
  819.  
  820.  
  821.   /* Read saucer image */
  822.   ulx = StartX-(r+1);
  823.   uly = StartY-14;
  824.   lrx = StartX+(r+1);
  825.   lry = StartY+(r/3)+3;
  826.   width = lrx - ulx + 1;
  827.   height = lry - uly + 1;
  828.   size = imagesize(ulx, uly, lrx, lry);
  829.  
  830.   Saucer = malloc( size );
  831.   getimage(ulx, uly, lrx, lry, Saucer);
  832.   putimage(ulx, uly, Saucer, XOR_PUT);
  833.  
  834. /* Plot some "stars"  */
  835.   for ( i=0 ; i<1000; ++i )
  836.     putpixel(random(MaxX), random(MaxY), random( MaxColors-1 )+1);
  837.   x = MaxX / 2;
  838.   y = MaxY / 2;
  839.   PauseTime = 70;
  840.  
  841.   /* until a key is hit */
  842.   while ( !kbhit() ) {
  843.  
  844.     /* Draw the Saucer */
  845.     putimage(x, y, Saucer, XOR_PUT);             /*  draw image  */
  846.     delay(PauseTime);
  847.     putimage(x, y, Saucer, XOR_PUT);             /* erase image  */
  848.  
  849.     /* Move Saucer */
  850.  
  851.     step = random( 2*r );
  852.     if ((step/2) % 2 != 0 )
  853.       step = -1 * step;
  854.     x = x + step;
  855.     step = random( r );
  856.     if ((step/2) % 2 != 0 )
  857.       step = -1 * step;
  858.     y = y + step;
  859.  
  860.     if (vp.left + x + width - 1 > vp.right)
  861.       x = vp.right-vp.left-width + 1;
  862.     else
  863.       if (x < 0)
  864.     x = 0;
  865.     if (vp.top + y + height - 1 > vp.bottom)
  866.       y = vp.bottom-vp.top-height + 1;
  867.     else
  868.       if (y < 0)
  869.     y = 0;
  870.   }
  871.   free( Saucer );
  872.   Pause();
  873. }
  874.  
  875.  
  876. /*                                    */
  877. /*    LINETODEMO: Display a pattern using moveto and lineto commands. */
  878. /*                                    */
  879.  
  880. #define MAXPTS    15
  881.  
  882. void LineToDemo(void)
  883. {
  884.   struct viewporttype vp;
  885.   struct PTS points[MAXPTS];
  886.   int i, j, h, w, xcenter, ycenter;
  887.   int radius, angle, step;
  888.   double  rads;
  889.  
  890.   MainWindow( "MoveTo / LineTo Demonstration" );
  891.  
  892.   getviewsettings( &vp );
  893.   h = vp.bottom - vp.top;
  894.   w = vp.right    - vp.left;
  895.  
  896.   xcenter = w / 2;            /* Determine the center of circle */
  897.   ycenter = h / 2;
  898.   radius  = (h - 30) / (AspectRatio * 2);
  899.   step      = 360 / MAXPTS;        /* Determine # of increments    */
  900.  
  901.   angle = 0;                /* Begin at zero degrees    */
  902.   for( i=0 ; i<MAXPTS ; ++i ){        /* Determine circle intercepts    */
  903.     rads = (double)angle * PI / 180.0;    /* Convert angle to radians    */
  904.     points[i].x = xcenter + (int)( cos(rads) * radius );
  905.     points[i].y = ycenter - (int)( sin(rads) * radius * AspectRatio );
  906.     angle += step;            /* Move to next increment    */
  907.   }
  908.  
  909.   circle( xcenter, ycenter, radius );    /* Draw bounding circle     */
  910.  
  911.   for( i=0 ; i<MAXPTS ; ++i ){        /* Draw the cords to the circle */
  912.     for( j=i ; j<MAXPTS ; ++j ){    /* For each remaining intersect */
  913.       moveto(points[i].x, points[i].y); /* Move to beginning of cord    */
  914.       lineto(points[j].x, points[j].y); /* Draw the cord        */
  915.     }
  916.   }
  917.  
  918.   Pause();                /* Wait for user's response     */
  919.  
  920. }
  921.  
  922. /*                                    */
  923. /*    LINESTYLEDEMO: Display a pattern using all of the standard    */
  924. /*    line styles that are available.                 */
  925. /*                                    */
  926.  
  927. void LineStyleDemo(void)
  928. {
  929.   int style, step;
  930.   int x, y, w;
  931.   struct viewporttype vp;
  932.   char buffer[40];
  933.  
  934.   MainWindow( "Pre-defined line styles" );
  935.  
  936.   getviewsettings( &vp );
  937.   w = vp.right    - vp.left;
  938.  
  939.   x = 35;
  940.   y = 10;
  941.   step = w / 11;
  942.  
  943.   settextjustify( LEFT_TEXT, TOP_TEXT );
  944.   outtextxy( x, y, "Normal Width" );
  945.  
  946.   settextjustify( CENTER_TEXT, TOP_TEXT );
  947.  
  948.   for( style=0 ; style<4 ; ++style ){
  949.     setlinestyle( style, 0, NORM_WIDTH );
  950.     line( x, y+20, x, vp.bottom-40 );
  951.     itoa( style, buffer, 10 );
  952.     outtextxy( x, vp.bottom-30, buffer );
  953.     x += step;
  954.   }
  955.  
  956.   x += 2 * step;
  957.  
  958.   settextjustify( LEFT_TEXT, TOP_TEXT );
  959.   outtextxy( x, y, "Thick Width" );
  960.   settextjustify( CENTER_TEXT, TOP_TEXT );
  961.  
  962.   for( style=0 ; style<4 ; ++style ){
  963.     setlinestyle( style, 0, THICK_WIDTH );
  964.     line( x, y+20, x, vp.bottom-40 );
  965.     itoa( style, buffer, 10 );
  966.     outtextxy( x, vp.bottom-30, buffer );
  967.     x += step;
  968.   }
  969.  
  970.   settextjustify( LEFT_TEXT, TOP_TEXT );
  971.  
  972.   Pause();                /* Wait for user's response     */
  973.  
  974. }
  975.  
  976. /*                                    */
  977. /*    CRTMODEDEMO: Demonstrate the effects of the change mode     */
  978. /*    commands on the current screen.                 */
  979. /*                                    */
  980.  
  981. void CRTModeDemo(void)
  982. {
  983.   struct viewporttype vp;
  984.   int mode;
  985.  
  986.   MainWindow( "SetGraphMode / RestoreCRTMode demo" );
  987.   getviewsettings( &vp );
  988.   mode = getgraphmode();
  989.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  990.  
  991.   outtextxy( (vp.right-vp.left)/2, (vp.bottom-vp.top)/2,
  992.   "Now you are in graphics mode..." );
  993.   StatusLine( "Press any key for text mode..." );
  994.   getch();
  995.  
  996.   restorecrtmode();
  997.   printf( "Now you are in text mode.\n\n" );
  998.   printf( "Press any key to go back to graphics..." );
  999.   getch();
  1000.  
  1001.   setgraphmode( mode );
  1002.   MainWindow( "SetGraphMode / RestoreCRTMode demo" );
  1003.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  1004.   outtextxy( (vp.right-vp.left)/2, (vp.bottom-vp.top)/2,
  1005.   "Back in Graphics Mode..." );
  1006.  
  1007.   Pause();                /* Wait for user's response     */
  1008.  
  1009. }
  1010.  
  1011. /*                                    */
  1012. /*    USERLINESTYLEDEMO: Display line styles showing the user     */
  1013. /*    defined line style functions.                    */
  1014. /*                                    */
  1015.  
  1016. void UserLineStyleDemo(void)
  1017. {
  1018.   int x, y, i, h, flag;
  1019.   unsigned int style;
  1020.   struct viewporttype vp;
  1021.  
  1022.   MainWindow( "User defined line styles" );
  1023.  
  1024.   getviewsettings( &vp );
  1025.   h = vp.bottom - vp.top;
  1026.  
  1027.   x = 4;
  1028.   y = 10;
  1029.   style = 0;
  1030.   i = 0;
  1031.  
  1032.   settextjustify( CENTER_TEXT, TOP_TEXT );
  1033.   flag = TRUE;                /* Set the bits in this pass    */
  1034.  
  1035.   while( x < vp.right-2 ){        /* Draw lines across the screen */
  1036.  
  1037.     if( flag )                /* If flag, set bits...     */
  1038.       style = style | (1 << i);     /*    Set the Ith bit in word    */
  1039.     else                /* If no flag, clear bits    */
  1040.     style = style & !(0x8000 >> i);    /*    Clear the Ith bit in word */
  1041.  
  1042.     setlinestyle( USERBIT_LINE, style, NORM_WIDTH );
  1043.     line( x, y, x, h-y );        /* Draw the new line pattern    */
  1044.  
  1045.     x += 5;                /* Move the X location of line    */
  1046.     i = ++i % 16;            /* Advance to next bit pattern    */
  1047.  
  1048.     if( style == 0xffff ){        /* Are all bits set?        */
  1049.       flag = FALSE;            /*   begin removing bits    */
  1050.       i = 0;                /* Start with whole pattern    */
  1051.     }
  1052.     else{                /* Bits not all set...        */
  1053.       if( style == 0 )            /* Are all bits clear?        */
  1054.     flag = TRUE;            /*   begin setting bits     */
  1055.     }
  1056.   }
  1057.  
  1058.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1059.  
  1060.   Pause();                /* Wait for user's response     */
  1061.  
  1062. }
  1063.  
  1064. /*                                    */
  1065. /*    FILLSTYLEDEMO: Display the standard fill patterns available.    */
  1066. /*                                    */
  1067.  
  1068. void FillStyleDemo(void)
  1069. {
  1070.   int h, w, style;
  1071.   int i, j, x, y;
  1072.   struct viewporttype vp;
  1073.   char buffer[40];
  1074.  
  1075.   MainWindow( "Pre-defined Fill Styles" );
  1076.  
  1077.   getviewsettings( &vp );
  1078.   w = 2 * ((vp.right  +  1) / 13);
  1079.   h = 2 * ((vp.bottom - 10) / 10);
  1080.  
  1081.   x = w / 2;
  1082.   y = h / 2;        /* Leave 1/2 blk margin     */
  1083.   style = 0;
  1084.  
  1085.   for( j=0 ; j<3 ; ++j ){        /* Three rows of boxes        */
  1086.     for( i=0 ; i<4 ; ++i ){        /* Four column of boxes     */
  1087.       setfillstyle(style, MaxColors-1); /* Set the fill style and WHITE */
  1088.       bar( x, y, x+w, y+h );        /* Draw the actual box        */
  1089.       rectangle( x, y, x+w, y+h );    /* Outline the box        */
  1090.       itoa( style, buffer, 10 );    /* Convert style 3 to ASCII    */
  1091.       outtextxy( x+(w / 2), y+h+4, buffer );
  1092.       ++style;                /* Go on to next style #    */
  1093.       x += (w / 2) * 3;         /* Go to next column        */
  1094.     }                /* End of coulmn loop        */
  1095.     x = w / 2;                /* Put base back to 1st column    */
  1096.     y += (h / 2) * 3;            /* Advance to next row        */
  1097.   }                    /* End of Row loop        */
  1098.  
  1099.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1100.  
  1101.   Pause();                /* Wait for user's response     */
  1102.  
  1103. }
  1104.  
  1105. /*                                    */
  1106. /*    FILLPATTERNDEMO: Demonstrate how to use the user definable    */
  1107. /*    fill patterns.                            */
  1108. /*                                    */
  1109.  
  1110. void FillPatternDemo(void)
  1111. {
  1112.   int style;
  1113.   int h, w;
  1114.   int x, y, i, j;
  1115.   char buffer[40];
  1116.   struct viewporttype vp;
  1117.   static char patterns[][8] = {
  1118.     { 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55 },
  1119.     { 0x33, 0x33, 0xCC, 0xCC, 0x33, 0x33, 0xCC, 0xCC },
  1120.     { 0xF0, 0xF0, 0xF0, 0xF0, 0x0F, 0x0F, 0x0F, 0x0F },
  1121.     { 0x00, 0x10, 0x28, 0x44, 0x28, 0x10, 0x00, 0x00 },
  1122.     { 0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00 },
  1123.     { 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00 },
  1124.     { 0x00, 0x00, 0x3C, 0x3C, 0x3C, 0x3C, 0x00, 0x00 },
  1125.     { 0x00, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x00 },
  1126.     { 0x00, 0x00, 0x22, 0x08, 0x00, 0x22, 0x1C, 0x00 },
  1127.     { 0xFF, 0x7E, 0x3C, 0x18, 0x18, 0x3C, 0x7E, 0xFF },
  1128.     { 0x00, 0x10, 0x10, 0x7C, 0x10, 0x10, 0x00, 0x00 },
  1129.     { 0x00, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x00 }
  1130.   };
  1131.  
  1132.   MainWindow( "User Defined Fill Styles" );
  1133.  
  1134.   getviewsettings( &vp );
  1135.   w = 2 * ((vp.right  +  1) / 13);
  1136.   h = 2 * ((vp.bottom - 10) / 10);
  1137.  
  1138.   x = w / 2;
  1139.   y = h / 2;        /* Leave 1/2 blk margin     */
  1140.   style = 0;
  1141.  
  1142.   for( j=0 ; j<3 ; ++j ){        /* Three rows of boxes        */
  1143.     for( i=0 ; i<4 ; ++i ){        /* Four column of boxes     */
  1144.       setfillpattern( &patterns[style][0], MaxColors-1 );
  1145.       bar( x, y, x+w, y+h );        /* Draw the actual box        */
  1146.       rectangle( x, y, x+w, y+h );    /* Outline the box        */
  1147.       itoa( style, buffer, 10 );    /* Convert style 3 to ASCII    */
  1148.       outtextxy( x+(w / 2), y+h+4, buffer );
  1149.       ++style;                /* Go on to next style #    */
  1150.       x += (w / 2) * 3;         /* Go to next column        */
  1151.     }                /* End of coulmn loop        */
  1152.     x = w / 2;                /* Put base back to 1st column    */
  1153.     y += (h / 2) * 3;            /* Advance to next row        */
  1154.   }                    /* End of Row loop        */
  1155.  
  1156.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1157.  
  1158.   Pause();                /* Wait for user's response     */
  1159.  
  1160. }
  1161.  
  1162. /*                                    */
  1163. /*    POLYDEMO: Display a random pattern of polygons on the screen    */
  1164. /*    until the user says enough.                    */
  1165. /*                                    */
  1166.  
  1167. void PaletteDemo(void)
  1168. {
  1169.   int i, j, x, y, color;
  1170.   struct viewporttype vp;
  1171.   int height, width;
  1172.  
  1173.   MainWindow( "Palette Demonstration" );
  1174.   StatusLine( "Press any key to continue, ESC to Abort" );
  1175.  
  1176.   getviewsettings( &vp );
  1177.   width  = (vp.right - vp.left) / 15;    /* get width of the box     */
  1178.   height = (vp.bottom - vp.top) / 10;    /* Get the height of the box    */
  1179.  
  1180.   x = y = 0;                /* Start in upper corner    */
  1181.   color = 1;                /* Begin at 1st color        */
  1182.  
  1183.   for( j=0 ; j<10 ; ++j ){        /* For 10 rows of boxes     */
  1184.     for( i=0 ; i<15 ; ++i ){        /* For 15 columns of boxes    */
  1185.       setfillstyle( SOLID_FILL, color++ );    /* Set the color of box */
  1186.       bar( x, y, x+width, y+height );        /* Draw the box     */
  1187.       x += width + 1;                /* Advance to next col    */
  1188.       color = 1 + (color % (MaxColors - 2));    /* Set new color    */
  1189.     }                /* End of COLUMN loop        */
  1190.     x = 0;                /* Goto 1st column        */
  1191.     y += height + 1;            /* Goto next row        */
  1192.   }                    /* End of ROW loop        */
  1193.  
  1194.   while( !kbhit() ){            /* Until user enters a key...    */
  1195.     setpalette( 1+random(MaxColors - 2), random( 65 ) );
  1196.   }
  1197.  
  1198.   setallpalette( &palette );
  1199.  
  1200.   Pause();                /* Wait for user's response     */
  1201.  
  1202. }
  1203.  
  1204. /*                                    */
  1205. /*    POLYDEMO: Display a random pattern of polygons on the screen    */
  1206. /*    until the user says enough.                    */
  1207. /*                                    */
  1208.  
  1209. #define MaxPts        6        /* Maximum # of pts in polygon    */
  1210.  
  1211. void PolyDemo(void)
  1212. {
  1213.   struct PTS poly[ MaxPts ];        /* Space to hold datapoints    */
  1214.   int color;                /* Current drawing color    */
  1215.   int i;
  1216.  
  1217.   MainWindow( "DrawPoly / FillPoly Demonstration" );
  1218.   StatusLine( "ESC Aborts - Press a Key to stop" );
  1219.  
  1220.   while( !kbhit() ){            /* Repeat until a key is hit    */
  1221.  
  1222.     color = 1 + random( MaxColors-1 );    /* Get a random color # (no blk)*/
  1223.     setfillstyle( random(10), color );    /* Set a random line style    */
  1224.     setcolor( color );            /* Set the desired color    */
  1225.  
  1226.     for( i=0 ; i<(MaxPts-1) ; i++ ){    /* Determine a random polygon    */
  1227.       poly[i].x = random( MaxX );    /* Set the x coord of point    */
  1228.       poly[i].y = random( MaxY );    /* Set the y coord of point    */
  1229.     }
  1230.  
  1231.     poly[i].x = poly[0].x;        /* last point = first point    */
  1232.     poly[i].y = poly[1].y;
  1233.  
  1234.     fillpoly( MaxPts, (int far *)poly );    /* Draw the actual polygon        */
  1235.   }                    /* End of WHILE not KBHIT    */
  1236.  
  1237.   Pause();                /* Wait for user's response     */
  1238.  
  1239. }
  1240.  
  1241.  
  1242. /*                                    */
  1243. /*    SAYGOODBYE: Give a closing screen to the user before leaving.    */
  1244. /*                                    */
  1245.  
  1246. void SayGoodbye(void)
  1247. {
  1248.   struct viewporttype viewinfo;     /* Structure to read viewport    */
  1249.   int h, w;
  1250.  
  1251.   MainWindow( "== Finale ==" );
  1252.  
  1253.   getviewsettings( &viewinfo );     /* Read viewport settings    */
  1254.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  1255.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  1256.  
  1257.   h = viewinfo.bottom - viewinfo.top;
  1258.   w = viewinfo.right  - viewinfo.left;
  1259.   outtextxy( w/2, h/2, "That's all, folks!" );
  1260.  
  1261.   StatusLine( "Press any key to EXIT" );
  1262.   getch();
  1263.  
  1264.   cleardevice();            /* Clear the graphics screen    */
  1265.  
  1266. }
  1267.  
  1268. /*                                    */
  1269. /*    PAUSE: Pause until the user enters a keystroke. If the        */
  1270. /*    key is an ESC, then exit program, else simply return.        */
  1271. /*                                    */
  1272.  
  1273. void Pause(void)
  1274. {
  1275.   static char msg[] = "Esc aborts or press a key...";
  1276.   int c;
  1277.  
  1278.   StatusLine( msg );            /* Put msg at bottom of screen    */
  1279.  
  1280.   c = getch();                /* Read a character from kbd    */
  1281.  
  1282.   if( ESC == c ){            /* Does user wish to leave?    */
  1283.     closegraph();            /* Change to text mode        */
  1284.     exit( 1 );                /* Return to OS         */
  1285.   }
  1286.  
  1287.   if( 0 == c ){             /* Did use hit a non-ASCII key? */
  1288.     c = getch();            /* Read scan code for keyboard    */
  1289.   }
  1290.  
  1291.   cleardevice();            /* Clear the screen        */
  1292.  
  1293. }
  1294.  
  1295. /*                                    */
  1296. /*    MAINWINDOW: Establish the main window for the demo and set    */
  1297. /*    a viewport for the demo code.                    */
  1298. /*                                    */
  1299.  
  1300. void MainWindow( char *header )
  1301. {
  1302.   int height;
  1303.  
  1304.   cleardevice();            /* Clear graphics screen    */
  1305.   setcolor( MaxColors - 1 );        /* Set current color to white    */
  1306.   setviewport( 0, 0, MaxX, MaxY, 1 );    /* Open port to full screen    */
  1307.  
  1308.   height = textheight( "H" );           /* Get basic text height        */
  1309.  
  1310.   changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  1311.   settextjustify( CENTER_TEXT, TOP_TEXT );
  1312.   outtextxy( MaxX/2, 2, header );
  1313.   setviewport( 0, height+4, MaxX, MaxY-(height+4), 1 );
  1314.   DrawBorder();
  1315.   setviewport( 1, height+5, MaxX-1, MaxY-(height+5), 1 );
  1316.  
  1317. }
  1318.  
  1319. /*                                    */
  1320. /*    STATUSLINE: Display a status line at the bottom of the screen.    */
  1321. /*                                    */
  1322.  
  1323. void StatusLine( char *msg )
  1324. {
  1325.   int height;
  1326.  
  1327.   setviewport( 0, 0, MaxX, MaxY, 1 );    /* Open port to full screen    */
  1328.   setcolor( MaxColors - 1 );        /* Set current color to white    */
  1329.  
  1330.   changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  1331.   settextjustify( CENTER_TEXT, TOP_TEXT );
  1332.   setlinestyle( SOLID_LINE, 0, NORM_WIDTH );
  1333.   setfillstyle( EMPTY_FILL, 0 );
  1334.  
  1335.   height = textheight( "H" );           /* Detemine current height      */
  1336.   bar( 0, MaxY-(height+4), MaxX, MaxY );
  1337.   rectangle( 0, MaxY-(height+4), MaxX, MaxY );
  1338.   outtextxy( MaxX/2, MaxY-(height+2), msg );
  1339.   setviewport( 1, height+5, MaxX-1, MaxY-(height+5), 1 );
  1340.  
  1341. }
  1342.  
  1343. /*                                    */
  1344. /*    DRAWBORDER: Draw a solid single line around the current     */
  1345. /*    viewport.                            */
  1346. /*                                    */
  1347.  
  1348. void DrawBorder(void)
  1349. {
  1350.   struct viewporttype vp;
  1351.  
  1352.   setcolor( MaxColors - 1 );        /* Set current color to white    */
  1353.  
  1354.   setlinestyle( SOLID_LINE, 0, NORM_WIDTH );
  1355.  
  1356.   getviewsettings( &vp );
  1357.   rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top );
  1358.  
  1359. }
  1360.  
  1361. /*                                    */
  1362. /*    CHANGETEXTSTYLE: similar to settextstyle, but checks for    */
  1363. /*    errors that might occur whil loading the font file.        */
  1364. /*                                    */
  1365.  
  1366. void changetextstyle(int font, int direction, int charsize)
  1367. {
  1368.   int ErrorCode;
  1369.  
  1370.   graphresult();            /* clear error code        */
  1371.   settextstyle(font, direction, charsize);
  1372.   ErrorCode = graphresult();        /* check result         */
  1373.   if( ErrorCode != grOk ){        /* if error occured        */
  1374.     closegraph();
  1375.     printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) );
  1376.     exit( 1 );
  1377.   }
  1378. }
  1379.  
  1380. /*                                    */
  1381. /*    GPRINTF: Used like PRINTF except the output is sent to the    */
  1382. /*    screen in graphics mode at the specified co-ordinate.        */
  1383. /*                                    */
  1384.  
  1385. int gprintf( int *xloc, int *yloc, char *fmt, ... )
  1386. {
  1387.   va_list  argptr;            /* Argument list pointer    */
  1388.   char str[140];            /* Buffer to build sting into    */
  1389.   int cnt;                /* Result of SPRINTF for return */
  1390.  
  1391.   va_start( argptr, format );        /* Initialize va_ functions    */
  1392.  
  1393.   cnt = vsprintf( str, fmt, argptr );    /* prints string to buffer    */
  1394.   outtextxy( *xloc, *yloc, str );    /* Send string in graphics mode */
  1395.   *yloc += textheight( "H" ) + 2;       /* Advance to next line         */
  1396.  
  1397.   va_end( argptr );            /* Close va_ functions        */
  1398.  
  1399.   return( cnt );            /* Return the conversion count    */
  1400.  
  1401. }
  1402.