home *** CD-ROM | disk | FTP | other *** search
/ Deathday Collection / dday.bin / edit / doombsp / drawing.m < prev    next >
Text File  |  1994-04-06  |  4KB  |  261 lines

  1. // drawing.m
  2. #import "doombsp.h"
  3.  
  4. id             window_i, view_i;
  5. float        scale = 0.125;
  6. NXRect        worldbounds;
  7.  
  8. /*
  9. ================
  10. =
  11. = IDRectFromPoints
  12. =
  13. = Makes the rectangle just touch the two points
  14. =
  15. ================
  16. */
  17.  
  18. void IDRectFromPoints(NXRect *rect, NXPoint const *p1, NXPoint const *p2 )
  19. {
  20. // return a rectangle that encloses the two points
  21.     if (p1->x < p2->x)
  22.     {
  23.         rect->origin.x = p1->x;
  24.         rect->size.width = p2->x - p1->x + 1;
  25.     }
  26.     else
  27.     {
  28.         rect->origin.x = p2->x;
  29.         rect->size.width = p1->x - p2->x + 1;
  30.     }
  31.     
  32.     if (p1->y < p2->y)
  33.     {
  34.         rect->origin.y = p1->y;
  35.         rect->size.height = p2->y - p1->y + 1;
  36.     }
  37.     else
  38.     {
  39.         rect->origin.y = p2->y;
  40.         rect->size.height = p1->y - p2->y + 1;
  41.     }
  42. }
  43.  
  44.  
  45. /*
  46. ==================
  47. =
  48. = IDEnclosePoint
  49. =
  50. = Make the rect enclose the point if it doesn't allready
  51. =
  52. ==================
  53. */
  54.  
  55. void IDEnclosePoint (NXRect *rect, NXPoint const *point)
  56. {
  57.     float    right, top;
  58.     
  59.     right = rect->origin.x + rect->size.width - 1;
  60.     top = rect->origin.y + rect->size.height - 1;
  61.     
  62.     if (point->x < rect->origin.x)
  63.         rect->origin.x = point->x;
  64.     if (point->y < rect->origin.y)
  65.         rect->origin.y = point->y;        
  66.     if (point->x > right)
  67.         right = point->x;
  68.     if (point->y > top)
  69.         top = point->y;
  70.         
  71.     rect->size.width = right - rect->origin.x + 1;
  72.     rect->size.height = top - rect->origin.y + 1;
  73. }
  74.  
  75.  
  76. /*
  77. ===========
  78. =
  79. = BoundLineStore
  80. =
  81. ===========
  82. */
  83.  
  84. void BoundLineStore (id lines_i, NXRect *r)
  85. {
  86.     int                i,c;
  87.     worldline_t        *line_p;
  88.     
  89.     c = [lines_i count];
  90.     if (!c)
  91.         Error ("BoundLineStore: empty list");
  92.         
  93.     line_p = [lines_i elementAt:0];
  94.     IDRectFromPoints (r, &line_p->p1, &line_p->p2);
  95.     
  96.     for (i=1 ; i<c ; i++)
  97.     {
  98.         line_p = [lines_i elementAt:i];
  99.         IDEnclosePoint (r, &line_p->p1);
  100.         IDEnclosePoint (r, &line_p->p2);
  101.     }    
  102. }
  103.  
  104.  
  105. /*
  106. ===========
  107. =
  108. = DrawLineStore
  109. =
  110. = Draws all of the lines in the given storage object
  111. =
  112. ===========
  113. */
  114.  
  115. void DrawLineStore (id lines_i)
  116. {
  117.     int                i,c;
  118.     worldline_t        *line_p;
  119.     
  120.     if (!draw)
  121.         return;
  122.         
  123.     c = [lines_i count];
  124.     
  125.     for (i=0 ; i<c ; i++)
  126.     {
  127.         line_p = [lines_i elementAt:i];
  128.         PSmoveto (line_p->p1.x, line_p->p1.y);
  129.         PSlineto (line_p->p2.x, line_p->p2.y);
  130.         PSstroke ();
  131.     }
  132.     NXPing ();
  133. }
  134.  
  135. /*
  136. ===========
  137. =
  138. = DrawLine
  139. =
  140. = Draws all of the lines in the given storage object
  141. =
  142. ===========
  143. */
  144.  
  145. void DrawLineDef (maplinedef_t *ld)
  146. {
  147.     mapvertex_t        *v1, *v2;
  148.     
  149.     if (!draw)
  150.         return;
  151.     
  152.     v1 = [mapvertexstore_i elementAt: ld->v1];
  153.     v2 = [mapvertexstore_i elementAt: ld->v2];
  154.         
  155.     PSmoveto (v1->x, v1->y);
  156.     PSlineto (v2->x, v2->y);
  157.     PSstroke ();
  158.     NXPing ();
  159. }
  160.  
  161.  
  162. /*
  163. ===========
  164. =
  165. = DrawMap
  166. =
  167. ===========
  168. */
  169.  
  170. void DrawMap (void)
  171. {
  172.     NXRect    scaled;
  173.     
  174.     BoundLineStore (linestore_i, &worldbounds);
  175.     worldbounds.origin.x -= 8;
  176.     worldbounds.origin.y -= 8;
  177.     worldbounds.size.width += 16;
  178.     worldbounds.size.height += 16;
  179.     
  180.     if (!draw)
  181.         return;
  182.         
  183.     scaled.origin.x = 300;
  184.     scaled.origin.y = 80;
  185.     scaled.size.width = worldbounds.size.width*scale;
  186.     scaled.size.height = worldbounds.size.height* scale;
  187.     
  188.     window_i =
  189.     [[Window alloc]
  190.         initContent:    &scaled
  191.         style:            NX_TITLEDSTYLE
  192.         backing:        NX_RETAINED
  193.         buttonMask:        0
  194.         defer:            NO
  195.     ];
  196.     
  197.     [window_i display];
  198.     [window_i orderFront: nil];
  199.     view_i = [window_i contentView];
  200.     
  201.     [view_i
  202.         setDrawSize:    worldbounds.size.width
  203.         :                worldbounds.size.height];
  204.     [view_i 
  205.         setDrawOrigin:    worldbounds.origin.x 
  206.         :                 worldbounds.origin.y];
  207.             
  208.     [view_i lockFocus];
  209.     PSsetgray (NX_BLACK);    
  210.     DrawLineStore (linestore_i);
  211. }
  212.  
  213.  
  214. /*
  215. ===========
  216. =
  217. = EraseWindow
  218. =
  219. ===========
  220. */
  221.  
  222. void EraseWindow (void)
  223. {
  224.     if (!draw)
  225.         return;
  226.         
  227.     NXEraseRect (&worldbounds);
  228.     NXPing ();
  229. }
  230.  
  231.  
  232. /*
  233. ============================
  234. =
  235. = DrawDivLine
  236. =
  237. ============================
  238. */
  239.  
  240. void DrawDivLine (divline_t *div)
  241. {
  242.     float    vx,vy, dist;
  243.     
  244.     if (!draw)
  245.         return;
  246.         
  247.     PSsetgray (NX_BLACK);
  248.     
  249.     dist = sqrt (pow(div->dx,2)+pow(div->dy,2));
  250.     vx = div->dx/dist;
  251.     vy = div->dy/dist;
  252.     
  253.     dist = MAX(worldbounds.size.width,worldbounds.size.height);
  254.     
  255.     PSmoveto (div->pt.x - vx*dist, div->pt.y - vy*dist);
  256.     PSlineto (div->pt.x + vx*dist, div->pt.y + vy*dist);
  257.     PSstroke ();
  258.     NXPing ();
  259. }
  260.  
  261.