thanks, jk
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.9 KiB

  1. mod skyline;
  2. use ::texture::{Buffer2d, ResizeableBuffer2d};
  3. use ::geometry::Rect;
  4. pub use self::skyline::SkylinePacker;
  5. pub trait Packer {
  6. type Buffer: Buffer2d;
  7. fn new(b: Self::Buffer) -> Self;
  8. fn pack<O>(&mut self, buf: &O) -> Option<Rect<usize>>
  9. where O: Buffer2d<Pixel=<Self::Buffer as Buffer2d>::Pixel>; // buffer with same pixel type as this atlas
  10. fn set_margin(&mut self, margin: usize);
  11. fn buf(&self) -> &Self::Buffer;
  12. fn buf_mut(&mut self) -> &mut Self::Buffer;
  13. fn into_buf(self) -> Self::Buffer; // TODO: consider actually implementing Into?
  14. fn dimensions(&self) -> (usize, usize);
  15. fn set_dimensions(&mut self, w: usize, h: usize);
  16. }
  17. pub trait GrowingPacker: Packer {
  18. /// `resize_fn` should describe a strategy for growing the atlas buffer based on the current size.
  19. fn pack_resize<F, O>(&mut self, buf: &O, resize_fn: F) -> Rect<usize>
  20. where O: Buffer2d<Pixel=<<Self as Packer>::Buffer as Buffer2d>::Pixel>,
  21. F: Fn((usize, usize)) -> (usize, usize);
  22. }
  23. impl <A, P> GrowingPacker for P
  24. where A: ResizeableBuffer2d, P: Packer<Buffer = A>
  25. {
  26. fn pack_resize<F, O>(&mut self, buf: &O, resize_fn: F) -> Rect<usize>
  27. where O: Buffer2d<Pixel=<<Self as Packer>::Buffer as Buffer2d>::Pixel>,
  28. F: Fn((usize, usize)) -> (usize, usize)
  29. {
  30. match self.pack(buf) {
  31. Some(p) => p,
  32. None => {
  33. let (w, h) = self.dimensions();
  34. let (w_, h_) = resize_fn((w, h));
  35. if w_ <= w || h_ <= h {
  36. panic!("Resize functions must make the buffer larger!");
  37. }
  38. self.buf_mut().resize(w_, h_);
  39. self.set_dimensions(w_, h_);
  40. // recurse, trying this new size
  41. self.pack_resize(buf, resize_fn)
  42. }
  43. }
  44. }
  45. }