Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // sgscppbctest.h
- #include <math.h>
- #include "cppbc/sgs_cppbc.h"
- struct Vec3
- {
- SGS_OBJECT;
- Vec3( float _x, float _y, float _z ) : x(_x), y(_y), z(_z){}
- float _get_length(){ return (float) sqrtf(x*x+y*y+z*z); }
- SGS_PROPERTY float x;
- SGS_PROPERTY float y;
- SGS_PROPERTY float z;
- SGS_PROPERTY_FUNC( READ _get_length ) SGS_ALIAS( float length );
- SGS_METHOD float getLength(){ return _get_length(); }
- SGS_METHOD void setLength( float len )
- {
- if( x == 0 && y == 0 && z == 0 )
- x = 1;
- else
- {
- float ol = _get_length();
- len /= ol;
- }
- x *= len;
- y *= len;
- z *= len;
- }
- SGS_IFUNC( SOP_CONVERT ) int _convert( SGS_CTX, sgs_VarObj* data, int type )
- {
- Vec3* V = (Vec3*) data->data;
- if( type == SGS_VT_STRING )
- {
- char bfr[ 128 ];
- sprintf( bfr, "Vec3(%g;%g;%g)", V->x, V->y, V->z );
- sgs_PushString( C, bfr );
- return SGS_SUCCESS;
- }
- return SGS_ENOTSUP;
- }
- };
- // sgscppbctest.cpp
- #include "sgscppbctest.h"
- void pushVec3( SGS_CTX, float x, float y, float z )
- {
- SGS_PUSHCLASS( C, Vec3, (x,y,z) );
- }
- int main( int argc, char** argv )
- {
- printf( "\n//\n/// SGScript / CPPBC test\n//\n" );
- SGS_CTX = sgs_CreateEngine();
- printf( "\n> push Vec3(1,2,3)" );
- pushVec3( C, 1, 2, 3 );
- printf( "\n> print object: " );
- sgs_PushItem( C, -1 );
- sgs_GlobalCall( C, "print", 1, 0 );
- printf( "\n> print property 'Vec3.length': " );
- sgs_PushProperty( C, -1, "length" );
- sgs_GlobalCall( C, "print", 1, 0 );
- printf( "\n> print result of method 'Vec3.getLength()': " );
- sgs_PushItem( C, -1 );
- sgs_PushProperty( C, -1, "getLength" );
- sgs_ThisCall( C, 0, 1 );
- sgs_GlobalCall( C, "print", 1, 0 );
- printf( "\n> print object after method 'Vec3.setLength(4.5)': " );
- sgs_PushItem( C, -1 );
- sgs_PushReal( C, 4.5 );
- sgs_PushProperty( C, -2, "setLength" );
- sgs_ThisCall( C, 1, 0 );
- sgs_GlobalCall( C, "print", 1, 0 );
- printf( "\n" );
- sgs_DestroyEngine( C );
- return 0;
- }
- // test output
- //
- /// SGScript / CPPBC test
- //
- > push Vec3(1,2,3)
- > print object: Vec3(1;2;3)
- > print property 'Vec3.length': 3.74166
- > print result of method 'Vec3.getLength()': 3.74166
- > 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