Advertisement
snake5

SGScript - C++ binding compiler vec3 example

Feb 18th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. // sgscppbctest.h
  2.  
  3. #include <math.h>
  4.  
  5. #include "cppbc/sgs_cppbc.h"
  6.  
  7. struct Vec3
  8. {
  9.     SGS_OBJECT;
  10.    
  11.     Vec3( float _x, float _y, float _z ) : x(_x), y(_y), z(_z){}
  12.    
  13.     float _get_length(){ return (float) sqrtf(x*x+y*y+z*z); }
  14.    
  15.     SGS_PROPERTY float x;
  16.     SGS_PROPERTY float y;
  17.     SGS_PROPERTY float z;
  18.    
  19.     SGS_PROPERTY_FUNC( READ _get_length ) SGS_ALIAS( float length );
  20.     SGS_METHOD float getLength(){ return _get_length(); }
  21.     SGS_METHOD void setLength( float len )
  22.     {
  23.         if( x == 0 && y == 0 && z == 0 )
  24.             x = 1;
  25.         else
  26.         {
  27.             float ol = _get_length();
  28.             len /= ol;
  29.         }
  30.         x *= len;
  31.         y *= len;
  32.         z *= len;
  33.     }
  34.     SGS_IFUNC( SOP_CONVERT ) int _convert( SGS_CTX, sgs_VarObj* data, int type )
  35.     {
  36.         Vec3* V = (Vec3*) data->data;
  37.         if( type == SGS_VT_STRING )
  38.         {
  39.             char bfr[ 128 ];
  40.             sprintf( bfr, "Vec3(%g;%g;%g)", V->x, V->y, V->z );
  41.             sgs_PushString( C, bfr );
  42.             return SGS_SUCCESS;
  43.         }
  44.         return SGS_ENOTSUP;
  45.     }
  46. };
  47.  
  48.  
  49. // sgscppbctest.cpp
  50.  
  51. #include "sgscppbctest.h"
  52.  
  53. void pushVec3( SGS_CTX, float x, float y, float z )
  54. {
  55.     SGS_PUSHCLASS( C, Vec3, (x,y,z) );
  56. }
  57.  
  58. int main( int argc, char** argv )
  59. {
  60.     printf( "\n//\n/// SGScript / CPPBC test\n//\n" );
  61.    
  62.     SGS_CTX = sgs_CreateEngine();
  63.    
  64.     printf( "\n> push Vec3(1,2,3)" );
  65.     pushVec3( C, 1, 2, 3 );
  66.    
  67.     printf( "\n> print object: " );
  68.     sgs_PushItem( C, -1 );
  69.     sgs_GlobalCall( C, "print", 1, 0 );
  70.    
  71.     printf( "\n> print property 'Vec3.length': " );
  72.     sgs_PushProperty( C, -1, "length" );
  73.     sgs_GlobalCall( C, "print", 1, 0 );
  74.    
  75.     printf( "\n> print result of method 'Vec3.getLength()': " );
  76.     sgs_PushItem( C, -1 );
  77.     sgs_PushProperty( C, -1, "getLength" );
  78.     sgs_ThisCall( C, 0, 1 );
  79.     sgs_GlobalCall( C, "print", 1, 0 );
  80.    
  81.     printf( "\n> print object after method 'Vec3.setLength(4.5)': " );
  82.     sgs_PushItem( C, -1 );
  83.     sgs_PushReal( C, 4.5 );
  84.     sgs_PushProperty( C, -2, "setLength" );
  85.     sgs_ThisCall( C, 1, 0 );
  86.     sgs_GlobalCall( C, "print", 1, 0 );
  87.    
  88.     printf( "\n" );
  89.     sgs_DestroyEngine( C );
  90.    
  91.     return 0;
  92. }
  93.  
  94.  
  95. // test output
  96.  
  97.  
  98. //
  99. /// SGScript / CPPBC test
  100. //
  101.  
  102. > push Vec3(1,2,3)
  103. > print object: Vec3(1;2;3)
  104. > print property 'Vec3.length': 3.74166
  105. > print result of method 'Vec3.getLength()': 3.74166
  106. > print object after method 'Vec3.setLength(4.5)': Vec3(1.20268;2.40535;3.60803)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement