Differences

This shows you the differences between two versions of the page.

Link to this comparison view

manual_v1:macros [2013/03/05 10:19] (current)
Line 1: Line 1:
 +====== 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:
 +
 +<code objc>
 +NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 +// ...
 +[pool release];
 +</​code>​
 +
 +you can use the following code:
 +
 +<code objc>
 +SP_CREATE_POOL(pool);​
 +// ...
 +SP_RELEASE_POOL(pool);​
 +</​code>​
 +
 +Beginning with iOS SDK 5, it's not recommended to use this macro any longer. Instead, use the new ''​@autoreleasepool''​ syntax:
 +
 +<code objc>
 +@autoreleasepool ​
 +{
 +    // ...
 +}
 +</​code>​
 +==== 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.
 +
 +<code objc>
 +float degrees = SP_R2D(PI); ​ // -> 180.0f
 +float radians = SP_D2R(180);​ // -> PI
 +</​code>​
 +
 +==== Colors ====
 +
 +In Sparrow, colors are specified in hexadecimal format. Here are a few samples:
 +
 +<code objc>
 +// format: ​  ​0xRRGGBB
 +uint red   = 0xff0000;
 +uint green = 0x00ff00; // or 0xff00
 +uint blue  = 0x0000ff; // or 0xff
 +uint white = 0xffffff;
 +uint black = 0x000000; // or simply 0
 +</​code>​
 +
 +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.
 +
 +<code objc>
 +uint yellow = SP_COLOR(255,​ 255, 0); // -> 0xffff00
 +</​code>​
 +
 +Sometimes, you've got a color and want to find out the red, green and blue components. You can do that, too. 
 +
 +<code objc>
 +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
 +</​code>​
 +
 +==== 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.
 +
 +<code objc>
 +float f = 0.00001f;
 +bool isNearlyZero = SP_IS_FLOAT_EQUAL(f,​ 0.0f); // -> true
 +</​code>​
 +
 +-----
 +
 +//Next section: [[Pivot Points]]//
  manual_v1/macros.txt · Last modified: 2013/03/05 10:19 (external edit)
 
Powered by DokuWiki