StbRectPackSharp is C# port of the stb_rect_pack.h, which is C library to pack rectangles on atlas.
There are two ways of referencing StbRectPackSharp in the project:
Through nuget: https://www.nuget.org/packages/StbRectPackSharp/
As submodule:
a.
git submodule add https://github.com/rds1983/StbRectPackSharp.gitb. Now there are two options:
Add src/StbRectPackSharp.csproj to the solution
Include *.cs from folder "src" directly in the project. In this case, it might make sense to add STBSHARP_INTERNAL build compilation symbol to the project, so StbRectPackSharp classes would become internal.
StbRectPackSharp exposes API similar to stb_rect_pack.h.
Also it has utility class Packer.
Sample usage code
// Create packer with size 256x256Packerpacker=newPacker(256,256);// Pack a few rectangles// data1, data2, data3 are arbitrary user objects that will be stored within instances of PackRectangle class// PackRect returns either object of PackerRectangle class(packing was succesful) or null(no more place)packer.PackRect(10,10,data1);packer.PackRect(15,10,data2);packer.PackRect(2,2,data3);// Enumerate packed rectanglesforeach(PackerRectanglepackRectinpacker.PackRectangles){// ...}If there's no more space to fit the new rectangle, then PackRect method will return null.
It could be addressed by creating newer and bigger Packer.
I.e.
PackerRectanglepr=packer.PackRect(800,600,data4);// If pr is null, it means there's no place for the new rect// Double the size of the packer until the new rectangle will fitwhile(pr==null){PackernewPacker=newPacker(packer.Width*2,packer.Height*2);// Place existing rectanglesforeach(PackerRectangleexistingRectinpacker.PackRectangles){newPacker.PackRect(existingRect.Width,existingRect.Height,existingRect.Data);}// Now dispose old packer and assign new onepacker.Dispose();packer=newPacker;// Try to fit the rectangle againpr=packer.PackRect(800,600,data4);}See also sample VisualizePacking that uses above code.
Public Domain