AreaAllocator.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Math/Rect.h"
  5. namespace Urho3D
  6. {
  7. /// Rectangular area allocator.
  8. class URHO3D_API AreaAllocator
  9. {
  10. public:
  11. /// Default construct with empty size.
  12. AreaAllocator();
  13. /// Construct with given width and height.
  14. AreaAllocator(i32 width, i32 height, bool fastMode = true);
  15. /// Construct with given width and height, and set the maximum it allows to grow.
  16. AreaAllocator(i32 width, i32 height, i32 maxWidth, i32 maxHeight, bool fastMode = true);
  17. /// Reset to given width and height and remove all previous allocations.
  18. void Reset(i32 width, i32 height, i32 maxWidth = 0, i32 maxHeight = 0, bool fastMode = true);
  19. /// Try to allocate an area. Return true if successful, with x & y coordinates filled.
  20. bool Allocate(i32 width, i32 height, i32& x, i32& y);
  21. /// Return the current width.
  22. i32 GetWidth() const { return size_.x_; }
  23. /// Return the current height.
  24. i32 GetHeight() const { return size_.y_; }
  25. /// Return whether uses fast mode. Fast mode uses a simpler allocation scheme which may waste free space, but is OK for eg. fonts.
  26. bool GetFastMode() const { return fastMode_; }
  27. private:
  28. /// Remove space from a free rectangle. Return true if the original rectangle should be erased from the free list. Not called in fast mode.
  29. bool SplitRect(i32 freeAreaIndex, const IntRect& reserve);
  30. /// Clean up redundant free space. Not called in fast mode.
  31. void Cleanup();
  32. /// Free rectangles.
  33. Vector<IntRect> freeAreas_;
  34. /// Current size.
  35. IntVector2 size_;
  36. /// Maximum size it allows to grow. It is zero when it is not allowed to grow.
  37. IntVector2 maxSize_;
  38. /// The dimension use for next growth. Used internally.
  39. bool doubleWidth_{true};
  40. /// Fast mode flag.
  41. bool fastMode_{true};
  42. };
  43. }