我是同時嘗試安裝 Windows, Ubuntu 版,因為等網路下載很慢
結果到最後只有 Windows Cygwin 安裝成功
Ubuntu 上好像是因為遇到權限的問題,無法成功覆蓋 gcc 路徑
實際編譯 C Code 產生 SWC Lib. 畫面:
在編譯過程中,可以看到它是先產生中間 as 檔案,然後才編譯為 SWC 的
把中間 as 檔案複製出來看一下
結果大致像這樣:
裡面有一部份 ActionScript 3.0,然後剩下的都是奇怪的代碼
假如想要把 C function 拿進 Alchemy 給 Flash 用也不是那麼簡單的事情
請先參考這個 Developing with Alchemy:C API
我照著 Alchemy 內的範例,添加了一個迴圈加總的函式
//Simple String Echo example
//mike chambers
//mchamber@adobe.com
#include <stdlib.h>
#include <stdio.h>
//Header file for AS3 interop APIs
//this is linked in by the compiler (when using flaccon)
#include "AS3.h"
//Method exposed to ActionScript
//Takes a String and echos it
static AS3_Val echo(void* self, AS3_Val args) {
//initialize string to null
char* val = NULL;
//parse the arguments. Expect 1.
//pass in val to hold the first argument, which
//should be a string
AS3_ArrayValue( args, "StrType", &val );
//if no argument is specified
if (val == NULL) {
char* nullString = "null";
//return the string "null"
return AS3_String(nullString);
}
//otherwise, return the string that was passed in
return AS3_String(val);
}
static AS3_Val sum(void* self, AS3_Val args) {
int val = 0;
int res = 0;
AS3_ArrayValue( args, "IntType", &val );
int i;
for (i = 0 ; i < val ; ++i) {
res += i;
}
return AS3_Int(res);
}
//entry point for code
int main() {
//define the methods exposed to ActionScript
//typed as an ActionScript Function instance
AS3_Val echoMethod = AS3_Function( NULL, echo );
AS3_Val sumMethod = AS3_Function( NULL, sum );
// construct an object that holds references to the functions
AS3_Val result = AS3_Object( "echo: AS3ValType, sum: AS3ValType", echoMethod, sumMethod );
// Release
AS3_Release( echoMethod );
AS3_Release( sumMethod );
// notify that we initialized -- THIS DOES NOT RETURN!
AS3_LibInit( result );
// should never get here!
return 0;
}
編譯為 SWC Lib. 之後,使用以下 AS3 程式進行測試:
package {
import flash.display.Sprite;
import flash.utils.getTimer;
import cmodule.sum.CLibInit;
public class AlchemyTest extends Sprite {
public function AlchemyTest() {
var loader:CLibInit = new CLibInit();
var lib:Object = loader.init();
trace(lib.sum);
trace(lib.echo);
var no:int = 1000000;
var st:int = getTimer();
trace(lib.sum(no));
trace(getTimer() - st);
st = getTimer();
trace(sum(no));
trace(getTimer() - st);
st = getTimer();
trace(lib.sum(no));
trace(getTimer() - st);
}
public function sum(i:int):int{
var res:int = 0;
var j:int = 0;
for (j = 0 ; j < i ; ++j) {
res += j;
}
return res;
}
}
}
// Ticore's Blog - http://ticore.blogspot.com/
結果~~~
function Function() {}
function Function() {}
1783293664
178
1783293664
86
1783293664
178
天啊,我還刻意跑了好多次
Alchemy v0.4a 產生的 Lib. 居然比 AS3 實作的還要慢一倍左右
真是無言以對,難道我的測試過程中有問題嗎?
還是 Alchemy 還有其它的什麼特異功能?
相關連結:
了解 Adobe Alchemy
Adobe Alchemy 煉金術
Alchemy:Documentation:Getting Started
Adobe Alchemy
Developing with Alchemy:C API
Developing with Alchemy:AS3 API
Adobe Labs Forums - Alchemy