Pixel-Composer/scripts/pack_skyline/pack_skyline.gml

47 lines
1.5 KiB
Plaintext
Raw Normal View History

2023-03-19 09:17:39 +01:00
function sprite_pack_skyline(rectangles, width, height) {
var maxw = 0;
var maxh = 0;
2023-10-18 14:58:55 +02:00
array_sort(rectangles, function(a, b) { return b.w - a.w; });
2023-03-19 09:17:39 +01:00
2023-10-06 11:51:11 +02:00
var skyline = [ new Rectangle(0, 0, width, height) ];
2023-03-19 09:17:39 +01:00
var packed = [];
for (var i = 0; i < array_length(rectangles); i++) {
var rect = rectangles[i];
var bestStrip = noone;
var bestWasted = width * height;
for (var j = 0; j < array_length(skyline); j++) {
var strip = skyline[j];
if (strip.w >= rect.w && strip.h >= rect.h) {
var wasted = strip.w * strip.h;
if (wasted <= bestWasted) {
bestStrip = strip;
bestWasted = wasted;
}
}
}
if (bestStrip == noone) continue;
rect.x = bestStrip.x;
rect.y = bestStrip.y;
array_push(packed, rect);
if (bestStrip.w > rect.w)
2023-10-06 11:51:11 +02:00
array_push(skyline, new Rectangle(bestStrip.x + rect.w, bestStrip.y, bestStrip.w - rect.w, bestStrip.h));
2023-03-19 09:17:39 +01:00
if (bestStrip.h > rect.h)
2023-10-06 11:51:11 +02:00
array_push(skyline, new Rectangle(bestStrip.x, bestStrip.y + rect.h, rect.w, bestStrip.h - rect.h));
2023-03-19 09:17:39 +01:00
array_remove(skyline, bestStrip);
2023-10-18 14:58:55 +02:00
array_sort(skyline, function(a, b) { return a.x - b.x; });
2023-03-19 09:17:39 +01:00
maxw = max(maxw, rect.x + rect.w);
maxh = max(maxh, rect.y + rect.h);
}
return [ new Rectangle(0, 0, maxw, maxh), packed ];
}