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