codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
/* * main.cpp * * Created on: 21.10.2011 * Author: Simon */ #include <iostream> #include "angelscript.h" #include <string> #include "stdio.h" #include "assert.h" #include "scriptstdstring.h" #include "scriptarray.h" #include "scriptbuilder.h" // globals asIScriptEngine* AS_ENGINE = 0; // prototypes //void loadScriptFile( const std::string & filePath, const std::string & scriptName ); void printText( const std::string& text ); class Unit { private: std::string _name; int _life; int _refCount; public: Unit( const std::string& name ) { _refCount = 1; _name = name; } static Unit* asFactory( const std::string& name ) { return new Unit( name ); } void asAddRef() { _refCount++; } void asRelease() { _refCount--; if( _refCount <= 0 ) delete this; } virtual ~Unit() { } void setLife( int newLife ) { _life = newLife; } virtual void print() { printText( _name.append( "\n\tLeben: " + _life ) ); } }; /*void loadScriptFile( const std::string & filePath, const std::string & scriptName ) { std::string tmp; // Open the file in binary mode FILE *f = fopen( filePath.c_str(), "rb" ); // Determine the size of the file fseek( f, 0, SEEK_END ); int len = ftell( f ); fseek( f, 0, SEEK_SET ); // Load the entire file in one call tmp.resize( len ); fread( &tmp[0], len, 1, f ); fclose( f ); asIScriptModule* mod = AS_ENGINE->GetModule( scriptName.c_str(), asGM_ALWAYS_CREATE ); mod->AddScriptSection( scriptName.c_str(), tmp.c_str() ); mod->Build(); }*/ int main() { AS_ENGINE = asCreateScriptEngine( ANGELSCRIPT_VERSION ); RegisterStdString( AS_ENGINE ); RegisterScriptArray( AS_ENGINE, true ); RegisterStdStringUtils( AS_ENGINE ); int r = AS_ENGINE->RegisterGlobalFunction( "void printText( const string &in )", asFUNCTIONPR(printText, (const std::string&), void), asCALL_CDECL ); assert( r>= 0 ); // RegisterObjectMethod("mytype", "void ClassMethod()", asMETHOD(MyClass,ClassMethod), asCALL_THISCALL); //r = AS_ENGINE->RegisterObjectMethod("Unit", "Unit( const string& in )", asMETHODPR(Unit,Unit, (const std::string&), Unit), asCALL_THISCALL ); assert( r>= 0 ); //r = AS_ENGINE->RegisterObjectMethod("Unit", "~Unit()", asMETHOD(Unit,~Unit), asCALL_THISCALL ); assert( r>= 0 ); r = AS_ENGINE->RegisterObjectType( "Unit", 0, asOBJ_REF ); assert( r>=0 ); /*r = AS_ENGINE->RegisterObjectProperty( "Unit", "string _name", offsetof(Unit,_name) ); assert( r>=0 ); r = AS_ENGINE->RegisterObjectProperty( "Unit", "int _life", offsetof(Unit,_life) ); assert( r>=0 );*/ r = AS_ENGINE->RegisterObjectBehaviour( "Unit", asBEHAVE_FACTORY, "Unit@ Unit(const string &in)", asFUNCTIONPR(Unit::asFactory,(const std::string&),Unit*), asCALL_STDCALL ); assert( r>=0 ); r = AS_ENGINE->RegisterObjectBehaviour( "Unit", asBEHAVE_ADDREF, "void Unit()", asMETHOD(Unit,asAddRef), asCALL_THISCALL ); assert( r >= 0 ); r = AS_ENGINE->RegisterObjectBehaviour( "Unit", asBEHAVE_RELEASE, "void Unit()", asMETHOD(Unit,asRelease), asCALL_THISCALL ); assert( r >= 0 ); r = AS_ENGINE->RegisterObjectMethod( "Unit", "void setLife(int)", asMETHODPR(Unit,setLife,(int),void), asCALL_THISCALL ); assert( r>= 0 ); r = AS_ENGINE->RegisterObjectMethod( "Unit", "void print()", asMETHOD(Unit,print), asCALL_THISCALL ); assert( r>= 0 ); // Register an interface /*r = AS_ENGINE->RegisterInterface( "Unit" ); assert( r>= 0 ); // You can also register methods with the interface if you wish to force the script class to implement them //r = AS_ENGINE->RegisterInterfaceMethod( "Unit", "void setLife(int)" ); //assert( r>= 0 ); r = AS_ENGINE->RegisterInterfaceMethod( "Unit", "void print()" ); assert( r>= 0 ); r = AS_ENGINE->RegisterInterfaceMethod( "Unit", "void asAddRef()" ); assert( r>= 0 ); r = AS_ENGINE->RegisterInterfaceMethod( "Unit", "void asRelease()" ); assert( r>= 0 );*/ // Register a function that receives a handle to the interface //r = AS_ENGINE->RegisterGlobalFunction( "Unit@ Unit(const string &in)", asFUNCTIONPR(Unit::asFactory,(const std::string&),Unit*), asCALL_CDECL ); //assert( r>= 0 ); std::cout << "loading script..." << std::endl; //loadScriptFile( "script.as", "Frosch" ); CScriptBuilder builder; builder.StartNewModule( AS_ENGINE, "Frosch" ); builder.AddSectionFromFile( "script.as" ); builder.BuildModule(); std::cout << "FINISHED!" << std::endl; asIScriptModule* mod = AS_ENGINE->GetModule( "Frosch", asGM_ONLY_IF_EXISTS ); if( mod ) { std::cout << "Modul Frosch erfolgreich geladen!" << std::endl; std::cout << "Modul: " << mod->GetName() << std::endl; std::cout << "Anzahl Funktionen in Modul: " << mod->GetFunctionCount() << std::endl; int tid = mod->GetTypeIdByDecl( "Frosch" ); if( tid > 0 ) { asIObjectType* type = mod->GetObjectTypeByIndex( tid ); std::cout << "Froschid: " << type << std::endl; int factoryID = type->GetFactoryIdByDecl( "Frosch @Frosch()" ); std::cout << "Factoryid: " << factoryID << std::endl; // Prepare the context to call the factory function asIScriptContext *context = AS_ENGINE->CreateContext(); context->Prepare( factoryID ); // Execute the call context->Execute(); // Get the object that was created Unit *obj = *(Unit**) context->GetAddressOfReturnValue(); // If you're going to store the object you must increase the reference, // otherwise it will be destroyed when the context is reused or destroyed. obj->asAddRef(); obj->setLife( 50 ); obj->print(); } } else { std::cout << "Fehler beim Laden des Moduls" << std::endl; AS_ENGINE->Release(); return -1; } /*int funcID = mod->GetFunctionIdByDecl( "void erstelleFrosch()" ); if( funcID < 0 ) { std::cout << "Konnte Funktion erstelleFrosch nicht finden" << std::endl; AS_ENGINE->Release(); return -1; } asIScriptContext *context = AS_ENGINE->CreateContext(); context->Prepare( funcID ); context->Execute(); context->Release();*/ AS_ENGINE->Release(); return 0; } void printText( const std::string& text ) { std::cout << text << std::endl; }
Private
[
?
]
Run code
Submit