Home > Android > Android: Using OpenGL for 2D rendering – the easy way

Android: Using OpenGL for 2D rendering – the easy way

This is an extension to the project developed by Chris Pruett, which can be found here [1]. Feel free to read the README provided.

 

Basically, what this project aims to do is to offer you a simple class that can be used to render 2D objects in an OpenGL surface.

I modified a little the project written by Chris Pruett by adding a wrapper class corresponding to a scene. This allows you to use OpenGL very easily in order to render 2D objects. You can instantiate this class, add various sprites and have them drawn on the surface view.

The project can be downloaded here [2].

This is the API for the Scene class:

public interface Scene {
	public SurfaceView getSurfaceView();

	/**
	 * Sets a bitmap as the background
	 *
	 * @param bitmap
	 */
	public void setBitmapBackground(Bitmap bitmap);

	/**
	 * Sets a nine patch resource as the background. It will stretch to fill the
	 * entire screen. When screen orientation will change, this background will
	 * also change to fill the new screen.
	 *
	 * @param pictureId
	 */
	public void setNinePatchBackground(int pictureId);

	/**
	 * Sets the background color of the scene. No bitmap will be used any more
	 * for the background.
	 *
	 * @param color
	 */
	public void setBackgroundColor(CustomColor color);

	/**
	 * Adds a sprite to the scene.
	 *
	 * @param sprite
	 * @param addToMover
	 *            Whether to pass this to the Mover thread
	 */
	public void addSprite(GLSprite sprite, boolean addToMover);

	/**
	 * Sets the mover thread. This will handle the sprites' movement.
	 *
	 * @param mover
	 */
	public void setMover(Mover mover);

	/**
	 * Creates a sprite corresponding to the given resource id
	 *
	 * @param context
	 * @param resourceId
	 * @return
	 */
	public Renderable createSprite(Context context, int resourceId);

	/**
	 * Returns a sprite corresponding to a given bitmap
	 *
	 * @param bitmap
	 *            The bitmap used for the sprite
	 * @param bitmapId
	 *            The bitmap id is used to re-use textures (use same ID for same
	 *            bitmaps content)
	 * @return
	 */
	public Renderable createSprite(Bitmap bitmap, int bitmapId);

}

Usage:

public class MainActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Scene scene = createScene();
		setContentView(scene.getSurfaceView());
	}

	private Scene createScene() {
		// our OpenGL scene
		GLScene scene = new GLScene(this);

		// can set a nine patch for the background
		scene.setNinePatchBackground(R.drawable.nine_patch_bg);

		// can set a bitmap for the background
		// scene.setBitmapBackground(someBitmap);

		// can set a color for the background
		// scene.setBackgroundColor(new CustomColor(0, 0, 0, 1));

		// create some sprites
		GLSprite sprite = scene.createSprite(getApplicationContext(), R.drawable.skate1);
		// initial position
		sprite.x = 100;
		sprite.y = 200;
		GLSprite sprite2 = scene.createSprite(getApplicationContext(), R.drawable.skate2);
		// initial position
		sprite2.x = 300;
		sprite2.y = 400;

		// add sprites to the scene
		scene.addSprite(sprite, true);
		scene.addSprite(sprite2, true);
		scene.addSprite(sprite.clone(), true);
		scene.addSprite(sprite2.clone(), true);

		// this thread controls the sprites' positions
		scene.setMover(new Mover());
		return scene;
	}
}

Preview:
GL 2D scene preview

[1] http://code.google.com/p/apps-for-android/source/browse/SpriteMethodTest/
[2] https://github.com/costimuraru/Simple-Android-OpenGL-2D-Rendering

About these ads
Categories: Android
  1. Michael A.
    January 24, 2013 at 11:56 am

    Hi,

    Does this use traditional x-y screen coordinates or the flipped coordinates of OpenGL.

    Have been looking for a simple 2D openGL implementation for a while now, so this seems quite interesting.

    Regards,

    Michael a.

    Like

    • Michael A.
      January 24, 2013 at 12:15 pm

      Also ( a smaller question), does your code allow the app to avoid the problematical 16-64mb limit for the graphics?

      Like

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: