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 << 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() > 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 >= 4 && lineIndex <= 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 > 0)
{
frames = new AFrame[frameNum];
}
} else if (lineIndex == 8)
{
animNum = parseInt(strTemp);
if (animNum > 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 >= 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 >= 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 >= 5)
{
for(i=4;i<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 >= 6)
{
for(i=5;i<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() > 0)
{
if (blUnicode)
{
if (strTemp.length() > 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 < 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 < 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 < 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 < frames.length; i++)
{
frame = frames[i];
for (j = 0; j < 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 < frames.length; i++)
{
frame = frames[i];
n = frame.moduelCnt;
Util.writeInt(os, n);
Util.writeInt(os, m);
m += n;
}
for (i = 0; i < 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 < 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 < anims.length; i++)
{
anim = anims[i];
for (j = 0; j < 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 < 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 < 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 < fImage.palCount; i++)
{
pal = fImage.pals[i];
for (j = 0; j < fImage.colorCount; j++)
{
Util.writeInt(os, pal[j]);
}
}
for (i = 0; i < 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 < 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 <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 < 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 < 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 < 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 < 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 < modules.length; i++)
{
modules[i] = null;
}
modules = null;
}
if (frames != null)
{
for (i = 0; i < frames.length; i++)
{
frames[i] = null;
}
frames = null;
}
if (anims != null)
{
for (i = 0; i < anims.length; i++)
{
anims[i] = null;
}
anims = null;
}
fImage = null;
}
}