Thursday, May 15, 2008

Porting monolithandroid from m3 to m5


The application that I am developing, monolithandroid, currently does not run under m5 series SDK. Why? My app uses OpenGL/ES and there have been significant changes to the way OpenGL/ES works under android from m3 to m5. In a nutshell, in m3 you can use a plain view and intermix OpenGL and Canvas calls. In m5 this is not currently allowed. This was a major problem because the onPaint method of the View Object was used to draw text such as score and level information, as well as the application backdrop (a picture of the moon). There are ways to circumvent these problems and fix the application so that it works on m5. How? You can use a SurfaceView to draw the OpenGL objects, and you can use an overlay to draw text on top of the SurfaceView. And the backdrop? Actually you can use a textured image (an OpenGL object) to draw the backdrop, with the added bonus that you can easily rotate and scale the image. To sum up, monolithandroid under m3 used a class named GLView to draw everything on screen. For m5, 4 new classes were introduced in place of GLView:
  1. GameOverlay (used for drawing status text and scores)
  2. GameSurfaceView (used for holding a surface for OpenGL drawing)
  3. GLThread (used for actually drawing OpenGL objects)
  4. Square (used to load a textured square for the backdrop image of the moon)
In order to create an overlay, you can use the following code


public void onCreate(Bundle icicle) {
super.onCreate(icicle);
overlay = new GameOverlay(this);
optionsView = new OptionsView(getApplication());
android.content.AssetManager mgr = getApplication().getAssets();
gsf = new GameSurfaceView(this,overlay);
gsf.setViewType(GLThread.VIEW_GAME);
gsf.setGameType(Monolith.GAME_MONOLITH);
setContentView(gsf);
gsf.setVisibility(View.VISIBLE);
this.addContentView(overlay,new android.view.ViewGroup.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.FILL_PARENT));
}
You can browse the full code at http://code.google.com/p/monolithandroid/source/browse

It is under svn->trunk->monolithandroid->src->org->teacake->monolith
So after I made these changes, monolithandroid sort of works. However, there are some problems to be addressed, like the time it takes on startup to load the moon image and convert it into a texture.
I would like to take the opportunity to thank Ed Burnette for sending me a preview PDF of his forthcoming android book, "HelloAndroid", and I must say that it looks very, very promising. Way to go, Ed! You can find out more about it at: http://www.pragprog.com/categories/upcoming
I admit that I shamelessly copied some code (the code for loading textures) from it.
Also I would like to thank plusminus and all the guys at http://www.anddev.org where you can find many interesting tutorials, like the following: http://www.anddev.org/textured_cube_opengl_code_sample-t813.html
Excellent work, guys.

No comments:

Search the web