glue.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Copyright (c) 2024-2025 Bruce A Henderson
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source distribution.
  16. */
  17. #include "rect_pack.h"
  18. #include "brl.mod/blitz.mod/blitz.h"
  19. extern "C" {
  20. void brl_rectpacker_TRectPacker__GetSize(BBObject * packer, int index, int * width, int * height, int * id);
  21. BBArray * brl_rectpacker_TRectPacker__NewSheetArray(int size);
  22. void brl_rectpacker_TRectPacker__SetSheet(BBArray * sheets, int index, BBObject * sheet);
  23. BBObject * brl_rectpacker_TPackedSheet__Create(int width, int height, int size);
  24. void brl_rectpacker_TPackedSheet__SetRect(BBObject * sheet, int index, int id, int x, int y, int width, int height, int rotated);
  25. BBArray * bmx_rectpacker_pack(BBObject * packer, int packingMethod, int maxSheets, int powerOfTwo, int square, int allowRotate, int alignWidth, int borderPadding, int sheetPadding, int overAllocate, int minWidth, int minHeight, int maxWidth, int maxHeight, int count);
  26. }
  27. BBArray * bmx_rectpacker_pack(BBObject * packer, int packingMethod, int maxSheets, int powerOfTwo, int square, int allowRotate, int alignWidth, int borderPadding, int sheetPadding, int overAllocate, int minWidth, int minHeight, int maxWidth, int maxHeight, int count) {
  28. rect_pack::Settings settings;
  29. settings.method = static_cast<rect_pack::Method>(packingMethod);
  30. settings.max_sheets = maxSheets;
  31. settings.power_of_two = static_cast<bool>(powerOfTwo);
  32. settings.square = static_cast<bool>(square);
  33. settings.allow_rotate = static_cast<bool>(allowRotate);
  34. settings.align_width = static_cast<bool>(alignWidth);
  35. settings.border_padding = sheetPadding;
  36. settings.over_allocate = overAllocate;
  37. settings.min_width = minWidth;
  38. settings.min_height = minHeight;
  39. settings.max_width = maxWidth;
  40. settings.max_height = maxHeight;
  41. std::vector<rect_pack::Size> sizes;
  42. for (int i = 0; i < count; i++) {
  43. rect_pack::Size s;
  44. brl_rectpacker_TRectPacker__GetSize(packer, i, &s.width, &s.height, &s.id);
  45. if ( borderPadding > 0 ) {
  46. s.width += borderPadding * 2;
  47. s.height += borderPadding * 2;
  48. }
  49. sizes.push_back(s);
  50. }
  51. std::vector<rect_pack::Sheet> sheets = rect_pack::pack(settings, sizes);
  52. BBArray * result = brl_rectpacker_TRectPacker__NewSheetArray(sheets.size());
  53. for (int i = 0; i < sheets.size(); i++) {
  54. BBObject * sheet = brl_rectpacker_TPackedSheet__Create(sheets[i].width, sheets[i].height, sheets[i].rects.size());
  55. for (int j = 0; j < sheets[i].rects.size(); j++) {
  56. rect_pack::Rect r = sheets[i].rects[j];
  57. if ( borderPadding > 0 ) {
  58. r.x += borderPadding;
  59. r.y += borderPadding;
  60. r.width -= borderPadding * 2;
  61. r.height -= borderPadding * 2;
  62. }
  63. brl_rectpacker_TPackedSheet__SetRect(sheet, j, r.id, r.x, r.y, r.width, r.height, r.rotated);
  64. }
  65. brl_rectpacker_TRectPacker__SetSheet(result, i, sheet);
  66. }
  67. return result;
  68. }