Guest User

Untitled

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