AreaAllocator.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 = freeAreas_.End();
  60. int bestFreeArea = M_MAX_INT;
  61. for(;;)
  62. {
  63. for (PODVector<IntRect>::Iterator i = freeAreas_.Begin(); i != freeAreas_.End(); ++i)
  64. {
  65. int freeWidth = i->Width();
  66. int freeHeight = i->Height();
  67. if (freeWidth >= width && freeHeight >= height)
  68. {
  69. // Calculate rank for free area. Lower is better
  70. int freeArea = freeWidth * freeHeight;
  71. if (freeArea < bestFreeArea)
  72. {
  73. best = i;
  74. bestFreeArea = freeArea;
  75. }
  76. }
  77. }
  78. if (best == freeAreas_.End())
  79. {
  80. if (doubleWidth_ && size_.x_ < maxSize_.x_)
  81. {
  82. int oldWidth = size_.x_;
  83. size_.x_ <<= 1;
  84. IntRect newArea(oldWidth, 0, size_.x_, size_.y_);
  85. freeAreas_.Push(newArea);
  86. }
  87. else if (!doubleWidth_ && size_.y_ < maxSize_.y_)
  88. {
  89. int oldHeight = size_.y_;
  90. size_.y_ <<= 1;
  91. IntRect newArea(0, oldHeight, size_.x_, size_.y_);
  92. freeAreas_.Push(newArea);
  93. }
  94. else
  95. return false;
  96. doubleWidth_ = !doubleWidth_;
  97. }
  98. else
  99. break;
  100. }
  101. IntRect reserved(best->left_, best->top_, best->left_ + width, best->top_ + height);
  102. x = best->left_;
  103. y = best->top_;
  104. // Reserve the area by splitting up the remaining free area
  105. best->left_ = reserved.right_;
  106. if (best->Height() > 2 * height)
  107. {
  108. IntRect splitArea(reserved.left_, reserved.bottom_, best->right_, best->bottom_);
  109. best->bottom_ = reserved.bottom_;
  110. freeAreas_.Push(splitArea);
  111. }
  112. return true;
  113. }
  114. }