home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c015 / 1.ddi / BGIDEMO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-13  |  48.7 KB  |  1,409 lines

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