Macros

Sparrow contains several handy preprocessor macros that can make your life easier.

Autorelease-Pool

A part of Objective-C's memory management relies on so-called autorelease-pools. There are two macros that make their usage a little simpler. Instead of writing:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// ...
[pool release];

you can use the following code:

SP_CREATE_POOL(pool);
// ...
SP_RELEASE_POOL(pool);

Beginning with iOS SDK 5, it's not recommended to use this macro any longer. Instead, use the new @autoreleasepool syntax:

@autoreleasepool 
{
    // ...
}

Angles

Sparrow expects all angles in radians (different to Flash, which uses degrees in some places and radians in others). To convert between degrees and radians, you can use the following simple macros.

float degrees = SP_R2D(PI);  // -> 180.0f
float radians = SP_D2R(180); // -> PI

Colors

In Sparrow, colors are specified in hexadecimal format. Here are a few samples:

// format:   0xRRGGBB
uint red   = 0xff0000;
uint green = 0x00ff00; // or 0xff00
uint blue  = 0x0000ff; // or 0xff
uint white = 0xffffff;
uint black = 0x000000; // or simply 0

If you're not comfortable with the hexadecimal format, there are macros that help you with that. The macro SP_COLOR expects the red, green and blue components in a range between 0 and 255.

uint yellow = SP_COLOR(255, 255, 0); // -> 0xffff00

Sometimes, you've got a color and want to find out the red, green and blue components. You can do that, too.

uint yellow = 0xffff00;
uint redPartOfYellow   = SP_COLOR_PART_RED(yellow);   // -> 255
uint greenPartOfYellow = SP_COLOR_PART_GREEN(yellow); // -> 255
uint bluePartOfYellow  = SP_COLOR_PART_BLUE(yellow);  // -> 0

Comparing floats

Be careful when you compare 2 float or double values. Due to the nature of this datatype, it might lead to unexpected results. Sparrow has a simple macro that compares if two floating point values are reasonably equal.

Beware, though, that this macro is best suited for small ranges (say, smaller than 1000). For larger values, the used delta value may be too small.

float f = 0.00001f;
bool isNearlyZero = SP_IS_FLOAT_EQUAL(f, 0.0f); // -> true

Next section: Pivot Points

  manual_v1/macros.txt · Last modified: 2013/03/05 10:19 (external edit)
 
Powered by DokuWiki