rodrigolopezpeker

Genome2D > ATF v1.2 fix

Jan 15th, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Code by rodrigolopezpeker (aka 7interactive™) on 1/15/14 11:12 AM.
  3.  */
  4. package genome {
  5. import com.genome2d.error.GError;
  6. import com.genome2d.textures.GTexture;
  7. import com.genome2d.textures.GTextureAtlas;
  8. import com.genome2d.textures.GTextureSourceType;
  9.  
  10. import flash.geom.Rectangle;
  11.  
  12. import flash.utils.ByteArray;
  13.  
  14. public class GUtils {
  15.  
  16.     static public function createTextureFromATF(p_id:String, p_atfData:ByteArray, p_uploadCallback:Function = null):GTexture {
  17.         var atf:String = String.fromCharCode(p_atfData[0], p_atfData[1], p_atfData[2]);
  18.         if (atf != "ATF") throw new GError(GError.INVALID_ATF_DATA);
  19.         var type:int;
  20.         var transparent:Boolean = true;
  21.         var offset:int = p_atfData[6] == 255 ? 12 : 6;
  22.         switch (p_atfData[offset]) {
  23.             case 0:
  24.             case 1:
  25.                 type = GTextureSourceType.ATF_BGRA;
  26.                 break;
  27.             case 2:
  28.             case 3:
  29.                 type = GTextureSourceType.ATF_COMPRESSED;
  30.                 transparent = false;
  31.                 break;
  32.             case 4:
  33.             case 5:
  34.                 type = GTextureSourceType.ATF_COMPRESSEDALPHA;
  35.                 break;
  36.         }
  37.         var width:int = Math.pow(2, p_atfData[int(++offset)]);
  38.         var height:int = Math.pow(2, p_atfData[int(++offset)]);
  39.         return new GTexture(p_id, type, p_atfData, new Rectangle(0, 0, width, height), transparent, 0, 0, p_uploadCallback);
  40.     }
  41.  
  42.     static public function createTextureAtlasFromATFAndXML(p_id:String, p_atfData:ByteArray, p_xml:XML, p_uploadCallback:Function = null):GTextureAtlas {
  43.         var atf:String = String.fromCharCode(p_atfData[0], p_atfData[1], p_atfData[2]);
  44.         if (atf != "ATF") throw new GError(GError.INVALID_ATF_DATA);
  45.         var type:int;
  46.         var offset:int = p_atfData[6] == 255 ? 12 : 6;
  47.         var transparent:Boolean = true;
  48.         switch (p_atfData[offset]) {
  49.             case 0:
  50.             case 1:
  51.                 type = GTextureSourceType.ATF_BGRA;
  52.                 break;
  53.             case 2:
  54.             case 3:
  55.                 type = GTextureSourceType.ATF_COMPRESSED;
  56.                 transparent = false;
  57.                 break;
  58.             case 4:
  59.             case 5:
  60.                 type = GTextureSourceType.ATF_COMPRESSEDALPHA;
  61.                 break;
  62.             // with older versions
  63.             default:
  64.                 throw new Error("Invalid ATF format");
  65.         }
  66.         // ATF V1.0 > index 7 width / 8 height
  67.         // ATF V1.2 > index 13 width / 14 height
  68.         var width:int = Math.pow(2, p_atfData[int(++offset)]);
  69.         var height:int = Math.pow(2, p_atfData[int(++offset)]);
  70.         var textureAtlas:GTextureAtlas = new GTextureAtlas(p_id, type, width, height, p_atfData, transparent, p_uploadCallback);
  71.         for (var i:int = 0; i < p_xml.children().length(); ++i) {
  72.             var node:XML = p_xml.children()[i];
  73.             var region:Rectangle = new Rectangle(int(node.@x), int(node.@y), int(node.@width), int(node.@height));
  74.             var pivotX:Number = (node.@frameX == undefined && node.@frameWidth == undefined) ? 0 : Number(node.@frameWidth - region.width) / 2 + Number(node.@frameX);
  75.             var pivotY:Number = (node.@frameY == undefined && node.@frameHeight == undefined) ? 0 : Number(node.@frameHeight - region.height) / 2 + Number(node.@frameY);
  76.             textureAtlas.addSubTexture(node.@name, region, pivotX, pivotY);
  77.         }
  78.         textureAtlas.invalidate();
  79.         return textureAtlas;
  80.     }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment