home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c496 / 1.img / SOURCE.WPK / GRDEMO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-20  |  8.6 KB  |  382 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <conio.h>
  5. #include <graph.h>
  6. #include <math.h>
  7. #include <time.h>
  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        TitleColour;
  18. int        BorderColour;
  19. int        AxisColour;
  20. unsigned char    Masks[ 8 ][ 8 ] = {
  21.     { 0xff, 0x81, 0xff, 0x42, 0xff, 0x24, 0xff, 0x18 },
  22.     { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 },
  23.     { 0x99, 0x18, 0x24, 0xc3, 0xc3, 0x24, 0x18, 0x99 },
  24.     { 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55 },
  25.     { 0x88, 0x44, 0x22, 0x11, 0x88, 0x44, 0x22, 0x11 },
  26.     { 0x18, 0xdb, 0x3c, 0x18, 0x18, 0x3c, 0xdb, 0x18 },
  27.     { 0x11, 0x22, 0x44, 0x88, 0x11, 0x22, 0x44, 0x88 },
  28.     { 0x18, 0x18, 0x18, 0xff, 0xff, 0x18, 0x18, 0x18 }
  29. };
  30.  
  31. #define NUMSECT     10
  32. int            Values[ NUMSECT ] = {   /* Scaled with max of 100 */
  33.     20, 30, 40, 35, 50, 60, 75, 70, 80, 90
  34. };
  35.  
  36.  
  37. int main( void )
  38. /*============*/
  39. {
  40.     if( !InitScreen() ) {
  41.     puts( "No graphics adapter present" );
  42.     return( 1 );
  43.     }
  44.     Do_Demo1();
  45.     Press_any_key();
  46.     Do_Demo2();
  47.     Press_any_key();
  48.     _setvideomode( _DEFAULTMODE );    /* reset the screen */
  49.     return( 0 );
  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.  
  76.     if( VC.adapter > _MCGA ) {
  77.     FadeColors();
  78.     }
  79. }
  80.  
  81. static void Press_any_key()
  82. /*=======================*/
  83.  
  84. /*  wait for keyboard input */
  85. {
  86.     _settextposition( VC.numtextrows, VC.numtextcols - 16 );
  87.     _outtext( "Press any key..." );
  88.     getch();
  89. }
  90.  
  91.  
  92. static void DrawText( short width, short y )
  93. /*==========================================
  94.  
  95.     This routine displays the text strings. */
  96.  
  97. {
  98.     int         xc;
  99.  
  100.     xc = VC.numxpixels / 2;
  101.     _setcharsize( width, width * 3 / 2 );
  102.     _settextalign( _CENTER, _BOTTOM );
  103.     _grtext( xc, y, "WATCOM C" );
  104.     _setcharsize( width, width );
  105.     _settextalign( _CENTER, _TOP );
  106.     _grtext( xc, VC.numypixels - y, "GRAPHICS" );
  107. }
  108.  
  109.  
  110. static int InitScreen( void )
  111. /*===========================
  112.  
  113.     This routine selects the best video mode for a given adapter. */
  114.  
  115. {
  116.     int         mode;
  117.  
  118.     _getvideoconfig( &VC );
  119.     switch( VC.adapter ) {
  120.     case _VGA :
  121.     mode = _VRES16COLOR;
  122.     break;
  123.     case _MCGA :
  124.     mode = _MRES256COLOR;
  125.     break;
  126.     case _EGA :
  127.     if( VC.monitor == _MONO ) {
  128.         mode = _ERESNOCOLOR;
  129.     } else {
  130.         mode = _ERESCOLOR;
  131.     }
  132.     break;
  133.     case _CGA :
  134.     mode = _MRES4COLOR;
  135.     break;
  136.     case _HERCULES :
  137.     mode = _HERCMONO;
  138.     break;
  139.     default :
  140.     return( 0 );          /* report insufficient hardware */
  141.     }
  142.  
  143.     if( _setvideomode( mode ) == 0 ) {
  144.     return( 0 );
  145.     }
  146.     _getvideoconfig( &VC );
  147.     if( VC.numcolors < 4 ) {
  148.     TextColour = 1;
  149.     TextColour2 = 1;
  150.     BorderColour = 1;
  151.     } else {
  152.     TextColour = 1;
  153.     TextColour2 = 3;
  154.     BorderColour = 2;
  155.     }
  156.     if( VC.adapter >= _MCGA ) {
  157.     /* set up new colours */
  158.     _remappalette( TextColour, 0x3f0000 );  /* light blue */
  159.     _remappalette( TextColour2, 0x3f0000 ); /* light blue */
  160.     _remappalette( BorderColour, _BLACK );  /* black     */
  161.     }
  162.     return( 1 );
  163. }
  164.  
  165.  
  166. static void FadeColors( void )
  167. /*============================
  168.  
  169.     This routine gradually fades the background text, brightening
  170.     the foreground text and the border at the same time. */
  171.  
  172. {
  173.     int         i;
  174.     long        red, blue, green;
  175.  
  176.     for( i = 1; i < 64; i++ ) {
  177.     red = i;
  178.     green = i << 8;
  179.     blue = (long) ( 63 - i ) << 16;
  180.     _remappalette( TextColour, blue );
  181.     _remappalette( TextColour2, blue + green );
  182.     _remappalette( BorderColour, red );
  183.     }
  184. }
  185.  
  186. void Do_Demo2( void )
  187. /*===================
  188.  
  189.     This program draws bar and pie graphs for the
  190.     data specified above. */
  191.  
  192. {
  193.     _setvideomode( _MAXCOLORMODE );
  194.     _getvideoconfig( &VC ); /* fill videoconfig structure */
  195.     NewColours();
  196.  
  197.     Title();
  198.     BarGraph();
  199.     PieGraph();
  200. }
  201.  
  202.  
  203. static long        NewColourSet[] = {
  204.     _BLACK, _LIGHTCYAN, _LIGHTMAGENTA, _BRIGHTWHITE,
  205.     _GREEN, _LIGHTBLUE, _GRAY, _LIGHTRED,
  206.     _CYAN, _YELLOW, _RED, _LIGHTGREEN,
  207.     _BROWN, _MAGENTA, _BLUE, _WHITE
  208. };
  209.  
  210.  
  211. static void NewColours( void )
  212. /*============================
  213.  
  214.     Select a new colour set. */
  215.  
  216. {
  217.     int         i;
  218.  
  219.     if( VC.adapter >= _MCGA ) {
  220.     for( i = 0; i < 16; ++i ) {
  221.         _remappalette( i, NewColourSet[ i ] );
  222.     }
  223.     }
  224.     if( VC.numcolors == 2 ) {
  225.     AxisColour = 1;
  226.     TitleColour = 1;
  227.     BorderColour = 1;
  228.     } else {
  229.     AxisColour = 1;
  230.     TitleColour = 2;
  231.     BorderColour = 3;
  232.     }
  233. }
  234.  
  235.  
  236. static void Title( void )
  237. /*=======================
  238.  
  239.     Draw main title and graph boxes. */
  240.  
  241. {
  242.     _setcolor( BorderColour );
  243.     _settextalign( _CENTER, _TOP );
  244.     _setcharsize_w( 0.08, 1.0 / strlen( Main_Title ) );
  245.     _grtext_w( 0.5, 1.0, Main_Title );
  246.     _rectangle_w( _GBORDER, 0.00, 0.00, 0.49, 0.90 );      // left half
  247.     _rectangle_w( _GBORDER, 0.51, 0.00, 1.00, 0.90 );      // right half
  248. }
  249.  
  250.  
  251. static void DoAxes( float xleft, float ybottom, float xlen, float ylen )
  252. /*======================================================================
  253.  
  254.     Draw axes of bar graph. */
  255.  
  256. {
  257.     float        xright, ytop;
  258.     float        y, yinc;
  259.  
  260.     xright  = xleft + xlen;
  261.     ytop = ybottom + ylen;
  262.  
  263. /*  Draw the axes */
  264.  
  265.     _setcolor( AxisColour );
  266.     _moveto_w( xleft,  ytop );
  267.     _lineto_w( xleft,  ybottom );
  268.     _lineto_w( xright, ybottom );
  269.  
  270. /*  Draw the tick marks on the y-axis */
  271.  
  272.     yinc = ylen / 10;
  273.     for( y = ybottom; y < ytop; y += yinc ) {
  274.     _moveto_w( xleft, y );
  275.     _lineto_w( xleft - 0.01, y );
  276.     }
  277.  
  278. /*  Draw the x-axis and y-axis titles */
  279.  
  280.     _setcolor( TitleColour );
  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;
  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.