NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
customtypes.h
Go to the documentation of this file.
00001 
00006 #pragma once
00007 
00008 #include <assert.h>
00009 #include <angelscript.h>
00010 #include <d3dx9math.h>
00011 #include <NLTmx\NLTmxMap.h>
00012 
00013 void QuatFactory(void* memory) {
00014   new(memory) D3DXQUATERNION(0.0f, 0.0f, 0.0f, 1.0f);
00015 }
00016 
00017 void QuatFactory(const float& x, const float& y, const float& z, const float& w, void* memory) {
00018   new(memory) D3DXQUATERNION(x, y, z, w);
00019 }
00020 
00021 // Wrapper to convert calling convention from Win32's silly stdcall to a normal cdecl.
00022 D3DXQUATERNION* MyQuaternionRotationYawPitchRoll(
00023   __inout  D3DXQUATERNION *pOut,
00024   __in     FLOAT Yaw,
00025   __in     FLOAT Pitch,
00026   __in     FLOAT Roll
00027 ) {
00028   return D3DXQuaternionRotationYawPitchRoll(pOut, Yaw, Pitch, Roll);
00029 }
00030 
00031 void VectorFactory(void* memory) {
00032   new(memory) D3DXVECTOR3(0.0f, 0.0f, 0.0f);
00033 }
00034 
00035 void VectorFactory(const float& x, const float& y, const float& z, void* memory) {
00036   new(memory) D3DXVECTOR3(x, y, z);
00037 }
00038 
00039 D3DXVECTOR3 VectorApplyRotation(const D3DXVECTOR3& left, const D3DXQUATERNION& right) {
00040   D3DXVECTOR3 out(left);
00041   RotateVectorByQuaternion(&out, &right);
00042   return out;
00043 }
00044 
00045 D3DXVECTOR3 AddVectors(const D3DXVECTOR3& left, const D3DXVECTOR3& right) {
00046   return left + right;
00047 }
00048 
00049 D3DXVECTOR3 SubtractVectors(const D3DXVECTOR3& left, const D3DXVECTOR3& right) {
00050   return left - right;
00051 }
00052 
00053 float VectorMagnitudeSq(const D3DXVECTOR3& vector) {
00054   return D3DXVec3LengthSq(&vector);
00055 }
00056 
00057 float VectorMagnitude(const D3DXVECTOR3& vector) {
00058   return sqrt(VectorMagnitudeSq(vector));
00059 }
00060 
00061 D3DXVECTOR3 VectorNormalize(const D3DXVECTOR3& vector) {
00062   D3DXVECTOR3 out(vector);
00063   D3DXVec3Normalize(&out, &out);
00064   return out;
00065 }
00066 
00067 void ColorFactory(void* memory) {
00068   new(memory) D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.0f);
00069 }
00070 
00071 void ColorFactory(const float& r, const float& g, const float& b, const float& a, void* memory) {
00072   new(memory) D3DXCOLOR(r, g, b, a);
00073 }
00074 
00075 
00076 
00077 void RegisterCustomScriptTypes(asIScriptEngine *engine) {
00078   int ret = 0;
00079   
00080   // Quaternion rotations
00081   ret = engine->RegisterObjectType("Rotation", sizeof(D3DXQUATERNION), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CA); assert(ret >= 0);
00082   ret = engine->RegisterObjectBehaviour("Rotation", asBEHAVE_CONSTRUCT, "void f()", asFUNCTIONPR(QuatFactory, (void*), void), asCALL_CDECL_OBJLAST); assert(ret >= 0);
00083   ret = engine->RegisterObjectBehaviour("Rotation", asBEHAVE_CONSTRUCT, "void f(const float &in, const float &in, const float &in, const float &in)", asFUNCTIONPR(QuatFactory, (const float&, const float&, const float&, const float&, void*), void), asCALL_CDECL_OBJLAST); assert(ret >= 0);
00084   ret = engine->RegisterObjectProperty("Rotation", "float x", offsetof(D3DXQUATERNION, x)); assert(ret >= 0);
00085   ret = engine->RegisterObjectProperty("Rotation", "float y", offsetof(D3DXQUATERNION, y)); assert(ret >= 0);
00086   ret = engine->RegisterObjectProperty("Rotation", "float z", offsetof(D3DXQUATERNION, z)); assert(ret >= 0);
00087   ret = engine->RegisterObjectProperty("Rotation", "float w", offsetof(D3DXQUATERNION, w)); assert(ret >= 0);
00088   ret = engine->RegisterObjectMethod("Rotation", "Rotation SetYawPitchRoll(float, float, float)", asFUNCTION(MyQuaternionRotationYawPitchRoll), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00089   
00090   // 3D Vectors
00091   ret = engine->RegisterObjectType("Vector", sizeof(D3DXVECTOR3), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CA); assert(ret >= 0);
00092   ret = engine->RegisterObjectBehaviour("Vector", asBEHAVE_CONSTRUCT, "void f()", asFUNCTIONPR(VectorFactory, (void*), void), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00093   ret = engine->RegisterObjectBehaviour("Vector", asBEHAVE_CONSTRUCT, "void f(const float &in, const float &in, const float &in)", asFUNCTIONPR(VectorFactory, (const float&, const float&, const float&, void*), void), asCALL_CDECL_OBJLAST); assert(ret >= 0);
00094   ret = engine->RegisterObjectProperty("Vector", "float x", offsetof(D3DXVECTOR3, x)); assert(ret >= 0);
00095   ret = engine->RegisterObjectProperty("Vector", "float y", offsetof(D3DXVECTOR3, y)); assert(ret >= 0);
00096   ret = engine->RegisterObjectProperty("Vector", "float z", offsetof(D3DXVECTOR3, z)); assert(ret >= 0);
00097   ret = engine->RegisterObjectMethod("Vector", "Vector opMul(const Rotation &in) const",   asFUNCTION(VectorApplyRotation), asCALL_CDECL_OBJFIRST); assert( ret >= 0 );
00098   ret = engine->RegisterObjectMethod("Vector", "Vector opMul_r(const Rotation &in) const", asFUNCTION(VectorApplyRotation), asCALL_CDECL_OBJFIRST); assert( ret >= 0 );
00099   ret = engine->RegisterObjectMethod("Vector", "Vector opAdd(const Vector &in) const", asFUNCTION(AddVectors), asCALL_CDECL_OBJFIRST); assert( ret >= 0 );
00100   ret = engine->RegisterObjectMethod("Vector", "Vector opSub(const Vector &in) const", asFUNCTION(SubtractVectors), asCALL_CDECL_OBJFIRST); assert( ret >= 0 );
00101   ret = engine->RegisterObjectMethod("Vector", "float MagnitudeSq(const Vector &in) const", asFUNCTION(VectorMagnitudeSq), asCALL_CDECL_OBJFIRST); assert( ret >= 0 );
00102   ret = engine->RegisterObjectMethod("Vector", "float Magnitude(const Vector &in) const", asFUNCTION(VectorMagnitude), asCALL_CDECL_OBJFIRST); assert( ret >= 0 );
00103   ret = engine->RegisterObjectMethod("Vector", "Vector Normalize(const Vector &in) const", asFUNCTION(VectorNormalize), asCALL_CDECL_OBJFIRST); assert( ret >= 0 );
00104   
00105   // Color
00106   ret = engine->RegisterObjectType("Color", sizeof(D3DXCOLOR), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CA); assert(ret >= 0);
00107   ret = engine->RegisterObjectBehaviour("Color", asBEHAVE_CONSTRUCT, "void f()", asFUNCTIONPR(ColorFactory, (void*), void), asCALL_CDECL_OBJLAST); assert(ret >= 0);
00108   ret = engine->RegisterObjectBehaviour("Color", asBEHAVE_CONSTRUCT, "void f(const float &in, const float &in, const float &in, const float &in)", asFUNCTIONPR(ColorFactory, (const float&, const float&, const float&, const float&, void*), void), asCALL_CDECL_OBJLAST); assert(ret >= 0);
00109   ret = engine->RegisterObjectProperty("Color", "float r", offsetof(D3DXCOLOR, r)); assert(ret >= 0);
00110   ret = engine->RegisterObjectProperty("Color", "float g", offsetof(D3DXCOLOR, g)); assert(ret >= 0);
00111   ret = engine->RegisterObjectProperty("Color", "float b", offsetof(D3DXCOLOR, b)); assert(ret >= 0);
00112   ret = engine->RegisterObjectProperty("Color", "float a", offsetof(D3DXCOLOR, a)); assert(ret >= 0);
00113 
00114 
00115   // NLTmxMap Types
00116   // Tileset
00117   ret = engine->RegisterObjectType("NLTmxMapTileset", sizeof(NLTmxMapTileset), asOBJ_REF); assert(ret >= 0);
00118   ret = engine->RegisterObjectProperty("NLTmxMapTileset", "int firstGid", offsetof(NLTmxMapTileset, firstGid)); assert(ret >= 0);
00119   ret = engine->RegisterObjectProperty("NLTmxMapTileset", "string name", offsetof(NLTmxMapTileset, name)); assert(ret >= 0);
00120   ret = engine->RegisterObjectProperty("NLTmxMapTileset", "int filewidth", offsetof(NLTmxMapTileset, filewidth)); assert(ret >= 0);
00121   ret = engine->RegisterObjectProperty("NLTmxMapTileset", "int fileheight", offsetof(NLTmxMapTileset, fileheight)); assert(ret >= 0);
00122   ret = engine->RegisterObjectProperty("NLTmxMapTileset", "int tileWidth", offsetof(NLTmxMapTileset, tileWidth)); assert(ret >= 0);
00123   ret = engine->RegisterObjectProperty("NLTmxMapTileset", "int tileHeight", offsetof(NLTmxMapTileset, tileHeight)); assert(ret >= 0);
00124   ret = engine->RegisterObjectProperty("NLTmxMapTileset", "string filename", offsetof(NLTmxMapTileset, filename)); assert(ret >= 0);
00125   ret = engine->RegisterObjectBehaviour("NLTmxMapTileset", asBEHAVE_ADDREF, "void f()", asMETHOD(NLTmxMapTileset,AddRef), asCALL_THISCALL); assert( ret >= 0 );
00126   ret = engine->RegisterObjectBehaviour("NLTmxMapTileset", asBEHAVE_RELEASE, "void f()", asMETHOD(NLTmxMapTileset,Release), asCALL_THISCALL); assert( ret >= 0 );
00127 
00128   // Layer
00129   ret = engine->RegisterObjectType("NLTmxMapLayer", sizeof(NLTmxMapLayer), asOBJ_REF); assert(ret >= 0);
00130   ret = engine->RegisterObjectProperty("NLTmxMapLayer", "string name", offsetof(NLTmxMapLayer, name)); assert(ret >= 0);
00131   ret = engine->RegisterObjectProperty("NLTmxMapLayer", "int width", offsetof(NLTmxMapLayer, width)); assert(ret >= 0);
00132   ret = engine->RegisterObjectProperty("NLTmxMapLayer", "int height", offsetof(NLTmxMapLayer, height)); assert(ret >= 0);
00133   ret = engine->RegisterObjectMethod("NLTmxMapLayer", "int GetData(uint)", asMETHOD(NLTmxMapLayer, GetData), asCALL_THISCALL); assert( ret >= 0 );
00134   ret = engine->RegisterObjectBehaviour("NLTmxMapLayer", asBEHAVE_ADDREF, "void f()", asMETHOD(NLTmxMapLayer,AddRef), asCALL_THISCALL); assert( ret >= 0 );
00135   ret = engine->RegisterObjectBehaviour("NLTmxMapLayer", asBEHAVE_RELEASE, "void f()", asMETHOD(NLTmxMapLayer,Release), asCALL_THISCALL); assert( ret >= 0 );
00136 
00137   // Object
00138   ret = engine->RegisterObjectType("NLTmxMapObject", sizeof(NLTmxMapObject), asOBJ_REF); assert(ret >= 0);
00139   ret = engine->RegisterObjectProperty("NLTmxMapObject", "string name", offsetof(NLTmxMapObject, name)); assert(ret >= 0);
00140   ret = engine->RegisterObjectProperty("NLTmxMapObject", "int gid", offsetof(NLTmxMapObject, gid)); assert(ret >= 0);
00141   ret = engine->RegisterObjectProperty("NLTmxMapObject", "int x", offsetof(NLTmxMapObject, x)); assert(ret >= 0);
00142   ret = engine->RegisterObjectProperty("NLTmxMapObject", "int y", offsetof(NLTmxMapObject, y)); assert(ret >= 0);
00143   ret = engine->RegisterObjectProperty("NLTmxMapObject", "int width", offsetof(NLTmxMapObject, width)); assert(ret >= 0);
00144   ret = engine->RegisterObjectProperty("NLTmxMapObject", "int height", offsetof(NLTmxMapObject, height)); assert(ret >= 0);
00145   ret = engine->RegisterObjectProperty("NLTmxMapObject", "PropertyMap properties", offsetof(NLTmxMapObject, properties)); assert(ret >= 0);
00146   ret = engine->RegisterObjectBehaviour("NLTmxMapObject", asBEHAVE_ADDREF, "void f()", asMETHOD(NLTmxMapObject,AddRef), asCALL_THISCALL); assert( ret >= 0 );
00147   ret = engine->RegisterObjectBehaviour("NLTmxMapObject", asBEHAVE_RELEASE, "void f()", asMETHOD(NLTmxMapObject,Release), asCALL_THISCALL); assert( ret >= 0 );
00148 
00149   // ObjectGroup
00150   ret = engine->RegisterObjectType("NLTmxMapObjectGroup", sizeof(NLTmxMapObjectGroup), asOBJ_REF); assert(ret >= 0);
00151   ret = engine->RegisterObjectProperty("NLTmxMapObjectGroup", "string name", offsetof(NLTmxMapObjectGroup, name)); assert(ret >= 0);
00152   ret = engine->RegisterObjectProperty("NLTmxMapObjectGroup", "int width", offsetof(NLTmxMapObjectGroup, width)); assert(ret >= 0);
00153   ret = engine->RegisterObjectProperty("NLTmxMapObjectGroup", "int height", offsetof(NLTmxMapObjectGroup, height)); assert(ret >= 0);
00154   ret = engine->RegisterObjectProperty("NLTmxMapObjectGroup", "bool visible", offsetof(NLTmxMapObjectGroup, visible)); assert(ret >= 0);
00155   ret = engine->RegisterObjectMethod("NLTmxMapObjectGroup", "NLTmxMapObject@ FirstObject()", asMETHOD(NLTmxMapObjectGroup, FirstObject), asCALL_THISCALL); assert( ret >= 0 );
00156   ret = engine->RegisterObjectMethod("NLTmxMapObjectGroup", "NLTmxMapObject@ LastObject()", asMETHOD(NLTmxMapObjectGroup, LastObject), asCALL_THISCALL); assert( ret >= 0 );
00157   ret = engine->RegisterObjectMethod("NLTmxMapObjectGroup", "NLTmxMapObject@ NextObject()", asMETHOD(NLTmxMapObjectGroup, NextObject), asCALL_THISCALL); assert( ret >= 0 );
00158   ret = engine->RegisterObjectMethod("NLTmxMapObjectGroup", "NLTmxMapObject@ PrevObject()", asMETHOD(NLTmxMapObjectGroup, PrevObject), asCALL_THISCALL); assert( ret >= 0 );
00159   ret = engine->RegisterObjectBehaviour("NLTmxMapObjectGroup", asBEHAVE_ADDREF, "void f()", asMETHOD(NLTmxMapObjectGroup,AddRef), asCALL_THISCALL); assert( ret >= 0 );
00160   ret = engine->RegisterObjectBehaviour("NLTmxMapObjectGroup", asBEHAVE_RELEASE, "void f()", asMETHOD(NLTmxMapObjectGroup,Release), asCALL_THISCALL); assert( ret >= 0 );
00161 
00162   // Map
00163   ret = engine->RegisterObjectType("NLTmxMap", sizeof(NLTmxMap), asOBJ_REF); assert(ret >= 0);
00164   ret = engine->RegisterObjectProperty("NLTmxMap", "int width", offsetof(NLTmxMap, width)); assert(ret >= 0);
00165   ret = engine->RegisterObjectProperty("NLTmxMap", "int height", offsetof(NLTmxMap, height)); assert(ret >= 0);
00166   ret = engine->RegisterObjectProperty("NLTmxMap", "int tileWidth", offsetof(NLTmxMap, tileWidth)); assert(ret >= 0);
00167   ret = engine->RegisterObjectProperty("NLTmxMap", "int tileHeight", offsetof(NLTmxMap, tileHeight)); assert(ret >= 0);
00168   ret = engine->RegisterObjectProperty("NLTmxMap", "PropertyMap properties", offsetof(NLTmxMap, properties)); assert(ret >= 0);
00169   ret = engine->RegisterObjectMethod("NLTmxMap", "NLTmxMapObjectGroup@ FirstObjectGroup()", asMETHOD(NLTmxMap, FirstObjectGroup), asCALL_THISCALL); assert( ret >= 0 );
00170   ret = engine->RegisterObjectMethod("NLTmxMap", "NLTmxMapObjectGroup@ LastObjectGroup()", asMETHOD(NLTmxMap, LastObjectGroup), asCALL_THISCALL); assert( ret >= 0 );
00171   ret = engine->RegisterObjectMethod("NLTmxMap", "NLTmxMapObjectGroup@ NextObjectGroup()", asMETHOD(NLTmxMap, NextObjectGroup), asCALL_THISCALL); assert( ret >= 0 );
00172   ret = engine->RegisterObjectMethod("NLTmxMap", "NLTmxMapObjectGroup@ PrevObjectGroup()", asMETHOD(NLTmxMap, PrevObjectGroup), asCALL_THISCALL); assert( ret >= 0 );
00173   ret = engine->RegisterObjectMethod("NLTmxMap", "NLTmxMapLayer@ FirstLayer()", asMETHOD(NLTmxMap, FirstLayer), asCALL_THISCALL); assert( ret >= 0 );
00174   ret = engine->RegisterObjectMethod("NLTmxMap", "NLTmxMapLayer@ LastLayer()", asMETHOD(NLTmxMap, LastLayer), asCALL_THISCALL); assert( ret >= 0 );
00175   ret = engine->RegisterObjectMethod("NLTmxMap", "NLTmxMapLayer@ NextLayer()", asMETHOD(NLTmxMap, NextLayer), asCALL_THISCALL); assert( ret >= 0 );
00176   ret = engine->RegisterObjectMethod("NLTmxMap", "NLTmxMapLayer@ PrevLayer()", asMETHOD(NLTmxMap, PrevLayer), asCALL_THISCALL); assert( ret >= 0 );
00177   ret = engine->RegisterObjectMethod("NLTmxMap", "NLTmxMapTileset@ FirstTileset()", asMETHOD(NLTmxMap, FirstTileset), asCALL_THISCALL); assert( ret >= 0 );
00178   ret = engine->RegisterObjectMethod("NLTmxMap", "NLTmxMapTileset@ LastTileset()", asMETHOD(NLTmxMap, LastTileset), asCALL_THISCALL); assert( ret >= 0 );
00179   ret = engine->RegisterObjectMethod("NLTmxMap", "NLTmxMapTileset@ NextTileset()", asMETHOD(NLTmxMap, NextTileset), asCALL_THISCALL); assert( ret >= 0 );
00180   ret = engine->RegisterObjectMethod("NLTmxMap", "NLTmxMapTileset@ PrevTileset()", asMETHOD(NLTmxMap, PrevTileset), asCALL_THISCALL); assert( ret >= 0 );
00181   ret = engine->RegisterObjectBehaviour("NLTmxMap", asBEHAVE_ADDREF, "void f()", asMETHOD(NLTmxMap,AddRef), asCALL_THISCALL); assert( ret >= 0 );
00182   ret = engine->RegisterObjectBehaviour("NLTmxMap", asBEHAVE_RELEASE, "void f()", asMETHOD(NLTmxMap,Release), asCALL_THISCALL); assert( ret >= 0 );
00183 
00184   ret = engine->RegisterGlobalFunction("NLTmxMap@+ NLLoadTmxMap(string &in)", asFUNCTION(NLLoadTmxMap), asCALL_CDECL); assert( ret >= 0 );
00185   
00186 }