选择显示字体大小

使用bincompiler转换游戏资源文件为二进制文件

我们可能想用于游戏开发的资源文件,例如图片,转换成二进制的文件并且可以在程序中使用。很多专业的游戏开发都采用了这样的处理。本文将介绍一种功能不算很强大,但是简单可用的工具: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();
&#125; catch (exception e) &#123;
buffer = null;
e.printstacktrace();
system.gc();
return null;
&#125;

return buffer;
&#125;

//读取指定文件并返回图片
public image readimage(string binfile, int pos)
&#123;
byte buffer[];
int len;

try &#123;
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();
&#125; catch (exception e) &#123;
buffer = null;
e.printstacktrace();
system.gc();
return null;
&#125;

return image.createimage(buffer, 0, buffer.length);
&#125;
使用这两个方法非常简单,我们只需要把输出文件的名字和要读取文件的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 &#123;
private display display = null;

public void startapp() &#123;
display = display.getdisplay(this);
form form = new form("image");
image image = this.readimage("map.bin", 0);
form.append(image);
display.setcurrent(form);
&#125;

public void pauseapp() &#123;
&#125;

public void destroyapp(boolean unconditional) &#123;
&#125;
/**************************************************************************************
* reads a file from the bin file and return data as a byte buffer
**************************************************************************************/
public byte[] readfile(string binfile, int pos)
&#123;
byte buffer[];
int len;

try &#123;

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();
&#125; catch (exception e) &#123;
buffer = null;
e.printstacktrace();
system.gc();
return null;
&#125;

return buffer;
&#125;

/**************************************************************************************
* reads a file from the bin file and return data as an image
**************************************************************************************/
public image readimage(string binfile, int pos)
&#123;
byte buffer[];
int len;

try &#123;
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();
&#125; catch (exception e) &#123;
buffer = null;
e.printstacktrace();
system.gc();
return null;
&#125;

return image.createimage(buffer, 0, buffer.length);
&#125;
&#125;


备注:本例中的图片与文中使用的图片不同,笔者只是为了演示用。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons