home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c495 / watcm951.arj / CPPSRC.WPK / GRDEMO.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-16  |  9.1 KB  |  382 lines

  1. #include <graph.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <conio.h>
  5. #include <math.h>
  6. #include <dos.h>
  7.  
  8.  
  9. #define PI          3.141592654
  10.  
  11. struct videoconfig  VC;
  12. char            Main_Title[] = "Business Applications";
  13. char            Y_Axis_Title[] = "Net Gain (x $1000)";
  14. char            X_Axis_Title[] = "Fiscal Year";
  15. int             TextColour;
  16. int             TextColour2;
  17. int             BorderColour;
  18. int             TitleColour;
  19. unsigned char   Masks[ 8 ][ 8 ] = {
  20.     { 0xff, 0x81, 0xff, 0x42, 0xff, 0x24, 0xff, 0x18 },
  21.     { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 },
  22.     { 0x99, 0x18, 0x24, 0xc3, 0xc3, 0x24, 0x18, 0x99 },
  23.     { 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55 },
  24.     { 0x88, 0x44, 0x22, 0x11, 0x88, 0x44, 0x22, 0x11 },
  25.     { 0x18, 0xdb, 0x3c, 0x18, 0x18, 0x3c, 0xdb, 0x18 },
  26.     { 0x11, 0x22, 0x44, 0x88, 0x11, 0x22, 0x44, 0x88 },
  27.     { 0x18, 0x18, 0x18, 0xff, 0xff, 0x18, 0x18, 0x18 }
  28. };
  29.  
  30. #define NUMSECT     10
  31. int                 Values[ NUMSECT ] = {   /* Scaled with max of 100 */
  32.     20, 30, 40, 35, 50, 60, 75, 70, 80, 90
  33. };
  34.  
  35. static void Do_Demo1(void);
  36. static void Press_any_key(void);
  37. static void DrawText( short width, short y );
  38. static int InitScreen(void);
  39. static void FadeColors(void);
  40. void Do_Demo2(void);
  41. static void Title(void);
  42. static void BarGraph(void);
  43. static void PieGraph(void);
  44.  
  45. int main( void )
  46. /*============*/
  47. {
  48.     if( !InitScreen() ) {
  49.     puts( "No graphics adapter present" );
  50.     return( 1 );
  51.     }
  52.     Do_Demo1();
  53.     Press_any_key();
  54.     Do_Demo2();
  55.     Press_any_key();
  56.     _setvideomode( _DEFAULTMODE );      /* reset the screen */
  57.     return( 0 );
  58. }
  59.  
  60.  
  61. static void Do_Demo1()
  62. /*==================*/
  63. {
  64.     int                 width, y;
  65.  
  66. /*  sweep text in from top & bottom of screen, gradually increasing
  67.     character size as the center of the screen is approached. */
  68.  
  69.     _setcolor( TextColour );
  70.     width = 0;
  71.     for( y = 5; y < VC.numypixels / 2 - 10; y += 5, ++width ) {
  72.     DrawText( width, y );
  73.     }
  74.  
  75. /*  draw text over final positions using a different color index */
  76.  
  77.     _setcolor( TextColour2 );
  78.     DrawText( width, y );
  79.  
  80. /*  draw a border around the screen */
  81.  
  82.     _setcolor( BorderColour );
  83.     _rectangle( _GBORDER, 0, 0, VC.numxpixels - 1, VC.numypixels - 1 );
  84. #if defined( _98RES16COLOR )
  85.     FadeColors();
  86. #else
  87.     if( VC.adapter > _MCGA ) {
  88.     FadeColors();
  89.     }
  90. #endif
  91. }
  92.  
  93.  
  94. static void Press_any_key()
  95. /*=======================*/
  96.  
  97. /*  wait for keyboard input */
  98. {
  99.     _settextposition( VC.numtextrows, VC.numtextcols - 16 );
  100.     _outtext( "Press any key..." );
  101.     getch();
  102. }
  103.  
  104.  
  105. static void DrawText( short width, short y )
  106. /*==========================================
  107.  
  108.     This routine displays the text strings. */
  109.  
  110. {
  111.     int                 xc;
  112.  
  113.     xc = VC.numxpixels / 2;
  114.     _setcharsize( width, width * 3 / 2 );
  115.     _settextalign( _CENTER, _BOTTOM );
  116.     _grtext( xc, y, "WATCOM C" );
  117.     _setcharsize( width, width );
  118.     _settextalign( _CENTER, _TOP );
  119.     _grtext( xc, VC.numypixels - y, "GRAPHICS" );
  120. }
  121.  
  122.  
  123. static int InitScreen( void )
  124. /*===========================
  125.  
  126.     This routine selects the best video mode for a given adapter. */
  127.  
  128. {
  129.     int                 mode;
  130.  
  131. #if defined( _98RES16COLOR )
  132.     mode = _MAXCOLORMODE;
  133. #else
  134.     _getvideoconfig( &VC );
  135.     switch( VC.adapter ) {
  136.     case _VGA :
  137.     case _SVGA :
  138.     mode = _VRES16COLOR;
  139.     break;
  140.     case _MCGA :
  141.     mode = _MRES256COLOR;
  142.     break;
  143.     case _EGA :
  144.     if( VC.monitor == _MONO ) {
  145.         mode = _ERESNOCOLOR;
  146.     } else {
  147.         mode = _ERESCOLOR;
  148.     }
  149.     break;
  150.     case _CGA :
  151.     mode = _MRES4COLOR;
  152.     break;
  153.     case _HERCULES :
  154.     mode = _HERCMONO;
  155.     break;
  156.     default :
  157.     return( 0 );          /* report insufficient hardware */
  158.     }
  159. #endif
  160.  
  161.     if( _setvideomode( mode ) == 0 ) {
  162.     return( 0 );
  163.     }
  164.     _getvideoconfig( &VC );
  165.     if( VC.numcolors < 4 ) {
  166.     TextColour = 1;
  167.     TextColour2 = 1;
  168.     BorderColour = 1;
  169.     } else {
  170.     TextColour = 1;
  171.     TextColour2 = 3;
  172.     BorderColour = 2;
  173.     }
  174. #if defined( _98RES16COLOR )
  175.     /* set up new colours */
  176.     _remappalette( TextColour, _98BLUE );       /* light blue */
  177.     _remappalette( TextColour2, _98BLUE );      /* light blue */
  178.     _remappalette( BorderColour, _98BLACK );    /* black      */
  179. #else
  180.     if( VC.adapter >= _MCGA ) {
  181.     /* set up new colours */
  182.     _remappalette( TextColour, 0x3f0000 );  /* light blue */
  183.     _remappalette( TextColour2, 0x3f0000 ); /* light blue */
  184.     _remappalette( BorderColour, _BLACK );  /* black      */
  185.     }
  186. #endif
  187.     return( 1 );
  188. }
  189.  
  190.  
  191. #if defined( _98RES16COLOR )
  192.   #define _MAX 15   // 4 colour bits
  193. #else
  194.   #define _MAX 63   // 6 colour bits
  195. #endif
  196.  
  197.  
  198. static void FadeColors( void )
  199. /*============================
  200.  
  201.     This routine gradually fades the background text, brightening
  202.     the foreground text and the border at the same time. */
  203.  
  204. {
  205.     int                 i;
  206.     long                red, blue, green;
  207.  
  208.     for( i = 1; i <= _MAX; i++ ) {
  209.     red = i;
  210.     green = i << 8;
  211.     blue = (long) ( _MAX - i ) << 16;
  212.     _remappalette( TextColour, blue );
  213.     _remappalette( TextColour2, blue + green );
  214.     _remappalette( BorderColour, red );
  215. #if defined( _98RES16COLOR )
  216.     delay( 125 );
  217. #endif
  218.     }
  219. }
  220.  
  221.  
  222. void Do_Demo2( void )
  223. /*===================
  224.  
  225.     This program draws bar and pie graphs for the
  226.     data specified above. */
  227.  
  228. {
  229.     _setvideomode( _MAXCOLORMODE );
  230.     _getvideoconfig( &VC ); /* fill videoconfig structure */
  231.     TitleColour = ( VC.numcolors - 1 ) % 16;
  232.     Title();
  233.     BarGraph();
  234.     PieGraph();
  235. }
  236.  
  237.  
  238. static void Title( void )
  239. /*=======================
  240.  
  241.     Draw main title and graph boxes. */
  242.  
  243. {
  244.     _setcolor( TitleColour );
  245.     _settextalign( _CENTER, _TOP );
  246.     _setcharsize_w( 0.08, 1.0 / strlen( Main_Title ) );
  247.     _grtext_w( 0.5, 1.0, Main_Title );
  248.     _rectangle_w( _GBORDER, 0.00, 0.00, 0.49, 0.90 );     // left half
  249.     _rectangle_w( _GBORDER, 0.51, 0.00, 1.00, 0.90 );     // right half
  250. }
  251.  
  252.  
  253. static void DoAxes( float xleft, float ybottom, float xlen, float ylen )
  254. /*======================================================================
  255.  
  256.     Draw axes of bar graph. */
  257.  
  258. {
  259.     float               xright, ytop;
  260.     float               y, yinc;
  261.  
  262.     xright  = xleft + xlen;
  263.     ytop = ybottom + ylen;
  264.  
  265. /*  Draw the axes */
  266.  
  267.     _moveto_w( xleft,  ytop );
  268.     _lineto_w( xleft,  ybottom );
  269.     _lineto_w( xright, ybottom );
  270.  
  271. /*  Draw the tick marks on the y-axis */
  272.  
  273.     yinc = ylen / 10;
  274.     for( y = ybottom; y < ytop; y += yinc ) {
  275.     _moveto_w( xleft, y );
  276.     _lineto_w( xleft - 0.01, y );
  277.     }
  278.  
  279. /*  Draw the x-axis and y-axis titles */
  280.  
  281.     _settextalign( _CENTER, _HALF );
  282.     _settextorient( 0, 1 );
  283.     _setcharsize_w( 0.06, ylen / strlen( Y_Axis_Title ) *
  284.             ( (float) VC.numypixels / VC.numxpixels ) );
  285.     _grtext_w( xleft - 0.05, ybottom + ylen / 2, Y_Axis_Title );
  286.     _setcharsize_w( 0.06, xlen / strlen( X_Axis_Title ) );
  287.     _settextorient( 1, 0 );
  288.     _grtext_w( xleft + xlen / 2, ybottom - 0.05, X_Axis_Title );
  289. }
  290.  
  291.  
  292. static void DoBars( float xleft, float ybottom, float xlen, float ylen )
  293. /*======================================================================
  294.  
  295.     Draw bars of graph. */
  296.  
  297. {
  298.     int                 i;
  299.     float               x1, y1;
  300.     float               x2, y2;
  301.     float               bar_width;
  302.  
  303.     bar_width = ( 2 * xlen ) / ( 3 * NUMSECT + 1 );
  304.     y1 = ybottom + 1.0 / VC.numypixels;
  305.     for( i = 0; i < NUMSECT; ++i ) {
  306.     x1 = xleft + ( 3 * i + 1 ) * bar_width / 2;
  307.     x2 = x1 + bar_width;
  308.     y2 = y1 + ylen * Values[ i ] / 100;
  309.     _setcolor( i % ( VC.numcolors - 1 ) + 1 );
  310.     _setfillmask( Masks[ i % 8 ] );
  311.     _rectangle_w( _GFILLINTERIOR, x1, y1, x2, y2 );
  312.     _rectangle_w( _GBORDER, x1, y1, x2, y2 );
  313.     }
  314. }
  315.  
  316.  
  317. static void BarGraph( void )
  318. /*==========================
  319.  
  320.     Draw bar graph on left side of the screen. */
  321.  
  322. {
  323.     DoAxes( 0.10, 0.15, 0.35, 0.7 );
  324.     DoBars( 0.10, 0.15, 0.35, 0.7 );
  325. }
  326.  
  327.  
  328. static void PieGraph( void )
  329. /*==========================
  330.  
  331.     Draw pie graph. */
  332.  
  333. {
  334.     int                 i;
  335.     float               x1, y1;
  336.     float               x2, y2;
  337.     float               x3, y3;
  338.     float               x4, y4;
  339.     float               xc, yc;
  340.     float               xradius, yradius;
  341.     float               theta;
  342.     long                total;
  343.  
  344. /*  Calculate data for pie graph. */
  345.  
  346.     total = 0;
  347.     for( i = 0; i < NUMSECT; ++i ) {
  348.     total += Values[ i ];
  349.     }
  350.  
  351. /*  Calculate centre and radius of pie */
  352.  
  353.     xc = 0.75;
  354.     yc = 0.45;
  355.     xradius = 0.20;
  356.     yradius = 0.20 * 4 / 3;
  357.  
  358. /*  Calculate bounding rectangle */
  359.  
  360.     x1 = xc - xradius;
  361.     y1 = yc - yradius;
  362.     x2 = xc + xradius;
  363.     y2 = yc + yradius;
  364.  
  365. /*  Draw the slices */
  366.  
  367.     x3 = xc + xradius;
  368.     y3 = yc;
  369.     theta = 0.0;
  370.     for( i = 0; i < NUMSECT; ++i ) {
  371.     theta += Values[ i ] * 2 * PI / total;
  372.     x4 = xc + xradius * cos( theta );
  373.     y4 = yc + yradius * sin( theta );
  374.     _setcolor( i % ( VC.numcolors - 1 ) + 1 );
  375.     _setfillmask( Masks[ i % 8 ] );
  376.     _pie_w( _GFILLINTERIOR, x1, y1, x2, y2, x3, y3, x4, y4 );
  377.     _pie_w( _GBORDER, x1, y1, x2, y2, x3, y3, x4, y4 );
  378.     x3 = x4;
  379.     y3 = y4;
  380.     }
  381. }
  382.