show packed bboxes

This commit is contained in:
Fabien Freling 2022-02-15 13:59:17 +01:00
parent 81463d9eb0
commit 8698cf8160
3 changed files with 28 additions and 17 deletions

View file

@ -98,6 +98,7 @@ int main(int argc, const char* argv[]) {
i += 4;
}
const std::vector<Color> bbox_colors = {RED, GREEN, BLUE};
std::vector<freling::BoundingBox> packed_bboxes;
Vector2 win_size = {800, 450};
const int padding = 5;
@ -120,16 +121,13 @@ int main(int argc, const char* argv[]) {
SetWindowSize(win_size.x, win_size.y);
const Vector2 b_size = {150, 30};
Button b_add_box = {{padding, padding, b_size.x, b_size.y}, "Add box"};
Button b_pack = {{padding * 2 + b_size.x, padding, b_size.x, b_size.y},
"Pack rects"};
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
//
// Buttons
// Widgets: buttons, ...
//
if (GuiButton((Rectangle){padding, padding, b_size.x, b_size.y},
"Add Box")) {
@ -139,7 +137,8 @@ int main(int argc, const char* argv[]) {
if (GuiButton((Rectangle){padding * 2 + b_size.x, padding, b_size.x,
b_size.y},
"Pack Rects")) {
std::optional<Frame> f_packed = pack(*in_frame, bboxes);
std::optional<Frame> f_packed =
pack(*in_frame, bboxes, packed_bboxes);
if (packed) {
UnloadTexture(pack_texture);
@ -179,10 +178,18 @@ int main(int argc, const char* argv[]) {
// Packed image
//
if (packed) {
const int offset_x = in_offset.x + in_texture.width + padding;
DrawTexture(pack_texture, offset_x, in_offset.y, WHITE);
DrawRectangleLines(offset_x, in_offset.y, pack_texture.width,
Vector2 pack_offset = {in_offset.x + in_texture.width + padding,
in_offset.y};
DrawTexture(pack_texture, pack_offset.x, pack_offset.y, WHITE);
DrawRectangleLines(pack_offset.x, pack_offset.y, pack_texture.width,
pack_texture.height, BLACK);
int c = 0;
for (const auto& b : packed_bboxes) {
const auto& color = bbox_colors[c % bboxes.size()];
draw(b, pack_offset, color);
++c;
};
}
EndDrawing();

View file

@ -16,21 +16,21 @@ void blit(const Frame& in_frame,
assert(in_box.height() == out_box.height());
const int data_width = in_box.width() * sizeof(Pixel);
const int in_row_size = in_frame.width * sizeof(Pixel);
const int out_row_size = out_frame.width * sizeof(Pixel);
const int in_row_size = in_frame.width;
const int out_row_size = out_frame.width;
for (unsigned int i = 0; i < in_box.height(); ++i) {
const int in_offset =
in_box.left * sizeof(Pixel) + (i + in_box.top) * in_row_size;
const int out_offset =
out_box.left * sizeof(Pixel) + (i + out_box.top) * out_row_size;
const int in_offset = in_box.left + (i + in_box.top) * in_row_size;
const int out_offset = out_box.left + (i + out_box.top) * out_row_size;
memcpy(out_frame.data + out_offset, in_frame.data + in_offset,
data_width);
}
}
std::optional<Frame> pack(const Frame& in_frame,
const std::vector<BoundingBox>& bboxes) {
const std::vector<BoundingBox>& bboxes,
std::vector<BoundingBox>& packed_bboxes) {
if (bboxes.size() == 0) {
std::cerr << "No bounding box, cannot pack.\n";
return {};
@ -71,9 +71,12 @@ std::optional<Frame> pack(const Frame& in_frame,
const auto& largest = sorted_bboxes[0];
BoundingBox out_largest = {0, 0, static_cast<uint32_t>(largest.width()),
static_cast<uint32_t>(largest.height())};
packed_bboxes.clear();
packed_bboxes.push_back(out_largest);
Frame packed_frame(largest.width(), largest.height());
blit(in_frame, largest, packed_frame, out_largest);
return Frame(in_frame);
return packed_frame;
}
} // namespace freling

View file

@ -9,6 +9,7 @@
namespace freling {
std::optional<Frame> pack(const Frame& in_frame,
const std::vector<BoundingBox>& bboxes);
const std::vector<BoundingBox>& bboxes,
std::vector<BoundingBox>& packed_bboxes);
}