初試 Adobe Alchemy

2009-05-14
配置好了Alchemy後先去Adobe Labs裡看一看Getting Start GuideAlchemy API,下面的例子從AS裡傳入兩個整型參數到C函數中,C計算兩者之和並返回給AS:

先看看C的代碼:
#include <stdlib.h>
#include <stdio.h>
#include "AS3.h"

//供AS調用的函數,args即為AS傳入給C函數的參數
static AS3_Val myfun(void *self, AS3_Val args)
{ 
    //提取參數
    int a, b;
    AS3_ArrayValue(args, "IntType, IntType", &a, &b);
    
    //計算a, b之和,轉換為AS裡的Int數據類型並返回之
    return AS3_Int(a+b);
}

int main()
{
    //定義一個公開給AS調用的函數
    AS3_Val myMethod = AS3_Function(NULL, myfun);

    //創建一個持有該函數引用的Object
    AS3_Val result = AS3_Object("myfun:AS3ValType", myMethod);

    //Release
    AS3_Release(myMethod);

    //告知初始化完成,此方法不會返回
    AS3_LibInit(result);

    //永遠不會執行到這裡
    return 0;
}

下面是AS代碼:
package{

    import flash.display.Sprite;
    import flash.utils.ByteArray;
    import cmodule.test.CLibInit;

    public class Test extends Sprite
    {
        public function Test()
        {
            var loader:CLibInit = new CLibInit();
            var lib:Object = loader.init();
            trace(lib.myfun(10, 20));
        }
    }
}
OK,然後編譯一下:
C: gcc test.c -O3 -Wall -swc -o test.swc
AS: mxmlc -library-path+=test.swc Test.as

M-OSCAR | Powered by Blogger | Entries (RSS) | Comments (RSS) | Designed by MB Web Design | XML Coded By Cahayabiru.com