Monday, June 8, 2009

Alchemy Tutorial : Sending data to Alchemy and back

Following up on my previous post on getting started with Alchemy, today, we're going to see how to send a variable from Flex to a C method as arguments. In this example, we're just going to pass an integer from Flex to a C function which will return the square of the integer value. Ok, lets get to the code.
The C code : square.c

#include "AS3.h"

static AS3_Val sqre( void* self, AS3_Val num )
{
int a;
a = AS3_IntValue(num);
int square = a*a;
return AS3_Int(square);
}

int main()
{
AS3_Val cMethod = AS3_Function( NULL, sqre );
AS3_Val result = AS3_Object( "sqre : AS3ValType" , cMethod);
AS3_Release( cMethod );
AS3_LibInit( result );
return 0;
}
As you can see, the main function is still pretty much the same. When passing arguments to a C method, that method should always return an AS3_Val object and always accept a pointer in addition to the other parameter.The rest of the code is very simple.
Compile the C code into a swc using
gcc square.c -O3 -Wall -swc -o square.swc
Now, we create an ActionScript project in Flex and import the swc. Type the following code :


package {
import flash.display.Sprite;
import cmodule.square.CLibInit;

public class Squaring extends Sprite
{
public function Squaring()
{
var loader:CLibInit = new CLibInit;
var lib:Object = loader.init();
trace(lib.sqre(5));//Output : 25
}
}
}

Now save and debug. You should get the output as 25. Now that we know how to send and receive data from Alchemy and Flex, we can move on to some real world applications.Hope this post was useful. Happy experimenting :D

Friday, June 5, 2009

Getting Started with Alchemy : Hello World tutorial in Flash CS4 and Flex 3

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.swc
This 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 :

  1. Open Flash CS4 and create a new ActionScript 3 document.
  2. In the Properties panel, click on the Edit profile and click on ActionScript 3 Settings.
  3. In the Library path tab, navigate to the SWC file and add it.
  4. 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());
  5. Save and run the file. The text "Hello World" should be displayed in the Output panel.

Using the SWC file in Flex Builder 3 :

  1. Start Flex Builder 3 and create a new ActionScript project.
  2. Right-click the project, select Properies and select ActionScript Build Path. In the Library path tab, add the SWC file.
  3. 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());
    }
    }
    }
  4. 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

Monday, June 1, 2009

Flash Builder 4 and Flash Catalyst are on Labs

Looks like Adobe finally released the betas of Flash Builder 4 and Flash Catalyst on Adobe Labs. Read more at Ryan Stewart's blog. Happy experimenting :D


EDIT : Looks like Flash Catalyst is free ( Adobe is providing a serial number below the download link) but Flash Builder 4 is a 30 day trial. You can extend the trial if you have a valid Flex Builder 3 serial key.