home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / vifs / xorline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-04  |  4.3 KB  |  251 lines

  1. /*
  2.  *    xorline.c  --  draw a line exoring the color with the background.
  3.  *               works with EGA in Turbo C EGAHI mode.
  4.  *
  5.  *    Turbo C graphics settings are not affected.
  6.  *    The first video page is used (at 0xa000).
  7.  *    The coordinates are inclusive (both end points are drawn).
  8.  *    Anything outside the screen is not drawn.
  9.  *
  10.  *    29 june 1989  Olle Olsson.
  11.  */
  12.  
  13. #include <graphics.h>        /* for getmax?() */
  14. #include "egatc.h"
  15.  
  16. #define LIMIT_CHECK 1        /* check screen limits */
  17.  
  18. /*#define LPATT 1        /* for patterned lines */
  19.  
  20. #ifdef LPATT
  21. #define SOLID 0xff */
  22. static char linmod = SOLID;        /* default solid */
  23. #endif
  24.  
  25. /* local functions */
  26. void setxor( int color );
  27. void unsetxor( void );
  28.  
  29. /* make inline code for speed */
  30. #define XPT( x, y )                                    \
  31.     {                                                       \
  32.     bmask = bitmask( x );                                   \
  33.     LOADGR( E_BM, bmask );                                \
  34.     offset = maddr( x, y );                     \
  35.     pokeb( EGASEG, offset, peekb( EGASEG, offset ) );    \
  36.     }
  37.  
  38. void xorline( int x0, int y0, int x1, int y1, int color )
  39. {
  40. int dx, dy;
  41. int xinc, yinc;
  42. int res1;
  43. int res2;
  44. #ifdef LPATT
  45. int slope;
  46. #endif
  47. register unsigned char bmask;
  48. register short offset;
  49. #ifdef LIMIT_CHECK
  50. int mx, my;            /* maximum */
  51.  
  52. /* limits check */
  53. mx = getmaxx();
  54. my = getmaxy();
  55.  
  56. if ((x0 < 0 && x1 < 0) || (x0 > mx && x1 > mx))
  57.     return;
  58.  
  59. if ((y0 < 0 && y1 < 0) || (y0 > my && y1 > my))
  60.     return;
  61. #endif
  62.  
  63. /* setup */
  64. setxor( color );
  65.  
  66. #ifdef traceon
  67. if (trace > 1)
  68.     fprintf( stderr, "Line (%d, %d) to (%d, %d)\n", x0, y0, x1, y1 );
  69. #endif
  70.  
  71. /* setup */
  72. #ifdef LPATT
  73. slope =
  74. #endif
  75. xinc = yinc = 1;
  76.  
  77. if ((dx = x1 - x0) < 0)
  78.     {
  79.     xinc = -1;
  80.     dx = -dx;
  81. #ifdef LPATT
  82.     slope = -1;
  83. #endif
  84.     }
  85.  
  86. if ((dy = y1 - y0) < 0)
  87.     {
  88.     yinc = -1;
  89.     dy = -dy;
  90. #ifdef LPATT
  91.     slope = -slope;
  92. #endif
  93.     }
  94.  
  95. res1 = res2 = 0;
  96.  
  97. /* fill in x or y direction? */
  98. if (dx >= dy) while (x0 != x1)
  99.     {
  100. #ifdef LIMIT_CHECK
  101.     if (x0 >= 0 && x0 <= mx && y0 >= 0 && y0 <= my)
  102.         {
  103. #ifdef LPATT
  104.         /* could be dotted or dashed */
  105.         if (linmod == SOLID)
  106.             XPT( x0, y0 );
  107.         else if ((x0 + slope * y0) & linmod)
  108. #endif
  109.             XPT( x0, y0 );
  110.         }
  111. #else
  112.     /* could have LPATT here too */
  113.     XPT( x0, y0 );
  114. #endif
  115.  
  116.     /* increment for next */
  117.     if (res1 > res2)
  118.         {
  119.         res2 += dx - res1;
  120.         res1 = 0;
  121.         y0 += yinc;
  122.  
  123. #ifdef LIMIT_CHECK
  124.         /* limit check */
  125.         if (yinc > 0)
  126.             {
  127.             if (y0 > my)
  128.                 return;
  129.             }
  130.         else if (y0 < 0)
  131.             return;
  132. #endif
  133.         }
  134.  
  135.     res1 += dy;
  136.     x0 += xinc;
  137.  
  138. #ifdef LIMIT_CHECK
  139.     /* limit check */
  140.     if (xinc > 0)
  141.         {
  142.         if (x0 > mx)
  143.             return;
  144.         }
  145.     else if (x0 < 0)
  146.         return;
  147. #endif
  148.     }
  149. else while (y0 != y1)
  150.     {
  151.     /* same as above but y instead of x */
  152. #ifdef LIMIT_CHECK
  153.     if (x0 >= 0 && x0 <= mx && y0 >= 0 && y0 <= my)
  154.         {
  155. #ifdef LPATT
  156.         if (linmod == SOLID)
  157.             XPT( x0, y0 );
  158.         else if ((x0 + slope * y0) & linmod)
  159. #endif
  160.             XPT( x0, y0 );
  161.         }
  162. #else
  163.     /* could have LPATT here too */
  164.     XPT( x0, y0 );
  165. #endif
  166.  
  167.     if (res1 > res2)
  168.         {
  169.         res2 += dy - res1;
  170.         res1 = 0;
  171.         x0 += xinc;
  172.  
  173. #ifdef LIMIT_CHECK
  174.         /* limit check */
  175.         if (xinc > 0)
  176.             {
  177.             if (x0 > mx)
  178.                 return;
  179.             }
  180.         else if (x0 < 0)
  181.             return;
  182. #endif
  183.         }
  184.  
  185.     res1 += dx;
  186.     y0 += yinc;
  187.  
  188. #ifdef LIMIT_CHECK
  189.     /* limit check */
  190.     if (yinc > 0)
  191.         {
  192.         if (y0 > my)
  193.             return;
  194.         }
  195.     else if (y0 < 0)
  196.         return;
  197. #endif
  198.     }
  199.  
  200. /* last point */
  201. #ifdef LPATT
  202. if (linmod == SOLID)
  203.     XPT( x1, y1 );
  204. else if ((x1 + slope * y1) & linmod)
  205. #endif
  206.     XPT( x1, y1 );
  207.  
  208. /* restore */
  209. unsetxor();
  210. }
  211.  
  212. void xorpoint( int x, int y, int color )
  213. {
  214. unsigned char bmask;
  215. short offset;
  216.  
  217. #ifdef LIMIT_CHECK
  218. if (x < 0 || y < 0 || x > getmaxx() || y > getmaxy())
  219.     return;
  220. #endif
  221.  
  222. /* setup */
  223. setxor( color );
  224.  
  225. /* do it */
  226. XPT( x, y );
  227.  
  228. /* restore */
  229. unsetxor();
  230. }
  231.  
  232.  
  233. /* local functions */
  234. void setxor( int color )
  235. {
  236. /* setup for xorline(), except for bit mask register */
  237. LOADGR( E_SR, color );        /* colour to SET/RESET reg */
  238. LOADGR( E_ESR, 0x0f );        /* enable SET/RESET all planes */
  239. LOADGR( E_RF, F_XOR );        /* xor function */
  240.  
  241. }
  242.  
  243. void unsetxor( void )
  244. {
  245. /* restore from setxor() */
  246. LOADGR( E_BM, 0xff );        /* restore BITMASK */
  247. LOADGR( E_ESR, 0 );        /* disable all planes */
  248. LOADGR( E_RF, F_UNMOD );    /* disable xor function */
  249. }
  250.  
  251.