Lessons learned from porting Roloengine from iOS to Android

2,240 views
2,032 views

Published on

Talk at DroidCon 2013 titled "Lessons learned from porting Roloengine from iOS to Android"

Published in: Technology, Art & Photos
1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total views
2,240
On SlideShare
0
From Embeds
0
Number of Embeds
1,447
Actions
Shares
0
Downloads
5
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

Lessons learned from porting Roloengine from iOS to Android

  1. 1. Porting from iOS to Android Lessons Learned Manish Mathai Game Programmer@Rolocule
  2. 2. roloengine iOS Games Touch Squash (2009) Super Badminton (2010) Flick Tennis (2011) Motion Tennis (2013)
  3. 3. roloengine Android Games Flick Tennis (2013) ….. More coming soon
  4. 4. roloengine iOS Internals • • • • • • Written in Obj-C OpenGL ES 2.0 for rendering OpenAL for audio Custom Physics engine Custom Animation engine Custom asset format for models, animations
  5. 5. Decision time Completely Native vs Hybrid approach NDK provided “NativeActivity” Standard UI combined with C/C++ code invoked via JNI Pros :● Almost Ultimate control over memory management ● Faster (?) Pros :● Easy access to java APIs ● Leverage standard UI Cons :● No UI support ● Many APIs not available via NDK Cons :● Adds GC overhead on UI ● UI communication a hassle
  6. 6. roloengine iOS Android Internals • • • • • • Written in Obj-C C++ (NDK) & Java (UI) OpenGL ES 2.0 for rendering OpenAL-soft for audio Custom Physics engine Custom Animation engine Custom asset format for models, animations
  7. 7. Rendering • Competent OpenGL ES 2.0 implementations from Android 2.3 onwards. • DO NOT MIX OpenGL 1.0 & 2.0 calls. • Recreate glBegin() / glEnd(), glPushMatrix() / glPopMatrix semantics in 2.0 if needed.
  8. 8. OpenGL context creation Pure Native a.k.a the hard way static int engine_init_display(struct engine* engine) { const EGLint attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_NONE }; EGLint w, h, dummy, format; EGLint numConfigs; EGLConfig config; EGLSurface surface; EGLContext context; EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  9. 9. OpenGL context creation eglInitialize(display, 0, 0); eglChooseConfig(display, attribs, &config, 1, &numConfigs); eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format); surface = eglCreateWindowSurface(display, config, engine->app->window, NULL); context = eglCreateContext(display, config, NULL, NULL); if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) { LOGW("Unable to eglMakeCurrent"); return -1; } return 0; }
  10. 10. OpenGL context creation Context creation in Java GLSurfaceView mGLView = new GLSurfaceView(this); mGLView.setRenderer(new GLRenderer()); class GLRenderer implements GLSurfaceView.Renderer { … public native void native_render(); public void onDrawFrame(GL10 gl) { native_render(); } }
  11. 11. OpenGL context creation Rendering via JNI calls to C/C++ JNIEXPORT void JNICALL Java_com_example_package_native_render(JNIEnv *env_, jobject obj_) { // Drawing code goes here }
  12. 12. OpenGL Tips • All OpenGL calls on render thread. All UI changes in UI thread. • Avoid touching Java heap during the game loop. • Block OpenGL thread on application pause.
  13. 13. Audio • Java APIs (SoundPool & Media Player) vs Native API OpenSL ES • OpenSL ES has a horrible & convoluted API • OpenAL-soft - Software impl of OpenAL with OpenEL ES as the backend https://github.com/AerialX/openal-soft-android
  14. 14. C++ utils & tools • Boost - surprisingly easy to build https://github.com/MysticTreeGames/Boost-for-Android Boost::Bind, Boost::IO • tinyxml2 - small and efficient XML parser https://github.com/leethomason/tinyxml2 • libpng + libzip for compressed textures
  15. 15. UI • Android is stingy with memory (16-24MB) • Easy to hit OOM with large bitmaps. • Modern phones (1 GB+ RAM) too Solution :BitmapFactory.Options options = new BitmapFactory.Options(); options.inPurgeable = true; options.inInputShareable = true;
  16. 16. UI • Layouts for all screen sizes a must, for consistent UI placement. • Stock android fonts are limited.
  17. 17. Thank You ! manish.mathai@rolocule.com

×