`

(5)、andengine之SpriteBatch(精灵族--动态和静态)

阅读更多

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.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.batch.DynamicSpriteBatch;
import org.andengine.entity.sprite.batch.SpriteBatch;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.color.Color;

import android.util.DisplayMetrics;
/**
 * 使用SpriteBatch批量绘制Sprite
 */
public class SpriteBatchActivity extends SimpleBaseGameActivity
{
 private static int winWidth = 854;
 private static int winHeight = 480;
 //图片纹理区域
 private BitmapTextureAtlas mBitmapTextureAtlas;
 private TextureRegion mTextureRegion;
 
 
 @Override
 public EngineOptions onCreateEngineOptions()
 {
  setScreenDisplay();
  Camera camera = new Camera(0, 0, winWidth, winHeight);
  EngineOptions engineOptions = new EngineOptions(
    true, ScreenOrientation.LANDSCAPE_FIXED,
    new RatioResolutionPolicy(winWidth, winHeight),
    camera);
  return engineOptions;
 }

 @Override
 protected void onCreateResources()
 {
  //设置图片的路径
  BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("images/");
  mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.DEFAULT);
  //后面两个参数是指该纹理加载到纹理区域上的什么位置
  mTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, this, "face_box.png", 0, 0);
  //真正加载
  mBitmapTextureAtlas.load();
 }

 @Override
 protected Scene onCreateScene()
 {
  this.mEngine.registerUpdateHandler(new FPSLogger());
  Scene scene = new Scene();
  //颜色:真实值/255
  scene.setBackground(new Background(new Color(0.6f,0.8f,0.6f)));
  
  final float centerX = (winWidth - mTextureRegion.getWidth()) / 2;
  final float centerY = (winHeight - mTextureRegion.getHeight()) / 2;
  
  final Sprite sprite1 = new Sprite(-50, 0, mTextureRegion, this.getVertexBufferObjectManager());
  final Sprite sprite2 = new Sprite(-50, 150, mTextureRegion, this.getVertexBufferObjectManager());
  //缩放原来的3倍
  sprite1.setScale(3);
  //精灵中心为轴顺时针方向旋转50度
  sprite1.setRotation(50);
  /**
   * SpriteBatch的使用,我们知道精灵无非是一个纹理和他对应范围的组合;
   * 那精灵族可以直观的把它看成同一纹理对应多个范围的形式。我们可以通过draw方法在其中增加新的显示范围,包括定义其大小方向等等,
   * 但是mTexture是不能改变的,相当于同一张图片渲染了多次。
   * 可以使用SpriteBatch批量绘制Sprite,
   * SpriteBatch又分为静态的和动态的,把SpriteBatch设想为画布,
   * 动态的无需在初始化的时候指定范围,而是根据每个Sprite自身的参数绘制,
   * 当需要渲染的时候他会回调onUpdateSpriteBatch让程序更新当时情况设置。
   *
   * 静态的必须要指定每个Sprite在画布中的绘制位置,长宽,比例,旋转等参数,
   * 添加完成之后一定要提交,这样才能完成渲染,动态的提交写如begin方法里面,我们可以不管。
   * 最后,要把SpriteBatch添加到Scene中。
   */
  //动态的精灵族
  SpriteBatch dynamicSpriteBatch = new DynamicSpriteBatch(mBitmapTextureAtlas,2,this.getVertexBufferObjectManager())
  {
   /**
    * 该方法是一直在循环执行的
    */
   @Override
   protected boolean onUpdateSpriteBatch()
   {
    draw(sprite1);
    draw(sprite2);
    return true;
   }
  };
  
  //静态精灵组
  SpriteBatch staticSpriteBatch = new SpriteBatch(mBitmapTextureAtlas, 2, this.getVertexBufferObjectManager());
  //后面的颜色是精灵的背景色
  staticSpriteBatch.draw(mTextureRegion, 20, 20, mTextureRegion.getWidth(), mTextureRegion.getHeight(), -45, 1, 1, 1, 1);
  staticSpriteBatch.draw(mTextureRegion, 20, 120, mTextureRegion.getWidth(), mTextureRegion.getHeight(), 45, 1, 1, 1, 0.3f);
  //添加完成之后一定要提交,这样才能完成渲染,动态的提交写如begin方法里面,我们可以不管
  staticSpriteBatch.submit();
  //设置整组精灵的初始位置
  dynamicSpriteBatch.setPosition(centerX, centerY);
  
  scene.attachChild(dynamicSpriteBatch);
  scene.attachChild(staticSpriteBatch);
  
  return scene;
 }

 /**
  * 设置屏幕大小
  */
 private void setScreenDisplay()
 {
  DisplayMetrics outMetrics = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
  winWidth = outMetrics.widthPixels;
  winHeight = outMetrics.heightPixels;
 }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics