bool TexturePacker::assignCoords(unsigned int *width, unsigned int *height){ Set <TextureRectangle *> sortedRects; sortedRects.setCount(rects.getCount()); memcpy(sortedRects.getArray(), rects.getArray(), rects.getCount() * sizeof(TextureRectangle *)); sortedRects.sort(compare); TextureNode *top = new TextureNode(0, 0, *width, *height); *width = 0; *height = 0; for (unsigned int i = 0; i < sortedRects.getCount(); i++){ if (top->assignRectangle(sortedRects[i])){ unsigned int x = sortedRects[i]->x + sortedRects[i]->width; unsigned int y = sortedRects[i]->y + sortedRects[i]->height; if (x > *width ) *width = x; if (y > *height) *height = y; } else { delete top; return false; } } delete top; return true; }
bool TextureNode::assignRectangle(TextureRectangle *newRect){ if (rect == NULL){ if (left->assignRectangle(newRect)) return true; return right->assignRectangle(newRect); } else { if (newRect->width <= rect->width && newRect->height <= rect->height){ newRect->x = rect->x; newRect->y = rect->y; left = new TextureNode(rect->x, rect->y + newRect->height, newRect->width, rect->height - newRect->height); right = new TextureNode(rect->x + newRect->width, rect->y, rect->width - newRect->width, rect->height); delete rect; rect = NULL; return true; } return false; } }