AreaAllocator.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Math/Rect.h"
  24. namespace Atomic
  25. {
  26. /// Rectangular area allocator.
  27. class ATOMIC_API AreaAllocator
  28. {
  29. public:
  30. /// Default construct with empty size.
  31. AreaAllocator();
  32. /// Construct with given width and height.
  33. AreaAllocator(int width, int height, bool fastMode = true);
  34. /// Construct with given width and height, and set the maximum it allows to grow.
  35. AreaAllocator(int width, int height, int maxWidth, int maxHeight, bool fastMode = true);
  36. /// Reset to given width and height and remove all previous allocations.
  37. void Reset(int width, int height, int maxWidth = 0, int maxHeight = 0, bool fastMode = true);
  38. /// Try to allocate an area. Return true if successful, with x & y coordinates filled.
  39. bool Allocate(int width, int height, int& x, int& y);
  40. /// Return the current width.
  41. int GetWidth() const { return size_.x_; }
  42. /// Return the current height.
  43. int GetHeight() const { return size_.y_; }
  44. /// Return whether uses fast mode. Fast mode uses a simpler allocation scheme which may waste free space, but is OK for eg. fonts.
  45. bool GetFastMode() const { return fastMode_; }
  46. private:
  47. /// Remove space from a free rectangle. Return true if the original rectangle should be erased from the free list. Not called in fast mode.
  48. bool SplitRect(unsigned freeAreaIndex, const IntRect& reserve);
  49. /// Clean up redundant free space. Not called in fast mode.
  50. void Cleanup();
  51. /// Free rectangles.
  52. PODVector<IntRect> freeAreas_;
  53. /// Current size.
  54. IntVector2 size_;
  55. /// Maximum size it allows to grow. It is zero when it is not allowed to grow.
  56. IntVector2 maxSize_;
  57. /// The dimension use for next growth. Used internally.
  58. bool doubleWidth_;
  59. /// Fast mode flag.
  60. bool fastMode_;
  61. };
  62. }