服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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

        我们可能想用于游戏开发的资源文件,例如图片,转换成二进制的文件并且可以在程序中使用。很多专业的游戏开发都采用了这样的处理。本文将介绍一种功能不算很强大,但是简单可用的工具:bincompiler。通过具体的实例讲解如何bincompiler转换游戏资源文件,并在midp应用中使用。

        首先从本站下载中心下载bincompiler,解压后双击运行。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 e) {
   buffer = null;
   e.printstacktrace();
   system.gc();
   return null;
  }

  return image.createimage(buffer, 0, buffer.length);
 }
}

使用bincompiler转换游戏资源文件为二进制文件(图二)
备注:本例中的图片与文中使用的图片不同,笔者只是为了演示用。

扫描关注微信公众号