Sunday, November 3, 2013

Migrating from Alchemy to FlasCC/CrossBridge - Simplify the File System Using CrossBridge's "addFile" as an Alternative to "supplyFile" in Alchemy

Many times, your C/C++ code may rely on external files to work, such as texture files/model files for a 3D engine. One way to supply those file to the CModule in CrossBrige is to use the memory API, which is fast, but need more wrapper codes. Sometimes, use the standard C/C++ I/O API, such as "FILE" and "ifstream", is more convenient for porting existing C/C++ libraries.

CrossBridge needs to process the external files using "genfs" before you can use them. That's why I tried the URLLoaderVFS, a class by twistedjoe from And for different kinds of methods of loading files - Embed, use http/URLLoader and use local shared objects, you need to genfs the external files differently and link with different .as class, which is detailed documented at
However, these are for compilng a swf directly from C/C++ source files. If you compile C/C++ to a swc first, and then compile the swf from your swc and a file, things can be greatly simplified.

I always thought the FlasCC/CrossBridge's file system is more complicated than Alchemy, before I realized that there is a similar way for supplying external files to the C module.

In Alchemy, we only need to use the function "supplyFile(fileName:String, fileData:ByteArray)", and handle the different methods of loading the data file in AS3, use the "Embed" tag, URLLoader or local shared objects.
In CrossBridge, actually we have an alternative function - CModule.vfs.addFile(fileName:String, fileData:ByteArray).
You only need to call the function to supply the data file needed before the line "CModule.startAsync(this);".
And for the data file itself, you can use AS3 code to load. No matter using which ways, treat them as ByteArrays. Once they are loaded or ready, call the function "addFile".

You can check the source code for details:

Sunday, October 27, 2013

Simple Chinese Word Segmentation Lib for Flash AS3 - SCWS Ported to Flash Using CrossBridge

Unlike English sentences, in a Chinese sentence, there is no space between two words ( This can cause lots of trouble for processing the language on computer.

SCWS is a simple Chinese word segmentation C lib. I just ported it to Flash using CrossBridge - the latest open source version of FlasCC. You can use the pre-build swc library "libscws.swc" in your Flash/AS3 projects.

The SCWS lib depends on an extra ".xdb" dictionary file and a ".ini" rule file, which can be downloaded at However, the CrossBridge's file system is not as simple as the old Alchemy(See this post, and simplified code), so I use the class by twistedjoe from, which doesn't require any genfs processing on the files.

There is almost no modification of the original C source files, except for the file "lock.c", I commented the line to pass the gcc complains:

//#warning no proper flock supported

To use the swc library, you must set compiler options "enable strict mode" to false! Otherwise, the AS3 compiler will throw error "Error: Call to a possibly undefined method addEventListener through a reference with static type CrossBridge.libscws.vfs:URLLoaderVFS".

There are two main functions in the AS3 library: "initialize_SCWS_AS3()" and "scws_send_text_AS3()".
For using the "libscws.swc", firstly, load the dictionary file and the rule file and supply them to the C module. This can be done in common CorssBridge/FlasCC routine: use a URLLoaderVFS's "loadManifest" function to load the manifest file, which contains the files' names and paths.(See the demo's source code for more details, for the manifest file, gives more information.) After the dictionary file and the rule file were loaded, call "initialize_SCWS_AS3()", which will initialize the library for use. Then you can call the function "scws_send_text_AS3(input:String):String", with the text to be processed as the parameter, and it will return the processed text, with space as delimiter.

Here is the demo(Input the texts at the bottom, Return Key for sending to the console.):

Full source code of the demo and the lib:


Saturday, October 19, 2013

Simple HTML Page Creator in AS3

I wrote this simple HTML page generation tool for the small flash game portal site: Just fill in some embedding parameters for the swf, such as the url, width and height, then it can produce the simple page for the game.

Nothing complicated here. UI is using MinimalComps and the AS3 code only does some string replacing for an HTML template.
Two things to note:
First, for multi line string in AS3 (e.g., the string variable in my code for holding the HTML template), we can use the "CDATA" tag:

private var myString:String = ( <![CDATA[
    Here is my string 
    that spans multiple 
    ]]> ).toString();

Second, the build-in AS3 string replace function can only replace the first match in the string. A custom replace function is needed for replacing all the occurrences:
  private function strReplace(str:String, search:String, replace:String):String
   return str.split(search).join(replace);
Finally, you can try the tool here:

And the source code:

Monday, September 2, 2013

Linux on Android Installation Notes

Recently when I was browsing google play, I found this app " Complete Linux Installer". Never did I expect it is so easy to install Linux system on Android phones. Just tested Ubuntu 10.10 on my old Android phone (lenovo lephone p700), and it works! Although the screen size is very small and the control is awkward, an almost fully functional Linux distribution was installed on the phone. And now I can use FireFox to browse the web, GIMP to draw and Emacs to edit texts, really cool!

Here are some notes for installing Linux on android phone (make sure your phone is rooted first):
1. Download and install the Complete Linux Installer app on Google Play:

2. Choose and download one Linux distribution on Android:
I chose Ubuntu 10.10(
After downloading, unzip and copy the file "ubuntu.img" to your phone's SD card.

3. Install Android Terminal Emulator:
and android-vnc-viewer:

4. Now, run the Complete Linux Installer app on your phone, go to Launch->Settings-Add, to add the file path of "ubuntu.img". After that, choose the added distribution from Launch,tap Start Linux.

5. Finally, open android-vnc-viewer, fill the Address with "localhost", Port"5900", Password "ubuntu", then tap "Connect". Now you're entering the Linux desktop.

solve the "Error: Unable to mount the loop device!" problem:

If you're trying ubuntu 10.10 on your android device and want to install some new software via the ubuntu software center:
How to install software or upgrade from old unsupported release?
Requires installation of untrusted packages?

Monday, May 20, 2013

Watercolor Brush - New Version Released for Android!

Here comes the updated version of my simple & free painting tool - Watercolor Brush. This new version is much smoother than the old one.
Build with AIR, this is my first AIR project for mobile platforms.
Newer version for web will come soon and I may also release an IOS version in the future.

Download the APK (v0.1):

Bugs, feedbacks, features requests are welcomed!

Finally, some notes for AIR mobile projets:

1. works on AIR, but you need some declaration in "application.xml" to enable the save function:

2. AIR Guesture events on mobile platforms:

3. Some optimization tips on AIR mobile performance:

The old version for WEB (Flash, outdated, new web version is coming soon):

Sunday, March 24, 2013

Fast way for per pixel bitmap manipulation in HaXe NME

I'm not sure whether this is the fastest way, but the nme.Memory API is something you must know for per pixel bitmap manipulation in NME. I learned the method from the stackoverflow thread here:
nme.Memory support both flash and cpp targets. For flash, it use the Alchemy fast memory opcodes which can greatly boost the speed.

I ported Ralph Hauwert's Alchemy lookup-table effects to NME as a simple example for showing you how to use nme.Memory API for per pixel bitmap manipulation.

The basic idea is to do everything using a ByteArray. Create a ByteArray to hold your screen buffer, select it and use the getI32/setI32 function of the nme.Memory API. One problem is when you need to use extra data, such as some texture buffer in the process, you may need another ByteArray to hold your data. Because "selecting different memory blocks in cycles may lead to a performance loss", as stated in the API's docs, the simple trick is to create a single ByteArray as the virtual RAM, and write everything into it, while store the different position variables of the data block for later use.

In my example, I use the first part of the virtual RAM ByteArray for screen buffer and the next part for holding the texture. So you can just use the "select" function only once and then get/set values from different data blocks by the position variables you stored as the offsets of the virtual RAM's addresses.

Source Code of the example:

For pure AS3 projects, the Azoth tool also provides similar memory API for utilizing the Alchemy opcodes. If you don't want any Alchemy opcodes, you may try Vectors in AS3.

Update 2013/04/11: ASC2 now supports using fast memory opcodes in AS3.
Update 2013/05/08: Only AIR SDK 3.6 support those fast opcodes, the latest SDK 3.7 won't compile. You can download the SDK 3.6 here:

Update 2013/05/07: This method also works on Android platform:
Try this pre-build apk:

Note: When compiling for Android, run "nme setup android" first, you will install Android SDK, Android NDK, Apache ANT and Java SDK.Then open Android SDK manager and install Android 2.2 (API 8), otherwise you may encounter the "Unable to resolve target 'android-8'" problem:

FlasCC version of the example:
Compile AS3 Fast Memory Opcodes by ASC2:

TIPS: There is no good tools for auto-formatting HaXe source code as far as I know. (FlashDevelop only supports AS3 formatting.) So I use Emacs. First rename the XXX.hx file to, open it in Emacs, C-x h (M-x mark-whole-buffer) C-M-\ (M-x indent-region), rename it back to XXX.hx, that's it.

Wednesday, January 2, 2013

FLASH DID IT! - Awesome Realistic Flash Effects

My collections of some awesome realistic flash experiments by various people.

Sea and Clouds

Volumetric Light


Water Caustics

Realistic Sunlight through Clouds

Andre Michelle's
Particle explosion

Water and rain


Gskinner's trees and vanes

ARSE i Am's growth

Head i Am head.swf

Wind blowing through hair

Interactive water AS3 experiment

Lightning / Thunderbolt / Electric discharge Class


Voxel head