Monday, September 21, 2009

Flash Development on Linux (III): swc libraries

In this stage, we already know how to set up our Flash development environment on Linux, and how to compile basic programs in Flash (Actionscript).

If we intend to develop something a bit more advanced, it is very common to need precompiled libraries. These libraries are packaged in the form of .swc files, so we are going to know how to create such libraries, and how to use them once created.



The AXDT plugin form Eclipse includes a simplified version of the Flex SDK provided by Adobe. The SDK from Adobe includes additional tools, and in this scenario, in order to create a swc library, we need the component compiler (compc). This compiler can be found included with the Flex SDK tools.

The installation of such environment presents some minor difficulties that we are going to solve here:

First of all, we go to the Adobe website to download the open source Flex SDK. In this tutorial we have worked with the version 3.4.0.9271 (Aug 18, 2009)


$ sudo mkdir /opt/flex3

Then, we will choose an appropriate directory to unpack the compiler in such place. Notice the -a option: It is necessary in order to convert text files (CR/LF characters) properly.


$ sudo unzip -a -d /opt/flex3 flex_sdk_3.4.0.9271_mpl.zip

Optionally, you can remove the files on the bin directory intended for other platforms


$ sudo rm -f /opt/flex3/bin/*.exe /opt/flex3/bin/*.bat

Once uncompressed, we need to give the execution permissions to the files in the bin folder. The zip file definitely was created on a non-Linux system.


$ sudo chmod +x /opt/flex3/bin/*

To illustrate the usage of the component compiler, we are going to create the .swc library file corresponding to the Away3D library. Away3D is an open source flash 3D library which is quickly becoming one of the most popular Flash libraries on its field.

Once again, we download the source code of the library. Since we are developing for Flash 9, we download the 2.x version. If you intend to develop Flash 10 programs, you can go with the 3.x version. In our sample, we are working with the 2.4.0 version.

We create a directory for the library inside our workspace directory and uncompress there the away3D source code.


$ mkdir $HOME/workspace/away3d
$ unzip -a -d $HOME/workspace/away3d away3d_2_4_0.zip

The component compiler, apart from having a very limited documentation has a syntax a bit inconvenient. We can not indicate to the compiler to include all the classes under a directory tree, but we have to enumerate all the classes that we want to pack into the .swc file. In the case of the Away3D library, that means that the command line to invoke the compiler must contain the name of about 320 classes (including their package names).

To avoid that, we are going to create a compilation script in a more or less automated way. Since I am not an expert on shell scripts, suggestions on how to improve / ease this script are welcome.


$ cd $HOME/workspace/away3d
$ echo '/opt/flex3/bin/compc -compiler.source-path . -output ./away3d.swc -include-classes \' > compile_away3d
$ find . -name '*.as' | grep -v 'away3d/test' | sed -e 's/\.as/ \\/g' -e 's/\.\///g' -e 's/\//\./g' -e '$s/ \\//g' >> compile_away3d
$ chmod +x compile_away3d


The compile_away3d script file now has the command line necessary to compile the library, excluding the sample sources under the test directory. All we need to do right now is just to invoke the compilation script. Have a look inside it before if you are interested in a closer examination or do not worry about it if you just want to compile the library disregarding the details.


$ ./compile_away3d

Once in this step, if the compiler complains about a duplicate variable definition on the lensFlare.as file (line 118), everything is going right.

The as3 language manages the scope of the variables in a different manner than the usual one (as, for example in C++ or Java). Block scope does not exist, and the minimal scope for variables is the whole function. There is a bug related to that in the current latest stable version (2.4.0) of the Away3D library (the bug has already been corrected in the svn version), so we change a fragment of code in the lensFlare.as in order to avoid having a duplicate variable declaration. The change consists just in getting the variable declaration (ctVal) out of the conditions, and declare it just once.


if(useBurning && _burnClip)
{
var ctVal:Number;
if(_burnMethod == LensFlare.BURN_METHOD_BRIGHTNESS)
{
var bsVal:Number = 5*burnFactor/_projectionLength;
bsVal = bsVal < 1 ? 1 : bsVal;
bsVal = bsVal > 3 ? 3 : bsVal;
//TweenMax.to(_burnClip, 0, {colorMatrixFilter:{contrast:bsVal, brightness:bsVal}});
//TODO: setup colorMatrixFilter tween without TweenMax
ctVal = 500*burnFactor/_projectionLength;
_ct = new ColorTransform(1, 1, 1, 1, ctVal, ctVal, ctVal, 0);
_burnClip.transform.colorTransform = _ct;
}
else if(_burnMethod == LensFlare.BURN_METHOD_COLOR_TRANSFORM)
{
ctVal = 500*burnFactor/_projectionLength;
_ct = new ColorTransform(1, 1, 1, 1, ctVal, ctVal, ctVal, 0);
_burnClip.transform.colorTransform = _ct;
}
}


Now, the library compiles without any kind of problem. We will see the away3d.swc file (about 590KB size) on the current directory.

4 comments:

dev0x said...

Thanks for the post but I am kinda curious.

I tried to run the compile_away3d script and I get this error: /home/myuser/eclipse-workspace/away3d/away3d_2_4_0/wumedia/parsers/swf/Edge.as: Error: A file found in a source-path must have the same package structure 'away3d_2_4_0.wumedia.parsers.swf', as the definition's package, 'wumedia.parsers.swf'.

I'm trying to dig around to find out more about compc as well as how all this works in linux. Thanks so much for the posts you've gotten so far. Any ideas on my problem?

Bill Hicks on Marketing said...

To anyone who gets this kind of message:

Error: A file found in a source-path must have the same package structure '#####', as the definition's package, '####'.

The fix is fairly simple: when you unpack away, the directory structure looks as $HOME/.../away3d/away3d_2_5_0/src/away3d/ .The location where you need to create this script is not the topmost "away3d" folder, but the "src/". Try that, it really fixes the problem.

Good luck=)

K.

Bill Hicks on Marketing said...

to everyone who had this errormessage:

.../away3d/away3d_2_5_0/src/away3d/...: Error: A file found in a source-path must have the same package structure 'away3d_2_5_0.src.away3d....', as the definition's package, 'away3d....'.

Watch out where you run the compiler script from! When you unpack away3d, the directory structure is

$HOME/.../away3d/away3d_2_5_0/src/away3d/

Try creating your compile_away3d not at the topmost "away3d/", but at the "src/" directory, everything should go well now.

Good luck;)

K.

andy_e said...

This is a nice series!

First of all, I'd like to let you know that AXDT has been updated again (note: perhaps one last time) to work with Eclipse 3.7 (Indigo) and Xtext 2.2.1.

But now I have a question as well, regarding "eye-candy": could it be the only thing you can do with this IDE is hand-drawn, dynamically generated graphics?
What if you want to code a game with a main sprite, an enemy sprite, and so on?

On Windows, you'd use .FLA files for that, and put your ActionScript code into. But AFAICS, there is no FLA support in AXDT, nor any way to bind an external .FLA to your code.

Looks if you want to create eye-candy, this IDE is not for you and you still have to use stinking Windows?!