FlasCC例子研究之bitmapdata

bitmapdata是FlasCC官方例子02_Interop中的例子。这例子比起c++interop来说,多了一个鼠标事件监听。

我们逐行分析一下吧。

#include <AS3/AS3.h>
#include <Flash++.h>

using namespace AS3::ui;

static const int COORDBITS = 7; // 7 bits => dim of 128
static const int DIM = 1 << COORDBITS;

这个函数主要是根据一个给出的offset值,返回一个计算后的值

static int swizzleOffset(int offs)
{
  int result = 0;
  int coordBits = COORDBITS;
  int offsHi = offs >> COORDBITS; // take the higher order bits
  int coordBits2 = COORDBITS * 2;
  while(coordBits--)
  {
    // put the lowest bit in the "lo" offset bits
    // into the highest bit of the result...
    // it'll get shifted into a lower position
    // as the loop executes
    result |= ((offs & 1) << coordBits2);
    offs >>= 1;
    result >>= 1;
    // same for the "hi" offset bits
    result |= ((offsHi & 1) << coordBits2);
    offsHi >>= 1;
    result >>= 1;
  }
  return result;
}

 

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

    while(offs--)
    {
        int swiz = swizzleOffset(offs);
        dst[swiz] = src[offs];
    }
}

 

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

    // allocate space for the pixels
    unsigned *src = new unsigned[DIM * DIM];
    unsigned *dst = new unsigned[DIM * DIM];
    // copy current pixels directly to ram
    bitmapData->copyPixelsToByteArray(bitmapData->rect, ram, src);
    // swizzle them!
    swizzlePixels(dst, src);
    // write new pixels directly from ram
    bitmapData->setPixels(bitmapData->rect, ram, dst);
    // clean up
    delete dst;
    delete src;
    return internal::_undefined;
}

 

int main()
{
    //取得舞台
    flash::display::Stage stage = internal::get_Stage();

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

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

    graphics->beginFill(0xff00ff, 0.5);
    graphics->drawCircle(64.0, 64.0, 64.0);
    graphics->endFill();
    graphics->beginFill(0xffff00, 0.5);
    graphics->drawCircle(64.0, 64.0, 40.0);
    graphics->endFill();
    graphics->beginFill(0x00ffff, 0.5);
    graphics->drawCircle(64.0, 64.0, 16.0);
    graphics->endFill();

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

    myBitmapData->draw(myShape);

    // 创建一个Bitmap

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

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

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

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

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

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

还是上个图吧。

image

posted @ 2013-05-15 23:46  麒麟子MrKylin  阅读(406)  评论(0编辑  收藏  举报