Differences

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

Link to this comparison view

Both sides previous revision Previous revision
manual:macros [2013/05/25 16:15]
daniel
manual:macros [2013/05/27 10:58] (current)
daniel
Line 1: Line 1:
 +====== Macros ======
  
 +Sparrow contains several handy preprocessor macros that can make your life easier.
 +
 +
 +===== 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 1 million). 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: [[Event Handling]]//​
  manual/macros.txt · Last modified: 2013/05/27 10:58 by daniel
 
Powered by DokuWiki