Переглянути джерело

borderPadding now applies to individual rects (#365)

Added sheetPadding to add padding around the edge of the sheet.
Brucey 4 місяців тому
батько
коміт
63eff1c538
3 змінених файлів з 29 додано та 11 видалено
  1. 14 4
      rectpacker.mod/glue.cpp
  2. 14 6
      rectpacker.mod/rectpacker.bmx
  3. 1 1
      rectpacker.mod/source.bmx

+ 14 - 4
rectpacker.mod/glue.cpp

@@ -1,5 +1,5 @@
 /*
-  Copyright (c) 2024 Bruce A Henderson
+  Copyright (c) 2024-2025 Bruce A Henderson
   
   This software is provided 'as-is', without any express or implied
   warranty. In no event will the authors be held liable for any damages
@@ -29,10 +29,10 @@ extern "C" {
     BBObject * brl_rectpacker_TPackedSheet__Create(int width, int height, int size);
     void brl_rectpacker_TPackedSheet__SetRect(BBObject * sheet, int index, int id, int x, int y, int width, int height, int rotated);
 
-    BBArray * bmx_rectpacker_pack(BBObject * packer, int packingMethod, int maxSheets, int powerOfTwo, int square, int allowRotate, int alignWidth, int borderPadding, int overAllocate, int minWidth, int minHeight, int maxWidth, int maxHeight, int count);
+    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);
 }
 
-BBArray * bmx_rectpacker_pack(BBObject * packer, int packingMethod, int maxSheets, int powerOfTwo, int square, int allowRotate, int alignWidth, int borderPadding, int overAllocate, int minWidth, int minHeight, int maxWidth, int maxHeight, int count) {
+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) {
     rect_pack::Settings settings;
     settings.method = static_cast<rect_pack::Method>(packingMethod);
     settings.max_sheets = maxSheets;
@@ -40,7 +40,7 @@ BBArray * bmx_rectpacker_pack(BBObject * packer, int packingMethod, int maxSheet
     settings.square = static_cast<bool>(square);
     settings.allow_rotate = static_cast<bool>(allowRotate);
     settings.align_width = static_cast<bool>(alignWidth);
-    settings.border_padding = borderPadding;
+    settings.border_padding = sheetPadding;
     settings.over_allocate = overAllocate;
     settings.min_width = minWidth;
     settings.min_height = minHeight;
@@ -52,6 +52,10 @@ BBArray * bmx_rectpacker_pack(BBObject * packer, int packingMethod, int maxSheet
     for (int i = 0; i < count; i++) {
         rect_pack::Size s;
         brl_rectpacker_TRectPacker__GetSize(packer, i, &s.width, &s.height, &s.id);
+        if ( borderPadding > 0 ) {
+            s.width += borderPadding * 2;
+            s.height += borderPadding * 2;
+        }
         sizes.push_back(s);
     }
 
@@ -63,6 +67,12 @@ BBArray * bmx_rectpacker_pack(BBObject * packer, int packingMethod, int maxSheet
         BBObject * sheet = brl_rectpacker_TPackedSheet__Create(sheets[i].width, sheets[i].height, sheets[i].rects.size());
         for (int j = 0; j < sheets[i].rects.size(); j++) {
             rect_pack::Rect r = sheets[i].rects[j];
+            if ( borderPadding > 0 ) {
+                r.x += borderPadding;
+                r.y += borderPadding;
+                r.width -= borderPadding;
+                r.height -= borderPadding;
+            }
             brl_rectpacker_TPackedSheet__SetRect(sheet, j, r.id, r.x, r.y, r.width, r.height, r.rotated);
         }
         brl_rectpacker_TRectPacker__SetSheet(result, i, sheet);

+ 14 - 6
rectpacker.mod/rectpacker.bmx

@@ -1,4 +1,4 @@
-' Copyright (c) 2024 Bruce A Henderson
+' Copyright (c) 2024-2025 Bruce A Henderson
 ' 
 ' This software is provided 'as-is', without any express or implied
 ' warranty. In no event will the authors be held liable for any damages
@@ -24,11 +24,14 @@ about: Useful for creating texture atlases, sprite sheets, and other similar thi
 End Rem
 Module BRL.RectPacker
 
-ModuleInfo "Version: 1.00"
+ModuleInfo "Version: 1.01"
 ModuleInfo "License: zlib/libpng"
-ModuleInfo "Copyright: 2024 Bruce A Henderson"
+ModuleInfo "Copyright: 2024-2025 Bruce A Henderson"
 ModuleInfo "rect_pack: Albert Kalchmair 2021, Sean Barrett 2014, Jukka Jylänki"
 
+ModuleInfo "History: 1.01"
+ModuleInfo "History: borderPadding now applies to individual rects."
+ModuleInfo "History: Added sheetPadding to add padding around the edge of the sheet."
 ModuleInfo "History: 1.00 Initial Release"
 
 ModuleInfo "CPP_OPTS: -std=c++11"
@@ -84,10 +87,15 @@ Type TRectPacker
 	Field alignWidth:Int = False
 
 	Rem
-	bbdoc: The amount of padding to add.
+	bbdoc: The amount of padding to add around individual rects.
 	End Rem
 	Field borderPadding:Int
 
+	Rem
+	bbdoc: The amount of padding to add around the edge of the sheet.
+	End Rem
+	Field sheetPadding:Int
+
 	Rem
 	bbdoc: The amount to over-allocate the sheet by.
 	about: This is useful if you want to add a border around the sheet, or if you want to add some padding around the rectangles.
@@ -132,7 +140,7 @@ Type TRectPacker
 	Any rectangles that don't fit into the sheets will be discarded, and not be included in the returned array.
 	End Rem
 	Method Pack:TPackedSheet[]()
-		Return bmx_rectpacker_pack(Self, packingMethod, maxSheets, powerOfTwo, square, allowRotate, alignWidth, borderPadding, overAllocate, minWidth, minHeight, maxWidth, maxHeight, sizes.Count())
+		Return bmx_rectpacker_pack(Self, packingMethod, maxSheets, powerOfTwo, square, allowRotate, alignWidth, borderPadding, sheetPadding, overAllocate, minWidth, minHeight, maxWidth, maxHeight, sizes.Count())
 	End Method
 
 Private
@@ -282,6 +290,6 @@ End Type
 
 Extern
 
-	Function bmx_rectpacker_pack:TPackedSheet[](packer:TRectPacker, packingMethod:EPackingMethod, maxSheets:Int, powerOfTwo:Int, square:Int, allowRotate:Int, alignWidth:Int, borderPadding:Int, overAllocate:Int, minWidth:Int, minHeight:Int, maxWidth:Int, maxHeight:Int, count:Int)
+	Function bmx_rectpacker_pack:TPackedSheet[](packer:TRectPacker, packingMethod:EPackingMethod, 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:Int)
 
 End Extern

+ 1 - 1
rectpacker.mod/source.bmx

@@ -1,4 +1,4 @@
-' Copyright (c) 2024 Bruce A Henderson
+' Copyright (c) 2024-2025 Bruce A Henderson
 ' 
 ' This software is provided 'as-is', without any express or implied
 ' warranty. In no event will the authors be held liable for any damages