title: Low frame rate with Irrlicht on Android
body: <p>I've compiled an Android port of Irrlicht from <a href="http://gitorious.org/irrlichtandroid/" rel="nofollow">http://gitorious.org/irrlichtandroid/</a> and made an application to just load a lo res SkyBox. However, I get unpredictable frame rates. On emulator, fps never goes beyond 5. On my DELL XCD35 with eclair, it usually doesn't go beyond 10 fps, BUT, in about 1 out of 10 launches, the application runs nice at 60 fps.
  Activity is configured for fullscreen landscape mode.</p>
  
  <p>Following is the code, I've omitted class header files to keep the post short.</p>
  
  <p><strong>BlueStoneActivity.java</strong></p>
  
  <pre><code>public class BlueStoneActivity extends Activity {
      static {
          System.loadLibrary("irrlicht");
          System.loadLibrary("bluestone");
      }
  
      GLSurfaceView glView;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          this.requestWindowFeature(Window.FEATURE_NO_TITLE);
          this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
          glView=new GLSurfaceView(this);
          glView.setRenderer(new IrrlichtRenderer(this));
          setContentView(glView);
          Debug.startMethodTracing("bstone");
          nativeOnCreate();
      }
  
      @Override
      protected void onDestroy() {
          nativeOnDestroy();
          super.onDestroy();
          Debug.stopMethodTracing();
      }
  
      @Override
      protected void onPause() {
          super.onPause();
      }
  
      @Override
      protected void onResume() {
          super.onResume();
      }
  
      public native void nativeOnCreate();
      public native void nativeOnDestroy();
      public native void nativeOnPause();
      public native void nativeOnResume();
      public native void nativeOnResize(int w, int h);
      public native void nativeDrawIteration();
  }
  </code></pre>
  
  <p><strong>IrrlichtRender.java</strong></p>
  
  <pre><code> public class IrrlichtRenderer implements Renderer {
          BlueStoneActivity activity;
  
          IrrlichtRenderer(BlueStoneActivity activity){
              this.activity=activity;
          }
  
          @Override
          public void onDrawFrame(GL10 arg0) {
              activity.nativeDrawIteration();
          }
  
          @Override
          public void onSurfaceChanged(GL10 gl, int width, int height) {
              activity.nativeOnResize(width, height);
          }
  
          @Override
          public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
          }
      }
  </code></pre>
  
  <p><strong>JNI wrapper</strong></p>
  
  <pre><code>    #include &lt;jni.h&gt;
  #include &lt;android/log.h&gt;
  #include "EngineManager.h"
  #include "InputManager.h"
  
  game::EngineManager *engine;
  
  int importGLInit();
  void importGLDeinit();
  
  #ifndef _Included_com_devin_BlueStoneActivity
  #define _Included_com_devin_BlueStoneActivity
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  JNIEXPORT void JNICALL Java_com_devin_BlueStoneActivity_nativeOnCreate
    (JNIEnv *, jobject){
      engine=new game::EngineManager();
  }
  
  
  JNIEXPORT void JNICALL Java_com_devin_BlueStoneActivity_nativeOnDestroy
    (JNIEnv *, jobject){
      engine-&gt;device-&gt;drop();
      importGLDeinit();
  }
  
  JNIEXPORT void JNICALL Java_com_devin_BlueStoneActivity_nativeOnResize
    (JNIEnv *env, jobject thiz, jint width, jint height){
      __android_log_print(ANDROID_LOG_INFO, "NDK", "ONRESIZE - [%d %d]", width, height);
      engine-&gt;mWidth=width;
      engine-&gt;mHeight=height;
      importGLInit();
      engine-&gt;glInit();
  }
  
  
  JNIEXPORT void JNICALL Java_com_devin_BlueStoneActivity_nativeDrawIteration
    (JNIEnv *, jobject){
      engine-&gt;drawIteration();
  }
  
  
  JNIEXPORT void JNICALL Java_com_devin_BlueStoneActivity_nativeOnPause
    (JNIEnv *, jobject){
  }
  
  
  JNIEXPORT void JNICALL Java_com_devin_BlueStoneActivity_nativeOnResume
    (JNIEnv *, jobject){
  
  }
  
  
  #ifdef __cplusplus
  }
  #endif
  #endif
  </code></pre>
  
  <p><strong>EngineManager.cpp</strong></p>
  
  <pre><code>#include &lt;irrlicht.h&gt;
  #include "EngineManager.h"
  
  namespace game {
  
  EngineManager::EngineManager() {
      input=new game::InputManager();
      app=new game::ApplicationManager(input);
  }
  
  EngineManager::~EngineManager() {
  }
  
  void EngineManager::glInit(){
      device=createDevice( video::EDT_OGLES1, core::dimension2d&lt;u32&gt;(mWidth, mHeight), 16, false, false, false, 0);
      driver=device-&gt;getVideoDriver();
      scenegraph=device-&gt;getSceneManager();
      guienv=device-&gt;getGUIEnvironment();
  
      app-&gt;initApp(device);
  }
  
  void EngineManager::drawIteration(){
      device-&gt;run();
      driver-&gt;beginScene(true, true, app-&gt;clearColor);
  
      app-&gt;drawIteration();
  
      scenegraph-&gt;drawAll();
      guienv-&gt;drawAll();
      driver-&gt;endScene();
  }
  
  } /* namespace game */
  </code></pre>
  
  <p><strong>ApplicationManager.cpp</strong></p>
  
  <pre><code>#include "ApplicationManager.h"
  #include "InputManager.h"
  
  namespace game {
  
  ApplicationManager::ApplicationManager(InputManager *in) {
      this-&gt;input=in;
  }
  
  ApplicationManager::~ApplicationManager() {
  }
  
  void ApplicationManager::initApp(IrrlichtDevice *device){
      this-&gt;device=device;
      this-&gt;driver=device-&gt;getVideoDriver();
      this-&gt;scenegraph=device-&gt;getSceneManager();
      this-&gt;guienv=device-&gt;getGUIEnvironment();
  
      // Camera setup
      camera=scenegraph-&gt;addCameraSceneNode();
      camera-&gt;setPosition(core::vector3df(20.0f, 15.0f, 15.0f));
      camera-&gt;setTarget(core::vector3df(0.0f, 0.0f, 0.0f));
  
      // Sample objects
      bool sceneLoaded=scenegraph-&gt;loadScene("/sdcard/BlueStone/redsky.irr");
      if(sceneLoaded)
          __android_log_print(ANDROID_LOG_INFO, "NDK", "SceneLoaded");
      else
          __android_log_print(ANDROID_LOG_INFO, "NDK", "SceneLoaded false");
  
      clearColor=video::SColor(255, 20, 40, 40);
      statsText=L"START";
      text=guienv-&gt;addStaticText(statsText.c_str(), core::recti(50,50,50+100,50+60), true, true, 0, 18, false);
      text-&gt;setOverrideColor(video::SColor(255, 64, 20, 20));
  }
  
  void ApplicationManager::drawIteration(){
      statsText=L"FPS: ";
      statsText+=driver-&gt;getFPS();
      text-&gt;setText(statsText.c_str());
  }
  
  } /* namespace game */
  </code></pre>
  
  <p>Please help !!</p>