-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
102 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
|
||
#include "packing.h" | ||
|
||
namespace spright { | ||
|
||
void pack_lines(bool horizontal, const OutputPtr& output, | ||
SpriteSpan sprites, std::vector<Texture>& textures) { | ||
|
||
const auto add_texture = [&](SpriteSpan sprites) { | ||
auto texture = Texture{ output, | ||
static_cast<int>(textures.size()), | ||
0, 0, sprites, | ||
}; | ||
recompute_texture_size(texture); | ||
textures.push_back(std::move(texture)); | ||
}; | ||
|
||
const auto max_width = (output->max_width ? | ||
output->max_width- output->border_padding * 2 : | ||
std::numeric_limits<int>::max()); | ||
const auto max_height = (output->max_height ? | ||
output->max_height - output->border_padding * 2 : | ||
std::numeric_limits<int>::max()); | ||
const auto max_textures = static_cast<size_t>(output->filename.count()); | ||
|
||
auto pos = Point{ }; | ||
auto size = Size{ }; | ||
auto line_size = 0; | ||
|
||
// d = direction, p = perpendicular | ||
auto& pos_d = (horizontal ? pos.x : pos.y); | ||
auto& pos_p = (horizontal ? pos.y : pos.x); | ||
const auto& size_d = (horizontal ? size.x : size.y); | ||
const auto& size_p = (horizontal ? size.y : size.x); | ||
const auto max_d = (horizontal ? max_width : max_height); | ||
const auto max_p = (horizontal ? max_height : max_width); | ||
|
||
auto first_sprite = sprites.begin(); | ||
auto it = first_sprite; | ||
for (; it != sprites.end(); ++it) { | ||
auto& sprite = *it; | ||
size = get_sprite_size(sprite); | ||
size.x += output->shape_padding; | ||
size.y += output->shape_padding; | ||
|
||
if (pos_d + size_d > max_d) { | ||
pos_d = 0; | ||
pos_p += line_size; | ||
line_size = 0; | ||
} | ||
if (pos_p + size_p > max_p) { | ||
add_texture({ first_sprite, it }); | ||
if (textures.size() >= max_textures) | ||
break; | ||
first_sprite = it; | ||
pos.x = 0; | ||
pos.y = 0; | ||
line_size = 0; | ||
} | ||
if (pos.x + size.x > max_width || | ||
pos.y + size.y > max_height) | ||
break; | ||
|
||
const auto indent = get_sprite_indent(sprite); | ||
sprite.trimmed_rect = { | ||
pos.x + indent.x + output->border_padding, | ||
pos.y + indent.y + output->border_padding, | ||
sprite.trimmed_source_rect.w, | ||
sprite.trimmed_source_rect.h | ||
}; | ||
|
||
pos_d += size_d; | ||
line_size = std::max(line_size, size_p); | ||
} | ||
if (it != sprites.end()) | ||
throw std::runtime_error("not all sprites could be packed"); | ||
|
||
add_texture({ first_sprite, it }); | ||
} | ||
|
||
} // namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters