WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术进阶教程使用BINCompiler转换游戏资源文件为二进制文件

使用BINCompiler转换游戏资源文件为二进制文件
作者:mingjava  来源:j2medev  发布时间:2005-11-3 14:13:12
我们可能想用于游戏开发的资源文件,例如图片,转换成二进制的文件并且可以在程序中使用。很多专业的游戏开发都采用了这样的处理。本文将介绍一种功能不算很强大,但是简单可用的工具:BINCompiler。通过具体的实例讲解如何BINCompiler转换游戏资源文件,并在MIDP应用中使用。

首先从本站下载中心下载BINCompiler,解压后双击运行。BINCompiler的界面非常简单:

BINCompiler不支持子目录,因此你需要把你所有要转换的资源文件放在一个目录下。例如d:/temp/files,我们从WTK的DEMO中任意选择了两个PNG的图片,比如LightHouse-0.png ,jc_frame_46.png(他们位于相册应用程序的目录中)。选择Compilation Folder的Brows按钮,定位到d:/temp/files也就是我们放置文件的目录,然后选择输出的目录,比如F:/,然后点Create按钮。这时候BINCompiler就把这两个文件创建为一个.bin文件了。在输出文件的目录中还有一个重要的文件index.txt,我们需要根据这个文件中的信息从.bin文件中读取图片文件。

FName Index Pos Size
LightHouse-0.png 0 0 3756
jc_frame_46.png 1 3760 4075

我们使用如下两个java方法来读取图像文件:

//读取指定文件并返回字节数组

public byte[] readFile(String binfile, int pos)
{
byte buffer[];
int len;

try {

InputStream is = this.getClass().getResourceAsStream("/" + binfile);

is.skip(pos);

len = (is.read() & 0xFF) << 24;
len |= (is.read() & 0xFF) << 16;
len |= (is.read() & 0xFF) << 8;
len |= (is.read() & 0xFF);

buffer = new byte[len];

is.read(buffer, 0, buffer.length);

is.close();
is = null;

System.gc();
} catch (Exception e) {
buffer = null;
e.printStackTrace();
System.gc();
return null;
}

return buffer;
}

//读取指定文件并返回图片
public Image readImage(String binfile, int pos)
{
byte buffer[];
int len;

try {
InputStream is = this.getClass().getResourceAsStream("/" + binfile);

is.skip(pos);

len = (is.read() & 0xFF) << 24;
len |= (is.read() & 0xFF) << 16;
len |= (is.read() & 0xFF) << 8;
len |= (is.read() & 0xFF);

buffer = new byte[len];

is.read(buffer, 0, buffer.length);

is.close();
is = null;

System.gc();
} catch (Exception e) {
buffer = null;
e.printStackTrace();
System.gc();
return null;
}

return Image.createImage(buffer, 0, buffer.length);
}
使用这两个方法非常简单,我们只需要把输出文件的名字和要读取文件的pos值(从index读取)传递给方法就可以了,例如我们要读取图片LightHouse-0.png ,那么示例代码如下:

display = Display.getDisplay(this);
Form form = new Form("Image");
Image image = this.readImage("map.bin", 0);
form.append(image);
display.setCurrent(form);

读者可以使用eclipse或者netbeans编写一个简单的MIDlet测试一下,这里给出一个简单的MIDlet源码供参考:

/*
* MainMidlet.java
*
* Created on 2005年8月3日, 下午10:45
*/

package com.j2medev.test;

import java.io.InputStream;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
*
* @author Administrator
* @version
*/
public class MainMidlet extends MIDlet {
private Display display = null;

public void startApp() {
display = Display.getDisplay(this);
Form form = new Form("Image");
Image image = this.readImage("map.bin", 0);
form.append(image);
display.setCurrent(form);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
/**************************************************************************************
* Reads a file from the BIN file and return data as a byte buffer
**************************************************************************************/
public byte[] readFile(String binfile, int pos)
{
byte buffer[];
int len;

try {

InputStream is = this.getClass().getResourceAsStream("/" + binfile);

is.skip(pos);

len = (is.read() & 0xFF) << 24;
len |= (is.read() & 0xFF) << 16;
len |= (is.read() & 0xFF) << 8;
len |= (is.read() & 0xFF);

buffer = new byte[len];

is.read(buffer, 0, buffer.length);

is.close();
is = null;

System.gc();
} catch (Exception e) {
buffer = null;
e.printStackTrace();
System.gc();
return null;
}

return buffer;
}

/**************************************************************************************
* Reads a file from the BIN file and return data as an Image
**************************************************************************************/
public Image readImage(String binfile, int pos)
{
byte buffer[];
int len;

try {
InputStream is = this.getClass().getResourceAsStream("/" + binfile);

is.skip(pos);

len = (is.read() & 0xFF) << 24;
len |= (is.read() & 0xFF) << 16;
len |= (is.read() & 0xFF) << 8;
len |= (is.read() & 0xFF);

buffer = new byte[len];

is.read(buffer, 0, buffer.length);

is.close();
is = null;

System.gc();
} catch (Exception

[1] [2]  下一页

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码