home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / tc20 / bgidemo.c < prev    next >
Text File  |  1988-08-28  |  40KB  |  1,401 lines

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