00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "asIrrHelper.h"
00018 #include "asVector3d.h"
00019
00020
00021
00025 template<typename T>
00026 void constructor(void *memory)
00027 {
00028 new (memory) vector3d<T>();
00029 }
00030
00034 template<typename T>
00035 void constructor(const vector3d<T> &other, void *memory)
00036 {
00037 new (memory) vector3d<T>(other);
00038 }
00039
00043 template<typename T>
00044 void constructor(T n, void *memory)
00045 {
00046 new (memory) vector3d<T>(n);
00047 }
00048
00052 template<typename T>
00053 void constructor(T x, T y, T z, void *memory)
00054 {
00055 new (memory) vector3d<T>(x, y, z);
00056 }
00057
00061 template<typename T>
00062 void deconstructor(void *memory)
00063 {
00064 ((vector3d<T>*)memory)->~vector3d<T>();
00065 }
00066
00067
00072 template<typename T>
00073 void bindVector3dT(asIScriptEngine *engine, const char *typeName, const char *asType)
00074 {
00075
00076 int r;
00077 std::stringstream ss;
00078
00079
00080 r = engine->RegisterObjectType(typeName, sizeof(vector3d<T>), asOBJ_VALUE | asOBJ_APP_CLASS_CDA); assert(r >= 0);
00081
00082
00083
00084 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_CONSTRUCT, "void f()", asFUNCTIONPR(constructor<T>,
00085 (void*), void), asCALL_CDECL_OBJLAST); assert(r >= 0);
00086
00087 ss.str("");
00088 ss << "void f(const " << typeName << "&in)";
00089 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_CONSTRUCT, ss.str().c_str(), asFUNCTIONPR(constructor<T>,
00090 (const vector3d<T>&, void*), void), asCALL_CDECL_OBJLAST); assert(r >= 0);
00091
00092 ss.str("");
00093 ss << "void f(" << asType << ")";
00094 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_CONSTRUCT, ss.str().c_str(), asFUNCTIONPR(constructor<T>,
00095 (T, void*), void), asCALL_CDECL_OBJLAST); assert(r >= 0);
00096
00097 ss.str("");
00098 ss << "void f(" << asType << ", " << asType << ", " << asType << ")";
00099 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_CONSTRUCT, ss.str().c_str(), asFUNCTIONPR(constructor<T>,
00100 (T, T, T, void*), void), asCALL_CDECL_OBJLAST); assert(r >= 0);
00101
00102 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_DESTRUCT, "void f()", asFUNCTION(deconstructor<T>),
00103 asCALL_CDECL_OBJLAST); assert(r >= 0);
00104
00105
00106 ss.str("");
00107 ss << typeName << " f(const " << typeName << " &in, const " << typeName << " &in)";
00108 std::string declActionType = ss.str();
00109
00110 ss.str("");
00111 ss << typeName << " f(const " << typeName << " &in, " << asType << ")";
00112 std::string declActionValue = ss.str();
00113
00114 ss.str("");
00115 ss << typeName << "& f(const " << typeName << " &in)";
00116 std::string declAssignType = ss.str();
00117
00118 ss.str("");
00119 ss << typeName << "& f(const " << asType << ")";
00120 std::string declAssignValue = ss.str();
00121
00122
00123 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_ASSIGNMENT, declAssignType.c_str(),
00124 asMETHODPR(vector3d<T>, operator=, (const vector3d<T>&),
00125 vector3d<T>&), asCALL_THISCALL); assert(r >= 0);
00126
00127
00128 r = engine->RegisterGlobalBehaviour(asBEHAVE_MULTIPLY, declActionType.c_str(),
00129 asFUNCTIONPR((multiply< vector3d<T>, vector3d<T>, vector3d<T> >),
00130 (const vector3d<T>&, const vector3d<T>&), vector3d<T>),
00131 asCALL_CDECL); assert(r >= 0);
00132 r = engine->RegisterGlobalBehaviour(asBEHAVE_MULTIPLY, declActionValue.c_str(),
00133 asFUNCTIONPR((multiply< vector3d<T>, vector3d<T>, T >),
00134 (const vector3d<T>&, const T&), vector3d<T>),
00135 asCALL_CDECL); assert(r >= 0);
00136
00137
00138 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_MUL_ASSIGN, declAssignType.c_str(),
00139 asMETHODPR(vector3d<T>, operator*=, (const vector3d<T>&),
00140 vector3d<T>&), asCALL_THISCALL); assert(r >= 0);
00141 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_MUL_ASSIGN, declAssignValue.c_str(),
00142 asMETHODPR(vector3d<T>, operator*=, (const T), vector3d<T>&),
00143 asCALL_THISCALL); assert(r >= 0);
00144
00145
00146 r = engine->RegisterGlobalBehaviour(asBEHAVE_ADD, declActionType.c_str(),
00147 asFUNCTIONPR((add< vector3d<T>, vector3d<T>, vector3d<T> >),
00148 (const vector3d<T>&, const vector3d<T>&), vector3d<T>),
00149 asCALL_CDECL); assert(r >= 0);
00150 r = engine->RegisterGlobalBehaviour(asBEHAVE_ADD, declActionValue.c_str(),
00151 asFUNCTIONPR((add< vector3d<T>, vector3d<T>, T >),
00152 (const vector3d<T>&, const T&), vector3d<T>),
00153 asCALL_CDECL); assert(r >= 0);
00154
00155
00156 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_ADD_ASSIGN, declAssignType.c_str(),
00157 asMETHODPR(vector3d<T>, operator+=, (const vector3d<T>&),
00158 vector3d<T>&), asCALL_THISCALL); assert(r >= 0);
00159 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_ADD_ASSIGN, declAssignValue.c_str(),
00160 asMETHODPR(vector3d<T>, operator+=, (const T), vector3d<T>&),
00161 asCALL_THISCALL); assert(r >= 0);
00162
00163
00164 r = engine->RegisterGlobalBehaviour(asBEHAVE_SUBTRACT, declActionType.c_str(),
00165 asFUNCTIONPR((substract< vector3d<T>, vector3d<T>, vector3d<T> >),
00166 (const vector3d<T>&, const vector3d<T>&), vector3d<T>),
00167 asCALL_CDECL); assert(r >= 0);
00168 r = engine->RegisterGlobalBehaviour(asBEHAVE_SUBTRACT, declActionValue.c_str(),
00169 asFUNCTIONPR((substract< vector3d<T>, vector3d<T>, T >),
00170 (const vector3d<T>&, const T&), vector3d<T>),
00171 asCALL_CDECL); assert(r >= 0);
00172
00173
00174 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_SUB_ASSIGN, declAssignType.c_str(),
00175 asMETHODPR(vector3d<T>, operator-=, (const vector3d<T>&),
00176 vector3d<T>&), asCALL_THISCALL); assert(r >= 0);
00177 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_SUB_ASSIGN, declAssignValue.c_str(),
00178 asMETHODPR(vector3d<T>, operator-=, (const T), vector3d<T>&),
00179 asCALL_THISCALL); assert(r >= 0);
00180
00181
00182 r = engine->RegisterGlobalBehaviour(asBEHAVE_DIVIDE, declActionType.c_str(),
00183 asFUNCTIONPR((divide< vector3d<T>, vector3d<T>, vector3d<T> >),
00184 (const vector3d<T>&, const vector3d<T>&), vector3d<T>),
00185 asCALL_CDECL); assert(r >= 0);
00186 r = engine->RegisterGlobalBehaviour(asBEHAVE_DIVIDE, declActionValue.c_str(),
00187 asFUNCTIONPR((divide< vector3d<T>, vector3d<T>, T >),
00188 (const vector3d<T>&, const T&), vector3d<T>),
00189 asCALL_CDECL); assert(r >= 0);
00190
00191
00192 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_DIV_ASSIGN, declAssignType.c_str(),
00193 asMETHODPR(vector3d<T>, operator/=, (const vector3d<T>&),
00194 vector3d<T>&), asCALL_THISCALL); assert(r >= 0);
00195 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_DIV_ASSIGN, declAssignValue.c_str(),
00196 asMETHODPR(vector3d<T>, operator/=, (const T), vector3d<T>&),
00197 asCALL_THISCALL); assert(r >= 0);
00198
00199
00200 ss.str("");
00201 ss << "bool f(const " << typeName << " &in, const " << typeName << " &in)";
00202 std::string declComparison = ss.str();
00203
00204 r = engine->RegisterGlobalBehaviour(asBEHAVE_EQUAL, declComparison.c_str(), asFUNCTIONPR(equal< vector3d<T> >,
00205 (const vector3d<T>&, const vector3d<T>&), bool), asCALL_CDECL); assert(r >= 0);
00206 r = engine->RegisterGlobalBehaviour(asBEHAVE_NOTEQUAL, declComparison.c_str(), asFUNCTIONPR(not_equal< vector3d<T> >,
00207 (const vector3d<T>&, const vector3d<T>&), bool), asCALL_CDECL); assert(r >= 0);
00208 r = engine->RegisterGlobalBehaviour(asBEHAVE_LESSTHAN, declComparison.c_str(), asFUNCTIONPR(less_than< vector3d<T> >,
00209 (const vector3d<T>&, const vector3d<T>&), bool), asCALL_CDECL); assert(r >= 0);
00210 r = engine->RegisterGlobalBehaviour(asBEHAVE_GREATERTHAN, declComparison.c_str(), asFUNCTIONPR(greater_than< vector3d<T> >,
00211 (const vector3d<T>&, const vector3d<T>&), bool), asCALL_CDECL); assert(r >= 0);
00212 r = engine->RegisterGlobalBehaviour(asBEHAVE_LEQUAL, declComparison.c_str(), asFUNCTIONPR(l_equal< vector3d<T> >,
00213 (const vector3d<T>&, const vector3d<T>&), bool), asCALL_CDECL); assert(r >= 0);
00214 r = engine->RegisterGlobalBehaviour(asBEHAVE_GEQUAL, declComparison.c_str(), asFUNCTIONPR(g_equal< vector3d<T> >,
00215 (const vector3d<T>&, const vector3d<T>&), bool), asCALL_CDECL); assert(r >= 0);
00216
00217
00218 ss.str("");
00219 ss << typeName << " crossProduct(const " << typeName << " &in)";
00220 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00221 asMETHOD(vector3d<T>, crossProduct), asCALL_THISCALL); assert(r >= 0);
00222
00223 ss.str("");
00224 ss << asType << " dotProduct(const " << typeName << " &in)";
00225 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00226 asMETHOD(vector3d<T>, dotProduct), asCALL_THISCALL); assert(r >= 0);
00227
00228 ss.str("");
00229 ss << asType << " getDistanceFrom(const " << typeName << " &in)";
00230 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00231 asMETHOD(vector3d<T>, getDistanceFrom), asCALL_THISCALL); assert(r >= 0);
00232
00233 ss.str("");
00234 ss << asType << " getDistanceFromSQ(const " << typeName << " &in)";
00235 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00236 asMETHOD(vector3d<T>, getDistanceFromSQ), asCALL_THISCALL); assert(r >= 0);
00237
00238 ss.str("");
00239 ss << typeName << " getHorizontalAngle()";
00240 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00241 asMETHOD(vector3d<T>, getHorizontalAngle), asCALL_THISCALL); assert(r >= 0);
00242
00243 ss.str("");
00244 ss << typeName << " getInterpolated(const " << typeName << " &in, f64)";
00245 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00246 asMETHOD(vector3d<T>, getInterpolated), asCALL_THISCALL); assert(r >= 0);
00247
00248 ss.str("");
00249 ss << typeName << " getInterpolated_quadratic(const " << typeName << " &in, const " << typeName << "&in, f64)";
00250 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00251 asMETHOD(vector3d<T>, getInterpolated_quadratic), asCALL_THISCALL); assert(r >= 0);
00252
00253 ss.str("");
00254 ss << asType << " getLength()";
00255 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00256 asMETHOD(vector3d<T>, getLength), asCALL_THISCALL); assert(r >= 0);
00257
00258 ss.str("");
00259 ss << asType << " getLengthSQ()";
00260 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00261 asMETHOD(vector3d<T>, getLengthSQ), asCALL_THISCALL); assert(r >= 0);
00262
00263 ss.str("");
00264 ss << typeName << "& interpolate(const " << typeName << " &in, const " << typeName << "&in, f64)";
00265 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00266 asMETHOD(vector3d<T>, interpolate), asCALL_THISCALL); assert(r >= 0);
00267
00268 ss.str("");
00269 ss << typeName << "& invert()";
00270 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00271 asMETHOD(vector3d<T>, invert), asCALL_THISCALL); assert(r >= 0);
00272
00273 ss.str("");
00274 ss << "bool isBetweenPoints(const " << typeName << " &in, const " << typeName << "&in)";
00275 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00276 asMETHOD(vector3d<T>, isBetweenPoints), asCALL_THISCALL); assert(r >= 0);
00277
00278 ss.str("");
00279 ss << typeName << "& normalize()";
00280 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00281 asMETHOD(vector2d<T>, normalize), asCALL_THISCALL); assert(r >= 0);
00282
00283 ss.str("");
00284 ss << "void rotateXYBy(f64, const " << typeName << "&in)";
00285 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00286 asMETHOD(vector3d<T>, rotateXYBy), asCALL_THISCALL); assert(r >= 0);
00287
00288 ss.str("");
00289 ss << "void rotateXZBy(f64, const " << typeName << "&in)";
00290 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00291 asMETHOD(vector3d<T>, rotateXZBy), asCALL_THISCALL); assert(r >= 0);
00292
00293 ss.str("");
00294 ss << "void rotateYZBy(f64, const " << typeName << "&in)";
00295 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00296 asMETHOD(vector3d<T>, rotateYZBy), asCALL_THISCALL); assert(r >= 0);
00297
00298 ss.str("");
00299 ss << "void rotationToDirection(const " << typeName << "&in)";
00300 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00301 asMETHOD(vector3d<T>, rotationToDirection), asCALL_THISCALL); assert(r >= 0);
00302
00303
00304 ss.str("");
00305 ss << asType << " X";
00306 r = engine->RegisterObjectProperty(typeName, ss.str().c_str(), offsetof(vector3d<T>, X)); assert(r >= 0);
00307
00308 ss.str("");
00309 ss << asType << " Y";
00310 r = engine->RegisterObjectProperty(typeName, ss.str().c_str(), offsetof(vector3d<T>, Y)); assert(r >= 0);
00311
00312 ss.str("");
00313 ss << asType << " Z";
00314 r = engine->RegisterObjectProperty(typeName, ss.str().c_str(), offsetof(vector3d<T>, Z)); assert(r >= 0);
00315 }
00316
00319 void bindVector3d(asIScriptEngine *engine)
00320 {
00321 bindVector3dT<f32>(engine, "vector3df", "f32");
00322 bindVector3dT<s32>(engine, "vector3di", "s32");
00323 }
00324
00327 std::string getVector3dName(const std::string &asType)
00328 {
00329 if(asType == "f32") return std::string("vector3df");
00330 if(asType == "s32") return std::string("vector3di");
00331 else return std::string("");
00332 }
00333
00334