Skip to content

Commit

Permalink
Provide shrink as a public method
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Jun 8, 2017
1 parent f9be801 commit d139f87
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
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 << "clear 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

0 comments on commit d139f87

Please sign in to comment.