00001 #include <math.h>
00002 #include <stdint.h>
00003
00004 #ifdef __APPLE__
00005 #import "TargetConditionals.h"
00006 #endif
00007
00008 #if (TARGET_OS_IPHONE == 1) && (!defined CP_USE_CGPOINTS)
00009 #define CP_USE_CGPOINTS 1
00010 #endif
00011
00012 #ifdef CP_USE_CGPOINTS
00013 #if TARGET_OS_IPHONE
00014 #import <CoreGraphics/CGGeometry.h>
00015 #elif TARGET_OS_MAC
00016 #import <ApplicationServices/ApplicationServices.h>
00017 #endif
00018
00019 #if defined(__LP64__) && __LP64__
00020 #define CP_USE_DOUBLES 1
00021 #else
00022 #define CP_USE_DOUBLES 0
00023 #endif
00024 #endif
00025
00026 #ifndef CP_USE_DOUBLES
00027
00028 #define CP_USE_DOUBLES 1
00029 #endif
00030
00034
00035 #if CP_USE_DOUBLES
00036
00037
00038 typedef double cpFloat;
00039 #define cpfsqrt sqrt
00040 #define cpfsin sin
00041 #define cpfcos cos
00042 #define cpfacos acos
00043 #define cpfatan2 atan2
00044 #define cpfmod fmod
00045 #define cpfexp exp
00046 #define cpfpow pow
00047 #define cpffloor floor
00048 #define cpfceil ceil
00049 #else
00050 typedef float cpFloat;
00051 #define cpfsqrt sqrtf
00052 #define cpfsin sinf
00053 #define cpfcos cosf
00054 #define cpfacos acosf
00055 #define cpfatan2 atan2f
00056 #define cpfmod fmodf
00057 #define cpfexp expf
00058 #define cpfpow powf
00059 #define cpffloor floorf
00060 #define cpfceil ceilf
00061 #endif
00062
00063 #ifndef INFINITY
00064
00065 #ifdef _MSC_VER
00066 union MSVC_EVIL_FLOAT_HACK
00067 {
00068 unsigned __int8 Bytes[4];
00069 float Value;
00070 };
00071 static union MSVC_EVIL_FLOAT_HACK INFINITY_HACK = {{0x00, 0x00, 0x80, 0x7F}};
00072 #define INFINITY (INFINITY_HACK.Value)
00073 #endif
00074
00075 #ifdef __GNUC__
00076 #define INFINITY (__builtin_inf())
00077 #endif
00078
00079 #ifndef INFINITY
00080 #define INFINITY (1e1000)
00081 #endif
00082 #endif
00083
00084 #ifndef M_PI
00085 #define M_PI 3.14159265358979323846264338327950288
00086 #endif
00087
00088 #ifndef M_E
00089 #define M_E 2.71828182845904523536028747135266250
00090 #endif
00091
00092
00094 static inline cpFloat cpfmax(cpFloat a, cpFloat b)
00095 {
00096 return (a > b) ? a : b;
00097 }
00098
00100 static inline cpFloat cpfmin(cpFloat a, cpFloat b)
00101 {
00102 return (a < b) ? a : b;
00103 }
00104
00106 static inline cpFloat cpfabs(cpFloat f)
00107 {
00108 return (f < 0) ? -f : f;
00109 }
00110
00112 static inline cpFloat cpfclamp(cpFloat f, cpFloat min, cpFloat max)
00113 {
00114 return cpfmin(cpfmax(f, min), max);
00115 }
00116
00118 static inline cpFloat cpfclamp01(cpFloat f)
00119 {
00120 return cpfmax(0.0f, cpfmin(f, 1.0f));
00121 }
00122
00123
00124
00126 static inline cpFloat cpflerp(cpFloat f1, cpFloat f2, cpFloat t)
00127 {
00128 return f1*(1.0f - t) + f2*t;
00129 }
00130
00132 static inline cpFloat cpflerpconst(cpFloat f1, cpFloat f2, cpFloat d)
00133 {
00134 return f1 + cpfclamp(f2 - f1, -d, d);
00135 }
00136
00138 typedef uintptr_t cpHashValue;
00139
00140
00142 #ifdef CP_BOOL_TYPE
00143 typedef CP_BOOL_TYPE cpBool;
00144 #else
00145 typedef int cpBool;
00146 #endif
00147
00148 #ifndef cpTrue
00149
00150 #define cpTrue 1
00151 #endif
00152
00153 #ifndef cpFalse
00154
00155 #define cpFalse 0
00156 #endif
00157
00158 #ifdef CP_DATA_POINTER_TYPE
00159 typedef CP_DATA_POINTER_TYPE cpDataPointer;
00160 #else
00161
00162 typedef void * cpDataPointer;
00163 #endif
00164
00165 #ifdef CP_COLLISION_TYPE_TYPE
00166 typedef CP_COLLISION_TYPE_TYPE cpCollisionType;
00167 #else
00168
00169 typedef uintptr_t cpCollisionType;
00170 #endif
00171
00172 #ifdef CP_GROUP_TYPE
00173 typedef CP_GROUP_TYPE cpGroup;
00174 #else
00175
00176 typedef uintptr_t cpGroup;
00177 #endif
00178
00179 #ifdef CP_LAYERS_TYPE
00180 typedef CP_LAYERS_TYPE cpLayers;
00181 #else
00182
00183 typedef unsigned int cpLayers;
00184 #endif
00185
00186 #ifdef CP_TIMESTAMP_TYPE
00187 typedef CP_TIMESTAMP_TYPE cpTimestamp;
00188 #else
00189
00190 typedef unsigned int cpTimestamp;
00191 #endif
00192
00193 #ifndef CP_NO_GROUP
00194
00195 #define CP_NO_GROUP ((cpGroup)0)
00196 #endif
00197
00198 #ifndef CP_ALL_LAYERS
00199
00200 #define CP_ALL_LAYERS (~(cpLayers)0)
00201 #endif
00202
00203
00204
00205
00206 #ifdef CP_USE_CGPOINTS
00207 typedef CGPoint cpVect;
00208 #else
00209
00210
00211 typedef struct cpVect{cpFloat x,y;} cpVect;
00212 #endif
00213
00214