天天看點

AndEngine Example(1):LineExample

目标:

1.使得程式可以正常運作

2.搭建最簡單的場景

3.了解Scene,Camera等幾個基本的概念

4.了解整個的基本流程

----------------------------------------------------

建立一個Android工程,自動建立Activity,然後為其命名為LineExample。(如果改名了,不要忘記修改Manifest)

将下面的代碼複制進去,

然後添加andengine.jar到libs中。

運作。

package org.andengine.examples;

import java.util.Random;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.primitive.Line;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.SimpleBaseGameActivity;

/**
 * (c) 2010 Nicolas Gramlich 
 * (c) 2011 Zynga Inc.
 * 
 * @author Nicolas Gramlich
 * @since 11:54:51 - 03.04.2010
 */
public class LineExample extends SimpleBaseGameActivity {
	// ===========================================================
	// Constants
	// ===========================================================

	/* Initializing the Random generator produces a comparable result over different versions. */
	private static final long RANDOM_SEED = 1234567890;

	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;

	private static final int LINE_COUNT = 100;

	// ===========================================================
	// Fields
	// ===========================================================

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public EngineOptions onCreateEngineOptions() {
		final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

		return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
	}

	@Override
	public void onCreateResources() {

	}

	@Override
	public Scene onCreateScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		final Scene scene = new Scene();
		scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

		final Random random = new Random(RANDOM_SEED);

		final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
		for(int i = 0; i < LINE_COUNT; i++) {
			final float x1 = random.nextFloat() * CAMERA_WIDTH;
			final float x2 = random.nextFloat() * CAMERA_WIDTH;
			final float y1 = random.nextFloat() * CAMERA_HEIGHT;
			final float y2 = random.nextFloat() * CAMERA_HEIGHT;
			final float lineWidth = random.nextFloat() * 5;

			final Line line = new Line(x1, y1, x2, y2, lineWidth, vertexBufferObjectManager);

			line.setColor(random.nextFloat(), random.nextFloat(), random.nextFloat());

			scene.attachChild(line);
		}

		return scene;
	}

	// ===========================================================
	// Methods
	// ===========================================================

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
           

onCreateEngineOptions()将會建立一個EngineOptions 對象

EngineOptions 引擎的參數,建立它的時候,需要給定:螢幕方向,分辨率比率政策,和相機(這個相機是一個抽象的概念,并不是實際的硬體)

onCreateScene() 建立一個場景

這裡,我們建立了一個場景,并且往裡面加入了實體(Line is a Entity)

           Line extends Shape

           Shape extends Entity 

VertexBufferObjectManager 是一個管理者,它管理這些實體對象的buffer。

--------------------------------------------------------------------------

擴充閱讀:繪制是如何進行的?

EngineRenderer implements GLSurfaceView.Renderer

凡是對于OpenGL有一定了解的應該知道

GLSurfaceView.Renderer中有一個回調函數: public void onDrawFrame(final GL10 pGL)

繼承類在此回調中調用了

(1) Engine.onDrawFrame

(2) Engine.onUpdateDrawHandlers(pGLState, this.mCamera)

(3) DrawHandlerList.onDraw(final GLState pGLState, final Camera pCamera)

(4) IDrawHandler.onDraw(final GLState pGLState, final Camera pCamera)

(5) interface IEntity extends IDrawHandler

(6) class Entity implements IEntity

(7) Entity.onDraw(final GLState pGLState, final Camera pCamera)

(8) Entity.onManagedDraw(final GLState pGLState, final Camera pCamera)