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 http://forums.adobe.com/thread/1147910. 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 http://www.adobe.com/devnet-docs/flascc/docs/Reference.html#section_vfs.
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 main.as 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:
https://flaswf.googlecode.com/svn/trunk/flaswfblog/Tutorials/CrossBridgesupplyFile/
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
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 (http://en.wikipedia.org/wiki/Text_segmentation). 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 http://www.xunsearch.com/scws/download.php. 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 http://forums.adobe.com/thread/1147910, 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, https://github.com/twistedjoe/flascc-URLLoaderVFS 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:
https://flaswf.googlecode.com/svn/trunk/LibSCWS
Links:
http://www.xunsearch.com/scws/
http://nlp.stanford.edu/software/segmenter.shtml
http://ictclas.org/index.html
http://technology.chtsai.org/mmseg/
http://www.coreseek.cn/opensource/
https://github.com/fxsjy/jieba
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: http://play.flaswf.tk. 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:
//http://dougmccune.com/blog/2007/05/15/multi-line-strings-in-actionscript-3/ private var myString:String = ( <![CDATA[ Here is my string that spans multiple lines. ]]> ).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:
//http://actionscriptsnips.blogspot.com/2009/07/search-and-replace.html private function strReplace(str:String, search:String, replace:String):String { return str.split(search).join(replace); }
Finally, you can try the tool here:
http://play.flaswf.tk/SubmitYourGame.html
And the source code:
http://flaswf.googlecode.com/svn/trunk/HTMLWebCreator/
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:
https://play.google.com/store/apps/details?id=com.zpwebsites.linuxonandroid
2. Choose and download one Linux distribution on Android:
http://sourceforge.net/projects/linuxonandroid/
I chose Ubuntu 10.10(ubuntuV5-image.zip):
http://sourceforge.net/projects/linuxonandroid/files/Ubuntu/10.10/
After downloading, unzip and copy the file "ubuntu.img" to your phone's SD card.
3. Install Android Terminal Emulator:
https://play.google.com/store/apps/details?id=jackpal.androidterm
and android-vnc-viewer:
https://play.google.com/store/apps/details?id=android.androidVNC
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.
Links:
http://www.linuxonandroid.org
solve the "Error: Unable to mount the loop device!" problem:
http://www.geekpeek.net/linux-on-android-2/
http://superuser.com/questions/220164/how-to-stop-the-d-key-from-minimizing-all-windows-in-ubuntu-10-10-when-connect
http://forum.linuxonandroid.org/index.php?topic=41.0
http://linuxonandroid.org/sudoroot-issues-how-to-fix/
http://sourceforge.net/p/linuxonandroid/wiki/Linux%20Image%20FAQ/
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?
http://askubuntu.com/questions/91815/how-to-install-software-or-upgrade-from-old-unsupported-release
Requires installation of untrusted packages?
http://askubuntu.com/questions/184117/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):
https://docs.google.com/file/d/0B5V2PrQ8xX_EeEszYlFNV0l0Y2s/edit?usp=sharing
Bugs, feedbacks, features requests are welcomed!
Finally, some notes for AIR mobile projets:
1. FileReference.save() works on AIR, but you need some declaration in "application.xml" to enable the save function:
http://richard-heck.blogspot.com/2011/01/how-to-write-file-to-android-filesystem.html
2. AIR Guesture events on mobile platforms:
http://paultrani.com/2011/02/touch-events-and-gestures-on-mobile/
http://www.flashandmath.com/mobile/zoompan/
3. Some optimization tips on AIR mobile performance:
http://www.andymoore.ca/2012/01/how-to-improve-your-mobile-as3air-performance/
The old version for WEB (Flash, outdated, new web version is coming soon):
http://bruce-lab.blogspot.com/2011/09/paint-online-with-chinese-water-color.html
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:
http://stackoverflow.com/questions/10157787/haxe-nme-fastest-method-for-per-pixel-bitmap-manipulation
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:
https://flaswf.googlecode.com/svn/trunk/flaswfblog/Tutorials/HXFastBitmapData
Binary:
(Win-32)https://flaswf.googlecode.com/svn/trunk/flaswfblog/Tutorials/HXFastBitmapData/bin/w32-bin.zip
(Flash)https://flaswf.googlecode.com/svn/trunk/flaswfblog/Tutorials/HXFastBitmapData/bin/HXFastBitmapData.swf
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:
http://helpx.adobe.com/air/kb/archived-air-sdk-version.html
http://helpx.adobe.com/flash-player/release-note/fp_116_air_36_release_notes.html
Update 2013/05/07: This method also works on Android platform:
Try this pre-build apk:
https://flaswf.googlecode.com/svn/trunk/flaswfblog/Tutorials/HXFastBitmapData/bin/HXFastBitmapData-debug.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:
http://www.nme.io/community/forums/installing-nme/unable-to-resolve-target-android-8/
Links:
http://haxe.org/api/flash/memory
http://www.nme.io/api/types/nme/Memory.html
http://stackoverflow.com/questions/10157787/haxe-nme-fastest-method-for-per-pixel-bitmap-manipulation
FlasCC version of the example: http://bruce-lab.blogspot.com/2012/12/migrating-from-alchemy-to-flascc.html
http://philippe.elsass.me/2010/05/as3-fast-memory-access-without-alchemy/
Compile AS3 Fast Memory Opcodes by ASC2:
http://obtw.wordpress.com/2013/04/03/making-bytearray-faster/
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 XXX.java, 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
Sea and Clouds
http://wonderfl.net/c/2HBv
Volumetric Light
http://wonderfl.net/c/kAfX
Cloudeffect
http://wonderfl.net/c/64jE
Water Caustics
http://wonderfl.net/c/nVWft
Realistic Sunlight through Clouds
http://wonderfl.net/c/dUzj
Andre Michelle's
Particle explosion
http://lab.andre-michelle.com/particle-explosion
Water and rain
http://lab.andre-michelle.com/rain
http://lab.andre-michelle.com/water
Fire
http://lab.andre-michelle.com/fire-flame
Gskinner's trees and vanes
http://incomplet.gskinner.com/index2.html#tree
http://incomplet.gskinner.com/index2.html#sakura2
http://gskinner.com/blog/archives/2008/01/elm_tree_branch.html
http://gskinner.com/blog/archives/2008/01/interactive_elm.html
http://incomplet.gskinner.com/index2.html#jungleg
http://gskinner.com/blog/archives/2008/01/branching_exper_1.html
ARSE i Am's growth
http://www.arseiam.com/projects/growth
Head
https://the-backup-project.googlecode.com/svn/trunk/swfs/ARSE i Am head.swf
Wind blowing through hair
http://asluv.com/2007/09/23/wind-in-the-hair-interactive-as3-experiment/
Interactive water AS3 experiment
http://asluv.com/2007/09/20/interactive-water-as3-experiment/
Lightning / Thunderbolt / Electric discharge Class
http://blog.oaxoa.com/2009/07/27/actionscript-3-as3-lightning-thunderbolt-electric-discharge-class/
Raytracer
http://www.strille.net/works/as3/raytracer/raytracer_f9.swf
Voxel head
http://mrdoob.com/lab/actionscript/effects/voxels/head/
Spider
http://www.onemotion.com/flash/spider/