Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide shrink as a public method #11

Merged
merged 1 commit into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions include/shelf-pack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,21 +216,7 @@ class ShelfPack {
}
}

// Shrink the width/height of the sprite to the bare minimum.
// Since shelf-pack doubles first width, then height when running out of shelf space
// this can result in fairly large unused space both in width and height if that happens
// towards the end of bin packing.
if (shelves_.size()) {
int32_t w2 = 0;
int32_t h2 = 0;

for (auto& shelf : shelves_) {
h2 += shelf.h();
w2 = std::max(shelf.w() - shelf.wfree(), w2);
}

resize(w2, h2);
}
shrink();

return results;
}
Expand Down Expand Up @@ -354,6 +340,28 @@ class ShelfPack {
}


/**
*
* Shrink the width/height of the sprite to the bare minimum.
* Since shelf-pack doubles first width, then height when running out of shelf space
* this can result in fairly large unused space both in width and height if that happens
* towards the end of bin packing.
*/
void shrink() {
if (shelves_.size()) {
int32_t w2 = 0;
int32_t h2 = 0;

for (auto& shelf : shelves_) {
h2 += shelf.h();
w2 = std::max(shelf.w() - shelf.wfree(), w2);
}

resize(w2, h2);
}
}


/**
* Return a packed bin given its id, or nullptr if the id is not found
*
Expand Down
17 changes: 17 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,23 @@ void testClear() {
std::cout << " - OK" << std::endl;
}

void testShrink() {
std::cout << "shrink succeeds";

ShelfPack sprite(20, 20);
sprite.packOne(-1, 10, 5);

assert(sprite.width() == 20);
assert(sprite.height() == 20);

sprite.shrink();

assert(sprite.width() == 10);
assert(sprite.height() == 5);

std::cout << " - OK" << std::endl;
}

void testResize1() {
std::cout << "resize larger succeeds";

Expand Down