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