AreaAllocator.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Copyright (c) 2008-2013 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. #include "Precompiled.h"
  23. #include "AreaAllocator.h"
  24. namespace Urho3D
  25. {
  26. AreaAllocator::AreaAllocator(int width, int height) :
  27. size_(width, height),
  28. maxSize_(IntVector2::ZERO),
  29. doubleWidth_(true)
  30. {
  31. Reset(width, height);
  32. }
  33. AreaAllocator::AreaAllocator(int width, int height, int maxWidth, int maxHeight) :
  34. size_(width, height),
  35. maxSize_(maxWidth, maxHeight),
  36. doubleWidth_(true)
  37. {
  38. Reset(width, height);
  39. }
  40. void AreaAllocator::Reset(int width, int height)
  41. {
  42. freeAreas_.Clear();
  43. IntRect initialArea(0, 0, width, height);
  44. freeAreas_.Push(initialArea);
  45. }
  46. bool AreaAllocator::Allocate(int width, int height, int& x, int& y)
  47. {
  48. if (width < 0)
  49. width = 0;
  50. if (height < 0)
  51. height = 0;
  52. PODVector<IntRect>::Iterator best = freeAreas_.End();
  53. int bestFreeArea = M_MAX_INT;
  54. for(;;)
  55. {
  56. for (PODVector<IntRect>::Iterator i = freeAreas_.Begin(); i != freeAreas_.End(); ++i)
  57. {
  58. int freeWidth = i->Width();
  59. int freeHeight = i->Height();
  60. if (freeWidth >= width && freeHeight >= height)
  61. {
  62. // Calculate rank for free area. Lower is better
  63. int freeArea = freeWidth * freeHeight;
  64. if (freeArea < bestFreeArea)
  65. {
  66. best = i;
  67. bestFreeArea = freeArea;
  68. }
  69. }
  70. }
  71. if (best == freeAreas_.End())
  72. {
  73. if (doubleWidth_ && size_.x_ < maxSize_.x_)
  74. {
  75. int oldWidth = size_.x_;
  76. size_.x_ <<= 1;
  77. IntRect newArea(oldWidth, 0, size_.x_, size_.y_);
  78. freeAreas_.Push(newArea);
  79. }
  80. else if (!doubleWidth_ && size_.y_ < maxSize_.y_)
  81. {
  82. int oldHeight = size_.y_;
  83. size_.y_ <<= 1;
  84. IntRect newArea(0, oldHeight, size_.x_, size_.y_);
  85. freeAreas_.Push(newArea);
  86. }
  87. else
  88. return false;
  89. doubleWidth_ = !doubleWidth_;
  90. }
  91. else
  92. break;
  93. }
  94. IntRect reserved(best->left_, best->top_, best->left_ + width, best->top_ + height);
  95. x = best->left_;
  96. y = best->top_;
  97. // Reserved the area by spliting up the remaining free area
  98. best->left_ = reserved.right_;
  99. if (best->Height() > 2 * height)
  100. {
  101. IntRect splitArea(reserved.left_, reserved.bottom_, best->right_, best->bottom_);
  102. best->bottom_ = reserved.bottom_;
  103. freeAreas_.Push(splitArea);
  104. }
  105. return true;
  106. }
  107. }