00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "asVector2d.h"
00018
00019 #ifdef __COMPILE_WITH_ANGELSCRIPT__
00020
00021 #include "asIrrHelper.h"
00022
00023
00024
00028 template<typename T>
00029 void constructor(void *memory)
00030 {
00031 new (memory) vector2d<T>();
00032 }
00033
00037 template<typename T>
00038 void constructor(const vector2d<T> &other, void *memory)
00039 {
00040 new (memory) vector2d<T>(other);
00041 }
00042
00046 template<typename T>
00047 void constructor(T n, void *memory)
00048 {
00049 new (memory) vector2d<T>(n);
00050 }
00051
00055 template<typename T>
00056 void constructor(T x, T y, void *memory)
00057 {
00058 new (memory) vector2d<T>(x, y);
00059 }
00060
00064 template<typename T>
00065 void deconstructor(void *memory)
00066 {
00067 ((vector2d<T>*)memory)->~vector2d<T>();
00068 }
00069
00072 template<typename T>
00073 int compare(const vector2d<T> &a, const vector2d<T> &b)
00074 {
00075 if( a < b ) return -1;
00076 if( b > a ) return 1;
00077 return 0;
00078 }
00079
00080
00085 template<typename T>
00086 void bindVector2dT(asIScriptEngine *engine, const char *typeName, const char *asType)
00087 {
00088
00089 int r;
00090 std::stringstream ss;
00091
00092
00093 r = engine->RegisterObjectType(typeName, sizeof(vector2d<T>), asOBJ_VALUE | asOBJ_APP_CLASS_CDA); assert(r >= 0);
00094
00095
00096
00097 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_CONSTRUCT, "void f()", asFUNCTIONPR(constructor<T>,
00098 (void*), void), asCALL_CDECL_OBJLAST); assert(r >= 0);
00099
00100 ss.str("");
00101 ss << "void f(const " << typeName << "&in)";
00102 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_CONSTRUCT, ss.str().c_str(), asFUNCTIONPR(constructor<T>,
00103 (const vector2d<T>&, void*), void), asCALL_CDECL_OBJLAST); assert(r >= 0);
00104
00105 ss.str("");
00106 ss << "void f(" << asType << ")";
00107 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_CONSTRUCT, ss.str().c_str(), asFUNCTIONPR(constructor<T>,
00108 (T, void*), void), asCALL_CDECL_OBJLAST); assert(r >= 0);
00109
00110 ss.str("");
00111 ss << "void f(" << asType << ", " << asType << ")";
00112 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_CONSTRUCT, ss.str().c_str(), asFUNCTIONPR(constructor<T>,
00113 (T, T, void*), void), asCALL_CDECL_OBJLAST); assert(r >= 0);
00114
00115 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_DESTRUCT, "void f()", asFUNCTION(deconstructor<T>),
00116 asCALL_CDECL_OBJLAST); assert(r >= 0);
00117
00118
00119
00120 ss.str("");
00121 ss << typeName << "& opAssign(const " << typeName << " &in)";
00122 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00123 asMETHODPR(vector2d<T>, operator=, (const vector2d<T>&),
00124 vector2d<T>&), asCALL_THISCALL); assert(r >= 0);
00125
00126
00127 ss.str("");
00128 ss << typeName << " opMul(const " << typeName << " &in)";
00129 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00130 asFUNCTIONPR((multiply< vector2d<T>, vector2d<T>, vector2d<T> >),
00131 (const vector2d<T>&, const vector2d<T>&), vector2d<T>),
00132 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00133
00134 ss.str("");
00135 ss << typeName << " opMul(" << asType << ")";
00136 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00137 asFUNCTIONPR((multiply< vector2d<T>, vector2d<T>, T >),
00138 (const vector2d<T>&, const T&), vector2d<T>),
00139 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00140
00141
00142 ss.str("");
00143 ss << typeName << "& opMulAssign(const " << typeName << " &in)";
00144 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00145 asMETHODPR(vector2d<T>, operator*=, (const vector2d<T>&),
00146 vector2d<T>&), asCALL_THISCALL); assert(r >= 0);
00147 ss.str("");
00148 ss << typeName << "& opMulAssign(const " << asType << ")";
00149 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00150 asMETHODPR(vector2d<T>, operator*=, (const T), vector2d<T>&),
00151 asCALL_THISCALL); assert(r >= 0);
00152
00153
00154 ss.str("");
00155 ss << typeName << " opAdd(const " << typeName << " &in)";
00156 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00157 asFUNCTIONPR((add< vector2d<T>, vector2d<T>, vector2d<T> >),
00158 (const vector2d<T>&, const vector2d<T>&), vector2d<T>),
00159 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00160
00161 ss.str("");
00162 ss << typeName << " opAdd(" << asType << ")";
00163 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00164 asFUNCTIONPR((add< vector2d<T>, vector2d<T>, T >),
00165 (const vector2d<T>&, const T&), vector2d<T>),
00166 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00167
00168
00169 ss.str("");
00170 ss << typeName << "& opAddAssign(const " << typeName << " &in)";
00171 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00172 asMETHODPR(vector2d<T>, operator+=, (const vector2d<T>&),
00173 vector2d<T>&), asCALL_THISCALL); assert(r >= 0);
00174
00175 ss.str("");
00176 ss << typeName << "& opMulAssign(const " << asType << ")";
00177 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00178 asMETHODPR(vector2d<T>, operator+=, (const T), vector2d<T>&),
00179 asCALL_THISCALL); assert(r >= 0);
00180
00181
00182 ss.str("");
00183 ss << typeName << " opSub(const " << typeName << " &in)";
00184 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00185 asFUNCTIONPR((substract< vector2d<T>, vector2d<T>, vector2d<T> >),
00186 (const vector2d<T>&, const vector2d<T>&), vector2d<T>),
00187 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00188
00189 ss.str("");
00190 ss << typeName << " opSub(" << asType << ")";
00191 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00192 asFUNCTIONPR((substract< vector2d<T>, vector2d<T>, T >),
00193 (const vector2d<T>&, const T&), vector2d<T>),
00194 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00195
00196
00197 ss.str("");
00198 ss << typeName << "& opSubAssign(const " << typeName << " &in)";
00199 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00200 asMETHODPR(vector2d<T>, operator-=, (const vector2d<T>&),
00201 vector2d<T>&), asCALL_THISCALL); assert(r >= 0);
00202
00203 ss.str("");
00204 ss << typeName << "& opSubAssign(const " << asType << ")";
00205 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00206 asMETHODPR(vector2d<T>, operator-=, (const T), vector2d<T>&),
00207 asCALL_THISCALL); assert(r >= 0);
00208
00209
00210 ss.str("");
00211 ss << typeName << " opDiv(const " << typeName << " &in)";
00212 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00213 asFUNCTIONPR((divide< vector2d<T>, vector2d<T>, vector2d<T> >),
00214 (const vector2d<T>&, const vector2d<T>&), vector2d<T>),
00215 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00216
00217 ss.str("");
00218 ss << typeName << " opDiv(" << asType << ")";
00219 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00220 asFUNCTIONPR((divide< vector2d<T>, vector2d<T>, T >),
00221 (const vector2d<T>&, const T&), vector2d<T>),
00222 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00223
00224
00225 ss.str("");
00226 ss << typeName << "& opDivAssign(const " << typeName << " &in)";
00227 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00228 asMETHODPR(vector2d<T>, operator/=, (const vector2d<T>&),
00229 vector2d<T>&), asCALL_THISCALL); assert(r >= 0);
00230
00231 ss.str("");
00232 ss << typeName << "& opDivAssign(const " << asType << ")";
00233 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00234 asMETHODPR(vector2d<T>, operator/=, (const T), vector2d<T>&),
00235 asCALL_THISCALL); assert(r >= 0);
00236
00237
00238 ss.str("");
00239 ss << "bool opEquals(const " << typeName << " &in)";
00240 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(), asFUNCTIONPR(equal< vector2d<T> >,
00241 (const vector2d<T>&, const vector2d<T>&), bool), asCALL_CDECL_OBJFIRST); assert(r >= 0);
00242
00243 ss.str("");
00244 ss << "bool opCmp(const " << typeName << " &in)";
00245 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(), asFUNCTIONPR(compare< T >,
00246 (const vector2d<T>&, const vector2d<T>&), int), asCALL_CDECL_OBJFIRST); assert(r >= 0);
00247
00248
00249 ss.str("");
00250 ss << asType << " dotProduct(const " << typeName << " &in)";
00251 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00252 asMETHOD(vector2d<T>, dotProduct), asCALL_THISCALL); assert(r >= 0);
00253
00254 r = engine->RegisterObjectMethod(typeName, "f64 getAngle()",
00255 asMETHOD(vector2d<T>, getAngle), asCALL_THISCALL); assert(r >= 0);
00256
00257 r = engine->RegisterObjectMethod(typeName, "f64 getAngleTrig()",
00258 asMETHOD(vector2d<f64>, getAngleTrig), asCALL_THISCALL); assert(r >= 0);
00259
00260 ss.str("");
00261 ss << "f64 getAngleWith(const " << typeName << " &in)";
00262 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00263 asMETHOD(vector2d<T>, getAngleWith), asCALL_THISCALL); assert(r >= 0);
00264
00265 ss.str("");
00266 ss << asType << " getDistanceFrom(const " << typeName << " &in)";
00267 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00268 asMETHOD(vector2d<T>, getDistanceFrom), asCALL_THISCALL); assert(r >= 0);
00269
00270 ss.str("");
00271 ss << asType << " getDistanceFromSQ(const " << typeName << " &in)";
00272 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00273 asMETHOD(vector2d<T>, getDistanceFromSQ), asCALL_THISCALL); assert(r >= 0);
00274
00275 ss.str("");
00276 ss << typeName << " getInterpolated(const " << typeName << " &in, f64)";
00277 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00278 asMETHOD(vector2d<T>, getInterpolated), asCALL_THISCALL); assert(r >= 0);
00279
00280 ss.str("");
00281 ss << typeName << " getInterpolated_quadratic(const " << typeName << " &in, const " << typeName << "&in, f64)";
00282 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00283 asMETHOD(vector2d<T>, getInterpolated_quadratic), asCALL_THISCALL); assert(r >= 0);
00284
00285 ss.str("");
00286 ss << asType << " getLength()";
00287 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00288 asMETHOD(vector2d<T>, getLength), asCALL_THISCALL); assert(r >= 0);
00289
00290 ss.str("");
00291 ss << asType << " getLengthSQ()";
00292 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00293 asMETHOD(vector2d<T>, getLengthSQ), asCALL_THISCALL); assert(r >= 0);
00294
00295 ss.str("");
00296 ss << typeName << "& interpolate(const " << typeName << " &in, const " << typeName << "&in, f64)";
00297 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00298 asMETHOD(vector2d<T>, interpolate), asCALL_THISCALL); assert(r >= 0);
00299
00300 ss.str("");
00301 ss << "bool isBetweenPoints(const " << typeName << " &in, const " << typeName << "&in)";
00302 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00303 asMETHOD(vector2d<T>, isBetweenPoints), asCALL_THISCALL); assert(r >= 0);
00304
00305 ss.str("");
00306 ss << typeName << "& normalize()";
00307 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00308 asMETHOD(vector2d<T>, normalize), asCALL_THISCALL); assert(r >= 0);
00309
00310 ss.str("");
00311 ss << typeName << "& rotateBy(f64, const " << typeName << "&in)";
00312 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00313 asMETHOD(vector2d<T>, rotateBy), asCALL_THISCALL); assert(r >= 0);
00314
00315
00316 ss.str("");
00317 ss << asType << " X";
00318 r = engine->RegisterObjectProperty(typeName, ss.str().c_str(), offsetof(vector2d<T>, X)); assert(r >= 0);
00319
00320 ss.str("");
00321 ss << asType << " Y";
00322 r = engine->RegisterObjectProperty(typeName, ss.str().c_str(), offsetof(vector2d<T>, Y)); assert(r >= 0);
00323 }
00324
00327 void bindVector2d(asIScriptEngine *engine)
00328 {
00329 bindVector2dT<f32>(engine, "vector2df", "f32");
00330 bindVector2dT<s32>(engine, "vector2di", "s32");
00331 }
00332
00335 std::string getVector2dName(const std::string &asType)
00336 {
00337 if(asType == "f32") return std::string("vector2df");
00338 if(asType == "s32") return std::string("vector2di");
00339 else return std::string("");
00340 }
00341
00342 #endif // __COMPILE_WITH_ANGELSCRIPT__
00343
00344