Advertisement
snake5

SGScript - 2D Vector implementation

Dec 27th, 2012
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. /*
  2.     Vector2
  3. */
  4.  
  5. #define VEC2PTR mVec2* vec = (mVec2*) data->data
  6. void* vec2_iface[];
  7.  
  8. int vec2_destruct( SGS_CTX, sgs_VarObj* data )
  9. {
  10.     VEC2PTR;
  11.     delete vec;
  12.     return SGS_SUCCESS;
  13. }
  14.  
  15. int vec2_getprop( SGS_CTX, sgs_VarObj* data )
  16. {
  17.     VEC2PTR;
  18.     const char* prop = sgs_ToString( C, -1 );
  19.     if( gStrEqual( prop, "x" ) )        return sgs_PushReal( C, vec->x );
  20.     if( gStrEqual( prop, "y" ) )        return sgs_PushReal( C, vec->y );
  21.     if( gStrEqual( prop, "length" ) )   return sgs_PushReal( C, vec->Length() );
  22.     if( gStrEqual( prop, "length_squared" ) )   return sgs_PushReal( C, vec->LengthSq() );
  23.     if( gStrEqual( prop, "normalized" ) )       return sgs_PushObject( C, new mVec2( vec->Normalized() ), vec2_iface );
  24.     if( gStrEqual( prop, "perp" ) )     return sgs_PushObject( C, new mVec2( vec->Perp() ), vec2_iface );
  25.     if( gStrEqual( prop, "perp2" ) )    return sgs_PushObject( C, new mVec2( vec->Perp2() ), vec2_iface );
  26.     return SGS_ENOTFND;
  27. }
  28.  
  29. int vec2_setprop( SGS_CTX, sgs_VarObj* data )
  30. {
  31.     VEC2PTR;
  32.     const char* prop = sgs_ToString( C, -1 );
  33.     if( gStrEqual( prop, "x" ) )    vec->x = sgs_ToReal( C, -2 ); return SGS_SUCCESS;
  34.     if( gStrEqual( prop, "y" ) )    vec->y = sgs_ToReal( C, -2 ); return SGS_SUCCESS;
  35.     return SGS_ENOTFND;
  36. }
  37.  
  38. int vec2_gettype( SGS_CTX, sgs_VarObj* data )
  39. {
  40.     sgs_PushString( C, "vec2" );
  41.     return SGS_SUCCESS;
  42. }
  43.  
  44. int vec2_tostring( SGS_CTX, sgs_VarObj* data )
  45. {
  46.     VEC2PTR;
  47.     sgs_PushString( C, "vec2(" );
  48.     sgs_PushReal( C, vec->x );
  49.     sgs_PushString( C, ";" );
  50.     sgs_PushReal( C, vec->y );
  51.     sgs_PushString( C, ")" );
  52.     sgs_StringMultiConcat( C, 5 );
  53.     return SGS_SUCCESS;
  54. }
  55.  
  56. void* vec2_iface[] =
  57. {
  58.     SOP_DESTRUCT, vec2_destruct,
  59.     SOP_GETPROP, vec2_getprop,
  60.     SOP_SETPROP, vec2_setprop,
  61.     SOP_GETTYPE, vec2_gettype,
  62.     SOP_TOSTRING, vec2_tostring,
  63.     SOP_END,
  64. };
  65.  
  66. int sgs_vec2( SGS_CTX )
  67. {
  68.     if( sgs_StackSize( C ) > 2 )
  69.     {
  70.         sgs_Printf( C, SGS_ERROR, -1, "Too many arguments for vec2" );
  71.         return 0;
  72.     }
  73.     else if( sgs_StackSize( C ) > 1 )   sgs_PushObject( C, new mVec2( sgs_ToReal( C, 0 ), sgs_ToReal( C, 1 ) ), vec2_iface );
  74.     else if( sgs_StackSize( C ) > 0 )   sgs_PushObject( C, new mVec2( sgs_ToReal( C, 0 ), sgs_ToReal( C, 0 ) ), vec2_iface );
  75.     else                                sgs_PushObject( C, new mVec2( 0, 0 ), vec2_iface );
  76.     return 1;
  77. }
  78.  
  79. #define GETVEC2( idx ) (*(mVec2*)sgs_GetObjectData( C, idx ))
  80. int sgs_vec2_dot( SGS_CTX )
  81. {
  82.     if( !sgs_CheckArgs( C, "vec2,vec2" ) )
  83.         return 0;
  84.     sgs_PushObject( C, new mVec2( GETVEC2( 0 ) * GETVEC2( 1 ) ), vec2_iface );
  85.     return 1;
  86. }
  87.  
  88. void* vec2_funcs[] =
  89. {
  90.     "vec2", sgs_vec2,
  91.     "vec2_dot", sgs_vec2_dot,
  92.     NULL,
  93. };
  94.  
  95. int SGSRegister_Vec2( SGS_CTX )
  96. {
  97.     int err = SGSRegisterFunctions( C, vec2_funcs );
  98.     if( err ) return err;
  99.  
  100.     return SGS_SUCCESS;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement