Stage3d 中的DeviceLost(设备丢失)处理方案

/* @author Jave.Lin
    @date 2016-09-10
*/

IDeviceRes
{
  function onRestore(newDevice:Context3D):void;
}

GameVertexBufferRes : IDeviceRes
{
  perVertexNum:int;
  rawData:Vector.;
  vertexBuff:VertexBuffer3D;
  function onRestore(newDevice:Context3D):void
  {
    vertexBuff.dispose();
    vertexBuff = newDevice.createVertexBuffer(rawData.length, perVertexNum);
    vertexBuff.upload(rawData);
  }
}

GameIndexBufferRes : IDeviceRes
{
  rawData:Vector.;
  indexBuff:IndexBuffer3D;
  function onRestore(newDevice:Context3D):void
  {
    indexBuffer.dispose();
    indexBuffer = newDevice.createIndexBuffer(rawData.length);
    indexBuffer.upload(rawData);
  }
}

GameTextureRes : IDeviceRes
{
  width:Number, height:Number,
  format:String, optmizForRT:Boolean;
  tex:TextureBase;
  texType:EnumTexture; // @see EnumTexType
  rawData:Object;
  function onRestore(newDevice:Context3D):void
  {
    tex.dispose();
    tex = newDevice.createTexture(width, height, format, optmizForRT);
    switch (texType)
    {
      case EnumTexType.ATF:
      tex.uploadByATF(rawData as ByteArray);
      break;
      case EnumTexType.URL_PICS:
      ResMgr.getRes(rawData as String, function(bmd:BitmapData):void
      {
        tex.uploadByBmd(bmd);
      });
      break;
      case EnumTexType.TEMP:
      text.uploadByBmd(rawData as BitmapData);
      break;
    }
  }
}

EnumTexType
{
  ATF, // Adobe texture format
  URL_PICS, // url pics : jpg/png/bmp/dds/wds, etc.
  TEMP // new BitmapData upload
}

Game
{
 renderRes:Dictionary = new Dictionary();

 function addRenderRes(dr:IDeviceRes):void
 {
   renderRes[dr] = true;
 }

 functin onDeviceLostEvent(e:Event):void
 {
   var newDevice:Context3D = ...;
   for (var dr:IDeviceRes in renderRes)
   {
     dr.onRestore(newDevice);
   }
 }
}

你可能感兴趣的:(AS3)