home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / x / x11p-13.zip / do_segs.c < prev    next >
C/C++ Source or Header  |  1991-05-09  |  9KB  |  382 lines

  1. /*****************************************************************************
  2. Copyright 1988, 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
  3.  
  4.                         All Rights Reserved
  5.  
  6. Permission to use, copy, modify, and distribute this software and its 
  7. documentation for any purpose and without fee is hereby granted, 
  8. provided that the above copyright notice appear in all copies and that
  9. both that copyright notice and this permission notice appear in 
  10. supporting documentation, and that the name of Digital not be
  11. used in advertising or publicity pertaining to distribution of the
  12. software without specific, written prior permission.  
  13.  
  14. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  15. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  16. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  17. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  18. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  19. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  20. SOFTWARE.
  21.  
  22. ******************************************************************************/
  23.  
  24. #include "x11perf.h"
  25.  
  26. static XSegment *segments;
  27. static GC       pgc;
  28.  
  29. int InitSegments(xp, p, reps)
  30.     XParms  xp;
  31.     Parms   p;
  32.     int     reps;
  33. {
  34.     int     size;
  35.     int     half;
  36.     int     i;
  37.     int     rows;        /* Number of rows filled in current column      */
  38.     int     x, y;        /* base of square to draw in            */
  39.     int     x1, y1, x2, y2; /* offsets into square                */
  40.     int     phase;        /* how far into 0..8*size we are            */
  41.     int     phaseinc;       /* how much to increment phase at each segment  */
  42.     int     size8;        /* 8 * size                        */
  43.     XGCValues   gcv;
  44.  
  45.     pgc = xp->fggc;
  46.  
  47.     size = p->special;
  48.     size8 = 8 * size;
  49.     half = (size + 19) / 20;
  50.  
  51.     segments = (XSegment *)malloc((p->objects) * sizeof(XSegment));
  52.  
  53.     /* All this x, x1, etc. stuff is to create a pattern that
  54.     (1) scans down the screen vertically, with each new segment going
  55.         into a square of size^2.
  56.  
  57.     (2) rotates the endpoints clockwise around the square
  58.  
  59.     (3) rotates by ``large'' steps if we aren't going to paint enough
  60.         segments to get full coverage
  61.  
  62.     (4) uses CapNotLast so we can create segments of length 1 that
  63.         nonetheless have a distinct direction
  64.     */
  65.  
  66.     x     = half;  y     = half;
  67.     phase = 0;
  68.     phaseinc = size8 / p->objects;
  69.     if (phaseinc == 0) phaseinc = 1;
  70.     rows = 0;
  71.  
  72.     for (i = 0; i != p->objects; i++) {    
  73.     switch (phase / size) {
  74.     case 0:
  75.         x1 = 0;
  76.         y1 = 0;
  77.         x2 = size;
  78.         y2 = phase;
  79.         break;
  80.  
  81.     case 1:
  82.         x1 = phase % size;    
  83.         y1 = 0;
  84.         x2 = size;
  85.         y2 = size;
  86.         break;
  87.  
  88.     case 2:
  89.         x1 = size;
  90.         y1 = 0;
  91.         x2 = size - phase % size;
  92.         y2 = size;
  93.         break;
  94.  
  95.     case 3:
  96.         x1 = size;
  97.         y1 = phase % size;
  98.         x2 = 0;
  99.         y2 = size;
  100.         break;
  101.  
  102.     case 4:
  103.         x1 = size;
  104.         y1 = size;
  105.         x2 = 0;
  106.         y2 = size - phase % size;
  107.         break;
  108.  
  109.     case 5:
  110.         x1 = size - phase % size;
  111.         y1 = size;
  112.         x2 = 0;
  113.         y2 = 0;
  114.         break;
  115.  
  116.     case 6:
  117.         x1 = 0;
  118.         y1 = size;
  119.         x2 = phase % size;
  120.         y2 = 0;
  121.         break;
  122.  
  123.     case 7:
  124.         x1 = 0;
  125.         y1 = size - phase % size;
  126.         x2 = size;
  127.         y2 = 0;
  128.         break;
  129.     } /* end switch */
  130.  
  131.     segments[i].x1 = x + x1;
  132.     segments[i].y1 = y + y1;
  133.     segments[i].x2 = x + x2;
  134.     segments[i].y2 = y + y2;
  135.  
  136.     /* Change square to draw segment in */
  137.     rows++;
  138.     y += size;
  139.     if (y >= HEIGHT - size - half || rows == MAXROWS) {
  140.         /* Go to next column */
  141.         rows = 0;
  142.         y = half;
  143.         x += size;
  144.         if (x >= WIDTH - size - half) {
  145.         x = half;
  146.         }
  147.     }
  148.  
  149.     /* Increment phase */
  150.     phase += phaseinc;
  151.     if (phase >= size8) phase -= size8;
  152.  
  153.     }
  154.  
  155.     gcv.cap_style = CapNotLast;
  156.     XChangeGC(xp->d, xp->fggc, GCCapStyle, &gcv);
  157.     XChangeGC(xp->d, xp->bggc, GCCapStyle, &gcv);
  158.     
  159.     return reps;
  160. }
  161.    
  162.  
  163. int InitDashedSegments(xp, p, reps)
  164.     XParms  xp;
  165.     Parms   p;
  166.     int     reps;
  167. {
  168.     char dashes[2];
  169.  
  170.     (void)InitSegments(xp, p, reps);
  171.  
  172.     /* Modify GCs to draw dashed */
  173.     XSetLineAttributes
  174.     (xp->d, xp->bggc, 0, LineOnOffDash, CapNotLast, JoinMiter);
  175.     XSetLineAttributes
  176.     (xp->d, xp->fggc, 0, LineOnOffDash, CapNotLast, JoinMiter);
  177.     dashes[0] = 3;   dashes[1] = 2;
  178.     XSetDashes(xp->d, xp->fggc, 0, dashes, 2);
  179.     XSetDashes(xp->d, xp->bggc, 0, dashes, 2);
  180.     return reps;
  181. }
  182.  
  183. int InitDoubleDashedSegments(xp, p, reps)
  184.     XParms  xp;
  185.     Parms   p;
  186.     int     reps;
  187. {
  188.     char dashes[2];
  189.  
  190.     (void)InitSegments(xp, p, reps);
  191.  
  192.     /* Modify GCs to draw dashed */
  193.     XSetLineAttributes
  194.     (xp->d, xp->bggc, 0, LineDoubleDash, CapNotLast, JoinMiter);
  195.     XSetLineAttributes
  196.     (xp->d, xp->fggc, 0, LineDoubleDash, CapNotLast, JoinMiter);
  197.     dashes[0] = 3;   dashes[1] = 2;
  198.     XSetDashes(xp->d, xp->fggc, 0, dashes, 2);
  199.     XSetDashes(xp->d, xp->bggc, 0, dashes, 2);
  200.     return reps;
  201. }
  202.  
  203. int InitHorizSegments(xp, p, reps)
  204.     XParms  xp;
  205.     Parms   p;
  206.     int     reps;
  207. {
  208.     int     size;
  209.     int     half;
  210.     int     i;
  211.     int     rows;       /* Number of rows filled in current column      */
  212.     int     x, y;    /* base of square to draw in            */
  213.     int     y1;        /* y position inside square            */
  214.     int     inc;
  215.     XGCValues   gcv;
  216.  
  217.     pgc = xp->fggc;
  218.  
  219.     size = p->special;
  220.     half = (size + 19) / 20;
  221.  
  222.     segments = (XSegment *)malloc((p->objects) * sizeof(XSegment));
  223.  
  224.     x = half;
  225.     y = half;
  226.     y1 = 0;
  227.     rows = 0;
  228.     inc = size / p->objects;
  229.     if (inc == 0) inc = 1;
  230.  
  231.     for (i = 0; i != p->objects; i++) {
  232.     if (i % 2) {
  233.         segments[i].x1 = x + size;
  234.         segments[i].x2 = x;
  235.         segments[i].y1 = y + size - y1;
  236.         segments[i].y2 = y + size - y1;
  237.         y1 += inc;
  238.         if (y1 >= size) y1 -= size;
  239.     } else {
  240.         segments[i].x1 = x;
  241.         segments[i].x2 = x + size;
  242.         segments[i].y1 = y + y1;
  243.         segments[i].y2 = y + y1;
  244.     }
  245.     rows++;
  246.     y += size;
  247.     if (y >= HEIGHT - size - half || rows == MAXROWS) {
  248.         rows = 0;
  249.         y = half;
  250.         x += size;
  251.         if (x >= WIDTH - size - half)
  252.         x = half;
  253.     }
  254.     }
  255.     gcv.cap_style = CapNotLast;
  256.     XChangeGC(xp->d, xp->fggc, GCCapStyle, &gcv);
  257.     XChangeGC(xp->d, xp->bggc, GCCapStyle, &gcv);
  258.     return reps;
  259. }
  260.  
  261. int InitWideHorizSegments(xp, p, reps)
  262.     XParms  xp;
  263.     Parms   p;
  264.     int     reps;
  265. {
  266.     int size;
  267.  
  268.     (void)InitHorizSegments(xp, p, reps);
  269.  
  270.     size = p->special;
  271.     XSetLineAttributes(xp->d, xp->bggc, (int) ((size + 9) / 10),
  272.     LineSolid, CapRound, JoinRound);
  273.     XSetLineAttributes(xp->d, xp->fggc, (int) ((size + 9) / 10),
  274.     LineSolid, CapRound, JoinRound);
  275.  
  276.     return reps;
  277. }
  278.  
  279.  
  280. int InitVertSegments(xp, p, reps)
  281.     XParms  xp;
  282.     Parms   p;
  283.     int     reps;
  284. {
  285.     int     size;
  286.     int     half;
  287.     int     i;
  288.     int     rows;       /* Number of rows filled in current column      */
  289.     int     x, y;    /* base of square to draw in            */
  290.     int     x1;        /* x position inside square            */
  291.     int     inc;
  292.     XGCValues   gcv;
  293.  
  294.     pgc = xp->fggc;
  295.  
  296.     size = p->special;
  297.     half = (size + 19) / 20;
  298.  
  299.     segments = (XSegment *)malloc((p->objects) * sizeof(XSegment));
  300.  
  301.     x = half;
  302.     y = half;
  303.     x1 = 0;
  304.     rows = 0;
  305.     inc = size / p->objects;
  306.     if (inc == 0) inc = 1;
  307.  
  308.     for (i = 0; i != p->objects; i++) {
  309.     if (i % 2) {
  310.         segments[i].x1 = x + size - x1;
  311.         segments[i].x2 = x + size - x1;
  312.         segments[i].y1 = y + size;
  313.         segments[i].y2 = y;
  314.         x1 += inc;
  315.         if (x1 >= size) x1 -= size;
  316.     } else {
  317.         segments[i].x1 = x + x1;
  318.         segments[i].x2 = x + x1;
  319.         segments[i].y1 = y;
  320.         segments[i].y2 = y + size;
  321.     }
  322.     rows++;
  323.     y += size;
  324.     if (y >= HEIGHT - size - half || rows == MAXROWS) {
  325.         /* Go to next column */
  326.         rows = 0;
  327.         y = half;
  328.         x += size;
  329.         if (x >= WIDTH - size - half) {
  330.         x = half;
  331.         }
  332.     }
  333.     }
  334.     gcv.cap_style = CapNotLast;
  335.     XChangeGC(xp->d, xp->fggc, GCCapStyle, &gcv);
  336.     XChangeGC(xp->d, xp->bggc, GCCapStyle, &gcv);
  337.     return reps;
  338. }
  339.  
  340. int InitWideVertSegments(xp, p, reps)
  341.     XParms  xp;
  342.     Parms   p;
  343.     int     reps;
  344. {
  345.     int size;
  346.  
  347.     (void)InitVertSegments(xp, p, reps);
  348.  
  349.     size = p->special;
  350.     XSetLineAttributes(xp->d, xp->bggc, (int) ((size + 9) / 10),
  351.     LineSolid, CapRound, JoinRound);
  352.     XSetLineAttributes(xp->d, xp->fggc, (int) ((size + 9) / 10),
  353.     LineSolid, CapRound, JoinRound);
  354.  
  355.     return reps;
  356. }
  357.  
  358.  
  359. void DoSegments(xp, p, reps)
  360.     XParms  xp;
  361.     Parms   p;
  362.     int     reps;
  363. {
  364.     int i;
  365.  
  366.     for (i = 0; i != reps; i++) {
  367.         XDrawSegments(xp->d, xp->w, pgc, segments, p->objects);
  368.         if (pgc == xp->bggc)
  369.             pgc = xp->fggc;
  370.         else
  371.             pgc = xp->bggc;
  372.     }
  373. }
  374.  
  375. void EndSegments(xp, p)
  376.     XParms  xp;
  377.     Parms p;
  378. {
  379.     free(segments);
  380. }
  381.  
  382.