From 5498084cde9381452e5757f39abfdb932e322352 Mon Sep 17 00:00:00 2001 From: Sachith Hasaranga Seneviratne Date: Sat, 7 Jun 2014 08:07:18 +0530 Subject: [PATCH] Bound Vec3 class to scripts --- data/scripts/added_script.as | 6 ++ src/scriptengine/script_engine.cpp | 9 ++- src/scriptengine/script_engine.hpp | 30 +++++---- src/scriptengine/script_kart.cpp | 9 ++- src/scriptengine/script_kart.hpp | 6 +- src/scriptengine/script_physics.cpp | 6 +- src/scriptengine/script_physics.hpp | 6 +- src/scriptengine/script_track.cpp | 6 +- src/scriptengine/script_track.hpp | 6 +- src/scriptengine/scriptarray.cpp | 2 +- .../{scriptarray.h => scriptarray.hpp} | 0 src/scriptengine/scriptstdstring.cpp | 2 +- ...{scriptstdstring.h => scriptstdstring.hpp} | 0 src/scriptengine/scriptstdstring_utils.cpp | 4 +- src/scriptengine/scriptvec3.cpp | 66 +++++++++++++++++++ src/scriptengine/scriptvec3.hpp | 30 +++++++++ 16 files changed, 155 insertions(+), 33 deletions(-) rename src/scriptengine/{scriptarray.h => scriptarray.hpp} (100%) rename src/scriptengine/{scriptstdstring.h => scriptstdstring.hpp} (100%) create mode 100644 src/scriptengine/scriptvec3.cpp create mode 100644 src/scriptengine/scriptvec3.hpp diff --git a/data/scripts/added_script.as b/data/scripts/added_script.as index b7f570d5c..033dddd5b 100644 --- a/data/scripts/added_script.as +++ b/data/scripts/added_script.as @@ -2,4 +2,10 @@ void onTrigger() { displayMessage("This trigger was added by another script"); jumpKartTo( 0, 67.90, 99.49 ); + Vec3 a; + Vec3 b; + b=a; + Vec3 c = Vec3(); + Vec3 d = Vec3(2,3,4); + printVec3(d); } diff --git a/src/scriptengine/script_engine.cpp b/src/scriptengine/script_engine.cpp index 76fa29be7..56183dc72 100644 --- a/src/scriptengine/script_engine.cpp +++ b/src/scriptengine/script_engine.cpp @@ -23,7 +23,8 @@ #include "karts/kart.hpp" #include "modes/world.hpp" #include "script_engine.hpp" -#include "scriptstdstring.h" +#include "scriptstdstring.hpp" +#include "scriptvec3.hpp" #include // strstr() #include "states_screens/dialogs/tutorial_message_dialog.hpp" #include "tracks/track_object_manager.hpp" @@ -33,7 +34,8 @@ using namespace Scripting; -namespace Scripting{ +namespace Scripting +{ ScriptEngine::ScriptEngine() { @@ -189,7 +191,8 @@ void ScriptEngine::configureEngine(asIScriptEngine *engine) int r; // Register the script string type - RegisterStdString(engine); + RegisterStdString(engine); //register std::string + RegisterVec3(engine); //register Vec3 Scripting::Track::registerScriptFunctions(m_engine); diff --git a/src/scriptengine/script_engine.hpp b/src/scriptengine/script_engine.hpp index 74f8bd7eb..5d35ce178 100644 --- a/src/scriptengine/script_engine.hpp +++ b/src/scriptengine/script_engine.hpp @@ -24,9 +24,11 @@ class TrackObjectPresentation; -namespace Scripting{ +namespace Scripting +{ - namespace Physics{ + namespace Physics + { void registerScriptFunctions(asIScriptEngine *engine); asIScriptFunction* registerScriptCallbacks(asIScriptEngine *engine); @@ -35,11 +37,13 @@ namespace Scripting{ void setCollision(std::string collider1, std::string collider2); } - namespace Kart{ + namespace Kart + { void registerScriptFunctions(asIScriptEngine *engine); } - namespace Track{ + namespace Track + { void registerScriptFunctions(asIScriptEngine *engine); asIScriptFunction* registerScriptCallbacks(asIScriptEngine *engine); @@ -47,21 +51,21 @@ namespace Scripting{ class ScriptEngine { -public: + public: - ScriptEngine(); - ~ScriptEngine(); + ScriptEngine(); + ~ScriptEngine(); - void runScript(std::string scriptName); + void runScript(std::string scriptName); -private: - asIScriptEngine *m_engine; + private: + asIScriptEngine *m_engine; - void configureEngine(asIScriptEngine *engine); - int compileScript(asIScriptEngine *engine,std::string scriptName); + void configureEngine(asIScriptEngine *engine); + int compileScript(asIScriptEngine *engine,std::string scriptName); -}; // class ScriptEngine + }; // class ScriptEngine } #endif diff --git a/src/scriptengine/script_kart.cpp b/src/scriptengine/script_kart.cpp index de820eea6..1861460ef 100644 --- a/src/scriptengine/script_kart.cpp +++ b/src/scriptengine/script_kart.cpp @@ -22,11 +22,15 @@ #include "modes/world.hpp" #include "script_kart.hpp" +//debug +#include -namespace Scripting{ +namespace Scripting +{ - namespace Kart{ + namespace Kart + { void squashKart(asIScriptGeneric *gen) { @@ -95,7 +99,6 @@ namespace Scripting{ r = engine->RegisterGlobalFunction("void teleportKart(int id, float x, float y,float z)", asFUNCTION(teleportKart), asCALL_GENERIC); assert(r >= 0); r = engine->RegisterGlobalFunction("void setVelocity(int id, float x, float y,float z)", asFUNCTION(setVelocity), asCALL_GENERIC); assert(r >= 0); r = engine->RegisterGlobalFunction("void jumpKartTo(int id, float x, float y)", asFUNCTION(jumpKartTo), asCALL_GENERIC); assert(r >= 0); - } } } diff --git a/src/scriptengine/script_kart.hpp b/src/scriptengine/script_kart.hpp index d5e689d92..b214e7d1e 100644 --- a/src/scriptengine/script_kart.hpp +++ b/src/scriptengine/script_kart.hpp @@ -21,9 +21,11 @@ #include -namespace Scripting{ +namespace Scripting +{ - namespace Kart{ + namespace Kart + { void registerScriptFunctions(asIScriptEngine *engine); diff --git a/src/scriptengine/script_physics.cpp b/src/scriptengine/script_physics.cpp index 9de04d0dd..87a353745 100644 --- a/src/scriptengine/script_physics.cpp +++ b/src/scriptengine/script_physics.cpp @@ -20,9 +20,11 @@ #include #include "script_physics.hpp" -namespace Scripting{ +namespace Scripting +{ - namespace Physics{ + namespace Physics + { void getCollidingKart1(asIScriptGeneric *gen) { diff --git a/src/scriptengine/script_physics.hpp b/src/scriptengine/script_physics.hpp index f5f5b23f6..e8fa17e7c 100644 --- a/src/scriptengine/script_physics.hpp +++ b/src/scriptengine/script_physics.hpp @@ -22,9 +22,11 @@ #include #include -namespace Scripting{ +namespace Scripting +{ - namespace Physics{ + namespace Physics + { //private: //IDs of kart collisions diff --git a/src/scriptengine/script_track.cpp b/src/scriptengine/script_track.cpp index 5d1a25de2..7631755be 100644 --- a/src/scriptengine/script_track.cpp +++ b/src/scriptengine/script_track.cpp @@ -24,9 +24,11 @@ #include "tracks/track_object_manager.hpp" #include "tracks/track.hpp" -namespace Scripting{ +namespace Scripting +{ - namespace Track{ + namespace Track + { asIScriptFunction* registerScriptCallbacks(asIScriptEngine *engine) { diff --git a/src/scriptengine/script_track.hpp b/src/scriptengine/script_track.hpp index 588313ee0..3517cc6e6 100644 --- a/src/scriptengine/script_track.hpp +++ b/src/scriptengine/script_track.hpp @@ -21,9 +21,11 @@ #include -namespace Scripting{ +namespace Scripting +{ - namespace Track{ + namespace Track + { //script engine functions void registerScriptFunctions(asIScriptEngine *engine); diff --git a/src/scriptengine/scriptarray.cpp b/src/scriptengine/scriptarray.cpp index 16d1f43ed..0d9525c83 100644 --- a/src/scriptengine/scriptarray.cpp +++ b/src/scriptengine/scriptarray.cpp @@ -4,7 +4,7 @@ #include #include // sprintf -#include "scriptarray.h" +#include "scriptarray.hpp" using namespace std; diff --git a/src/scriptengine/scriptarray.h b/src/scriptengine/scriptarray.hpp similarity index 100% rename from src/scriptengine/scriptarray.h rename to src/scriptengine/scriptarray.hpp diff --git a/src/scriptengine/scriptstdstring.cpp b/src/scriptengine/scriptstdstring.cpp index 30b8d6d56..43b416a80 100644 --- a/src/scriptengine/scriptstdstring.cpp +++ b/src/scriptengine/scriptstdstring.cpp @@ -1,4 +1,4 @@ -#include "scriptstdstring.h" +#include "scriptstdstring.hpp" #include // assert() #include // std::stringstream #include // strstr() diff --git a/src/scriptengine/scriptstdstring.h b/src/scriptengine/scriptstdstring.hpp similarity index 100% rename from src/scriptengine/scriptstdstring.h rename to src/scriptengine/scriptstdstring.hpp diff --git a/src/scriptengine/scriptstdstring_utils.cpp b/src/scriptengine/scriptstdstring_utils.cpp index 7fee331d0..675ab826c 100644 --- a/src/scriptengine/scriptstdstring_utils.cpp +++ b/src/scriptengine/scriptstdstring_utils.cpp @@ -1,6 +1,6 @@ #include -#include "scriptstdstring.h" -#include "scriptarray.h" +#include "scriptstdstring.hpp" +#include "scriptarray.hpp" #include #include diff --git a/src/scriptengine/scriptvec3.cpp b/src/scriptengine/scriptvec3.cpp new file mode 100644 index 000000000..e78cf3cd6 --- /dev/null +++ b/src/scriptengine/scriptvec3.cpp @@ -0,0 +1,66 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2014 SuperTuxKart Team +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include +#include "karts/kart.hpp" +#include "modes/world.hpp" +#include "script_kart.hpp" + +//debug +#include + + +namespace Scripting +{ + + + void Constructor(void *memory) + { + // Initialize the pre-allocated memory by calling the + // object constructor with the placement-new operator + new(memory)Vec3(); + } + void Destructor(void *memory) + { + // Uninitialize the memory by calling the object destructor + ((Vec3*)memory)->~Vec3(); + } + void ConstructVector3FromFloats(float a, float b, float c, void *memory){ + //Constructor using 3 floats + new (memory)(Vec3)(Vec3(a, b, c)); + } + void printVec3(asIScriptGeneric *gen) + { + Vec3 *script_vec3 = (Vec3*)gen->GetArgObject(0); + std::cout << script_vec3->getX() << "," << script_vec3->getY() << "," << script_vec3->getZ() << std::endl; + } + void RegisterVec3(asIScriptEngine *engine) + { + int r; + r = engine->RegisterObjectType("Vec3", sizeof(Vec3), asOBJ_VALUE); assert(r >= 0); + // Register the behaviours + r = engine->RegisterObjectBehaviour("Vec3", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(Constructor), asCALL_CDECL_OBJLAST); assert(r >= 0); + r = engine->RegisterObjectBehaviour("Vec3", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(Destructor), asCALL_CDECL_OBJLAST); assert(r >= 0); + r = engine->RegisterObjectMethod("Vec3", "Vec3 &opAssign(const Vec3 &in)", asMETHODPR(Vec3, operator =, (const Vec3&), Vec3&), asCALL_THISCALL); assert(r >= 0); + r = engine->RegisterObjectBehaviour("Vec3", asBEHAVE_CONSTRUCT, "void f(float, float, float)", asFUNCTION(ConstructVector3FromFloats), asCALL_CDECL_OBJLAST); assert(r >= 0); + r = engine->RegisterGlobalFunction("void printVec3(Vec3 a)", asFUNCTION(printVec3), asCALL_GENERIC); assert(r >= 0); + + + } +} diff --git a/src/scriptengine/scriptvec3.hpp b/src/scriptengine/scriptvec3.hpp new file mode 100644 index 000000000..468fc3237 --- /dev/null +++ b/src/scriptengine/scriptvec3.hpp @@ -0,0 +1,30 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2014 SuperTuxKart Team +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef HEADER_SCRIPTVEC3_HPP +#define HEADER_SCRIPTVEC3_HPP + +#include + +namespace Scripting +{ + + void RegisterVec3(asIScriptEngine *engine); + +} +#endif