编译Irrlicht On Android(1)

0.为了与本文状况一致,建议各位看官的工具均升至最新版java

Eclipse Kepler+ADT 22.3.0+CDT-master-8.2.1+android-ndk-r9c-windows-x86_64android


1.下载Android版Irrlicht的源码。https://gitorious.org/irrlichtandroid/irrlichtandroid/source/f12c3b9743d64dc5cd61931f40e42e8ca64b40ef:git


2.将源码导入到Eclipse。怎么导入?蹲墙角检讨半小时~~程序员


3.导入完毕,代码结构以下图windows

p_w_picpathp_w_picpath

为了程序能支持OpenGL ES,Android的版本号要在8以上,这里我选择的是android-16。而且将AndroidManifest.xml中的minSdkVersion改为8app


4.右击项目,选择Android Tools→Add Native Support,在弹出的输入框中,输入irrlichtide

p_w_picpath

若是全部插件均是最新版,则会出现Include文件夹。函数

p_w_picpath


5.接下来,就是最激动人心的时刻,右击项目,Run As | Android Application。完成!!工具


6.好吧~,还没结束……,在通过几分钟的编译之后,若是你的NDK是r9版本,就会碰到第一个坑。ui

p_w_picpath

这是由于,r9版本之后,方法的接口名变了。只需将

__android_log_print(ANDROID_LOG_INFO, "log", message);改成

__android_log_print(ANDROID_LOG_INFO, "log", “%s”, message);


7.从新编译,在即将完成编译时,半路又杀出来一个程咬金p_w_picpath

multiple definition of 'GL_BGRA'。这是由于,同时编译了OpenGL ES1和OpenGL ES2,形成了重复定义。咱们首先使用ES1.x渲染。在include\irrCompileConfig.h,注释掉#define _IRR_COMPILE_WITH_OGLES2_,再次从新编译。


8.因为全部的文件都间接包含了irrCompileConfig.h,所以等因而又从新编译了一遍。

p_w_picpath

恭喜,编译成功。


9.oh on!闪退~~看看logcat吧

p_w_picpath请注意这句No OpenGL-ES2 support compiled in.其实,在/jni/android-activity.cpp的nativeInitGL()方法中,咱们能够找到答案。

   1: device = createDevice( video::EDT_OGLES2, dimension2d<u32>(gWindowWidth, gWindowHeight), 16, false, false, false, 0);

在建立device,用的是OGLES2,若是咱们一路跟进去createDevice()→new CIrrDeviceAndroid()→createDriver(),就会发现,因为咱们注释了_IRR_COMPILE_WITH_OGLES2_,致使程序irrlicht没有底层图形库去渲染图形,形成程序闪退。如今将createDevice的参数改成video::EDT_OGLES1

  1: device = createDevice( video::EDT_OGLES1, dimension2d<u32>(gWindowWidth, gWindowHeight), 16, false, false, false, 0);


10.再次运行,是否是点的想吐了?吐也不行,哥依然闪现给你看~~……。继续logcat吧

p_w_picpath

又发现线索,Could not load mesh, because file could not be opened: 和cannot getMesh。咱们能够在项目中搜索cannot getMesh试试看。在/jni/app-android.cpp的initSydney()方法中,咱们发现

   1: smgr = device->getSceneManager();
   2: guienv = device->getGUIEnvironment();
   3:
   4: stringc myDir = gSdCardPath;
   5: myDir += "/Irrlicht";
   6: device->getFileSystem()->changeWorkingDirectoryTo(myDir.c_str());
   7:
   8: stringc sydneyFilename = "/Irrlicht/sydney.md2";
   9: mesh = smgr->getMesh( (gSdCardPath+sydneyFilename).c_str() );
  10:if (!mesh){
  11:     device->drop();
  12:     __android_log_print(ANDROID_LOG_INFO, "Irrlicht", "cannot getMesh");
  13:return;
  14: }

若是你能看懂上面的代码,那差很少,也就知道错出在哪了。程序要从/sdcard/Irrlicht/获去sydney.md2,但手机的SD卡上并无这个路径。因此,咱们能够在SD卡上建立一个Irrlicht目录,并把/IrrlichtSDCard下的文件拷贝到/sdcard/Irrlicht/。


11.若是一切顺利,劳拉将会在红色的背景中原地踏步~~

p_w_picpath

帅不帅~~


12.总有些人怕麻烦,还得在SD卡建立文件夹,还要复制文件……,DDMS还只能一个一个复制~~麻烦啊~~,还有方法吗?

不怕麻烦的程序员不是好码农!秘密就在Utils.java中,直接看unpackOnSdCard()

   1:if (Environment.getExternalStorageState().compareTo(Environment.MEDIA_MOUNTED)==0) {
   2:     File sdcard = Environment.getExternalStorageDirectory();
   3:     String irrlichtPath = sdcard.getAbsoluteFile() + "/Irrlicht/";
   4:     File irrlichtDir = new File(irrlichtPath);
   5:if (irrlichtDir.exists() && !irrlichtDir.isDirectory()) {
   6:thrownew IOException("Irrlicht exists and is not a directory on SD Card");
   7:     } elseif (!irrlichtDir.exists()) {
   8:         irrlichtDir.mkdirs();
   9:     }
  10:// Note: /sdcard/irrlicht dir exists
  11:     String[] filenames = assetManager.list("data");
  12:for(String filename:filenames) {
  13:         InputStream inputStream = assetManager.open("data/" + filename);
  14:         OutputStream outputStream = new FileOutputStream(irrlichtPath + "/" + filename);
  15:// copy
  16:         byte[] buffer = new byte[4096];
  17:int length;
  18:while ( (length = inputStream.read(buffer)) > 0 ) {
  19:             outputStream.write(buffer, 0, length);
  20:         }
  21:         outputStream.flush();
  22:         outputStream.close();
  23:         inputStream.close();
  24:     }           
  25: } else {
  26:thrownew IOException("SD Card not available");
  27: }

#1—#9 正是在SD卡上建立Irrlicht目录

重点看11行

String[] filenames = assetManager.list("data");

该行代码是获取,/asset/data/下的全部文件名

由此,该函数的功能正是取代咱们以前的手动操做,将文件复制到/sdcard/Irrlicht/目录下。

删除以前的SD卡上的Irrlicht目录,在assets目录下新建data目录,将IrrlichtSdcard下的文件复制到data目录下

p_w_picpath

运行程序~~劳拉依旧飞奔~~

PS:在USB链接手机时,请将USB模式设置成仅充电,即不用经过windows的资源管理器访问到手机的SD卡,不然Environment.getExternalStorageState()将会返回shard。剩下的,你懂得~~

相关文章
相关标签/搜索