The Boost C++ Libraries on a Mac

•September 28, 2008 • 4 Comments

So I’ve recently adventured into the the world of C++, attempting to write a homebrew documentation system. I soon discovered that I needed to access various features of the filesystem, a feature that is lacking in C++ without making the application heavily dependent on platform. Naturally I turned to the boost libraries, notably the filesystem library.

Typically, boost libraries are header only libraries so no source files or lib files have to be linked against, however, the filesystem library is one of those libraries that is an exception to that rule.

The instructions for building the boost libraries that require building is fairly straight forward if you know a little UNIX, however the latest release of boost has files riddled with CRLF (carriage return, line feeds) that are used in the Windows world to denote the end of a line in a file. So what you MUST do before the configure bash script will work as intended, is convert all CRLF characters to just LF characters. For building the filesystem library this includes the configure file, and the two .sh files in the tools/jam/ directory. After doing this, then follow the build instructions and be sure to use the

–prefix=my path and –with-libraries=filesystem

command line options when running configure.

Actionscript 3 Hidden Gem: addFrameScript

•July 28, 2008 • 1 Comment

So it’s no big secret any more that in Actionscript 3, the MovieClip class has a hidden method that enables developers to add frame scripts at runtime. However, with great power comes great responsability. It would help if this were documented in the Flash docs but it isn’t. If you don’t want your SWF files to crash when using this method you MUST ensure that the playhead is stopped before calling this method. I discovered this by experimenting with when, and what I could do with this method, and yes you can remove any frame scripts added at design-time or run-time (at least with the current version of Flash player). Actually, the playhead does not have to be stopped if you are setting a null frame script.

What I had to do to make using this method easier was to override and provide a few extra operations to MovieClip objects:

override public function play () : void
{
if (this.__playing)
return;

this.__playing = true;
super.play();
}

override public function gotoAndPlay (pFrame : Object, pScene : String = null) : void
{
this.__playing = true;
super.gotoAndPlay(pFrame, pScene);
}

override public function stop () : void
{
this.__playing = false;
super.stop();
}

override public function gotoAndStop (pFrame : Object, pScene : String = null) : void
{
this.__playing = false;
super.gotoAndStop(pFrame, pScene);
}

override public function addFrameScript (… pArgs : Array) : void
{
if (pArgs.length != 2)
return;

var frame : int = int(pArgs[0]);
var func : Function = pArgs[1] as Function;

if (this.__playing && func != null)
{
var nextFrame : int = this.currentFrame + 1;
this.stop();
super.addFrameScript(frame, func);
this.gotoAndPlay(nextFrame);
}
else
{
super.addFrameScript(frame, func);
}
}

This works great and my SWF files don’t crash anymore. Also one quick note on the arguments for the method. The accepted arguments are:

addFrameScript( pFrameIndex : int, pActionFunc : Function );

Keep in mind that the frame index is not the same as the frame number, and you can pass a null function to remove a frame script. As noted above, if the function is null then the playhead does not have to be stopped.

AS3 Library Headaches

•July 15, 2008 • Leave a Comment

I’ve been yanking my hairs out for the past few days regarding a few of my algorithms cracking. It turns out that a there area a few classes in the AS3 library that are a little tricky. Here are my findings so far:

Rectangle class
Negative Dimensions
This class works fine IFF you ensure your rectangles have positive dimensions. If you attempt to query the top,left,right or bottom properties from a rectangle with any dimension as being negative, then the class breaks and the values you get back will not be the values you would expect. In other words, if height is negative then top will actually return the physical bottom of the rectangle. One example where you may have rectangles with negative dimensions is drag regions.

This issue is not that bad if you were expecting it to behave like this, or if you anticipated this scenario and made the adjustments accordingly when say updating a drag region.

Here’s my test:

var rect : Rectangle = new Rectangle(0, 0, -50, -50);
trace(”rect = ” + rect);
trace(”\nrelative properties:”);
traceRect(rect);
trace(”\nrelative properties corrected (as they should be):”);
traceCorrectedRect(rect);

function traceRect (pRect : Rectangle) : void
{
trace(”top=” + pRect.top + ” left=” + pRect.left + ” bottom=” + pRect.bottom + ” right=” + pRect.right);
}

function traceCorrectedRect (pRect : Rectangle) : void
{
var t : Number = pRect.top;
var b : Number = pRect.bottom;
var l : Number = pRect.left;
var r : Number = pRect.right;

if (pRect.width < 0)
{
l = pRect.x + pRect.width;
r = pRect.x;
}

if (pRect.height < 0)
{
t = pRect.y + pRect.height;
b = pRect.y;
}

trace(”top=” + t + ” left=” + l + ” bottom=” + b + ” right=” + r);
}

Here’s the output:

rect = (x=0, y=0, w=-50, h=-50)

relative properties:
top=0 left=0 bottom=-50 right=-50

relative properties corrected (as they should be):
top=-50 left=-50 bottom=0 right=0

Setting Top Left
Another tricky area I ran into with the Rectangle class is when you go to set the top left corner of the rectangle. What actually happens here (and even when you set top and left individually) is the top and left get updated properly as expected but, the width and height are modified as well. What you would expect is that the top and left would just position your rectangle and not explode it. This isn’t so bad either so long as you use x and y to position your rectangle.

Here’s my test:

var rect : Rectangle = new Rectangle(50, 50, 50, 50);
var copy : Rectangle;
trace(”rect = ” + rect);

trace(”\nsetting top and left to 0″);
copy = rect.clone();
copy.top = 0;
copy.left = 0;
trace(”rect = ” + copy);

trace(”\nsetting topLeft to (0,0)”);
copy = rect.clone();
copy.topLeft = new Point(0, 0);
trace(”rect = ” + copy);

trace(”\nreseting the rect”);
copy = rect.clone();
trace(”rect = ” + copy);

trace(”\nsetting the x and y to 0″);
copy.x = 0;
copy.y = 0;
trace(”rect = ” + copy);

Here’s the output:

rect = (x=50, y=50, w=50, h=50)

setting top and left to 0
rect = (x=0, y=0, w=100, h=100)

setting topLeft to (0,0)
rect = (x=0, y=0, w=100, h=100)

reseting the rect
rect = (x=50, y=50, w=50, h=50)

setting the x and y to 0
rect = (x=0, y=0, w=50, h=50)

Array class

Array As Argument: splice

The splice operation is described in the docs as treating each element in an Array argument separately, and not just inserting the Array itself. This is not that case as it turns out.

Here’s my test:

trace(”Test several methods of the Array class with an Array as a parameter.\n”);

var array : Array = new Array(1,2,3);
var newArray : Array = new Array(4,5,6);
var copy : Array;

trace(”The following Arrays are used in the test:”);
trace(”array = ” + array);
trace(”newArray = ” + newArray);

trace(”\nIn each case the operation specified is performed on ‘array’ and ‘newArray’\n”+
“is passed as an argument to the operation.\n”);

trace(”\nconcat:”);
traceArray(array.concat(newArray));

trace(”\nsplice:”);
copy = copyArray(array);
copy.splice(3, 0, newArray)
traceArray(copy);

trace(”\nThese results show that the only operation that behaves like it is documented is the concat() operation.”);

function copyArray (pArray : Array) : Array
{
return (pArray.slice(0));
}

function traceArray (pArray : Array) : void
{
var len : int = int(pArray.length);
for (var w : int = 0; w < len; w++)
{
trace(”element [" + w + "] = ” + pArray[w]);
}
}

Here’s the output:

Test several methods of the Array class with an Array as a parameter.

The following Arrays are used in the test:
array = 1,2,3
newArray = 4,5,6

In each case the operation specified is performed on ‘array’ and ‘newArray’
is passed as an argument to the operation.

concat:
element [0] = 1
element [1] = 2
element [2] = 3
element [3] = 4
element [4] = 5
element [5] = 6

splice:
element [0] = 1
element [1] = 2
element [2] = 3
element [3] = 4,5,6

These results show that the only operation that behaves like it is documented is the concat() operation.

Comparing Objects in Actionscript [FINAL]

•April 18, 2008 • 2 Comments

Alright, I’ve posted a few posts in the past about comparing objects in Actionscript and this one will be the last one. This will cover two approaches that I have used when having to compare objects in Actionscript (after having experimented from my other two posts).

OOD Approach

This approach leverages OOD principles. Basically as per my first post on comparing objects, you start out by providing the function valueOf() in any class that is to be compared. The valueOf() method is part of the Object prototype and is one of the functions that does NOT require the override keyword preceding it when it is defined. The purpose of this method is to basically return the primitive value for an object, whatever that may be is it up to how the class is used. Generally speaking your valueOf() method will return a value with type String, Number, int, uint, or Boolean. Now you can use an object of your class type in any logic comparison expression:

Example:

MyClass.vlaueOf :
public function valueOf () : Object
{
return -1;
}
var myObj : MyClass = new MyClass();
trace(myObj < 0); // traces true

However, as per my second post, the equality operator does not work for this approach so to test for equality you will have to perform the following:

var isGTZero : Boolean = myObj > 0;
var isLTZero : Boolean = myObj < 0;
var isEqualToZero : Boolean = !isLTZero && !isGTZero;

Method Strategy Approach

This other approach does not rely on the OO principles of Actionscript. For this approach you simply define a method like the following:

function compare (objA : MyClass, objB : MyClass) : int
{
if (objA.age < objB.age)
return -1;
else if (objA.age > objB.age)
return 1;
else
return 0;
}

However, this approach requires a compare function to be written for each class type you want to compare. You could leverage the valueOf() method of objA and objB and compare the objects directly like in the OOD approach. However this could lead into issues further along the road.

Best of Both Worlds Approach

So to leverage both techniques so that we have a robust yet easy to use way to compare our objects I would recommend first defining an interface for comparable objects. The interface’s job is to cause each object that can be compared to define their valueOf() method.

IComparable interface:

public interface IComparable
{
function valueOf () : Object;
}

Now with just this interface alone we can guarantee that any object implementing this interface will have a custom valueOf() method defined and can be safely used in a logic expression. This is magnitudes safer than just assuming an object’s class defines the valueOf() method.

What if in AS4 the valueOf() method is deprecated or that the valueOf() method no longer works in logic expressions? All your code will be trash and will HAVE to be rewritten. To avoid this, I suggest using a compare interface that compares two IComparable objects.

ICompare interface:

public interface ICompare
{
function compare (objA : IComparable, objB : IComparable) : int;
}

The interface provides just one method, the compare method. The method will return -1 if objA precedes objB, 1 if objA proceeds objB and 0 if objA is equal to objB. Now we are free to implement the comparison anyway we want. We could do the following:

Compare class:

public class Compare implements ICompare
{
public function compare (objA : IComparable, objB : IComparable) : int
{
if (objA < objB)
return -1;
else if (objA > objB)
return 1;
else
return 0;
}
}

You then can use the ICompare interface as a parameter to a function that relies on comparing objects.

Example:

function getSmallestInList (compare : ICompare) : IComparable;

This is the safest way I can think of comparing objects. Is there a better way? Most likely. There is more than one way of skinning a cat.

This approach works wonders for my situation and I would assume that it will work for most cases.

Hope this sheds some light on the matter and helps you with your coding endeavors.

AS3 Namespaces

•April 18, 2008 • Leave a Comment

http://blog.tengerstudio.com/2008/04/13/namespace-and-uri/

Recently I was experimenting with AS3 namespaces and found it to be a pain in the neck, since it was behaving oddly. I was getting compile errors whenever I tried to qualify a class member function or variable with a public namespace. The culprit? ALWAYS GIVE YOUR NAMESPACES A UNIQUE URI! See the above link for an in-depth explanation. This link saved me from a massive headache. Thank you Tenergri!

All Girl Arcade

•April 15, 2008 • 1 Comment

AGAAll Girl Arcade (www.allgirlarcade.com) is a brand new gaming portal for girls aged 8 – 10 developed by Fuel Industries. It’s a completely free site with the goal of helping to fill the void of great kids content online. The site is based around the adventures of Fuel’s own original characters, the All Girl Star Squad, a trio of video game champions who, thanks to their gaming skills, become the unlikely heroes to a group of wacky space creatures trying to stop the evil Raveena from turning the universe into her own personal playground.

In addition to a collection of games based on this story, the site also features console and handheld reviews, a friends network, webisodes, and a ‘mall’ where players can spend the gems they win playing games. The more you play, the more additional content and gifts you can unlock! In a sea of make-up and shopping games for girls, All Girl Arcade stands out with a great story and unique characters which give girls a chance to carve their own space in the gaming world.

Games As Stars

•April 3, 2008 • Leave a Comment

I had a eureka moment earlier today while thinking about Nintendo’s latest Mario game, Mario Galaxy and why Nintendo always seems to get the “fun” part of their games perfect every time. My realization was that I hardly every played games for just pure enjoyment anymore. My play sessions usually have a purpose, whether it be for reference of some play mechanic or some art style, there is always a purpose. Very seldom do I take a game at face value and digest it as a piece of entertainment. I wondered if other developers experienced this same eureka moment or if I was the only developer who seldom played games for enjoyment.

There is a movie that demonstrates this point very well, albeit for a different subject matter. The movie was Men In Black. The scene was near the beginning of the film where D and K were sitting down after K had blasted Mikey, an alien refugee. The dialog goes like this:

“They’re beautiful aren’t they?”

“What?”

“The stars. We never just look anymore.”

It was strange how it all just clicked all at the same moment. I think I’m going to make a point to play for fun more often.

You can watch the scene I’m talking about here.

CartoonSmart

•April 2, 2008 • 1 Comment

http://cartoonsmart.com/

By far one of the best Flash tutorial sites I ever found. Managed by a very creative Flash designer, the site offers a load of courses on drawing, animation, particle effects and game creation. It also offers intro lessons to Flash for free, for you to get your feet wet.

There are a host of advanced courses that offer insight into complex animations, lip syncing, photo galleries, music players, all created in Flash.

New to the course roster is 3D using Blender. Finally! A site that offers an understandable course on Blender. Check it out and be sure to sign up for the newsletter to get updates every month of new content.

Design Patterns

•March 31, 2008 • Leave a Comment

http://www.dofactory.com/Patterns/Patterns.aspx

I found this little ditty a while back and i have come back to it so often I’ve lost count. It may not contain a comprehensive list of all design patterns in use today, but it sure does provide a solid foundation in the patterns that are used the most.

I’ve used this sight when designing systems in C++, Java and even Actionscript. Yes Actionscript. So whether you’re a hardcore coder that uses C++ or a Flash Developer like myself, you should find a pattern for your application.

Fisix Flash Physics Engine

•March 31, 2008 • Leave a Comment

http://www.fisixengine.com/default.asp

This looks to be a very viable and production ready physics framework for the Flash AS3 environment. The engine is free to use in any personal project but comes at a price for commercial projects.I haven’t had the time to inquire about the licensing costs for a commercial project, but I have had time to look over the documentation and I like what I see. I am aware of free AS3 physics engines like APE, but none has the practical documentation like Fisix.

I’ll post again once I’ve had a bit more time to try the framework out, and hopefully a few examples will follow.