home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c500 / 1.ddi / SRC386.WPK / GRDEMO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-28  |  8.6 KB  |  383 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.     case _SVGA :
  122.     mode = _VRES16COLOR;
  123.     break;
  124.     case _MCGA :
  125.     mode = _MRES256COLOR;
  126.     break;
  127.     case _EGA :
  128.     if( VC.monitor == _MONO ) {
  129.         mode = _ERESNOCOLOR;
  130.     } else {
  131.         mode = _ERESCOLOR;
  132.     }
  133.     break;
  134.     case _CGA :
  135.     mode = _MRES4COLOR;
  136.     break;
  137.     case _HERCULES :
  138.     mode = _HERCMONO;
  139.     break;
  140.     default :
  141.     return( 0 );          /* report insufficient hardware */
  142.     }
  143.  
  144.     if( _setvideomode( mode ) == 0 ) {
  145.     return( 0 );
  146.     }
  147.     _getvideoconfig( &VC );
  148.     if( VC.numcolors < 4 ) {
  149.     TextColour = 1;
  150.     TextColour2 = 1;
  151.     BorderColour = 1;
  152.     } else {
  153.     TextColour = 1;
  154.     TextColour2 = 3;
  155.     BorderColour = 2;
  156.     }
  157.     if( VC.adapter >= _MCGA ) {
  158.     /* set up new colours */
  159.     _remappalette( TextColour, 0x3f0000 );    /* light blue */
  160.     _remappalette( TextColour2, 0x3f0000 ); /* light blue */
  161.     _remappalette( BorderColour, _BLACK );    /* black     */
  162.     }
  163.     return( 1 );
  164. }
  165.  
  166.  
  167. static void FadeColors( void )
  168. /*============================
  169.  
  170.     This routine gradually fades the background text, brightening
  171.     the foreground text and the border at the same time. */
  172.  
  173. {
  174.     int         i;
  175.     long        red, blue, green;
  176.  
  177.     for( i = 1; i < 64; i++ ) {
  178.     red = i;
  179.     green = i << 8;
  180.     blue = (long) ( 63 - i ) << 16;
  181.     _remappalette( TextColour, blue );
  182.     _remappalette( TextColour2, blue + green );
  183.     _remappalette( BorderColour, red );
  184.     }
  185. }
  186.  
  187. void Do_Demo2( void )
  188. /*===================
  189.  
  190.     This program draws bar and pie graphs for the
  191.     data specified above. */
  192.  
  193. {
  194.     _setvideomode( _MAXCOLORMODE );
  195.     _getvideoconfig( &VC ); /* fill videoconfig structure */
  196.     NewColours();
  197.  
  198.     Title();
  199.     BarGraph();
  200.     PieGraph();
  201. }
  202.  
  203.  
  204. static long        NewColourSet[] = {
  205.     _BLACK, _LIGHTCYAN, _LIGHTMAGENTA, _BRIGHTWHITE,
  206.     _GREEN, _LIGHTBLUE, _GRAY, _LIGHTRED,
  207.     _CYAN, _YELLOW, _RED, _LIGHTGREEN,
  208.     _BROWN, _MAGENTA, _BLUE, _WHITE
  209. };
  210.  
  211.  
  212. static void NewColours( void )
  213. /*============================
  214.  
  215.     Select a new colour set. */
  216.  
  217. {
  218.     int         i;
  219.  
  220.     if( VC.adapter >= _MCGA ) {
  221.     for( i = 0; i < 16; ++i ) {
  222.         _remappalette( i, NewColourSet[ i ] );
  223.     }
  224.     }
  225.     if( VC.numcolors == 2 ) {
  226.     AxisColour = 1;
  227.     TitleColour = 1;
  228.     BorderColour = 1;
  229.     } else {
  230.     AxisColour = 1;
  231.     TitleColour = 2;
  232.     BorderColour = 3;
  233.     }
  234. }
  235.  
  236.  
  237. static void Title( void )
  238. /*=======================
  239.  
  240.     Draw main title and graph boxes. */
  241.  
  242. {
  243.     _setcolor( BorderColour );
  244.     _settextalign( _CENTER, _TOP );
  245.     _setcharsize_w( 0.08, 1.0 / strlen( Main_Title ) );
  246.     _grtext_w( 0.5, 1.0, Main_Title );
  247.     _rectangle_w( _GBORDER, 0.00, 0.00, 0.49, 0.90 );      // left half
  248.     _rectangle_w( _GBORDER, 0.51, 0.00, 1.00, 0.90 );      // right half
  249. }
  250.  
  251.  
  252. static void DoAxes( float xleft, float ybottom, float xlen, float ylen )
  253. /*======================================================================
  254.  
  255.     Draw axes of bar graph. */
  256.  
  257. {
  258.     float        xright, ytop;
  259.     float        y, yinc;
  260.  
  261.     xright  = xleft + xlen;
  262.     ytop = ybottom + ylen;
  263.  
  264. /*  Draw the axes */
  265.  
  266.     _setcolor( AxisColour );
  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.     _setcolor( TitleColour );
  282.     _settextalign( _CENTER, _HALF );
  283.     _settextorient( 0, 1 );
  284.     _setcharsize_w( 0.06, ylen / strlen( Y_Axis_Title ) *
  285.             ( (float) VC.numypixels / VC.numxpixels ) );
  286.     _grtext_w( xleft - 0.05, ybottom + ylen / 2, Y_Axis_Title );
  287.     _setcharsize_w( 0.06, xlen / strlen( X_Axis_Title ) );
  288.     _settextorient( 1, 0 );
  289.     _grtext_w( xleft + xlen / 2, ybottom - 0.05, X_Axis_Title );
  290. }
  291.  
  292.  
  293. static void DoBars( float xleft, float ybottom, float xlen, float ylen )
  294. /*======================================================================
  295.  
  296.     Draw bars of graph. */
  297.  
  298. {
  299.     int         i;
  300.     float        x1, y1;
  301.     float        x2, y2;
  302.     float        bar_width;
  303.  
  304.     bar_width = ( 2 * xlen ) / ( 3 * NUMSECT + 1 );
  305.     y1 = ybottom;
  306.     for( i = 0; i < NUMSECT; ++i ) {
  307.     x1 = xleft + ( 3 * i + 1 ) * bar_width / 2;
  308.     x2 = x1 + bar_width;
  309.     y2 = y1 + ylen * Values[ i ] / 100;
  310.     _setcolor( i % ( VC.numcolors - 1 ) + 1 );
  311.     _setfillmask( Masks[ i % 8 ] );
  312.     _rectangle_w( _GFILLINTERIOR, x1, y1, x2, y2 );
  313.     _rectangle_w( _GBORDER, x1, y1, x2, y2 );
  314.     }
  315. }
  316.  
  317.  
  318. static void BarGraph( void )
  319. /*==========================
  320.  
  321.     Draw bar graph on left side of the screen. */
  322.  
  323. {
  324.     DoAxes( 0.10, 0.15, 0.35, 0.7 );
  325.     DoBars( 0.10, 0.15, 0.35, 0.7 );
  326. }
  327.  
  328.  
  329. static void PieGraph( void )
  330. /*==========================
  331.  
  332.     Draw pie graph. */
  333.  
  334. {
  335.     int         i;
  336.     float        x1, y1;
  337.     float        x2, y2;
  338.     float        x3, y3;
  339.     float        x4, y4;
  340.     float        xc, yc;
  341.     float        xradius, yradius;
  342.     float        theta;
  343.     long        total;
  344.  
  345. /*  Calculate data for pie graph. */
  346.  
  347.     total = 0;
  348.     for( i = 0; i < NUMSECT; ++i ) {
  349.     total += Values[ i ];
  350.     }
  351.  
  352. /*  Calculate centre and radius of pie */
  353.  
  354.     xc = 0.75;
  355.     yc = 0.45;
  356.     xradius = 0.20;
  357.     yradius = 0.20 * 4 / 3;
  358.  
  359. /*  Calculate bounding rectangle */
  360.  
  361.     x1 = xc - xradius;
  362.     y1 = yc - yradius;
  363.     x2 = xc + xradius;
  364.     y2 = yc + yradius;
  365.  
  366. /*  Draw the slices */
  367.  
  368.     x3 = xc + xradius;
  369.     y3 = yc;
  370.     theta = 0.0;
  371.     for( i = 0; i < NUMSECT; ++i ) {
  372.     theta += Values[ i ] * 2 * PI / total;
  373.     x4 = xc + xradius * cos( theta );
  374.     y4 = yc + yradius * sin( theta );
  375.     _setcolor( i % ( VC.numcolors - 1 ) + 1 );
  376.     _setfillmask( Masks[ i % 8 ] );
  377.     _pie_w( _GFILLINTERIOR, x1, y1, x2, y2, x3, y3, x4, y4 );
  378.     _pie_w( _GBORDER, x1, y1, x2, y2, x3, y3, x4, y4 );
  379.     x3 = x4;
  380.     y3 = y4;
  381.     }
  382. }
  383.