00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "asIrrHelper.h"
00018 #include "asVector2d.h"
00019
00020
00021
00025 template<typename T>
00026 void constructor(void *memory)
00027 {
00028 new (memory) vector2d<T>();
00029 }
00030
00034 template<typename T>
00035 void constructor(const vector2d<T> &other, void *memory)
00036 {
00037 new (memory) vector2d<T>(other);
00038 }
00039
00043 template<typename T>
00044 void constructor(T n, void *memory)
00045 {
00046 new (memory) vector2d<T>(n);
00047 }
00048
00052 template<typename T>
00053 void constructor(T x, T y, void *memory)
00054 {
00055 new (memory) vector2d<T>(x, y);
00056 }
00057
00061 template<typename T>
00062 void deconstructor(void *memory)
00063 {
00064 ((vector2d<T>*)memory)->~vector2d<T>();
00065 }
00066
00067
00072 template<typename T>
00073 void bindVector2dT(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(vector2d<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 vector2d<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 << ")";
00099 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_CONSTRUCT, ss.str().c_str(), asFUNCTIONPR(constructor<T>,
00100 (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(vector2d<T>, operator=, (const vector2d<T>&),
00125 vector2d<T>&), asCALL_THISCALL); assert(r >= 0);
00126
00127
00128 r = engine->RegisterGlobalBehaviour(asBEHAVE_MULTIPLY, declActionType.c_str(),
00129 asFUNCTIONPR((multiply< vector2d<T>, vector2d<T>, vector2d<T> >),
00130 (const vector2d<T>&, const vector2d<T>&), vector2d<T>),
00131 asCALL_CDECL); assert(r >= 0);
00132 r = engine->RegisterGlobalBehaviour(asBEHAVE_MULTIPLY, declActionValue.c_str(),
00133 asFUNCTIONPR((multiply< vector2d<T>, vector2d<T>, T >),
00134 (const vector2d<T>&, const T&), vector2d<T>),
00135 asCALL_CDECL); assert(r >= 0);
00136
00137
00138 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_MUL_ASSIGN, declAssignType.c_str(),
00139 asMETHODPR(vector2d<T>, operator*=, (const vector2d<T>&),
00140 vector2d<T>&), asCALL_THISCALL); assert(r >= 0);
00141 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_MUL_ASSIGN, declAssignValue.c_str(),
00142 asMETHODPR(vector2d<T>, operator*=, (const T), vector2d<T>&),
00143 asCALL_THISCALL); assert(r >= 0);
00144
00145
00146 r = engine->RegisterGlobalBehaviour(asBEHAVE_ADD, declActionType.c_str(),
00147 asFUNCTIONPR((add< vector2d<T>, vector2d<T>, vector2d<T> >),
00148 (const vector2d<T>&, const vector2d<T>&), vector2d<T>),
00149 asCALL_CDECL); assert(r >= 0);
00150 r = engine->RegisterGlobalBehaviour(asBEHAVE_ADD, declActionValue.c_str(),
00151 asFUNCTIONPR((add< vector2d<T>, vector2d<T>, T >),
00152 (const vector2d<T>&, const T&), vector2d<T>),
00153 asCALL_CDECL); assert(r >= 0);
00154
00155
00156 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_ADD_ASSIGN, declAssignType.c_str(),
00157 asMETHODPR(vector2d<T>, operator+=, (const vector2d<T>&),
00158 vector2d<T>&), asCALL_THISCALL); assert(r >= 0);
00159 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_ADD_ASSIGN, declAssignValue.c_str(),
00160 asMETHODPR(vector2d<T>, operator+=, (const T), vector2d<T>&),
00161 asCALL_THISCALL); assert(r >= 0);
00162
00163
00164 r = engine->RegisterGlobalBehaviour(asBEHAVE_SUBTRACT, declActionType.c_str(),
00165 asFUNCTIONPR((substract< vector2d<T>, vector2d<T>, vector2d<T> >),
00166 (const vector2d<T>&, const vector2d<T>&), vector2d<T>),
00167 asCALL_CDECL); assert(r >= 0);
00168 r = engine->RegisterGlobalBehaviour(asBEHAVE_SUBTRACT, declActionValue.c_str(),
00169 asFUNCTIONPR((substract< vector2d<T>, vector2d<T>, T >),
00170 (const vector2d<T>&, const T&), vector2d<T>),
00171 asCALL_CDECL); assert(r >= 0);
00172
00173
00174 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_SUB_ASSIGN, declAssignType.c_str(),
00175 asMETHODPR(vector2d<T>, operator-=, (const vector2d<T>&),
00176 vector2d<T>&), asCALL_THISCALL); assert(r >= 0);
00177 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_SUB_ASSIGN, declAssignValue.c_str(),
00178 asMETHODPR(vector2d<T>, operator-=, (const T), vector2d<T>&),
00179 asCALL_THISCALL); assert(r >= 0);
00180
00181
00182 r = engine->RegisterGlobalBehaviour(asBEHAVE_DIVIDE, declActionType.c_str(),
00183 asFUNCTIONPR((divide< vector2d<T>, vector2d<T>, vector2d<T> >),
00184 (const vector2d<T>&, const vector2d<T>&), vector2d<T>),
00185 asCALL_CDECL); assert(r >= 0);
00186 r = engine->RegisterGlobalBehaviour(asBEHAVE_DIVIDE, declActionValue.c_str(),
00187 asFUNCTIONPR((divide< vector2d<T>, vector2d<T>, T >),
00188 (const vector2d<T>&, const T&), vector2d<T>),
00189 asCALL_CDECL); assert(r >= 0);
00190
00191
00192 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_DIV_ASSIGN, declAssignType.c_str(),
00193 asMETHODPR(vector2d<T>, operator/=, (const vector2d<T>&),
00194 vector2d<T>&), asCALL_THISCALL); assert(r >= 0);
00195 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_DIV_ASSIGN, declAssignValue.c_str(),
00196 asMETHODPR(vector2d<T>, operator/=, (const T), vector2d<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< vector2d<T> >,
00205 (const vector2d<T>&, const vector2d<T>&), bool), asCALL_CDECL); assert(r >= 0);
00206 r = engine->RegisterGlobalBehaviour(asBEHAVE_NOTEQUAL, declComparison.c_str(), asFUNCTIONPR(not_equal< vector2d<T> >,
00207 (const vector2d<T>&, const vector2d<T>&), bool), asCALL_CDECL); assert(r >= 0);
00208 r = engine->RegisterGlobalBehaviour(asBEHAVE_LESSTHAN, declComparison.c_str(), asFUNCTIONPR(less_than< vector2d<T> >,
00209 (const vector2d<T>&, const vector2d<T>&), bool), asCALL_CDECL); assert(r >= 0);
00210 r = engine->RegisterGlobalBehaviour(asBEHAVE_GREATERTHAN, declComparison.c_str(), asFUNCTIONPR(greater_than< vector2d<T> >,
00211 (const vector2d<T>&, const vector2d<T>&), bool), asCALL_CDECL); assert(r >= 0);
00212 r = engine->RegisterGlobalBehaviour(asBEHAVE_LEQUAL, declComparison.c_str(), asFUNCTIONPR(l_equal< vector2d<T> >,
00213 (const vector2d<T>&, const vector2d<T>&), bool), asCALL_CDECL); assert(r >= 0);
00214 r = engine->RegisterGlobalBehaviour(asBEHAVE_GEQUAL, declComparison.c_str(), asFUNCTIONPR(g_equal< vector2d<T> >,
00215 (const vector2d<T>&, const vector2d<T>&), bool), asCALL_CDECL); assert(r >= 0);
00216
00217
00218 ss.str("");
00219 ss << asType << " dotProduct(const " << typeName << " &in)";
00220 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00221 asMETHOD(vector2d<T>, dotProduct), asCALL_THISCALL); assert(r >= 0);
00222
00223 r = engine->RegisterObjectMethod(typeName, "f64 getAngle()",
00224 asMETHOD(vector2d<T>, getAngle), asCALL_THISCALL); assert(r >= 0);
00225
00226 r = engine->RegisterObjectMethod(typeName, "f64 getAngleTrig()",
00227 asMETHOD(vector2d<T>, getAngleTrig), asCALL_THISCALL); assert(r >= 0);
00228
00229 ss.str("");
00230 ss << "f64 getAngleWith(const " << typeName << " &in)";
00231 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00232 asMETHOD(vector2d<T>, getAngleWith), asCALL_THISCALL); assert(r >= 0);
00233
00234 ss.str("");
00235 ss << asType << " getDistanceFrom(const " << typeName << " &in)";
00236 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00237 asMETHOD(vector2d<T>, getDistanceFrom), asCALL_THISCALL); assert(r >= 0);
00238
00239 ss.str("");
00240 ss << asType << " getDistanceFromSQ(const " << typeName << " &in)";
00241 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00242 asMETHOD(vector2d<T>, getDistanceFromSQ), asCALL_THISCALL); assert(r >= 0);
00243
00244 ss.str("");
00245 ss << typeName << " getInterpolated(const " << typeName << " &in, f64)";
00246 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00247 asMETHOD(vector2d<T>, getInterpolated), asCALL_THISCALL); assert(r >= 0);
00248
00249 ss.str("");
00250 ss << typeName << " getInterpolated_quadratic(const " << typeName << " &in, const " << typeName << "&in, f64)";
00251 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00252 asMETHOD(vector2d<T>, getInterpolated_quadratic), asCALL_THISCALL); assert(r >= 0);
00253
00254 ss.str("");
00255 ss << asType << " getLength()";
00256 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00257 asMETHOD(vector2d<T>, getLength), asCALL_THISCALL); assert(r >= 0);
00258
00259 ss.str("");
00260 ss << asType << " getLengthSQ()";
00261 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00262 asMETHOD(vector2d<T>, getLengthSQ), asCALL_THISCALL); assert(r >= 0);
00263
00264 ss.str("");
00265 ss << typeName << "& interpolate(const " << typeName << " &in, const " << typeName << "&in, f64)";
00266 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00267 asMETHOD(vector2d<T>, interpolate), asCALL_THISCALL); assert(r >= 0);
00268
00269 ss.str("");
00270 ss << "bool isBetweenPoints(const " << typeName << " &in, const " << typeName << "&in)";
00271 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00272 asMETHOD(vector2d<T>, isBetweenPoints), asCALL_THISCALL); assert(r >= 0);
00273
00274 ss.str("");
00275 ss << typeName << "& normalize()";
00276 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00277 asMETHOD(vector2d<T>, normalize), asCALL_THISCALL); assert(r >= 0);
00278
00279 ss.str("");
00280 ss << typeName << "& rotateBy(f64, const " << typeName << "&in)";
00281 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00282 asMETHOD(vector2d<T>, rotateBy), asCALL_THISCALL); assert(r >= 0);
00283
00284
00285 ss.str("");
00286 ss << asType << " X";
00287 r = engine->RegisterObjectProperty(typeName, ss.str().c_str(), offsetof(vector2d<T>, X)); assert(r >= 0);
00288
00289 ss.str("");
00290 ss << asType << " Y";
00291 r = engine->RegisterObjectProperty(typeName, ss.str().c_str(), offsetof(vector2d<T>, Y)); assert(r >= 0);
00292 }
00293
00296 void bindVector2d(asIScriptEngine *engine)
00297 {
00298 bindVector2dT<f32>(engine, "vector2df", "f32");
00299 bindVector2dT<s32>(engine, "vector2di", "s32");
00300 }
00301
00304 std::string getVector2dName(const std::string &asType)
00305 {
00306 if(asType == "f32") return std::string("vector2df");
00307 if(asType == "s32") return std::string("vector2di");
00308 else return std::string("");
00309 }
00310
00311