Browse Source

- fixed tiling example
- added "trim" and "extend" options to <image>

dmuratshin 10 years ago
parent
commit
250937a60a

+ 2 - 2
examples/Demo/data/xmls/res.xml

@@ -42,8 +42,8 @@
 		<image file="score_table.png"/>
 	</atlas>
 
-	<atlas>
-		<image file="tiled.png"/>		
+	<atlas> 
+		<image file="tiled.png" trim="false" extend="false"/>
 	</atlas>	
 	
 	<set load = "false" /> <!--do not load next atlasses by default-->

+ 9 - 1
examples/Demo/src/TestPolygon.h

@@ -19,8 +19,16 @@ public:
 
         ResAnim* rs = resources.getResAnim("tiled");
 
-        //access to texture
+
         //ResAnim "tiled" has only single frame and uses own separate atlas texture
+        //also it should have options trim=extend=false
+        /*
+        <atlas>
+            <image file="tiled.png" trim="false" extend="false"/>
+        </atlas>
+        */
+
+        //need access to texture
         AnimationFrame frame = rs->getFrame(0);
         spNativeTexture texture = frame.getDiffuse().base;
 

+ 25 - 21
oxygine/src/res/ResAtlas.cpp

@@ -388,6 +388,7 @@ namespace oxygine
 
         int w = node.attribute("width").as_int(defaultAtlasWidth);
         int h = node.attribute("height").as_int(defaultAtlasHeight);
+
         const char* format = node.attribute("format").as_string("8888");
 
         loadBase(node);
@@ -427,6 +428,10 @@ namespace oxygine
             }
 
 
+            bool trim = child_node.attribute("trim").as_bool(true);
+            bool extend = child_node.attribute("extend").as_bool(true);
+
+            Point offset = extend ? Point(2, 2) : Point(0, 0);
 
             MemoryTexture mt;
             ImageData im;
@@ -494,16 +499,11 @@ namespace oxygine
 
                         HitTestData adata;
                         ImageData src;
-                        Rect bounds;
-                        makeAlpha(srcImage_, bounds, _hitTestBuffer, adata, walker.getAlphaHitTest());
+                        Rect bounds(0, 0, im.w, im.h);
+                        if (trim)
+                            makeAlpha(srcImage_, bounds, _hitTestBuffer, adata, walker.getAlphaHitTest());
                         src = srcImage_.getRect(bounds);
 
-
-                        //mt.init(src.w + 4, src.h + 4, src.format);
-                        //mt.fill_zero();
-
-
-
                         Rect dest(0, 0, 0, 0);
 
                         if (!ad.texture)
@@ -512,30 +512,34 @@ namespace oxygine
                             next_atlas(w, h, tf, ad, atlas_id.c_str());
                         }
 
-                        bool s = ad.atlas.add(&ad.mt, src, dest);
+                        bool s = ad.atlas.add(&ad.mt, src, dest, offset);
                         if (s == false)
                         {
                             apply_atlas(ad, _linearFilter);
                             next_atlas(w, h, tf, ad, walker.getCurrentFolder().c_str());
-                            s = ad.atlas.add(&ad.mt, src, dest);
+                            s = ad.atlas.add(&ad.mt, src, dest, offset);
                             OX_ASSERT(s);
                         }
 
-                        //duplicate image edges
-                        MemoryTexture& mt = ad.mt;
-                        ImageData tmp;
+                        if (extend)
+                        {
+                            //duplicate image edges
+                            MemoryTexture& mt = ad.mt;
+                            ImageData tmp;
+
+                            tmp = mt.lock(Rect(dest.pos.x, dest.pos.y - 1, src.w, 1));
+                            operations::copy(src.getRect(Rect(0, 0, src.w, 1)), tmp);
 
-                        tmp = mt.lock(Rect(dest.pos.x, dest.pos.y - 1,    src.w, 1));
-                        operations::copy(src.getRect(Rect(0, 0, src.w, 1)),         tmp);
+                            tmp = mt.lock(Rect(dest.pos.x, dest.pos.y + src.h, src.w, 1));
+                            operations::copy(src.getRect(Rect(0, src.h - 1, src.w, 1)), tmp);
 
-                        tmp = mt.lock(Rect(dest.pos.x, dest.pos.y + src.h, src.w, 1));
-                        operations::copy(src.getRect(Rect(0, src.h - 1, src.w, 1)), tmp);
+                            tmp = mt.lock(Rect(dest.pos.x - 1, dest.pos.y, 1, src.h));
+                            operations::copy(src.getRect(Rect(0, 0, 1, src.h)), tmp);
 
-                        tmp = mt.lock(Rect(dest.pos.x - 1, dest.pos.y,    1, src.h));
-                        operations::copy(src.getRect(Rect(0, 0, 1, src.h)),         tmp);
+                            tmp = mt.lock(Rect(dest.pos.x + src.w, dest.pos.y, 1, src.h));
+                            operations::copy(src.getRect(Rect(src.w - 1, 0, 1, src.h)), tmp);
+                        }
 
-                        tmp = mt.lock(Rect(dest.pos.x + src.w, dest.pos.y, 1, src.h));
-                        operations::copy(src.getRect(Rect(src.w - 1, 0, 1, src.h)), tmp);
 
                         //operations::copy(src.getRect(Rect(0, 0, 1, 1)), mt.lock(&Rect(dest.pos.x - 1, dest.pos.y - 1, 1, 1)));
                         //operations::copy(src.getRect(Rect(src.w - 1, 0, 1, 1)), mt.lock(&Rect(dest.pos.x + src.w, dest.pos.y - 1, 1, 1)));

+ 2 - 2
oxygine/src/utils/AtlasTool.cpp

@@ -154,9 +154,9 @@ namespace oxygine
         //return a.size.y < b.size.y;
     }
 
-    bool Atlas2::add(Texture* dest, const ImageData& src, Rect& srcRect)
+    bool Atlas2::add(Texture* dest, const ImageData& src, Rect& srcRect, const Point& offset_)
     {
-        Point offset(2, 2);
+        Point offset = offset_;
         if (dest)
         {
             if (src.w == dest->getWidth())

+ 1 - 1
oxygine/src/utils/AtlasTool.h

@@ -60,7 +60,7 @@ namespace oxygine
         void init(int w, int h, int skipSize = 3);
         void clean();
 
-        bool add(Texture* dest, const ImageData& src, Rect& srcRect);
+        bool add(Texture* dest, const ImageData& src, Rect& srcRect, const Point& offset);
 
         const Rect& getBounds() const { return _bounds; }
 

+ 28 - 16
tools/resbuild/process_atlas.py

@@ -76,11 +76,12 @@ class alphaData(object):
 
 class frame(object):
 
-    def __init__(self, image, bbox, image_element, rs, adata):
+    def __init__(self, image, bbox, image_element, rs, adata, extend):
 
         self.image = image
         self.image_element = image_element
         self.adata = adata
+        self.extend = extend
 
         self.node = None
         self.resanim = rs
@@ -324,6 +325,14 @@ def processRS(context, walker):
     rows = as_int(image_el.getAttribute("rows"))
     frame_height = as_int(image_el.getAttribute("frame_height"))
     border = as_int(image_el.getAttribute("border"))
+
+    trim = as_bool(image_el.getAttribute("trim"), True)
+    extend = as_bool(image_el.getAttribute("extend"), True)
+    
+    if not extend:
+        pass
+    
+
     # sq = as_float(image_el.getAttribute("scale_quality"), 1)
     # next.scale_quality *= sq
 
@@ -429,7 +438,6 @@ def processRS(context, walker):
                     frame_image = frame_image.crop(
                         (0, 0, frame_size[0], frame_size[1]))
 
-            trim = as_bool(image_el.getAttribute("trim"), True)
 
             adata = None
 
@@ -447,15 +455,16 @@ def processRS(context, walker):
             if not frame_bbox:
                 frame_bbox = (0, 0, 0, 0)
 
-            # note: neither of these variables are used
-            w = frame_bbox[2] - frame_bbox[0]
-            h = frame_bbox[3] - frame_bbox[1]
-
             frame_image = frame_image.crop(frame_bbox)
 
-            fr = frame(frame_image, frame_bbox, image_el, resAnim, adata)
+            fr = frame(frame_image, frame_bbox, image_el, resAnim, adata, extend)
+            
             if border:
                 fr.border_left = fr.border_right = fr.border_top = fr.border_bottom = border
+                
+                
+            if not extend:
+                fr.border_left = fr.border_right = fr.border_top = fr.border_bottom = 0
 
             resAnim.frames.append(fr)
 
@@ -517,7 +526,9 @@ class atlas_Processor(process.Process):
                     p += 8 - v
                 return p
             sz = frame.image.size
-            return align_pixel(sz[0] + frame.border_left + frame.border_right), align_pixel(sz[1] + frame.border_top + frame.border_bottom)
+            if frame.extend:
+                return align_pixel(sz[0] + frame.border_left + frame.border_right), align_pixel(sz[1] + frame.border_top + frame.border_bottom)
+            return sz
 
         def get_original_frame_size(frame):
             sz = frame.image.size
@@ -552,17 +563,18 @@ class atlas_Processor(process.Process):
                 sz = fr.image.size
                 rect = bbox(x, y, sz[0], sz[1])
 
-                part = fr.image.crop(bbox(0, 0,     sz[0], 1))
-                image.paste(part,    bbox(x, y - 1, sz[0], 1))
+                if fr.extend:
+                    part = fr.image.crop(bbox(0, 0,     sz[0], 1))
+                    image.paste(part,    bbox(x, y - 1, sz[0], 1))
 
-                part = fr.image.crop(bbox(0, sz[1] - 1, sz[0], 1))
-                image.paste(part,    bbox(x, y + sz[1], sz[0], 1))
+                    part = fr.image.crop(bbox(0, sz[1] - 1, sz[0], 1))
+                    image.paste(part,    bbox(x, y + sz[1], sz[0], 1))
 
-                part = fr.image.crop(bbox(0, 0,     1, sz[1]))
-                image.paste(part,    bbox(x - 1, y,     1, sz[1]))
+                    part = fr.image.crop(bbox(0, 0,     1, sz[1]))
+                    image.paste(part,    bbox(x - 1, y,     1, sz[1]))
 
-                part = fr.image.crop(bbox(sz[0] - 1, 0,  1, sz[1]))
-                image.paste(part,    bbox(x + sz[0],  y,  1, sz[1]))
+                    part = fr.image.crop(bbox(sz[0] - 1, 0,  1, sz[1]))
+                    image.paste(part,    bbox(x + sz[0],  y,  1, sz[1]))
 
                 image.paste(fr.image, rect)