PageRenderTime 78ms CodeModel.GetById 8ms app.highlight 66ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/visualizations/Vortex/angelscript/add_on/scriptmath3d/scriptmath3d.cpp

http://github.com/xbmc/xbmc
C++ | 331 lines | 254 code | 53 blank | 24 comment | 8 complexity | 9fbca77ae2d477931721271ada7cc421 MD5 | raw file
  1#include <assert.h>
  2#include <string.h> // strstr
  3#include <new> // new()
  4#include <math.h>
  5#include "scriptmath3d.h"
  6
  7BEGIN_AS_NAMESPACE
  8
  9Vector3::Vector3()
 10{
 11	x = 0;
 12	y = 0;
 13	z = 0;
 14}
 15
 16Vector3::Vector3(const Vector3 &other)
 17{
 18	x = other.x;
 19	y = other.y;
 20	z = other.z;
 21}
 22
 23Vector3::Vector3(float _x, float _y, float _z)
 24{
 25	x = _x;
 26	y = _y;
 27	z = _z;
 28}
 29
 30bool operator==(const Vector3 &a, const Vector3 &b)
 31{
 32	return (a.x == b.x) && (a.y == b.y) && (a.z == b.z);
 33}
 34
 35bool operator!=(const Vector3 &a, const Vector3 &b)
 36{
 37	return !(a == b);
 38}
 39
 40Vector3 &Vector3::operator=(const Vector3 &other)
 41{
 42	x = other.x;
 43	y = other.y;
 44	z = other.z;
 45	return *this;
 46}
 47
 48Vector3 &Vector3::operator+=(const Vector3 &other)
 49{
 50	x += other.x;
 51	y += other.y;
 52	z += other.z;
 53	return *this;
 54}
 55
 56Vector3 &Vector3::operator-=(const Vector3 &other)
 57{
 58	x -= other.x;
 59	y -= other.y;
 60	z -= other.z;
 61	return *this;
 62}
 63
 64Vector3 &Vector3::operator*=(float s)
 65{
 66	x *= s;
 67	y *= s;
 68	z *= s;
 69	return *this;
 70}
 71
 72Vector3 &Vector3::operator/=(float s)
 73{
 74	x /= s;
 75	y /= s;
 76	z /= s;
 77	return *this;
 78}
 79
 80float Vector3::length() const
 81{
 82	return sqrtf(x*x + y*y + z*z);
 83}
 84
 85Vector3 operator+(const Vector3 &a, const Vector3 &b)
 86{
 87	// Return a new object as a script handle
 88	Vector3 res(a.x + b.x, a.y + b.y, a.z + b.z);
 89	return res;
 90}
 91
 92Vector3 operator-(const Vector3 &a, const Vector3 &b)
 93{
 94	// Return a new object as a script handle
 95	Vector3 res(a.x - b.x, a.y - b.y, a.z - b.z);
 96	return res;
 97}
 98
 99Vector3 operator*(float s, const Vector3 &v)
100{
101	// Return a new object as a script handle
102	Vector3 res(v.x * s, v.y * s, v.z * s);
103	return res;
104}
105
106Vector3 operator*(const Vector3 &v, float s)
107{
108	// Return a new object as a script handle
109	Vector3 res(v.x * s, v.y * s, v.z * s);
110	return res;
111}
112
113Vector3 operator/(const Vector3 &v, float s)
114{
115	// Return a new object as a script handle
116	Vector3 res(v.x / s, v.y / s, v.z / s);
117	return res;
118}
119
120//-----------------------
121// AngelScript functions
122//-----------------------
123
124static void Vector3DefaultConstructor(Vector3 *self)
125{
126	new(self) Vector3();
127}
128
129static void Vector3CopyConstructor(const Vector3 &other, Vector3 *self)
130{
131	new(self) Vector3(other);
132}
133
134static void Vector3InitConstructor(float x, float y, float z, Vector3 *self)
135{
136	new(self) Vector3(x,y,z);
137}
138
139//-----------------------
140// Generic calling convention
141//-----------------------
142
143static void Vector3DefaultConstructor_Generic(asIScriptGeneric *gen)
144{
145	Vector3 *self = (Vector3*)gen->GetObject();
146	new(self) Vector3();
147}
148
149static void Vector3CopyConstructor_Generic(asIScriptGeneric *gen)
150{
151	Vector3 *other = (Vector3*)gen->GetArgObject(0);
152	Vector3 *self = (Vector3*)gen->GetObject();
153	Vector3CopyConstructor(*other, self);
154}
155
156static void Vector3InitConstructor_Generic(asIScriptGeneric *gen)
157{
158	float x = gen->GetArgFloat(0);
159	float y = gen->GetArgFloat(1);
160	float z = gen->GetArgFloat(2);
161	Vector3 *self = (Vector3*)gen->GetObject();
162	Vector3InitConstructor(x,y,z,self);
163}
164
165static void Vector3Equal_Generic(asIScriptGeneric *gen)
166{
167	Vector3 *a = (Vector3*)gen->GetObject();
168	Vector3 *b = (Vector3*)gen->GetArgAddress(0);
169	bool r = *a == *b;
170    *(bool*)gen->GetAddressOfReturnLocation() = r;
171}
172
173static void Vector3Length_Generic(asIScriptGeneric *gen)
174{
175	Vector3 *s = (Vector3*)gen->GetObject();
176	gen->SetReturnFloat(s->length());
177}
178
179static void Vector3AddAssign_Generic(asIScriptGeneric *gen)
180{
181	Vector3 *a = (Vector3*)gen->GetArgAddress(0);
182	Vector3 *thisPointer = (Vector3*)gen->GetObject();
183	*thisPointer += *a;
184	gen->SetReturnAddress(thisPointer);
185}
186
187static void Vector3SubAssign_Generic(asIScriptGeneric *gen)
188{
189	Vector3 *a = (Vector3*)gen->GetArgAddress(0);
190	Vector3 *thisPointer = (Vector3*)gen->GetObject();
191	*thisPointer -= *a;
192	gen->SetReturnAddress(thisPointer);
193}
194
195static void Vector3MulAssign_Generic(asIScriptGeneric *gen)
196{
197	float s = gen->GetArgFloat(0);
198	Vector3 *thisPointer = (Vector3*)gen->GetObject();
199	*thisPointer *= s;
200	gen->SetReturnAddress(thisPointer);
201}
202
203static void Vector3DivAssign_Generic(asIScriptGeneric *gen)
204{
205	float s = gen->GetArgFloat(0);
206	Vector3 *thisPointer = (Vector3*)gen->GetObject();
207	*thisPointer /= s;
208	gen->SetReturnAddress(thisPointer);
209}
210
211static void Vector3Add_Generic(asIScriptGeneric *gen)
212{
213	Vector3 *a = (Vector3*)gen->GetObject();
214	Vector3 *b = (Vector3*)gen->GetArgAddress(0);
215	Vector3 res = *a + *b;
216	gen->SetReturnObject(&res);
217}
218
219static void Vector3Sub_Generic(asIScriptGeneric *gen)
220{
221	Vector3 *a = (Vector3*)gen->GetObject();
222	Vector3 *b = (Vector3*)gen->GetArgAddress(0);
223	Vector3 res = *a - *b;
224	gen->SetReturnObject(&res);
225}
226
227static void Vector3FloatMulVector3_Generic(asIScriptGeneric *gen)
228{
229	float s = gen->GetArgFloat(0);
230	Vector3 *v = (Vector3*)gen->GetObject();
231	Vector3 res = s * *v;
232	gen->SetReturnObject(&res);
233}
234
235static void Vector3Vector3MulFloat_Generic(asIScriptGeneric *gen)
236{
237	Vector3 *v = (Vector3*)gen->GetObject();
238	float s = gen->GetArgFloat(0);
239	Vector3 res = *v * s;
240	gen->SetReturnObject(&res);
241}
242
243static void Vector3Vector3DivFloat_Generic(asIScriptGeneric *gen)
244{
245	Vector3 *v = (Vector3*)gen->GetObject();
246	float s = gen->GetArgFloat(0);
247	Vector3 res = *v / s;
248	gen->SetReturnObject(&res);
249}
250
251//--------------------------------
252// Registration
253//-------------------------------------
254
255void RegisterScriptMath3D_Native(asIScriptEngine *engine)
256{
257	int r;
258
259	// Register the type
260	r = engine->RegisterObjectType("vector3", sizeof(Vector3), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CA); assert( r >= 0 );
261
262	// Register the object properties
263	r = engine->RegisterObjectProperty("vector3", "float x", offsetof(Vector3, x)); assert( r >= 0 );
264	r = engine->RegisterObjectProperty("vector3", "float y", offsetof(Vector3, y)); assert( r >= 0 );
265	r = engine->RegisterObjectProperty("vector3", "float z", offsetof(Vector3, z)); assert( r >= 0 );
266
267	// Register the constructors
268	r = engine->RegisterObjectBehaviour("vector3", asBEHAVE_CONSTRUCT,  "void f()",                     asFUNCTION(Vector3DefaultConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );
269	r = engine->RegisterObjectBehaviour("vector3", asBEHAVE_CONSTRUCT,  "void f(const vector3 &in)",       asFUNCTION(Vector3CopyConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );
270	r = engine->RegisterObjectBehaviour("vector3", asBEHAVE_CONSTRUCT,  "void f(float, float, float)",  asFUNCTION(Vector3InitConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );
271
272	// Register the operator overloads
273	r = engine->RegisterObjectMethod("vector3", "vector3 &opAddAssign(const vector3 &in)", asMETHODPR(Vector3, operator+=, (const Vector3 &), Vector3&), asCALL_THISCALL); assert( r >= 0 );
274	r = engine->RegisterObjectMethod("vector3", "vector3 &opSubAssign(const vector3 &in)", asMETHODPR(Vector3, operator-=, (const Vector3 &), Vector3&), asCALL_THISCALL); assert( r >= 0 );
275	r = engine->RegisterObjectMethod("vector3", "vector3 &opMulAssign(float)", asMETHODPR(Vector3, operator*=, (float), Vector3&), asCALL_THISCALL); assert( r >= 0 );
276	r = engine->RegisterObjectMethod("vector3", "vector3 &opDivAssign(float)", asMETHODPR(Vector3, operator/=, (float), Vector3&), asCALL_THISCALL); assert( r >= 0 );
277	r = engine->RegisterObjectMethod("vector3", "bool opEquals(const vector3 &in) const", asFUNCTIONPR(operator==, (const Vector3&, const Vector3&), bool), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
278	r = engine->RegisterObjectMethod("vector3", "vector3 opAdd(const vector3 &in) const", asFUNCTIONPR(operator+, (const Vector3&, const Vector3&), Vector3), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
279	r = engine->RegisterObjectMethod("vector3", "vector3 opSub(const vector3 &in) const", asFUNCTIONPR(operator-, (const Vector3&, const Vector3&), Vector3), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
280	r = engine->RegisterObjectMethod("vector3", "vector3 opMul(float) const", asFUNCTIONPR(operator*, (const Vector3&, float), Vector3), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
281	r = engine->RegisterObjectMethod("vector3", "vector3 opMul_r(float) const", asFUNCTIONPR(operator*, (float, const Vector3&), Vector3), asCALL_CDECL_OBJLAST); assert( r >= 0 );
282	r = engine->RegisterObjectMethod("vector3", "vector3 opDiv(float) const", asFUNCTIONPR(operator/, (const Vector3&, float), Vector3), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
283
284	// Register the object methods
285	r = engine->RegisterObjectMethod("vector3", "float length() const", asMETHOD(Vector3,length), asCALL_THISCALL); assert( r >= 0 );
286}
287
288void RegisterScriptMath3D_Generic(asIScriptEngine *engine)
289{
290	int r;
291
292	// Register the type
293	r = engine->RegisterObjectType("vector3", sizeof(Vector3), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CA); assert( r >= 0 );
294
295	// Register the object properties
296	r = engine->RegisterObjectProperty("vector3", "float x", offsetof(Vector3, x)); assert( r >= 0 );
297	r = engine->RegisterObjectProperty("vector3", "float y", offsetof(Vector3, y)); assert( r >= 0 );
298	r = engine->RegisterObjectProperty("vector3", "float z", offsetof(Vector3, z)); assert( r >= 0 );
299
300	// Register the constructors
301	r = engine->RegisterObjectBehaviour("vector3", asBEHAVE_CONSTRUCT,    "void f()",                     asFUNCTION(Vector3DefaultConstructor_Generic), asCALL_GENERIC); assert( r >= 0 );
302	r = engine->RegisterObjectBehaviour("vector3", asBEHAVE_CONSTRUCT,    "void f(const vector3 &in)",       asFUNCTION(Vector3CopyConstructor_Generic), asCALL_GENERIC); assert( r >= 0 );
303	r = engine->RegisterObjectBehaviour("vector3", asBEHAVE_CONSTRUCT,    "void f(float, float, float)", asFUNCTION(Vector3InitConstructor_Generic), asCALL_GENERIC); assert( r >= 0 );
304
305	// Register the operator overloads
306	r = engine->RegisterObjectMethod("vector3", "vector3 &opAddAssign(const vector3 &in)", asFUNCTION(Vector3AddAssign_Generic), asCALL_GENERIC); assert( r >= 0 );
307	r = engine->RegisterObjectMethod("vector3", "vector3 &opSubAssign(const vector3 &in)", asFUNCTION(Vector3SubAssign_Generic), asCALL_GENERIC); assert( r >= 0 );
308	r = engine->RegisterObjectMethod("vector3", "vector3 &opMulAssign(float)", asFUNCTION(Vector3MulAssign_Generic), asCALL_GENERIC); assert( r >= 0 );
309	r = engine->RegisterObjectMethod("vector3", "vector3 &opDivAssign(float)", asFUNCTION(Vector3DivAssign_Generic), asCALL_GENERIC); assert( r >= 0 );
310	r = engine->RegisterObjectMethod("vector3", "bool opEquals(const vector3 &in) const", asFUNCTION(Vector3Equal_Generic), asCALL_GENERIC); assert( r >= 0 );
311	r = engine->RegisterObjectMethod("vector3", "vector3 opAdd(const vector3 &in) const", asFUNCTION(Vector3Add_Generic), asCALL_GENERIC); assert( r >= 0 );
312	r = engine->RegisterObjectMethod("vector3", "vector3 opSub(const vector3 &in) const", asFUNCTION(Vector3Sub_Generic), asCALL_GENERIC); assert( r >= 0 );
313	r = engine->RegisterObjectMethod("vector3", "vector3 opMul_r(float) const", asFUNCTION(Vector3FloatMulVector3_Generic), asCALL_GENERIC); assert( r >= 0 );
314	r = engine->RegisterObjectMethod("vector3", "vector3 opMul(float) const", asFUNCTION(Vector3Vector3MulFloat_Generic), asCALL_GENERIC); assert( r >= 0 );
315	r = engine->RegisterObjectMethod("vector3", "vector3 opDiv(float) const", asFUNCTION(Vector3Vector3DivFloat_Generic), asCALL_GENERIC); assert( r >= 0 );
316
317	// Register the object methods
318	r = engine->RegisterObjectMethod("vector3", "float length() const", asFUNCTION(Vector3Length_Generic), asCALL_GENERIC); assert( r >= 0 );
319}
320
321void RegisterScriptMath3D(asIScriptEngine *engine)
322{
323	if( strstr(asGetLibraryOptions(), "AS_MAX_PORTABILITY") )
324		RegisterScriptMath3D_Generic(engine);
325	else
326		RegisterScriptMath3D_Native(engine);
327}
328
329END_AS_NAMESPACE
330
331