std::unique_ptr<Image> SpriteImporter::makeAtlas(const std::vector<BinPackResult>& result, Vector2i origSize, SpriteSheet& spriteSheet) { Vector2i size = shrinkAtlas(result); auto image = std::make_unique<Image>(Image::Format::RGBA, size); image->clear(0); for (auto& packedImg: result) { ImageData* img = reinterpret_cast<ImageData*>(packedImg.data); image->blitFrom(packedImg.rect.getTopLeft(), *img->img, img->clip, packedImg.rotated); const auto borderTL = img->clip.getTopLeft(); const auto borderBR = img->img->getSize() - img->clip.getSize() - borderTL; const auto offset = Vector2f(0.0001f, 0.0001f); SpriteSheetEntry entry; entry.size = Vector2f(img->clip.getSize()); entry.rotated = packedImg.rotated; entry.pivot = Vector2f(img->pivot - img->clip.getTopLeft()) / entry.size; entry.origPivot = img->pivot; entry.coords = (Rect4f(Vector2f(packedImg.rect.getTopLeft()) + offset, Vector2f(packedImg.rect.getBottomRight()) + offset)) / Vector2f(size); entry.trimBorder = Vector4s(short(borderTL.x), short(borderTL.y), short(borderBR.x), short(borderBR.y)); entry.slices = img->slices; for (auto& filename: img->filenames) { spriteSheet.addSprite(filename, entry); } } return image; }
SpriteSheet TexturePacker::createSpriteSheet(SpriteArray& sprites) { SpriteSheet sheet; QImage& image = sheet.newImage(2048, 2048); QPainter painter(&image); QPoint border(1, 1); const int baselength = Project::getActiveProject().getImagePath().length() + 1; for ( const Sprite& sprite : sprites ) { QImage image(sprite.mName); assert(!image.isNull()); Sprite newSprite; newSprite.mName = sprite.mName.right(sprite.mName.length() - baselength); newSprite.mCoordinates = sprite.mCoordinates; newSprite.mOriginalSize = sprite.mOriginalSize; newSprite.mRotated = sprite.mRotated; newSprite.translate(border); sheet.addSprite(newSprite); if ( newSprite.mRotated ) { RotateImagePainter rpainter(painter); rpainter.drawImage(newSprite.mCoordinates.topLeft(), 90, image.width(), image.height(), newSprite.mCoordinates.width(), newSprite.mCoordinates.height(), image); } else { painter.drawImage(newSprite.mCoordinates, image); } } return sheet; }