NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
sptrtypes.h
Go to the documentation of this file.
00001 
00008 #pragma once
00009 
00010 // Standard Includes
00011 #include <cassert>
00012 
00013 // Library Includes
00014 #include <angelscript/sptrwrapper/as_smart_ptr_wrapper.h>
00015 
00016 // Local Includes
00017 #include "../sharedbase/Entity.h"
00018 
00019 // Forward Declarations
00020 
00021 // Typedefs
00022 
00023 // Functions
00024 
00025 // Helper functions
00026 
00027 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00028 template <typename T>
00029 void construct(void* address) {
00030   new (address) T();
00031 }
00032 
00033 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00034 template <typename T>
00035 void destroy(T* object) {
00036   object->~T();
00037 }
00038 
00039 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00040 template <typename T>
00041 void copy_construct(void* address, T* other) {
00042   new (address) T(*other);
00043 }
00044 
00045 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00046 template <typename T>
00047 void assign(T * lhs, T* rhs) {
00048   *lhs = *rhs;
00049 }
00050 
00051 
00052 // Registration functions
00053 
00054 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00055 void RegisterEntity(asIScriptEngine* const engine) {
00056   int ret = 0;
00057   
00058   // *NOTE: Works, just there's an issue:
00059   // In current scripts, Entity ent; creates an object.
00060   // With this it creates a shared_ptr with a value pointed at garbage.  I'd like it to point at nullptr, but that's another issue.
00061   // This problem means that scripts need to add an extra set of parens to call the ctor: Entity ent();
00062   // This breaks backwards compat.
00063   
00064   // Register Object
00065   ret = engine->RegisterObjectType("Entity", sizeof(EntitySPTR), asOBJ_VALUE | asOBJ_APP_CLASS_CDAK); assert(ret >= 0);
00066   
00067   // Register behaviors and operations
00068   ret = engine->RegisterObjectBehaviour("Entity", asBEHAVE_CONSTRUCT,  "void f()",                                asFUNCTIONPR(Entity::FactoryAtAddress,(void*), void),   asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00069   ret = engine->RegisterObjectBehaviour("Entity", asBEHAVE_CONSTRUCT,  "void f(const string& in)",                asFUNCTIONPR(Entity::FactoryAtAddress,(void*, const std::string&), void),   asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00070   ret = engine->RegisterObjectBehaviour("Entity", asBEHAVE_CONSTRUCT,  "void f(const string& in, const int& in)", asFUNCTIONPR(Entity::FactoryAtAddress,(void*, const std::string&, const int&), void),   asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00071   ret = engine->RegisterObjectBehaviour("Entity", asBEHAVE_CONSTRUCT,  "void f(const Entity& in)", asFUNCTION(copy_construct<EntitySPTR>), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00072   ret = engine->RegisterObjectBehaviour("Entity", asBEHAVE_DESTRUCT,   "void f()",                 asFUNCTION(destroy<EntitySPTR>),        asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00073   ret = engine->RegisterObjectMethod("Entity", "Entity &opAssign(const Entity &in)", asFUNCTION(assign<EntitySPTR>), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00074   
00075   // Register properties
00076   ret = engine->RegisterObjectMethod("Entity", "const int& get_id() const",  REF_GETTER(Entity, id), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00077   ret = engine->RegisterObjectMethod("Entity", "void set_id(const int& in)", REF_SETTER(Entity, id), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00078   
00079   ret = engine->RegisterObjectMethod("Entity", "PropertyMap& get_properties() const",  REF_GETTER(Entity, properties), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00080   ret = engine->RegisterObjectMethod("Entity", "void set_properties(const PropertyMap& in)", REF_SETTER(Entity, properties), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00081   
00082   ret = engine->RegisterObjectMethod("Entity", "float& get_scale() const",  REF_GETTER(Entity, scale), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00083   ret = engine->RegisterObjectMethod("Entity", "void set_scale(const float& in)", REF_SETTER(Entity, scale), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00084   
00085   ret = engine->RegisterObjectMethod("Entity", "Vector& get_positionOffset() const",  REF_GETTER(Entity, location), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00086   ret = engine->RegisterObjectMethod("Entity", "void set_positionOffset(const Vector& in)", REF_SETTER(Entity, location), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00087   
00088   ret = engine->RegisterObjectMethod("Entity", "Rotation& get_rotationOffset() const",  REF_GETTER(Entity, rotation), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00089   ret = engine->RegisterObjectMethod("Entity", "void set_rotationOffset(const Rotation& in)", REF_SETTER(Entity, rotation), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00090   
00091   // Register methods
00092   ret = engine->RegisterObjectMethod("Entity", "void SetParent(Entity)", CALLER(Entity, SetParent),     asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00093   ret = engine->RegisterObjectMethod("Entity", "Entity GetParent()",     CALLER(Entity, GetParent),     asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00094   
00095   ret = engine->RegisterObjectMethod("Entity", "void ChangeScale(float)", CALLER(Entity, ChangeScale),   asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00096   ret = engine->RegisterObjectMethod("Entity", "float GetWorldScale()",   CALLER(Entity, GetWorldScale), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00097   
00098   ret = engine->RegisterObjectMethod("Entity", "Vector GetWorldPosition()", CALLER(Entity, GetWorldPosition), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00099   ret = engine->RegisterObjectMethod("Entity", "void SetWorldPosition(float, float, float)", CALLER(Entity, SetWorldPosition), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00100   ret = engine->RegisterObjectMethod("Entity", "void ChangePosition(Vector)", CALLER(Entity, ChangePosition), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00101   
00102   ret = engine->RegisterObjectMethod("Entity", "Rotation GetWorldRotation()", CALLER(Entity, GetWorldRotation), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00103   ret = engine->RegisterObjectMethod("Entity", "void SetRotation(float, float, float)", CALLER_PR(Entity, SetRotation, (float, float, float), void), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00104   ret = engine->RegisterObjectMethod("Entity", "void ChangeRotation(float, float, float)", CALLER_PR(Entity, ChangeRotation, (float, float, float), void), asCALL_CDECL_OBJFIRST); assert(ret >= 0);
00105 }