rectnode.bmx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. Strict
  2. Rem
  3. Simple rect packer
  4. Based on lightmap packing code by blackpawn
  5. To remove a rect, set its kind to HOLLOW and optimize the root rect
  6. End Rem
  7. Type TRectNode
  8. Const NODE=0,SOLID=1,HOLLOW=-1
  9. Field x,y,width,height,kind
  10. Field child0:TRectNode,child1:TRectNode
  11. Method Insert:TRectNode( w,h )
  12. Local r:TRectNode
  13. If kind=NODE
  14. r=child0.Insert( w,h )
  15. If r Return r
  16. Return child1.Insert( w,h )
  17. EndIf
  18. If kind=SOLID Return Null
  19. If w>width Or h>height Return Null
  20. If w=width And h=height
  21. kind=SOLID
  22. Return Self
  23. EndIf
  24. kind=NODE
  25. Local dw=width-w
  26. Local dh=height-h
  27. If dw>dh
  28. child0=Create( x,y,w,height )
  29. child1=Create( x+w,y,dw,height )
  30. Else
  31. child0=Create( x,y,width,h )
  32. child1=Create( x,y+h,width,dh )
  33. EndIf
  34. Return child0.Insert( w,h )
  35. End Method
  36. Method Optimize()
  37. If kind<>NODE Return
  38. child0.Optimize
  39. child1.Optimize
  40. If child0.kind<>HOLLOW Or child1.kind<>HOLLOW Return
  41. kind=HOLLOW
  42. child0=Null
  43. child1=Null
  44. End Method
  45. Function Create:TRectNode( x,y,w,h )
  46. Local r:TRectNode=New TRectNode
  47. r.x=x
  48. r.y=y
  49. r.width=w
  50. r.height=h
  51. r.kind=HOLLOW
  52. Return r
  53. End Function
  54. End Type