| 55 | Array<NodeType*> FreeNodes; |
| 56 | |
| 57 | struct SizeRect |
| 58 | { |
| 59 | Size X, Y, W, H; |
| 60 | |
| 61 | FORCE_INLINE SizeRect() |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | FORCE_INLINE SizeRect(Size x, Size y, Size w, Size h) |
| 66 | : X(x) |
| 67 | , Y(y) |
| 68 | , W(w) |
| 69 | , H(h) |
| 70 | { |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | void AddFreeNode(NodeType* node) |
| 75 | { |
| 76 | // Use binary search to find the insert location (assumes that FreeNodes are always sorted) |
| 77 | int32 left = 0, right = FreeNodes.Count() - 1; |
| 78 | const uint32 nodeSize = (uint32)node->Width * (uint32)node->Height; |
| 79 | while (left <= right) |
| 80 | { |
| 81 | int32 mid = left + (right - left) / 2; |
| 82 | const NodeType* midNode = FreeNodes.Get()[mid]; |
| 83 | const uint32 midSize = (uint32)midNode->Width * (uint32)midNode->Height; |
| 84 | if (nodeSize == midSize) |
| 85 | { |
| 86 | // Insert right after node of the same size |
| 87 | left = mid; |
| 88 | break; |
| 89 | } |
| 90 | if (nodeSize > midSize) |
| 91 | { |
| 92 | // Go to the left half (contains nodes with higher sizes) |
| 93 | right = mid - 1; |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | // Go to the right half (contains nodes with lower sizes) |
| 98 | left = mid + 1; |
| 99 | } |
| 100 | } |
| 101 | FreeNodes.Insert(left, node); |
| 102 | } |
| 103 | |
| 104 | public: |
| 105 | FORCE_INLINE bool IsInitialized() |
| 106 | { |
| 107 | return Width != 0; |
| 108 | } |
| 109 | |
| 110 | /// <summary> |
| 111 | /// Initializes the atlas of a given size. Clears any previously added nodes. This won't invoke OnFree for atlas tiles. |
| 112 | /// </summary> |
| 113 | /// <param name="atlasWidth">The atlas width (in pixels).</param> |
| 114 | /// <param name="atlasHeight">The atlas height (in pixels).</param> |