home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / assembly / talinasm.lha / rectfit.asm < prev    next >
Encoding:
Assembly Source File  |  1992-03-19  |  3.3 KB  |  90 lines

  1. * ============================================================================ *
  2. *    RectFit.asm: constrain a rectangle to fit inside another one.
  3. *
  4. *            void RectFit(struct IBox *outer, struct IBox *inner,struct IBox *result);
  5. *                            a0                   a1                 a2
  6. *
  7. *    This function moves causes the size and position of the rectangle "inner"
  8. *    to be constrained to fit into the rectangle "outer" and the resulting rectangle
  9. *    is placed in "result" (which can be the same pointer as "inner")
  10. *    Note that the Left and Top coords of the "outer" box are ignored, i.e. it
  11. *    is assumed that both "inner" and "result" are relative to the origin of
  12. *    "outer". (This would be the case when using RectFit to determine the size
  13. *    of screens and windows).
  14. * ============================================================================ *
  15.  
  16.             include        "exec/types.i"
  17.             include        "intuition/intuition.i"
  18.             include        "macros.i"
  19.  
  20.             SECTION        rectfit.asm,CODE
  21.  
  22.             xdef        _RectFit
  23.  
  24. _RectFit:                    ; (outer:a0, inner:a1, result:a2)
  25.  
  26. ; constrain the width to be non-negative and not creater than outer width
  27.  
  28.             move.w        ibox_Width(a1),d0        ; width of inner box
  29.             bpl.s        1$                        ; if negative
  30.             moveq        #0,d0                    ; then make it 0
  31. 1$            move.w        ibox_Width(a0),d1        ; width of outer box
  32.             cmp.w        d0,d1                    ; if (inner < outer)
  33.             bhs.s        2$                        ; then it's ok
  34.             move.w        d1,d0                    ; else clamp
  35. 2$            move.w        d0,ibox_Width(a2)        ; save as result
  36.  
  37. ; constrain the left edge of the box to be less than (outer-right - inner-width);
  38. ; The commented-out lines are if we ever want to change to absolute positioning.
  39.  
  40.             sub.w        d0,d1                    ; amount of width free
  41.             move.w        ibox_Left(a1),d0        ; d0 <-- inner left
  42. ;            sub.w        ibox_Left(a0),d0        ; relative to outer box
  43.             bpl.s        3$                        ; if positive, ok
  44.             moveq        #0,d0                    ; otherwise, make it 0
  45. 3$            cmp.w        d0,d1                    ; don't go too far right
  46.             bhs.s        4$                        ; otherwise
  47.             move.w        d1,d0                    ; clip it
  48. 4$
  49. ;            add.w        ibox_Left(a0),d0        ; relative to left edge
  50.             move.w        d0,ibox_Left(a2)
  51.             
  52. ; constrain the height to be non-negative and not creater than outer height
  53.  
  54.             move.w        ibox_Height(a1),d0        ; height of inner box
  55.             bpl.s        11$                        ; if negative
  56.             moveq        #0,d0                    ; then make it 0
  57. 11$            move.w        ibox_Height(a0),d1        ; height of outer box
  58.             cmp.w        d0,d1                    ; if (inner < outer)
  59.             bhs.s        12$                        ; then it's ok
  60.             move.w        d1,d0                    ; else clamp
  61. 12$            move.w        d0,ibox_Height(a2)        ; save as result
  62.  
  63. ; constrain the top edge of the box to be less than (outer-bottom - inner-height);
  64. ; The commented-out lines are if we ever want to change to absolute positioning.
  65.  
  66.             sub.w        d0,d1                    ; amount of height free
  67.             move.w        ibox_Top(a1),d0            ; d0 <-- inner top
  68. ;            sub.w        ibox_Top(a0),d0            ; relative to outer box
  69.             bpl.s        13$                        ; if positive, ok
  70.             moveq        #0,d0                    ; otherwise, make it 0
  71. 13$            cmp.w        d0,d1                    ; don't go too far down
  72.             bhs.s        14$                        ; otherwise
  73.             move.w        d1,d0                    ; clip it
  74. 14$
  75. ;            add.w        ibox_Top(a0),d0            ; relative to bottom edge
  76.             move.w        d0,ibox_Top(a2)
  77.             move.l        a2,d0                    ; return address of box
  78.             
  79.             rts
  80.  
  81.             end
  82. ; original C source
  83. void RectFit(struct IBox *outer, struct IBox *inner,struct IBox *result)
  84. {
  85.     result->Width        = clamp(0,inner->Width,outer->Width);
  86.     result->Height        = clamp(0,inner->Height,outer->Height);
  87.     result->Left        = clamp(0,inner->Left,outer->Width - result->Width);
  88.     result->Top            = clamp(0,inner->Top,outer->Height - result->Height);
  89. }
  90.