2010年6月16日 星期三

Testing a sprite to use as the "command line" for Adobe Alchemy

Alchemy:Documentation:Developing with Alchemy:AS3 API 提到setSprite()
可以將Sprite模擬成Command Shell,本範例測試Alchemy是否能使用C語言常用
的輸出入函數將資料導到STDIN、STDOUT、 STDERR

以下程式改自Alchemy samples其中的stringecho範例
這是C程式
#include <stdlib.h>
#include <stdio.h>
#include "AS3.h"

static AS3_Val echo(void* self, AS3_Val args)
{

char* val = NULL;
AS3_ArrayValue( args, "StrType", &val );

if(val == NULL)
{
char* nullString = "null";
//return the string "null"
return AS3_String(nullString);
}

printf("*****Test printf() OK!!!*****\n");
fprintf(stderr, "*****%s Test fprintf(stderr,...) OK!!!*****\n",val);
fprintf(stdout, "*****%s Test fprintf(stdout,...) OK!!!*****\n",val);

return AS3_String(val);
}

int main()
{

AS3_Val echoMethod = AS3_Function( NULL, echo );


AS3_Val result = AS3_Object( "echo: AS3ValType", echoMethod );


AS3_Release( echoMethod );


AS3_LibInit( result );


return 0;
}




這是as3程式
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.Event;
import flash.events.MouseEvent;

import cmodule.stringecho.CLibInit;

public class EchoTest extends Sprite
{
protected var alchemyLib : Object;

public function EchoTest()
{
addEventListener( Event.ADDED_TO_STAGE, addedToStageHandler );

}

protected function addedToStageHandler( event : Event ) : void
{
removeEventListener( Event.ADDED_TO_STAGE, addedToStageHandler );
var loader:CLibInit = new CLibInit;
loader.setSprite(this);
alchemyLib= loader.init();
trace(alchemyLib.echo("foo0"));
alchemyLib.echo("foo1");
throw Error(alchemyLib.echo("foo2"));
}

}
}



執行的結果如下
*****foo1 Test fprintf(stderr,...) OK!!!*****
*****foo2 Test fprintf(stderr,...) OK!!!*****


經過用力的測測測........
目前只有fprintf(stderr,...)能成功輸出
Alchemy名字取的真好
可真是"煉金術"啊
讓我可以體會自古以來西方鍊金術士鍊金過程的辛苦
發思古之幽情........

這是swf檔
[Demo]

2010年6月11日 星期五

Plasma Effect--Adobe Alchemy初體驗



本來看過Adobe Labs的Alchemy:Documentation:Getting Started"落落長"的內容有點猶豫不前,
想不到Adobe Alchemy在Ubuntu一下子就裝好了,接著搜尋到一些超炫Plasma Effect想說實驗看看

這裡有C語言實作的詳解,還有這篇Alchemy plasma experiment文章也有Alchemy實作的原始碼

以下是我修改後的C原始碼


#include <stdio.h>
#include <math.h>
#include "AS3.h"

int* palette;
int* plasma;
int* newPlasma;
int width;
int height;

AS3_Val generatePlasma(void* self, AS3_Val args)
{


AS3_ArrayValue(args, "IntType, IntType", &width, &height);

palette = malloc(256 * sizeof(int));
plasma = malloc(width * height * sizeof(int));
newPlasma = malloc(width * height * sizeof(int));

genPalette();
plasma_pattern();
return AS3_Ptr( plasma );
}


AS3_Val shiftPlasma(void* self, AS3_Val args)
{
int shift, x, y, index, paletteIndex;
AS3_ArrayValue(args, "IntType", &shift);

for(x=0; x<width; x++)
{
for(y=0; y<height; y++)
{
paletteIndex = (unsigned int)(plasma[index] + shift) % 256;
newPlasma[index] = palette[paletteIndex];
index++;
}
}

return AS3_Ptr(newPlasma);
}


void genPaletteHSV()
{
int x;
for(x=0; x<256; x++)
{
palette[x] = HSVtoRGB(x, 255, 255);
}
}

void plasma_pattern()
{
int x, y, index;
for(x=0; x<width; x++)
{
for(y=0; y<height; y++)
{
int color = (
128.0 + ( 128.0 * sin( x / 16.0 ) ) +
128.0 + ( 128.0 * sin( y / 8.0 ) ) +
128.0 + ( 128.0 * sin( ( x + y ) / 16.0 ) ) +
128.0 + ( 128.0 * sin( sqrt( x * x + y * y ) / 8.0 ) )
) / 4;
plasma[index++] = 0xff << 24 | color;
}
}
}

int main()
{
AS3_Val generatePlasmaMethod = AS3_Function( NULL, generatePlasma );
AS3_Val shiftPlasmaMethod = AS3_Function( NULL, shiftPlasma );
AS3_Val result = AS3_Object(
"generatePlasma: AS3ValType,shiftPlasma: AS3ValType",
generatePlasmaMethod, shiftPlasmaMethod
);
AS3_Release( generatePlasmaMethod );
AS3_Release( shiftPlasmaMethod );
AS3_LibInit( result );

return 0;
}




效果很炫
CPU也不怎麼操
看來書櫃裡塵封已久的C語言參考書將重見天日了