home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / toollib_2 / Examples / Tree / Builds / c / diagram_sa next >
Encoding:
Text File  |  1995-12-06  |  3.6 KB  |  164 lines

  1. /*** diagram_save.c ***/
  2. /* Save the line_store as a Draw diagram via save_buffer
  3.  * (c) Paul Field
  4.  */
  5.  
  6. #include "diagram_save.h"
  7.  
  8. #include <assert.h>
  9. #include "drawfile.h"
  10. #include "macros.h"
  11.  
  12. #include "box.h"
  13. #include "line_storer.h"
  14. #include "save_buffer.h"
  15.  
  16.  
  17. #define OSToDraw(os) ((os) * draw_OS_UNIT)
  18.  
  19.  
  20. /* A standard Draw file header. Requires the bounding box to be filled in */
  21. static drawfile_diagram default_header =
  22.  { { 'D','r','a','w' },
  23.    201,
  24.    0,
  25.    { 'T','r','e','e',' ',' ',' ',' ',' ',' ',' ',' ' },
  26.    {-1,-1,0,0}
  27.  };
  28.  
  29.  
  30. /* A 'template' path object for a path containing a single line */
  31. /* The non-const items need to be filled in for a particular line */
  32. static struct
  33.  { const drawfile_type       type;
  34.    const unsigned            size;
  35.          os_box              bbox;
  36.    const os_colour           fill;
  37.          os_colour           outline;
  38.    const unsigned            width;
  39.    const drawfile_path_style style;
  40.  
  41.    const unsigned            move_tag;
  42.          os_coord            start;
  43.    const unsigned            line_tag;
  44.          os_coord            end;
  45.    const unsigned            end_tag;
  46.  }
  47. line_template =
  48.  { drawfile_TYPE_PATH,
  49.    sizeof(line_template),
  50.    { 0,0,0,0 },
  51.    -1,
  52.    -1,
  53.    64,
  54.    {0,0,0,0},
  55.  
  56.    draw_MOVE_TO,
  57.    {0,0},
  58.    draw_LINE_TO,
  59.    {0,0},
  60.    draw_END_PATH
  61.  };
  62.  
  63.  
  64. static void diagram_get_line_from_store(unsigned index, colour *c, int *x0, int *y0, int *x1, int *y1)
  65.  /* As line_storer_get_line but returns the coordinates in Draw units (assuming the line_store uses OS units) */
  66.  { line_storer_get_line(index, c, x0, y0, x1, y1);
  67.    *x0 = OSToDraw(*x0);
  68.    *x1 = OSToDraw(*x1);
  69.    *y0 = OSToDraw(*y0);
  70.    *y1 = OSToDraw(*y1);
  71.  }
  72.  
  73.  
  74.  
  75. static void diagram_save_line_object(colour c, int x0, int y0, int x1, int y1)
  76.  { os_colour converted_colour;
  77.  
  78.    converted_colour = (os_colour)(c << 8);  /* An os_colour is of the form BGR0 */
  79.  
  80.    line_template.bbox.x0 = MIN(x0,x1);
  81.    line_template.bbox.x1 = MAX(x0,x1);
  82.    line_template.bbox.y0 = MIN(y0,y1);
  83.    line_template.bbox.y1 = MAX(y0,y1);
  84.  
  85.    line_template.outline = converted_colour;
  86.  
  87.    line_template.start.x = x0;
  88.    line_template.start.y = y0;
  89.  
  90.    line_template.end.x   = x1;
  91.    line_template.end.y   = y1;
  92.    save_buffer_send(&line_template, sizeof(line_template));
  93.  }
  94.  
  95.  
  96.  
  97.  
  98. static void diagram_line_bounds(unsigned index, os_box *bounds)
  99.  { colour   c;
  100.    int      x0;
  101.    int      y0;
  102.    int      x1;
  103.    int      y1;
  104.  
  105.    assert(bounds != NULL);
  106.  
  107.    diagram_get_line_from_store(index, &c, &x0, &y0, &x1, &y1);
  108.    bounds->x0 = MIN(x0,x1);
  109.    bounds->y0 = MIN(y0,y1);
  110.    bounds->x1 = MAX(x0,x1);
  111.    bounds->y1 = MAX(y0,y1);
  112.  }
  113.  
  114.  
  115.  
  116.  
  117. static void diagram_calculate_bounds(os_box *bounds)
  118.  { unsigned num_lines;
  119.    unsigned line_idx;
  120.  
  121.    assert(bounds != NULL);
  122.  
  123.    num_lines = line_storer_num_lines();
  124.  
  125.    if (num_lines >= 1)
  126.     { diagram_line_bounds(0, bounds);
  127.  
  128.       for (line_idx = 1; line_idx < num_lines; line_idx++)
  129.        { os_box line_bounds;
  130.  
  131.          diagram_line_bounds(line_idx, &line_bounds);
  132.          box_bounds(bounds, &line_bounds, bounds);
  133.        }
  134.     }
  135.    else
  136.     { bounds->x0 = 0;
  137.       bounds->y0 = 0;
  138.       bounds->x1 = 0;
  139.       bounds->y1 = 0;
  140.     }
  141.  }
  142.  
  143.  
  144.  
  145.  
  146. void diagram_save(void)
  147.  { unsigned line_idx;
  148.    colour   c;
  149.    int      x0;
  150.    int      y0;
  151.    int      x1;
  152.    int      y1;
  153.  
  154.    /* Output drawfile header */
  155.    diagram_calculate_bounds(&default_header.bbox);
  156.    save_buffer_send(&default_header, offsetof(drawfile_diagram, objects));
  157.  
  158.    /* Save lines */
  159.    for (line_idx = 0; line_idx < line_storer_num_lines(); line_idx++)
  160.     { diagram_get_line_from_store(line_idx, &c, &x0, &y0, &x1, &y1);
  161.       diagram_save_line_object(c, x0, y0, x1, y1);
  162.     }
  163.  }
  164.