home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 3.ddi / BGICLA.ZIP / LINE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-24  |  6.7 KB  |  237 lines

  1. /**************************************************************************
  2.     Listing 9 - LINE.CPP
  3.  
  4.     Written by Kevin D. Weeks, April 1990
  5.     Released to the Public Domain
  6. */
  7.  
  8. #include "line.hpp"
  9.  
  10. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  11.     Constructor - creates an empty instance. all attributes are set to
  12.     zero or to the appropriate BGI defaults.
  13. */
  14. Line::Line(void)
  15. {
  16.     struct linesettingstype line_type;
  17.  
  18.     // use BGI default line_style, width, and color
  19.     getlinesettings(&line_type);
  20.     style = (line_styles)line_type.linestyle;
  21.     width = (line_widths)line_type.thickness;
  22.     color = (COLORS)getcolor();
  23.     visible = FALSE;
  24. }
  25.  
  26. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  27.     Constructor - since the "copy constructor", Line(const Line &source),
  28.     won't accept a pointer easily so we define a separate pointer
  29.     constructor for convenience' sake.
  30. */
  31. Line::Line(const Line *source)
  32. {
  33.     start = source->start;
  34.     end = source->end;
  35.     style = source->style;
  36.     width = source->width;
  37.     color = source->color;
  38.     visible = FALSE;
  39. }
  40.  
  41. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  42.     Constructor - primary constructor. Copies of all attributes are
  43.     accepted although init_color, init_width, and init_style may be
  44.     defaults.
  45. */
  46. Line::Line(const Point begin, const Point finish, const COLORS init_color,
  47.            const line_styles init_style, const line_widths init_width)
  48. {
  49.     struct linesettingstype line_type;
  50.  
  51.     if ((init_style == DEFAULT) || (init_width == DEFAULT))
  52.         getlinesettings(&line_type);
  53.  
  54.     if (init_style == DEFAULT)
  55.         style = (line_styles)line_type.linestyle;
  56.     else
  57.         style = init_style;
  58.  
  59.     if (init_width == DEFAULT)
  60.         width = (line_widths)line_type.thickness;
  61.     else
  62.         width = init_width;
  63.  
  64.     if (init_color == DEFAULT)
  65.         color = (COLORS)getcolor();
  66.     else
  67.         color = init_color;
  68.  
  69.     start = begin;
  70.     end = finish;
  71.     visible = FALSE;
  72. }
  73.  
  74. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  75.     color can be set whether the line is visible or not
  76. */
  77. void    Line::set_color(const COLORS new_color)
  78. {
  79.     if (new_color == (COLORS)DEFAULT)
  80.         color = (COLORS)getcolor();
  81.     else
  82.         color = new_color;
  83.     if (visible)
  84.     {
  85.         erase();
  86.         draw();
  87.     }
  88. }
  89.  
  90. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  91.     style can be set whether the line is visible or not
  92. */
  93. void    Line::set_style(line_styles new_style)
  94. {
  95.     struct linesettingstype line_type;
  96.  
  97.     if (new_style == DEFAULT)
  98.     {
  99.         getlinesettings(&line_type);
  100.         new_style = (line_styles)line_type.linestyle;
  101.     }
  102.     if (visible)
  103.     {
  104.         erase();                            // erase using old style
  105.         style = new_style;                  // change style
  106.         draw();                             // draw using new style
  107.     }
  108.     else
  109.         style = new_style;
  110. }
  111.  
  112. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  113.     width can be set whether the line is visible or not
  114. */
  115. void    Line::set_width(line_widths new_width)
  116. {
  117.     struct linesettingstype line_type;
  118.  
  119.     if (new_width == DEFAULT)
  120.     {
  121.         getlinesettings(&line_type);
  122.         new_width = (line_widths)line_type.thickness;
  123.     }
  124.     if (visible)
  125.     {
  126.         erase();                            // erase using old width
  127.         width = new_width;                  // change style
  128.         draw();                             // draw using new width
  129.     }
  130.     else
  131.         width = new_width;
  132. }
  133.  
  134. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  135.     the draw() method first preserves the current BGI settings, then
  136.     modifies defaults for this particular object, and finally restores the
  137.     original settings. had we written the graphics routines from scratch
  138.     the step of preserving and then restoring the settings wouldn't be
  139.     neccessary.
  140. */
  141. void    Line::draw(void)
  142. {
  143.     struct linesettingstype hold_current;
  144.     int    default_color;
  145.  
  146.     // don't draw it twice
  147.     if (visible)
  148.         return;
  149.  
  150.     // preserve current settings
  151.     getlinesettings(&hold_current);
  152.     default_color = getcolor();
  153.  
  154.     // select color, line style, and width for this object and draw it
  155.     setcolor(color);
  156.     setlinestyle(style,0,width);
  157.     line(start.get_x(),start.get_y(),end.get_x(),end.get_y());
  158.  
  159.     // restore original settings
  160.     setlinestyle(hold_current.linestyle,hold_current.upattern,
  161.                  hold_current.thickness);
  162.     setcolor(default_color);
  163.     visible = TRUE;
  164. }
  165.  
  166. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  167.     the erase method draws over the existing line using the background
  168.     color. as with the draw method, the current BGI settings must be
  169.     preserved and then restored.
  170. */
  171. void    Line::erase(void)
  172. {
  173.     struct linesettingstype hold_current;
  174.     int    default_color;
  175.  
  176.     // don't erase it if it isn't visible
  177.     if (!visible)
  178.         return;
  179.  
  180.     // preserve current settings
  181.     getlinesettings(&hold_current);
  182.     default_color = getcolor();
  183.  
  184.     // set the forground current color to the background color and set the
  185.     // BGI defaults for this object and draw over it
  186.     setcolor(getbkcolor());
  187.     setlinestyle(style,0,width);
  188.     line(start.get_x(),start.get_y(),end.get_x(),end.get_y());
  189.  
  190.     // restore the defaults
  191.     setlinestyle(hold_current.linestyle,hold_current.upattern,
  192.                  hold_current.thickness);
  193.     setcolor(default_color);
  194.     visible = FALSE;
  195. }
  196.  
  197. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  198. void    Line::move_relative(Point &distance)
  199. {
  200.     erase();
  201.     start += distance;
  202.     end += distance;
  203.     draw();
  204. }
  205.  
  206. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  207. void    Line::move_absolute(Point &new_location, Point &reference)
  208. {
  209.     erase();
  210.     if (reference == start)
  211.     {
  212.         end += new_location - start;
  213.         start = new_location;
  214.     }
  215.     else
  216.     {
  217.         start += new_location - end;
  218.         end = new_location;
  219.     }
  220.     draw();
  221. }
  222.  
  223. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  224.     this is the private method used by the copy constructor and the =
  225.     operator. Note that the visible attribute is forced to FALSE.
  226. */
  227. Line    &Line::copy(const Line &source)
  228. {
  229.     start = source.start;
  230.     end = source.end;
  231.     style = source.style;
  232.     width = source.width;
  233.     color = source.color;
  234.     visible = FALSE;
  235.     return *this;
  236. }
  237.