守望者--AIR技术交流

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
热搜: ANE FlasCC 炼金术
查看: 2175|回复: 0

[基础入门] FlasCC例子研究之bitmapdata

[复制链接]
  • TA的每日心情
    擦汗
    2018-4-10 15:18
  • 签到天数: 447 天

    [LV.9]以坛为家II

    1742

    主题

    2094

    帖子

    13万

    积分

    超级版主

    Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18

    威望
    562
    贡献
    29
    金币
    52623
    钢镚
    1422

    开源英雄守望者

    发表于 2015-5-29 10:32:09 | 显示全部楼层 |阅读模式
    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. }
    复制代码

    还是上个图吧。




    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    守望者AIR技术交流社区(www.airmyth.com)
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    
    关闭

    站长推荐上一条 /4 下一条

    QQ|手机版|Archiver|网站地图|小黑屋|守望者 ( 京ICP备14061876号

    GMT+8, 2024-3-29 22:12 , Processed in 0.061114 second(s), 32 queries .

    守望者AIR

    守望者AIR技术交流社区

    本站成立于 2014年12月31日

    快速回复 返回顶部 返回列表