From ba53e802e69581df4beba6a7ad293fa71abf5b67 Mon Sep 17 00:00:00 2001
From: Vadim Lopatin <buggins@fromru.com>
Date: Tue, 20 May 2014 16:22:31 +0400
Subject: [PATCH] tiled images support, part 2

---
 dlanguilib.visualdproj                 |  2 +-
 examples/example1/example1.visualdproj |  2 +-
 examples/example1/src/main.d           |  2 ++
 src/dlangui/graphics/images.d          |  5 +++--
 src/dlangui/graphics/resources.d       | 18 ++++++++++++++++--
 5 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/dlanguilib.visualdproj b/dlanguilib.visualdproj
index 5e780b00..3363aac3 100644
--- a/dlanguilib.visualdproj
+++ b/dlanguilib.visualdproj
@@ -66,7 +66,7 @@
   <debuglevel>0</debuglevel>
   <debugids />
   <versionlevel>0</versionlevel>
-  <versionids>Unicode USE_SDL</versionids>
+  <versionids>Unicode USE_SDL USE_OPENGL</versionids>
   <dump_source>0</dump_source>
   <mapverbosity>0</mapverbosity>
   <createImplib>1</createImplib>
diff --git a/examples/example1/example1.visualdproj b/examples/example1/example1.visualdproj
index b04fc227..ab51d9aa 100644
--- a/examples/example1/example1.visualdproj
+++ b/examples/example1/example1.visualdproj
@@ -66,7 +66,7 @@
   <debuglevel>0</debuglevel>
   <debugids />
   <versionlevel>0</versionlevel>
-  <versionids>Unicode USE_SDL</versionids>
+  <versionids>Unicode USE_SDL USE_OPENGL</versionids>
   <dump_source>0</dump_source>
   <mapverbosity>3</mapverbosity>
   <createImplib>0</createImplib>
diff --git a/examples/example1/src/main.d b/examples/example1/src/main.d
index 58fd8931..ee9c5c37 100644
--- a/examples/example1/src/main.d
+++ b/examples/example1/src/main.d
@@ -115,6 +115,8 @@ extern (C) int UIAppMain(string[] args) {
 	// load theme from file "theme_default.xml"
 	Platform.instance.uiTheme = "theme_default";
 
+    //drawableCache.get("tx_fabric.tiled");
+
     // create window
     Window window = Platform.instance.createWindow("My Window", null);
 	
diff --git a/src/dlangui/graphics/images.d b/src/dlangui/graphics/images.d
index a0e36be8..7eb7fd51 100644
--- a/src/dlangui/graphics/images.d
+++ b/src/dlangui/graphics/images.d
@@ -112,6 +112,7 @@ static if (USE_FREEIMAGE) {
 		//get the image width and height, and size per pixel
 		int width = FreeImage_GetWidth(dib);
 		int height = FreeImage_GetHeight(dib);
+        int stride = FreeImage_GetLine(dib);
 		int bpp = FreeImage_GetBPP(dib);
 		int pixelSize = (bpp + 7)/8;
 		FREE_IMAGE_COLOR_TYPE colorType = FreeImage_GetColorType(dib);
@@ -125,7 +126,7 @@ static if (USE_FREEIMAGE) {
             transparencyCount = FreeImage_GetTransparencyCount(dib);
             transparencyTable = FreeImage_GetTransparencyTable(dib);
         }
-		int size = width*height*pixelSize;
+		//int size = stride*height;
 
 		ColorDrawBuf res = new ColorDrawBuf(width, height);
 
@@ -135,7 +136,7 @@ static if (USE_FREEIMAGE) {
 		uint r, g, b, a;
 		for( int i = 0, ii = height-1; i < height ; ++i, --ii ) {
 			dst = res.scanLine(i);
-			src = data + (ii * width) * pixelSize;
+			src = data + ii * stride;
 			for( int j = 0; j < width; ++j, ++dst, src += pixelSize ) {
                 if (colorType == FIC_PALETTE) {
                     ubyte index = src[0];
diff --git a/src/dlangui/graphics/resources.d b/src/dlangui/graphics/resources.d
index fe6ed560..a89a36b2 100644
--- a/src/dlangui/graphics/resources.d
+++ b/src/dlangui/graphics/resources.d
@@ -297,8 +297,22 @@ class ImageDrawable : Drawable {
 			tiley0 %= imgdy;
 			if (tiley0 < 0)
 				tiley0 += imgdy;
-
-			buf.drawRescaled(rc, _image.get, Rect(0, 0, _image.width, _image.height));
+            int xx0 = rc.left;
+            int yy0 = rc.top;
+            if (tilex0)
+                xx0 -= imgdx - tilex0;
+            if (tiley0)
+                yy0 -= imgdy - tiley0;
+            for (int yy = yy0; yy < rc.bottom; yy += imgdy) {
+                for (int xx = xx0; xx < rc.right; xx += imgdx) {
+                    Rect dst = Rect(xx, yy, xx + imgdx, yy + imgdy);
+                    Rect src = Rect(0, 0, imgdx, imgdy);
+                    if (dst.intersects(rc))
+                        buf.drawFragment(dst.left, dst.top, _image.get, src);
+                }
+            }
+            buf.drawImage(rc.left + 30, rc.top + 30, _image);
+            buf.drawFrame(Rect(rc.left + 30, rc.top + 30, rc.left + imgdx + 30, rc.top + imgdy + 30), 0x80800000, Rect(2,2,2,2));
 		} else {
             // rescaled or normal
             if (rc.width != _image.width || rc.height != _image.height)