AreaAllocator.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Copyright (c) 2008-2014 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() :
  27. size_(IntVector2::ZERO),
  28. maxSize_(IntVector2::ZERO),
  29. doubleWidth_(true)
  30. {
  31. Reset(0, 0);
  32. }
  33. AreaAllocator::AreaAllocator(int width, int height) :
  34. size_(width, height),
  35. maxSize_(IntVector2::ZERO),
  36. doubleWidth_(true)
  37. {
  38. Reset(width, height);
  39. }
  40. AreaAllocator::AreaAllocator(int width, int height, int maxWidth, int maxHeight) :
  41. size_(width, height),
  42. maxSize_(maxWidth, maxHeight),
  43. doubleWidth_(true)
  44. {
  45. Reset(width, height);
  46. }
  47. void AreaAllocator::Reset(int width, int height)
  48. {
  49. freeAreas_.Clear();
  50. IntRect initialArea(0, 0, width, height);
  51. freeAreas_.Push(initialArea);
  52. }
  53. bool AreaAllocator::Allocate(int width, int height, int& x, int& y)
  54. {
  55. if (width < 0)
  56. width = 0;
  57. if (height < 0)
  58. height = 0;
  59. PODVector<IntRect>::Iterator best;
  60. int bestFreeArea;
  61. for(;;)
  62. {
  63. best = freeAreas_.End();
  64. bestFreeArea = M_MAX_INT;
  65. for (PODVector<IntRect>::Iterator i = freeAreas_.Begin(); i != freeAreas_.End(); ++i)
  66. {
  67. int freeWidth = i->Width();
  68. int freeHeight = i->Height();
  69. if (freeWidth >= width && freeHeight >= height)
  70. {
  71. // Calculate rank for free area. Lower is better
  72. int freeArea = freeWidth * freeHeight;
  73. if (freeArea < bestFreeArea)
  74. {
  75. best = i;
  76. bestFreeArea = freeArea;
  77. }
  78. }
  79. }
  80. if (best == freeAreas_.End())
  81. {
  82. if (doubleWidth_ && size_.x_ < maxSize_.x_)
  83. {
  84. int oldWidth = size_.x_;
  85. size_.x_ <<= 1;
  86. // If no allocations yet, simply expand the single free area
  87. IntRect& first = freeAreas_.Front();
  88. if (freeAreas_.Size() == 1 && first.left_ == 0 && first.top_ == 0 && first.right_ == oldWidth && first.bottom_ == size_.y_)
  89. first.right_ = size_.x_;
  90. else
  91. {
  92. IntRect newArea(oldWidth, 0, size_.x_, size_.y_);
  93. freeAreas_.Push(newArea);
  94. }
  95. }
  96. else if (!doubleWidth_ && size_.y_ < maxSize_.y_)
  97. {
  98. int oldHeight = size_.y_;
  99. size_.y_ <<= 1;
  100. // If no allocations yet, simply expand the single free area
  101. IntRect& first = freeAreas_.Front();
  102. if (freeAreas_.Size() == 1 && first.left_ == 0 && first.top_ == 0 && first.right_ == size_.x_ && first.bottom_ == oldHeight)
  103. first.bottom_ = size_.y_;
  104. else
  105. {
  106. IntRect newArea(0, oldHeight, size_.x_, size_.y_);
  107. freeAreas_.Push(newArea);
  108. }
  109. }
  110. else
  111. return false;
  112. doubleWidth_ = !doubleWidth_;
  113. }
  114. else
  115. break;
  116. }
  117. IntRect reserved(best->left_, best->top_, best->left_ + width, best->top_ + height);
  118. x = best->left_;
  119. y = best->top_;
  120. // Reserve the area by splitting up the remaining free area
  121. best->left_ = reserved.right_;
  122. if (best->Height() > 2 * height)
  123. {
  124. IntRect splitArea(reserved.left_, reserved.bottom_, best->right_, best->bottom_);
  125. best->bottom_ = reserved.bottom_;
  126. freeAreas_.Push(splitArea);
  127. }
  128. return true;
  129. }
  130. }