home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pctech / hlsrc / hlgrafit.c < prev    next >
C/C++ Source or Header  |  1988-09-09  |  6KB  |  223 lines

  1. /*+
  2.     Name:    hlgrafit.c
  3.     Date:    07-Sep-1988
  4.     Author:    Kent J. Quirk
  5.         (c) Copyright 1988 Ziff Communications Co.
  6.     Abstract:    Contains routines to draw certain graphics displays.
  7.     History:    09-Sep-88   kjq     Version 1.00
  8. -*/    
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <graph.h>
  12. #include <conio.h>
  13. #include <string.h>
  14. #include "hl.h"
  15. #include "hltimes.h"
  16.  
  17. #define MAXROWS 10
  18. #define MAXCOLS 10
  19.  
  20. int nrows = 3;
  21. int ncols = 3;
  22. long vmax = -1;
  23.  
  24. int graphdata[MAXROWS][MAXCOLS] = { 0 };
  25.  
  26. extern int *colorlist;
  27. int base_color = 0;
  28. struct videoconfig config;
  29.  
  30. int check_vga_mono(), plotstr();
  31.  
  32. /**** f a t _ r e c t a n g l e ****
  33.     Abstract:    Draws a rectangle with a drop shadow
  34.     Parameters: short x1, y1, x2, y2 -- the corners of the rectangle
  35.         short depth - the depth of the shadow (in pixels)
  36.         int shadow, front -- the color of the shadow and front
  37.             (which is modified by base_color for visual interest)
  38.     Returns:    nothing
  39. ****************************/
  40. void fat_rectangle(x1, y1, x2, y2, depth, shadow, front)
  41. short x1, y1, x2, y2, depth;
  42. int shadow, front;
  43. {
  44.     int i;
  45.  
  46.     _setcolor(colorlist[(base_color+shadow) % 16]);
  47.     for (i=depth; i>0; i--)
  48.     _rectangle(_GBORDER, x1+i, y1-i, x2+i, y2-i);
  49.     _setcolor(colorlist[(base_color+front) % 16]);
  50.     _rectangle(_GFILLINTERIOR, x1, y1, x2, y2);
  51.     _setcolor(0);        /* draw detail for corners */
  52.     if (check_vga_mono())
  53.     {
  54.     _moveto(x2+depth+1, y2-depth);
  55.     _lineto(x2, y2);
  56.     _lineto(x2, y1);
  57.     _moveto(x1, y2);
  58.     _lineto(x2, y2);
  59.     }
  60.     else
  61.     {
  62.     _moveto(x2+depth+1, y2-depth);
  63.     _lineto(x2+1, y2);
  64.     }
  65.  
  66.     if ((y2 - y1) == 0)
  67.     {
  68.     _setcolor(colorlist[0]);
  69.     _moveto(x1, y1);
  70.     _lineto(x2+depth, y2-depth);
  71.     _moveto(x1+depth, y2-depth);
  72.     _lineto(x2, y2);
  73.     }
  74. }
  75.  
  76. #define ROWSIZE (config.numypixels/config.numtextrows)
  77. #define COLSIZE (config.numxpixels/config.numtextcols)
  78.  
  79. /**** b a r _ g r a p h ****
  80.     Abstract:    Given the global graphdata[], this draws a bar graph
  81.         with that data.
  82.     Parameters: int x, y -- the position of the lower left corner
  83.         int max_x, max_y -- the position of the upper right corner
  84.     Returns:    Nothing
  85. ****************************/
  86. void bar_graph(x, y, max_x, max_y)
  87. int x, y, max_x, max_y;
  88. {
  89.     int barwidth;
  90.     int yscale;
  91.     int i, j;
  92.     long t;
  93.     int xl, hgt;
  94.     int max_hgt;
  95.     long tsave = -1;
  96.     int depth;
  97.     char buf[20];
  98.  
  99.     buf[1] = 0;
  100.     _setlogorg(x, y);
  101.  
  102.     barwidth = max_x / (ncols * (nrows+1) - 1);
  103.     depth = barwidth / 4;
  104.  
  105.     for (i=0; i<ncols; i++)
  106.     {
  107.     for (j=0; j<nrows; j++)
  108.     {
  109.         xl = (i * (nrows+1) + j) * barwidth;
  110.         t = (long)graphdata[j][i] * (long)max_y;
  111.         hgt = (int)(t / vmax);
  112.         if (tsave < (long)graphdata[j][i])
  113.         {
  114.         max_hgt = hgt;
  115.         tsave = (long)graphdata[j][i];
  116.         }
  117.         fat_rectangle(xl+1, 0, xl + barwidth, hgt, depth, j, j+8);
  118.         if (j == 1)
  119.         {
  120.         itoa(i+1, buf, 10);
  121.         plotstr(buf, xl, 1, 0xFF, _GOR);
  122.         }
  123.     }
  124.     }
  125.     
  126.     _setcolor(0xFF);
  127.     _moveto(0, max_y);
  128.     _lineto(0, 0);
  129.     _moveto(-COLSIZE, 0);
  130.     _lineto(max_x, 0);
  131.     plotstr("0", -COLSIZE*2, -ROWSIZE, 0xFF, _GOR);
  132.     plotstr(time_secs(tsave), -COLSIZE*7, max_hgt-ROWSIZE, 0xFF, _GOR);
  133.     _moveto(0, max_hgt);
  134.     _lineto(-COLSIZE, max_hgt);
  135.     if (++base_color >= 16)
  136.     base_color = 0;
  137. }
  138.  
  139. /**** d r a w _ t i m e s ****
  140.     Abstract:    Given pointers to two arrays of time data, this 
  141.         draws a comparison graph.
  142. ****************************/
  143. draw_times(data1, data2, text, npts, f1, f2)
  144. long *data1, *data2;
  145. int npts;
  146. char text[][80];
  147. char *f1, *f2;
  148. {
  149.     int xl, xr, yt, yb;
  150.     int i,j;
  151.     char buf[80];
  152.  
  153.     if (!init_video((struct videoconfig far *)&config))
  154.     {
  155.     printf("Couldn't set any graphics mode!\n");
  156.     exit(1);
  157.     }
  158.     _setcolor(colorlist[0]);
  159.     _clearscreen(_GCLEARSCREEN);
  160.  
  161.     for (i=0; i<npts; i++)
  162.     {
  163.     graphdata[0][i] = max((int)data1[i], 0);
  164.     graphdata[1][i] = max((int)data2[i], 0);
  165.  
  166.     if ((long)graphdata[0][i] > vmax)
  167.         vmax = (long)graphdata[0][i];
  168.  
  169.     if ((long)graphdata[1][i] > vmax)
  170.         vmax = (long)graphdata[1][i];
  171.     }
  172.     nrows = 2;
  173.     ncols = npts;
  174.     vmax =  6L * vmax / 5L;
  175.     base_color = 2;
  176.  
  177.     /* We need eight columns on the left for numbers, one line at the top,
  178.     and six lines at the bottom for text, and one for labels */
  179.     xl = COLSIZE * 8;
  180.     xr = config.numxpixels - COLSIZE;    /* just to leave a little room */
  181.     yt = ROWSIZE;
  182.     yb = config.numypixels - ROWSIZE * 7;
  183.     bar_graph(xl, yb, xr-xl, yt-yb);
  184.     _settextposition(1,1);
  185.     _settextcolor(colorlist[2+8]);
  186.     _outtext(f1);
  187.     _settextposition(1,16);
  188.     _settextcolor(colorlist[3+8]);
  189.     _outtext(f2);
  190.     
  191.     if (config.numtextcols >= 60)
  192.     {
  193.     _settextposition(1,47);
  194.     _settextcolor(0xFF);
  195.     _outtext("PC Tech Journal System Benchmarks");
  196.     _settextposition(config.numtextrows,70);
  197.     _settextcolor(0xFF);
  198.     _outtext("Press ESC");
  199.     }
  200.  
  201.     for (i=0; i<npts; i++)
  202.     {
  203.     _settextposition(config.numtextrows-ncols+i+1, 1);
  204.     _settextcolor(0xFF);
  205.     strcpy(buf, text[i]);
  206.     _outtext(strtok(buf, "~"));
  207.     if (config.numtextcols >= 60)
  208.     {
  209.         _settextcolor(colorlist[2+8]);
  210.         _outtext(strtok(NULL, "~"));
  211.         _settextcolor(colorlist[3+8]);
  212.         _outtext(strtok(NULL, "~"));
  213.         _settextcolor(0xFF);
  214.         _outtext(strtok(NULL, ""));     /* return rest */
  215.     }
  216.     }
  217.  
  218.     getch();
  219.     _setvideomode(_DEFAULTMODE);
  220.  
  221.     return(0);
  222. }
  223.