守望者--AIR技术交流

标题: FlasCC例子研究之bitmapdata [打印本页]

作者: 破晓    时间: 2015-5-29 10:32
标题: FlasCC例子研究之bitmapdata
bitmapdata是FlasCC官方例子02_Interop中的例子。这例子比起c++interop来说,多了一个鼠标事件监听。
我们逐行分析一下吧。
  1. #include <AS3/AS3.h>
  2. #include <Flash++.h>

  3. using namespace AS3::ui;

  4. static const int COORDBITS = 7; // 7 bits => dim of 128
  5. static const int DIM = 1 << COORDBITS;
复制代码

这个函数主要是根据一个给出的offset值,返回一个计算后的值
  1. static int swizzleOffset(int offs)
  2. {
  3.   int result = 0;
  4.   int coordBits = COORDBITS;
  5.   int offsHi = offs >> COORDBITS; // take the higher order bits
  6.   int coordBits2 = COORDBITS * 2;
  7.   while(coordBits--)
  8.   {
  9.     // put the lowest bit in the "lo" offset bits
  10.     // into the highest bit of the result...
  11.     // it'll get shifted into a lower position
  12.     // as the loop executes
  13.     result |= ((offs & 1) << coordBits2);
  14.     offs >>= 1;
  15.     result >>= 1;
  16.     // same for the "hi" offset bits
  17.     result |= ((offsHi & 1) << coordBits2);
  18.     offsHi >>= 1;
  19.     result >>= 1;
  20.   }
  21.   return result;
  22. }



  23. // 将一个图像的像素进行混淆
  24. static void swizzlePixels(unsigned *dst, unsigned *src)
  25. {
  26.     int offs = DIM * DIM;

  27.     while(offs--)
  28.     {
  29.         int swiz = swizzleOffset(offs);
  30.         dst[swiz] = src[offs];
  31.     }
  32. }



  33. //监听鼠标点击事件,然后混淆BitmapData的像素值
  34. static var mouseDownHandler(void *arg, var args)
  35. {
  36.     flash::events::MouseEvent event = flash::events::MouseEvent(args[0]);
  37.     flash::display::Sprite sprite = flash::display::Sprite(event->target); // the container Sprite
  38.     flash::display::Bitmap bitmap = flash::display::Bitmap(sprite->getChildAt(0)); // Bitmap is the only child
  39.     flash::display::BitmapData bitmapData = bitmap->bitmapData;
  40.     // ByteArray corresponding to our ram!
  41.     // C ptrs are equivalent to offsets into this ByteArray
  42.     flash::utils::ByteArray ram = internal::get_ram();

  43.     // allocate space for the pixels
  44.     unsigned *src = new unsigned[DIM * DIM];
  45.     unsigned *dst = new unsigned[DIM * DIM];
  46.     // copy current pixels directly to ram
  47.     bitmapData->copyPixelsToByteArray(bitmapData->rect, ram, src);
  48.     // swizzle them!
  49.     swizzlePixels(dst, src);
  50.     // write new pixels directly from ram
  51.     bitmapData->setPixels(bitmapData->rect, ram, dst);
  52.     // clean up
  53.     delete dst;
  54.     delete src;
  55.     return internal::_undefined;
  56. }



  57. int main()
  58. {
  59.     //取得舞台
  60.     flash::display::Stage stage = internal::get_Stage();

  61.     //创建一个Shape
  62.     flash::display::Shape myShape = flash::display::Shape::_new();
  63.     flash::display::Graphics graphics = myShape->graphics;

  64.     //绘制简单的图形 这是一个像把子一样的圆

  65.     graphics->beginFill(0xff00ff, 0.5);
  66.     graphics->drawCircle(64.0, 64.0, 64.0);
  67.     graphics->endFill();
  68.     graphics->beginFill(0xffff00, 0.5);
  69.     graphics->drawCircle(64.0, 64.0, 40.0);
  70.     graphics->endFill();
  71.     graphics->beginFill(0x00ffff, 0.5);
  72.     graphics->drawCircle(64.0, 64.0, 16.0);
  73.     graphics->endFill();

  74.     //创建一个BitmapData
  75.     flash::display::BitmapData myBitmapData = flash::display::BitmapData::_new(DIM, DIM);
  76.     //把图形绘制到这个bitmapdata上

  77.     myBitmapData->draw(myShape);

  78.     // 创建一个Bitmap

  79.     flash::display::Bitmap myBitmap = flash::display::Bitmap::_new(myBitmapData);

  80.     // 加到一个Sprite中,以便显示

  81.     flash::display::Sprite mySprite = flash::display::Sprite::_new();

  82.     mySprite->addChild(myBitmap);
  83.     // 增加一个鼠标监听器
  84.     mySprite->addEventListener("mouseDown", Function::_new(&mouseDownHandler, NULL));
  85.     // 添加到舞台
  86.     stage->addChild(mySprite);

  87.     // 这个函数将会使C++与AS3进行同步,以使可以处理鼠标事件。
  88.     AS3_GoAsync();

  89.     // 因为运行机制的原因,这行代码是无法到达的。
  90.     return 0;
  91. }
复制代码

还是上个图吧。

[attach]1165[/attach]


http://www.cnblogs.com/geniusalex/archive/2013/05/15/3080980.html






欢迎光临 守望者--AIR技术交流 (http://www.airmyth.com/)