Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Vector2
- */
- #define VEC2PTR mVec2* vec = (mVec2*) data->data
- void* vec2_iface[];
- int vec2_destruct( SGS_CTX, sgs_VarObj* data )
- {
- VEC2PTR;
- delete vec;
- return SGS_SUCCESS;
- }
- int vec2_getprop( SGS_CTX, sgs_VarObj* data )
- {
- VEC2PTR;
- const char* prop = sgs_ToString( C, -1 );
- if( gStrEqual( prop, "x" ) ) return sgs_PushReal( C, vec->x );
- if( gStrEqual( prop, "y" ) ) return sgs_PushReal( C, vec->y );
- if( gStrEqual( prop, "length" ) ) return sgs_PushReal( C, vec->Length() );
- if( gStrEqual( prop, "length_squared" ) ) return sgs_PushReal( C, vec->LengthSq() );
- if( gStrEqual( prop, "normalized" ) ) return sgs_PushObject( C, new mVec2( vec->Normalized() ), vec2_iface );
- if( gStrEqual( prop, "perp" ) ) return sgs_PushObject( C, new mVec2( vec->Perp() ), vec2_iface );
- if( gStrEqual( prop, "perp2" ) ) return sgs_PushObject( C, new mVec2( vec->Perp2() ), vec2_iface );
- return SGS_ENOTFND;
- }
- int vec2_setprop( SGS_CTX, sgs_VarObj* data )
- {
- VEC2PTR;
- const char* prop = sgs_ToString( C, -1 );
- if( gStrEqual( prop, "x" ) ) vec->x = sgs_ToReal( C, -2 ); return SGS_SUCCESS;
- if( gStrEqual( prop, "y" ) ) vec->y = sgs_ToReal( C, -2 ); return SGS_SUCCESS;
- return SGS_ENOTFND;
- }
- int vec2_gettype( SGS_CTX, sgs_VarObj* data )
- {
- sgs_PushString( C, "vec2" );
- return SGS_SUCCESS;
- }
- int vec2_tostring( SGS_CTX, sgs_VarObj* data )
- {
- VEC2PTR;
- sgs_PushString( C, "vec2(" );
- sgs_PushReal( C, vec->x );
- sgs_PushString( C, ";" );
- sgs_PushReal( C, vec->y );
- sgs_PushString( C, ")" );
- sgs_StringMultiConcat( C, 5 );
- return SGS_SUCCESS;
- }
- void* vec2_iface[] =
- {
- SOP_DESTRUCT, vec2_destruct,
- SOP_GETPROP, vec2_getprop,
- SOP_SETPROP, vec2_setprop,
- SOP_GETTYPE, vec2_gettype,
- SOP_TOSTRING, vec2_tostring,
- SOP_END,
- };
- int sgs_vec2( SGS_CTX )
- {
- if( sgs_StackSize( C ) > 2 )
- {
- sgs_Printf( C, SGS_ERROR, -1, "Too many arguments for vec2" );
- return 0;
- }
- else if( sgs_StackSize( C ) > 1 ) sgs_PushObject( C, new mVec2( sgs_ToReal( C, 0 ), sgs_ToReal( C, 1 ) ), vec2_iface );
- else if( sgs_StackSize( C ) > 0 ) sgs_PushObject( C, new mVec2( sgs_ToReal( C, 0 ), sgs_ToReal( C, 0 ) ), vec2_iface );
- else sgs_PushObject( C, new mVec2( 0, 0 ), vec2_iface );
- return 1;
- }
- #define GETVEC2( idx ) (*(mVec2*)sgs_GetObjectData( C, idx ))
- int sgs_vec2_dot( SGS_CTX )
- {
- if( !sgs_CheckArgs( C, "vec2,vec2" ) )
- return 0;
- sgs_PushObject( C, new mVec2( GETVEC2( 0 ) * GETVEC2( 1 ) ), vec2_iface );
- return 1;
- }
- void* vec2_funcs[] =
- {
- "vec2", sgs_vec2,
- "vec2_dot", sgs_vec2_dot,
- NULL,
- };
- int SGSRegister_Vec2( SGS_CTX )
- {
- int err = SGSRegisterFunctions( C, vec2_funcs );
- if( err ) return err;
- return SGS_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement