---
title: "exportSprite之四（ASprite.java）"
author: "mmdev"
date: "2011-12-08T10:52:00Z"
tags: []
slug: "exportsprite-asprite-java"
draft: false
lastmod: "2011-12-10T16:20:48Z"
id: 1301572
url: "https://mmdev.iteye.com/blog/1301572"
---

ASprite.java

文件如下：

import java.io.\*;

public classASprite

{

String version = "";

int imageNum = 0;

int moduleNum = 0;

int frameNum = 0;

int animNum = 0;

AImage fImage;

Module modules\[];

AFrame frames\[];

Anim anims\[];

AFrame curFrame;

Anim curAnim;

int imageIndex = 0;

int moduleIndex = 0;

int frameIndex = 0;

int animIndex = 0;

int kFrame = 0;

int kAnim = 1;

Stack stack = new Stack();

int curDataType = 0;

final static int BS\_MODULE\_IMAGES = (1 &lt;&lt; 0);

boolean blUnicode = false;

final static int FLAG\_FLIP\_X=0x01;

final static int FLAG\_FLIP\_Y=0x02;

final static int FLAG\_FLIP\_XY=(FLAG\_FLIP\_X | FLAG\_FLIP\_Y);

final static int FLAG\_ROT\_0 =0x00;

final static int FLAG\_ROT\_90=0x04;

final static int FLAG\_ROT\_180=0x08;

final static int FLAG\_ROT\_270=0x0C;

final static int FLAG\_HYPER\_FM=0x10; // Hyper FModule, used by FModules

boolean Load(String fileName)

{

DataInputStream in = null;

int i = 0;

try

{

File file = new File(fileName);

if (!file.exists())

{

System.out.println("Erro!the file :" + fileName + " do not exist");

return false;

}

if (file.isDirectory())

{

System.out.println("Erro!the file : " + fileName + " is Directory");

return false;

}

in = new DataInputStream(new FileInputStream(fileName));

int lineIndex = 0;

String strTemp = in.readLine();

String strArray\[];

StringBuffer strBuf = new StringBuffer();

while (strTemp != null)

{

strTemp = strTemp.trim();

if (strTemp != null && strTemp.length() &gt; 0)

{

lineIndex++;

strTemp = strTemp.trim();

if (lineIndex == 1)

{

blUnicode = !strTemp.startsWith("//");

} else if (lineIndex == 4)

{

strArray = strTemp.split(" ");

version = strArray\[1].trim();

} else if (lineIndex &gt;= 4 && lineIndex &lt;= 8)

{

strArray = strTemp.split(":");

strTemp = strArray\[1];

strTemp = strTemp.trim();

if (lineIndex == 5)

{

} else if (lineIndex == 6)

{

moduleNum = parseInt(strTemp);

modules = new Module\[moduleNum];

} else if (lineIndex == 7)

{

frameNum = parseInt(strTemp);

if (frameNum &gt; 0)

{

frames = new AFrame\[frameNum];

}

} else if (lineIndex == 8)

{

animNum = parseInt(strTemp);

if (animNum &gt; 0)

{

anims = new Anim\[animNum];

}

}

} else if (strTemp.startsWith("\\\\\\\\"))

{

} else if (strTemp.startsWith("IMAGE"))

{

strArray = strTemp.split(" ");

String imageFileName = strArray\[2].trim();

imageFileName = Util.trimSemicolon(imageFileName);

if (imageFileName.indexOf(":") == -1)

{

if (imageFileName.startsWith(".\\\\"))

{

imageFileName = imageFileName.substring(2, imageFileName.length());

}

imageFileName = file.getParent() + "\\\\" + imageFileName;

}

strTemp = strArray\[4];

int transpColor = parseInt(strTemp);

strTemp = strArray\[strArray.length - 1];

int palNum = parseInt(strTemp);

palNum++;

fImage = new AImage(imageFileName, transpColor, palNum);

} else if (strTemp.startsWith("PALETTE"))

{

strArray = strTemp.split(" ");

fImage.appendPal(Util.trimSemicolon(strArray\[1]));

} else if (strTemp.startsWith("MD"))

{

strArray = strTemp.split("\\t");

int id = parseInt(strArray\[1]);

int fImageId = 0;

int x, y, w, h;

String des = "";

int moduleType = 0;

if (strArray\[2].equalsIgnoreCase("MD\_IMAGE"))

{

fImageId = parseInt(strArray\[3]);

x = parseInt(strArray\[4]);

y = parseInt(strArray\[5]);

w = parseInt(strArray\[6]);

h = Integer.parseInt(strArray\[7]);

if (strArray.length &gt;= 9)

{

des = strArray\[8];

}

moduleType = Module.MD\_IMAGE;

} else

{

fImageId = 0;

x = 0;

y = 0;

w = parseInt(strArray\[4]);

h = Integer.parseInt(strArray\[5]);

if (strArray.length &gt;= 7)

{

des = strArray\[6];

}

moduleType = Module.MD\_MAKER;

}

modules\[moduleIndex++] = new Module(id, moduleType, fImageId, x, y, w, h, des);

} else if (strTemp.startsWith("FRAME"))

{

curDataType = kFrame;

strArray = strTemp.split(" ");

String des = Util.trimSemicolon(strArray\[1]);

int fmouleNum = parseInt(strArray\[strArray.length - 1]);

curFrame = frames\[frameIndex++] = new AFrame(fmouleNum, des);

} else if (strTemp.startsWith("{"))

{

stack.push(curDataType);

} else if (strTemp.startsWith("}"))

{

stack.pop();

} else if (strTemp.startsWith("FM"))

{

strArray = strTemp.split("\\t");

int id = parseInt(strArray\[1]);

int x = parseInt(strArray\[2]);

int y = parseInt(strArray\[3]);

int flag = 0;

if (strArray.length &gt;= 5)

{

for(i=4;i&lt;strArray.length;i++)

flag|= parseFlag(strArray\[i]);

}

Module module=modules\[getModuleIndex(id)];

FModule fmodule = new FModule(id, x, y, module.w,module.h,flag);

curFrame.appendModule(fmodule);

} else if (strTemp.startsWith("0x"))

{

if (stack.getTopValue() == kFrame)

{

curFrame.id = parseInt(strTemp);

} else if (stack.getTopValue() == kAnim)

{

curAnim.id = parseInt(strTemp);

}

} else if (strTemp.startsWith("ANIM"))

{

curDataType = kAnim;

strArray = strTemp.split(" ");

String des = Util.trimSemicolon(strArray\[1]);

int fframeNum = parseInt(strArray\[strArray.length - 1]);

curAnim = anims\[animIndex++] = new Anim(fframeNum, des);

} else if (strTemp.startsWith("AF"))

{

strArray = strTemp.split("\\t");

int id = parseInt(strArray\[1]);

int time = parseInt(strArray\[2]);

int x = parseInt(strArray\[3]);

int y = parseInt(strArray\[4]);

int flag = 0;

if (strArray.length &gt;= 6)

{

for(i=5;i&lt;strArray.length;i++)

flag|= parseFlag(strArray\[i]);

}

FFrame fframe = new FFrame(id, time, x, y, flag);

curAnim.appendFrame(fframe);

}

}

strTemp = in.readLine();

if (strTemp != null && strTemp.length() &gt; 0)

{

if (blUnicode)

{

if (strTemp.length() &gt; 1)

{

strTemp = new String(strTemp.getBytes(), "Unicode");

}

}

}

}

in.close();

} catch (Exception e)

{

try

{

if (in != null)

{

in.close();

in = null;

}

} catch (Exception e2)

{

}

e.printStackTrace();

return false;

}

return true;

}

int parseInt(String str)

{

int value = 0;

if (blUnicode)

{

byte\[] tempByte = str.getBytes();

if (tempByte\[tempByte.length - 1] == 63)

{

str = new String(tempByte, 0, tempByte.length - 1);

}

StringBuffer strBuf = new StringBuffer();

char c = 0;

for (int i = 0; i &lt; str.length(); i++)

{

c = str.charAt(i);

if (c != 0)

{

strBuf.append(c);

}

}

str = strBuf.toString();

strBuf.delete(0, strBuf.length());

}

if (str.startsWith("0x"))

{

str = str.substring(2, str.length());

value = Integer.parseInt(str, 16);

} else

{

value = Integer.parseInt(str);

}

return value;

}

int parseFlag(String str)

{

int flag=0;

if(str.equals("+FLIP\_X"))

flag=FLAG\_FLIP\_X;

else if(str.equals("+FLIP\_Y"))

flag=FLAG\_FLIP\_Y;

else if (str.equals("+ROT\_90"))

flag=FLAG\_ROT\_90;

else if (str.equals("+ROT\_180"))

flag=FLAG\_ROT\_180;

else if (str.equals("+ROT\_270"))

flag=FLAG\_ROT\_270;

else if (str.equals("+HYPER\_FM"))

flag=FLAG\_HYPER\_FM;

return flag;

}

boolean ExportBSprite(String strOutFileName, int flag)

{

ByteArrayOutputStream byteos = null;

DataOutputStream os = null;

DataOutputStream outPutFileStream = null;

try

{

byteos = new ByteArrayOutputStream();

os = new DataOutputStream(byteos);

Util.writeShort(os, parseInt(version));

Util.writeInt(os, flag);

Util.writeInt(os, moduleNum);

int i = 0;

int j = 0;

int m = 0;

int n = 0;

int k = 0;

Module module;

for (i = 0; i &lt; modules.length; i++)

{

module = modules\[i];

if ((flag & BS\_MODULE\_IMAGES) == 0)

{

Util.writeInt(os, module.x);

Util.writeInt(os, module.y);

}

Util.writeInt(os, module.w);

Util.writeInt(os, module.h);

}

AFrame frame;

if (frames != null)

{

for (i = 0; i &lt; frames.length; i++)

{

frame = frames\[i];

m += frame.moduelCnt;

}

} else

{

m = 0;

}

Util.writeInt(os, m);

if (frames != null)

{

FModule fmodule;

for (i = 0; i &lt; frames.length; i++)

{

frame = frames\[i];

for (j = 0; j &lt; frame.moduelCnt; j++)

{

fmodule = frame.fModules\[j];

Util.writeInt(os, getModuleIndex(fmodule.moduleId));

Util.writeInt(os, fmodule.x);

Util.writeInt(os, fmodule.y);

Util.writeInt(os, fmodule.flag);

}

}

Util.writeInt(os, frames.length);

m = 0;

for (i = 0; i &lt; frames.length; i++)

{

frame = frames\[i];

n = frame.moduelCnt;

Util.writeInt(os, n);

Util.writeInt(os, m);

m += n;

}

for (i = 0; i &lt; frames.length; i++)

{

frame = frames\[i];

Util.writeInt(os, frame.rc\_x);

Util.writeInt(os, frame.rc\_y);

Util.writeInt(os, frame.rc\_w);

Util.writeInt(os, frame.rc\_h);

}

} else

{

Util.writeInt(os, 0);

}

Anim anim;

m=0;

if (anims != null)

{

for (i = 0; i &lt; anims.length; i++)

{

anim = anims\[i];

m += anim.frameCnt;

}

} else

{

m = 0;

}

Util.writeInt(os, m);

if (anims != null)

{

FFrame fframe;

for (i = 0; i &lt; anims.length; i++)

{

anim = anims\[i];

for (j = 0; j &lt; anim.frameCnt; j++)

{

fframe = anim.fFrame\[j];

k = getFrameIndex(fframe.frameId);

Util.writeInt(os, k);

Util.writeInt(os, fframe.x);

Util.writeInt(os, fframe.y);

Util.writeInt(os, fframe.flag);

Util.writeInt(os, fframe.time);

}

}

Util.writeInt(os, anims.length);

m = 0;

for (i = 0; i &lt; anims.length; i++)

{

anim = anims\[i];

n = anim.frameCnt;

Util.writeInt(os, n);

Util.writeInt(os, m);

m += n;

}

} else

{

Util.writeInt(os, 0);

}

AImage aImage;

if ((flag & BS\_MODULE\_IMAGES) == 0)

{

aImage = fImage;

Util.writeInt(os, fImage.width);

Util.writeInt(os, fImage.height);

int rgbData\[] = aImage.getRGB();

for (i = 0; i &lt; rgbData.length; i++)

{

if(rgbData\[i]==aImage.transp\_color)

os.writeInt(0);

else

os.writeInt(rgbData\[i]);

}

}

else if (fImage.isIndexedColor())

{

os.writeByte(fImage.palCount);

os.writeByte(fImage.colorCount);

int pal\[];

for (i = 0; i &lt; fImage.palCount; i++)

{

pal = fImage.pals\[i];

for (j = 0; j &lt; fImage.colorCount; j++)

{

Util.writeInt(os, pal\[j]);

}

}

for (i = 0; i &lt; modules.length; i++)

{

module = modules\[i];

aImage = fImage;

byte moduleData\[] = aImage.getImageData(module.x, module.y, module.w, module.h);

Util.writeInt(os, moduleData.length);

os.write(moduleData);

}

}

else

{

assert(fImage.palCount==0);

os.writeByte(0);

os.writeByte(0);

for (i = 0; i &lt; modules.length; i++)

{

module = modules\[i];

aImage = fImage;

int moduleRGBData\[] = aImage.getRGB(module.x, module.y, module.w, module.h);

Util.writeInt(os, moduleRGBData.length\*4);

for (j = 0; j &lt;moduleRGBData.length; j++)

{

if(moduleRGBData\[i]==aImage.transp\_color)

os.writeInt(0);

else

os.writeInt(moduleRGBData\[j]);

}

}

}

File outPutFile = new File(strOutFileName);

if (outPutFile.exists())

{

outPutFile.delete();

}

outPutFile.createNewFile();

outPutFileStream = new DataOutputStream(new FileOutputStream(strOutFileName));

outPutFileStream.write(byteos.toByteArray());

outPutFileStream.close();

os.close();

} catch (Exception e)

{

e.printStackTrace();

Util.closeDataputStream(os);

os = null;

Util.closeDataputStream(outPutFileStream);

outPutFileStream = null;

return false;

}

return true;

}

static ByteArrayOutputStream baos;

static DataOutputStream out;

static String interFaceFileName = null;

static boolean CreateInterface(String strFileName)

{

try

{

interFaceFileName = strFileName;

int index = strFileName.indexOf(".");

String name = strFileName.substring(0, index);

if((!blExportJavaInterface)&&strFileName.endsWith(".java"))

{

setExportJavaInterface(true);

Util.setDataWriteFormat(Util.kJavaFormatWrite);

}

baos = new ByteArrayOutputStream();

out = new DataOutputStream(baos);

if(blExportJavaInterface)

{

if(packageName!=null)

out.writeBytes("package " +packageName + ";\\n");

out.writeBytes("public interface " + name + "\\n{\\n");

}

else

{

out.writeBytes("#ifndef \_\_" + name + "\_\_H\\n");

out.writeBytes("#define \_\_" + name + "\_\_H\\n");

}

} catch (IOException e)

{

e.printStackTrace();

Util.closeDataputStream(out);

out = null;

return false;

}

return true;

}

static boolean CreateStruct(String name)

{

if(blExportJavaInterface)

{

throw new UnsupportedOperationException("CreateStruct "+"is not supported when we export java interface");

}

try

{

out.writeBytes("//////////////////////////////////////////////////\\n");

out.writeBytes("struct " + name + "\\n");

out.writeBytes("{\\n");

out.writeBytes("\\tenum \_enum\\n");

out.writeBytes("\\t{\\n");

} catch (Exception e)

{

e.printStackTrace();

Util.closeDataputStream(out);

out = null;

return false;

}

return true;

}

boolean ExportFramesID(String prefix)

{

String memberSeperator=",";

if(blExportJavaInterface)

{

prefix=kJavaMemberPrefix+prefix;

memberSeperator=";";

}

String des;

try

{

for (int i = 0; i &lt; frames.length; i++)

{

des = frames\[i].des;

if (des.length() == 0)

{

out.writeBytes("\\t\\t" + prefix + i + " = " + i + memberSeperator+"\\n");

}

else

out.writeBytes("\\t\\t" + prefix + frames\[i].des + " = " + i +memberSeperator+ "\\n");

}

} catch (Exception e)

{

e.printStackTrace();

Util.closeDataputStream(out);

out = null;

return false;

}

return true;

}

final static String kJavaMemberPrefix="public static int ";

boolean ExportAnimsID(String prefix)

{

String des;

String memberSeperator=",";

if(blExportJavaInterface)

{

prefix=kJavaMemberPrefix+prefix;

memberSeperator=";";

}

try

{

for (int i = 0; i &lt; anims.length; i++)

{

des = anims\[i].des;

if (des.length() == 0)

{

out.writeBytes("\\t\\t" + prefix + i + " = " + i + memberSeperator+"\\n");

}

else

out.writeBytes("\\t\\t" + prefix + anims\[i].des + " = " + i +memberSeperator+ "\\n");

}

} catch (Exception e)

{

e.printStackTrace();

Util.closeDataputStream(out);

out = null;

return false;

}

return true;

}

static boolean CloseInterface()

{

try

{

if(blExportJavaInterface)

{

out.writeBytes("}");

}else

{

out.writeBytes("#endif");

}

String fileName=null;

if(blExportJavaInterface&&packagePath!=null)

{

fileName =packagePath+"\\\\"+interFaceFileName;

}

else

fileName = interFaceFileName;

File file =new File(fileName);

if (file.exists())

{

file.delete();

}

if (!file.createNewFile())

{

return false;

}

DataOutputStream outPutFileStream = new DataOutputStream(new FileOutputStream(fileName));

outPutFileStream.write(baos.toByteArray());

outPutFileStream.flush();

outPutFileStream.close();

out.close();

baos = null;

out = null;

interFaceFileName = null;

} catch (Exception e)

{

e.printStackTrace();

Util.closeDataputStream(out);

out = null;

return false;

}

return true;

}

static boolean CloseStruct()

{

if(blExportJavaInterface)

{

throw new UnsupportedOperationException("CloseStruct "+"is not supported when we export java interface");

}

try

{

out.writeBytes("\\t};\\n");

out.writeBytes("};\\n");

} catch (Exception e)

{

e.printStackTrace();

Util.closeDataputStream(out);

out = null;

return false;

}

return true;

}

static boolean blExportJavaInterface=false;

private static void setExportJavaInterface(boolean val)

{

blExportJavaInterface=val;

}

static String packageName=null;

static String packagePath=null;

static void setJavaPackageName(String name)

{

packageName=name;

}

static void setJavaPackagePath(String path)

{

packagePath=path;

}

int getModuleIndex(int id)

{

for (int i = 0; i &lt; modules.length; i++)

{

if (modules\[i] == null)

{

break;

}

if (modules\[i].id == id)

{

return i;

}

}

return -1;

}

int getFrameIndex(int id)

{

for (int i = 0; i &lt; frames.length; i++)

{

if (frames\[i] == null)

{

break;

}

if (frames\[i].id == id)

{

return i;

}

}

return -1;

}

void reset()

{

version = "";

imageIndex = 0;

moduleIndex = 0;

frameIndex = 0;

animIndex = 0;

moduleNum = 0;

frameNum = 0;

animNum = 0;

int i = 0;

if (modules != null)

{

for (i = 0; i &lt; modules.length; i++)

{

modules\[i] = null;

}

modules = null;

}

if (frames != null)

{

for (i = 0; i &lt; frames.length; i++)

{

frames\[i] = null;

}

frames = null;

}

if (anims != null)

{

for (i = 0; i &lt; anims.length; i++)

{

anims\[i] = null;

}

anims = null;

}

fImage = null;

}

}