diff --git a/stb_rect_pack.h b/stb_rect_pack.h index 38cd75a3c5936789b0b5852f304863a93c41addb..f49d7bb042ff23c7b178e9a9e95859699cd74c81 100644 --- a/stb_rect_pack.h +++ b/stb_rect_pack.h @@ -1,4 +1,4 @@ -// stb_rect_pack.h - v0.01 - public domain - rectangle packing +// stb_rect_pack.h - v0.02 - public domain - rectangle packing // Sean Barrett 2014 // // Useful for e.g. packing rectangular textures into an atlas. @@ -311,9 +311,9 @@ static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int widt } } else { // best-fit - if (waste < best_waste) { + if (y + height <= c->height) { // can only use it if it first vertically - if (y + height <= c->height) { + if (y < best_y || (y == best_y && waste < best_waste)) { best_y = y; best_waste = waste; best = prev; @@ -361,12 +361,15 @@ static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int widt } assert(node->next->x > xpos && node->x <= xpos); y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste); - if (waste <= best_waste && y + height < c->height) { - if (waste < best_waste || y < best_y || (y==best_y && xpos < best_x)) { - best_x = xpos; - best_y = y; - best_waste = waste; - best = prev; + if (y + height < c->height) { + if (y <= best_y) { + if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) { + best_x = xpos; + assert(y <= best_y); + best_y = y; + best_waste = waste; + best = prev; + } } } tail = tail->next; @@ -472,6 +475,17 @@ static int rect_height_compare(const void *a, const void *b) return (p->w > q->w) ? -1 : (p->w < q->w); } +static int rect_width_compare(const void *a, const void *b) +{ + stbrp_rect *p = (stbrp_rect *) a; + stbrp_rect *q = (stbrp_rect *) b; + if (p->w > q->w) + return -1; + if (p->w < q->w) + return 1; + return (p->h > q->h) ? -1 : (p->h < q->h); +} + static int rect_original_order(const void *a, const void *b) { stbrp_rect *p = (stbrp_rect *) a;