From c915f1bcd46144578fad464e52b5a6013dc98de8 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 17 Mar 2023 02:05:42 +0000 Subject: Fix texture packer return value so positions match inputs --- assetFactory/texturePacker.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'assetFactory/texturePacker.cpp') diff --git a/assetFactory/texturePacker.cpp b/assetFactory/texturePacker.cpp index 31c9a0e..68a6010 100644 --- a/assetFactory/texturePacker.cpp +++ b/assetFactory/texturePacker.cpp @@ -1,14 +1,16 @@ #include "texturePacker.h" +#include "collections.hpp" #include #include #include #include #include -TexturePacker::TexturePacker(std::vector in) : inputImages {std::move(in)} +TexturePacker::TexturePacker(std::span in) : + inputImages {std::move(in)}, sortedIndexes {vectorOfN(inputImages.size(), size_t {})} { - std::sort(inputImages.rbegin(), inputImages.rend(), [](const auto & a, const auto & b) { - return area(a) < area(b); + std::sort(sortedIndexes.rbegin(), sortedIndexes.rend(), [this](const auto a, const auto b) { + return area(inputImages[a]) < area(inputImages[b]); }); } @@ -24,15 +26,16 @@ TexturePacker::pack(Size size) const using Spaces = std::set; Spaces spaces {{{}, size}}; - Positions result; - for (const auto & image : inputImages) { + Positions result(inputImages.size()); + for (const auto & idx : sortedIndexes) { + const auto & image = inputImages[idx]; if (const auto spaceItr = std::find_if(spaces.begin(), spaces.end(), [image](const Space & s) { return image.x <= s.size.x && image.y <= s.size.y; }); spaceItr != spaces.end()) { auto space = *spaceItr; - result.push_back(space.position); + result[idx] = space.position; spaces.erase(spaceItr); if (space.size.x > image.x) { spaces.emplace(Position {space.position.x + image.x, space.position.y}, -- cgit v1.2.3