Advertisement
Guest User

Angelscript mingw 4.7.0

a guest
Oct 9th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include "angelscriptAddOn/scriptbuilder/scriptbuilder.h"
  2. #include "stdio.h"
  3. #include <assert.h>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. void MessageCallback_(const asSMessageInfo *msg, void *param) {
  9.   const char *type = "ERR ";
  10.   if( msg->type == asMSGTYPE_WARNING )
  11.     type = "WARN";
  12.   else if( msg->type == asMSGTYPE_INFORMATION )
  13.     type = "INFO";
  14.   printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);
  15. }
  16.  
  17. class Person {
  18.     public:
  19.         Person() {cout<<(int)this<<endl;this->counter = 1;}
  20.         void printTwoInts(int a, int b) { cout << (int)this << " " << a << " " << b << endl; }
  21.         void AddRef() {cout<<this<<" "<<this->counter;}
  22.         void ReleaseRef() {cout<<this<<" "<<this->counter;}
  23.         static Person *refFactory() {
  24.             return new Person();
  25.         };
  26.         int counter;
  27.  
  28. };
  29.  
  30. int main() {
  31.     asIScriptEngine *scriptEngine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
  32.     int r = scriptEngine->SetMessageCallback(asFUNCTION(MessageCallback_), 0, asCALL_CDECL);assert( r >= 0 );
  33.     r = scriptEngine->RegisterObjectType("Person", sizeof(Person), asOBJ_REF); assert( r >= 0 );
  34.     r = scriptEngine->RegisterObjectBehaviour("Person", asBEHAVE_FACTORY, "Person @f()", asFUNCTION(Person::refFactory), asCALL_CDECL); assert( r >= 0 );
  35.     r = scriptEngine->RegisterObjectBehaviour("Person", asBEHAVE_ADDREF,  "void f()", asMETHOD(Person, AddRef), asCALL_THISCALL); assert( r >= 0 );
  36.     r = scriptEngine->RegisterObjectBehaviour("Person", asBEHAVE_RELEASE, "void f()", asMETHOD(Person, ReleaseRef), asCALL_THISCALL); assert( r >= 0 );
  37.  
  38.     r = scriptEngine->RegisterObjectMethod("Person", "void printTwoInts(int a, int b)", asMETHOD(Person, printTwoInts), asCALL_THISCALL);assert( r >= 0 );
  39.  
  40.     CScriptBuilder builder;
  41.     r = builder.StartNewModule(scriptEngine, "Test"); assert( r >= 0 );
  42.     r = builder.AddSectionFromMemory("void main() {\nPerson p;\np.printTwoInts(999,55);\n}"); assert( r >= 0 );
  43.     r = builder.BuildModule();if( r < 0 );  assert( r >= 0 );
  44.  
  45.     asIScriptModule *mod = scriptEngine->GetModule("Test");
  46.     asIScriptFunction *func = mod->GetFunctionByDecl("void main()");
  47.     asIScriptContext *ctx = scriptEngine->CreateContext();
  48.     ctx->Prepare(func);
  49.     r = ctx->Execute();
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement