Today, we are going to do a hello world example for Adobe Alchemy. For those, who don't know what Alchemy is,
Alchemy is a research project that allows users to compile C and C++ code that is targeted to run on the open source ActionScript Virtual Machine (AVM2). The purpose of this preview is to assess the level of community interest in reusing existing C and C++ libraries in Web applications that run on Adobe Flash Player and Adobe AIR.
First up, we have to set up alchemy. You can find the setup instructions in
Adobe Labs. InsideRIA also have an article about setting up alchemy. You can find that
here.
Now that you have setup Alchemy, lets do the traditional Hello World example with it. First up the C code - helloWorld.c :
#include "AS3.h"
static AS3_Val returnString()
{
char* text = "Hello World";
return AS3_String(text);
}
int main()
{
AS3_Val cMethod = AS3_Function( NULL, returnString );
AS3_Val result = AS3_Object( "returnString : AS3ValType" , cMethod);
AS3_Release( cMethod );
AS3_LibInit( result );
return 0;
}
Some code explanations:
- AS3_Val is the data type Alchemy uses to represent ActionScript objects.
- All functions visible to ActionScript have to declared with AS3_Val as the return type.
- The first step in main() is to declare all methods for ActionScript as AS3_Function instances.
- Next, create an AS3_Object which will hold references to all these functions.
- "Release" the unwanted methods by calling AS3_Release.
- Finally, notify the runtime that the library has been initialized by using AS3_LibInit. Pass the object containing all the functions visible to ActionScript. Note : This should be called last.
Now that we have written our C code, we have to compile and convert it into a SWC. Open Cygwin, turn on Alchemy using the
alc-on; command and navigate to the folder where you have saved your C program. Then type the following command :
gcc helloWorld.c -O3 -Wall -swc -o helloworld.swcThis should create the SWC file with the filename helloworld.swc in that folder.If there were any errors in the C program, it will be shown here.
Now that you have the SWC file, we can use it in Flash or Flex to call its functions.
Using the SWC file in Flash CS4 :
- Open Flash CS4 and create a new ActionScript 3 document.
- In the Properties panel, click on the Edit profile and click on ActionScript 3 Settings.
- In the Library path tab, navigate to the SWC file and add it.
- In the Actions panel, type the following code :
//CLibInit is the Alchemy bridge to the C/C++ methods.
import cmodule.helloworld.CLibInit;
var loader:CLibInit = new CLibInit;
var library:Object = loader.init();
trace(library.returnString());
- Save and run the file. The text "Hello World" should be displayed in the Output panel.
Using the SWC file in Flex Builder 3 :
- Start Flex Builder 3 and create a new ActionScript project.
- Right-click the project, select Properies and select ActionScript Build Path. In the Library path tab, add the SWC file.
- Add the following code : (My ActionScript project's name is HelloWorld )
package
{
import flash.display.Sprite;
import cmodule.helloworld.CLibInit;
public class HelloWorld extends Sprite
{
public function HelloWorld()
{
var loader:CLibInit = new CLibInit;
var lib:Object = loader.init();
trace(lib.returnString());
}
}
}
- Save and debug the application. You should see the "Hello World" in the Console.
Congratulations, you have written a function in a C program and called that function in ActionScript using Adobe Alchemy. Hope this post was useful to you. I'll post more in the coming days. Have fun :D