[b][size=large]第一步:首先能創建swf文件[/size][/b]
從Adobe網站下載Flex SDK,編寫一個as文件,編譯一下就生成了一個swf,類似於C程序開發。
編輯文件:box.as
package{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
public class box extends Sprite{
public function box() {
var label1:TextField = new TextField();
label1.text = "hello";
this.addChild(label1);
}
}
}
編譯:
./flex-sdk/bin/mxmlc box.as
會得到box.swf
[b][size=large]第二步:下載alchemy SDK並配置開發環境[/size][/b]
下載SDK包 [url]http://download.macromedia.com/pub/labs/alchemy/alchemy_sdk_ubuntu_p1_121008.zip[/url]
解壓後執行./config會生成alchemy-setup腳本
編輯alchemy-setup腳本,找到ADL變量聲明,去掉註釋,並修改爲實際的adl路徑,也就是$FLEX_SDK/bin/adl
創建一個環境設置腳本env.sh
source alchemy路徑/alchemy-setup
export PATH=$ALCHEMY_HOME/achacks:flexSDK路徑/bin:$PATH
執行source env.sh,則環境變量被設置好了。
此時gcc,g++都變成了使用achacks目錄中的命令了,不是原來系統自帶的gcc,g++。
[b][size=large]第三步:Hello world[/size][/b]
3.1編寫C代碼hello.c:提供一個函數,當調用此函數時在傳入字符串的前面增加一些內容。
C接口參考:[url]http://labs.adobe.com/wiki/index.php/Alchemy:Documentation:Developing_with_Alchemy:C_API[/url]
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "AS3.h"
static AS3_Val hello(void* self, AS3_Val args)
{
char hello[256] = "hello,";
char* val = NULL;
AS3_ArrayValue( args, "StrType", &val );
if(val != NULL) {
strcat(hello, val);
}
return AS3_String(hello); //hello是局部變量也可以,因爲此處新生成了對象
}
static AS3_Val wow(void*self, AS3_Val args) {
return AS3_String("wow");
}
//entry point for code
int main()
{
AS3_Val helloMethod = AS3_Function( NULL, hello); //函數名hello
AS3_Val wowMethod = AS3_Function( NULL, wow); //函數名wow
//保存接口列表,此處的成員類型爲AS3ValType,成員名稱可以和函數名不一樣,
//只要在As腳本中使用的名字和此處一直即可,不過推薦使用一致的名稱。
AS3_Val result = AS3_Object( "hello:AS3ValType, wow:AS3ValType", helloMethod, wowMethod );
// Release。在result中還有引用,不會被真正釋放。
AS3_Release( wowMethod);
AS3_Release( helloMethod );
// notify that we initialized -- THIS DOES NOT RETURN!
AS3_LibInit( result );
// should never get here!
return 0;
}
hello和wow兩個函數是供As腳本調用的函數接口,在main函數中通過兩個方法對象註冊後As腳本就可以調用了。
gcc hello.c -swc -o hello.swc
輸出文件名會影響包名,此處輸出文件名爲hello.swc,那麼As中import的包名就是:
cmodule.hello.CLibInit,如果輸出爲h.swc,那麼相應的就變成cmodule.h.CLibInit
3.2編寫AS代碼hello.as
AS接口參考:[url]http://labs.adobe.com/wiki/index.php/Alchemy:Documentation:Developing_with_Alchemy:AS3_API[/url]
package{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
//引用包
import cmodule.hello.CLibInit
public class hello extends Sprite{
public function hello() {
var label1:TextField = new TextField();
label1.x = 100;
label1.y = 100;
this.addChild(label1);
var label2:TextField = new TextField();
label2.x = 100;
label2.y = 140;
this.addChild(label2);
//初始化庫
var loader:CLibInit = new CLibInit();
var lib:Object = loader.init();
//通過函數綁定的對象名稱調用函數
label1.text = lib.hello("boy");
label2.text = lib.wow();
}
}
}
編譯生成swf
mxmlc -library-path+=./hello.swc -static-link-runtime-shared-libraries=true hello.as
最後得到hello.swf
在瀏覽器中或者用flashplayer就可以看到效果了,開始在Linux下屏幕是空白的,是因爲第三方插件對flash支持的不是太好。
到 [url]http://www.adobe.com/support/flashplayer/downloads.html[/url] 下載個Flash Player 10.3 Projector ,直接用flashplayer播放就看到效果了。
如果C代碼中有內嵌彙編是編譯不過的,從編譯過程上分析:
C代碼-->llvm中間碼-->彙編代碼(.s)-->二進制彙編(.o)-->鏈接成程序,如果有內嵌的彙編代碼,那麼llvm文檔中有講中間碼中只會簡單包含內嵌彙編代碼,在下一步處理中直接會變成.s,而alchemy應該是隻能支持到llvm中間碼,不能支持.s的,如果llvm中包含有.s代碼,那麼就不能運行了。