From f7fd3ef76ba6794676c2651ec5689a8edae044a7 Mon Sep 17 00:00:00 2001 From: Ketmar Dark Date: Sun, 27 Mar 2016 19:59:53 +0300 Subject: [PATCH] texture atlas fix: it seems to work now --- d2dadefs.d | 19 ++++++++++++---- tatlas.d | 77 +++++++++++++++++++++++++++++++++++--------------------------- 2 files changed, 58 insertions(+), 38 deletions(-) diff --git a/d2dadefs.d b/d2dadefs.d index 930b814..ea62d08 100644 --- a/d2dadefs.d +++ b/d2dadefs.d @@ -24,6 +24,8 @@ import dacs; import d2dgfx; import tatlas; +//version = tatlas_dump; + // ////////////////////////////////////////////////////////////////////////// // private enum BIT(ubyte n) = (1U<= 0 && y >= 0 && x < img.width && y < img.height) img.imageData.colors.ptr[y*img.width+x] = c; + } + + private void imgRect (int x, int y, int w, int h, Color c) { + foreach (int d; 0..w) { imgPutPixel(x+d, y, c); imgPutPixel(x+d, y+h-1, c); } + foreach (int d; 1..h-1) { imgPutPixel(x, y+d, c); imgPutPixel(x+w-1, y+d, c); } + } + + private void imgRect4 (int x, int y, int w, int h, Color ct, Color cb, Color cl, Color cr) { + foreach (int d; 0..w) { imgPutPixel(x+d, y, ct); imgPutPixel(x+d, y+h-1, cb); } + foreach (int d; 1..h-1) { imgPutPixel(x, y+d, cl); imgPutPixel(x+w-1, y+d, cr); } + } + // returns invalid rect if there's no room Rect insert (D2DImage di) { assert(di !is null && di.width > 0 && di.height > 0); @@ -86,52 +101,48 @@ public: foreach (immutable cidx; ri+1..rects.length) rects.ptr[cidx-1] = rects.ptr[cidx]; rects.length -= 1; rects.assumeSafeAppend; // for future; we probably won't have alot of best-fitting nodes initially - } else if (rc.w == res.w) { - // split vertically - rects.ptr[ri].y += res.h; - rects.ptr[ri].h -= res.h*2; - } else if (rc.h == res.h) { - // split horizontally - rects.ptr[ri].x += res.w; - rects.ptr[ri].w -= res.w*2; } else { - Rect nr; - // split in both directions (by longer edge) - if (rc.w-res.w < rc.h-res.h) { - // cut the right part - nr.x = rc.x+res.w; - nr.y = rc.y; - nr.w = rc.w-res.w*2; - nr.h = rc.h; - // cut the bottom part + if (rc.w == res.w) { + // split vertically rc.y += res.h; - rc.h -= res.h*2; - rc.w = res.w; - } else { - // cut the bottom part - nr.x = rc.x; - nr.y = rc.y+res.h; - nr.w = rc.w; - nr.h = rc.h-res.h*2; - // cut the right part + rc.h -= res.h; + } else if (rc.h == res.h) { + // split horizontally rc.x += res.w; - rc.w -= res.w*2; - rc.h = res.h; + rc.w -= res.w; + } else { + Rect nr = rc; + // split in both directions (by longer edge) + if (rc.w-res.w > rc.h-res.h) { + // cut the right part + nr.x += res.w; + nr.w -= res.w; + // cut the bottom part + rc.y += res.h; + rc.h -= res.h; + rc.w = res.w; + } else { + // cut the bottom part + nr.y += res.h; + nr.h -= res.h; + // cut the right part + rc.x += res.w; + rc.w -= res.w; + rc.h = res.h; + } + rects ~= nr; } rects.ptr[ri] = rc; - rects ~= nr; } // copy image data auto sp = di.asTCImage.imageData.colors.ptr; auto dp = img.imageData.colors.ptr+res.y*img.width+res.x; - version(tatlas_brect) dp[0..di.width] = Color(255, 0, 0, 255); foreach (immutable dy; 0..di.height) { dp[0..di.width] = sp[0..di.width]; - version(tatlas_brect) { dp[0] = Color(255, 0, 0, 255); dp[di.width-1] = Color(255, 0, 0, 255); } sp += di.width; dp += img.width; } - version(tatlas_brect) { dp -= img.width; dp[0..di.width] = Color(255, 0, 0, 255); } + version(tatlas_brect) imgRect4(res.x, res.y, res.w, res.h, Color(255, 0, 0), Color(255, 255, 0), Color(0, 0, 255), Color(0, 255, 0)); return res; } } -- 2.11.4.GIT