diff --git a/src/main_gui.cpp b/src/main_gui.cpp index 1f0f8b8..a621ef4 100644 --- a/src/main_gui.cpp +++ b/src/main_gui.cpp @@ -98,6 +98,7 @@ int main(int argc, const char* argv[]) { i += 4; } const std::vector bbox_colors = {RED, GREEN, BLUE}; + std::vector 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 f_packed = pack(*in_frame, bboxes); + std::optional 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(); diff --git a/src/pack.cpp b/src/pack.cpp index 3e56622..505e259 100644 --- a/src/pack.cpp +++ b/src/pack.cpp @@ -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 pack(const Frame& in_frame, - const std::vector& bboxes) { + const std::vector& bboxes, + std::vector& packed_bboxes) { if (bboxes.size() == 0) { std::cerr << "No bounding box, cannot pack.\n"; return {}; @@ -71,9 +71,12 @@ std::optional pack(const Frame& in_frame, const auto& largest = sorted_bboxes[0]; BoundingBox out_largest = {0, 0, static_cast(largest.width()), static_cast(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 diff --git a/src/pack.h b/src/pack.h index 6fa007b..f4b6a72 100644 --- a/src/pack.h +++ b/src/pack.h @@ -9,6 +9,7 @@ namespace freling { std::optional pack(const Frame& in_frame, - const std::vector& bboxes); + const std::vector& bboxes, + std::vector& packed_bboxes); }