mirror of
https://github.com/SFML/SFML.git
synced 2024-12-01 15:51:04 +08:00
git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1589 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
parent
b1a9967d56
commit
4b418bb4d7
5
ruby/sfml-system/extconf.rb
Normal file
5
ruby/sfml-system/extconf.rb
Normal file
@ -0,0 +1,5 @@
|
||||
require 'mkmf'
|
||||
|
||||
dir_config("system")
|
||||
have_library("sfml-system")
|
||||
create_makefile("sfml/system", "system")
|
48
ruby/sfml-system/system/Clock.cpp
Normal file
48
ruby/sfml-system/system/Clock.cpp
Normal file
@ -0,0 +1,48 @@
|
||||
#include "Clock.hpp"
|
||||
#include "System.hpp"
|
||||
#include <SFML/System/Clock.hpp>
|
||||
|
||||
VALUE globalClockClass;
|
||||
|
||||
static void Clock_Free( sf::Clock *anObject )
|
||||
{
|
||||
delete anObject;
|
||||
}
|
||||
|
||||
static VALUE Clock_GetElapsedTime( VALUE self )
|
||||
{
|
||||
sf::Clock *object = NULL;
|
||||
Data_Get_Struct( self, sf::Clock, object );
|
||||
return rb_float_new( object->GetElapsedTime() );
|
||||
}
|
||||
|
||||
static VALUE Clock_Reset( VALUE self )
|
||||
{
|
||||
sf::Clock *object = NULL;
|
||||
Data_Get_Struct( self, sf::Clock, object );
|
||||
object->Reset();
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
VALUE Clock_New( VALUE aKlass )
|
||||
{
|
||||
sf::Clock *object = new sf::Clock();
|
||||
VALUE rbData = Data_Wrap_Struct( aKlass, 0, Clock_Free, object );
|
||||
rb_obj_call_init( rbData, 0, 0 );
|
||||
return rbData;
|
||||
}
|
||||
|
||||
void Init_Clock( void )
|
||||
{
|
||||
globalClockClass = rb_define_class_under( GetNamespace(), "Clock", rb_cObject );
|
||||
|
||||
// Class methods
|
||||
rb_define_singleton_method( globalClockClass, "new", FUNCPTR( Clock_New ), 0 );
|
||||
|
||||
// Instance methods
|
||||
rb_define_method( globalClockClass, "getElapsedTime", FUNCPTR( Clock_GetElapsedTime ), 0 );
|
||||
rb_define_method( globalClockClass, "reset", FUNCPTR( Clock_Reset ), 0 );
|
||||
|
||||
// Aliases
|
||||
rb_define_alias( globalClockClass, "elapsedTime", "getElapsedTime" );
|
||||
}
|
11
ruby/sfml-system/system/Clock.hpp
Normal file
11
ruby/sfml-system/system/Clock.hpp
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef SFML_RUBYEXT_CLOCK_HEADER_
|
||||
#define SFML_RUBYEXT_CLOCK_HEADER_
|
||||
|
||||
#include "ruby.h"
|
||||
|
||||
VALUE Clock_New( VALUE aKlass );
|
||||
|
||||
// Ruby initiation function
|
||||
void Init_Clock( void );
|
||||
|
||||
#endif // SFML_RUBYEXT_CLOCK_HEADER_
|
19
ruby/sfml-system/system/System.cpp
Normal file
19
ruby/sfml-system/system/System.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include "System.hpp"
|
||||
#include "Clock.hpp"
|
||||
#include "Vector2.hpp"
|
||||
#include "Vector3.hpp"
|
||||
|
||||
VALUE globalSFMLNamespace;
|
||||
|
||||
VALUE GetNamespace( void )
|
||||
{
|
||||
return globalSFMLNamespace;
|
||||
}
|
||||
|
||||
void Init_system( void )
|
||||
{
|
||||
globalSFMLNamespace = rb_define_module( "SFML" );
|
||||
Init_Clock();
|
||||
Init_Vector2();
|
||||
Init_Vector3();
|
||||
}
|
15
ruby/sfml-system/system/System.hpp
Normal file
15
ruby/sfml-system/system/System.hpp
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef SFML_RUBYEXT_SYSTEM_HEADER_
|
||||
#define SFML_RUBYEXT_SYSTEM_HEADER_
|
||||
|
||||
#include "ruby.h"
|
||||
|
||||
VALUE GetNamespace( void );
|
||||
|
||||
// Ruby initiation function
|
||||
extern "C" void Init_system( void );
|
||||
|
||||
typedef VALUE ( *RubyFunctionPtr )( ... );
|
||||
|
||||
#define FUNCPTR( x ) ( reinterpret_cast< RubyFunctionPtr >( x ) )
|
||||
|
||||
#endif // SFML_RUBYEXT_SYSTEM_HEADER_
|
214
ruby/sfml-system/system/Vector2.cpp
Normal file
214
ruby/sfml-system/system/Vector2.cpp
Normal file
@ -0,0 +1,214 @@
|
||||
#include "Vector2.hpp"
|
||||
#include "System.hpp"
|
||||
|
||||
VALUE globalVector2Class;
|
||||
|
||||
VALUE Vector2_ForceType( VALUE someValue )
|
||||
{
|
||||
if( rb_obj_is_kind_of( someValue, rb_cArray ) == true )
|
||||
{
|
||||
VALUE arg1 = rb_ary_entry( someValue, 0 );
|
||||
VALUE arg2 = rb_ary_entry( someValue, 1 );
|
||||
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, arg1, arg2 );
|
||||
}
|
||||
else if( rb_obj_is_kind_of( someValue, globalVector2Class ) == true )
|
||||
{
|
||||
return someValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
rb_raise( rb_eRuntimeError, "expected Array or Vector2" );
|
||||
}
|
||||
}
|
||||
|
||||
static void Vector2_internal_CopyFrom( VALUE self, VALUE aSource )
|
||||
{
|
||||
VALUE vectorSource = Vector2_ForceType( aSource );
|
||||
VALUE x = rb_funcall( vectorSource, rb_intern( "x" ), 0 );
|
||||
VALUE y = rb_funcall( vectorSource, rb_intern( "y" ), 0 );
|
||||
|
||||
rb_funcall( self, rb_intern( "x=" ), 1, x );
|
||||
rb_funcall( self, rb_intern( "y=" ), 1, y );
|
||||
rb_iv_set( self, "@dataType", rb_iv_get( vectorSource, "@dataType" ) );
|
||||
}
|
||||
|
||||
static void Vector2_internal_ValidateTypes( VALUE aFirst, VALUE aSecond )
|
||||
{
|
||||
if( CLASS_OF( aFirst ) != CLASS_OF( aSecond ) )
|
||||
{
|
||||
rb_raise( rb_eRuntimeError, "x and y must be of same type" );
|
||||
}
|
||||
|
||||
if( rb_obj_is_kind_of( aFirst, rb_cNumeric ) == Qfalse )
|
||||
{
|
||||
rb_raise( rb_eRuntimeError, "x and y must be numeric!" );
|
||||
}
|
||||
}
|
||||
|
||||
static VALUE Vector2_Negate( VALUE self )
|
||||
{
|
||||
VALUE x = rb_funcall( self, rb_intern( "x" ), 0 );
|
||||
VALUE y = rb_funcall( self, rb_intern( "y" ), 0 );
|
||||
VALUE negatedX = rb_funcall( x, rb_intern( "-@" ), 0 );
|
||||
VALUE negatedY = rb_funcall( y, rb_intern( "-@" ), 0 );
|
||||
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, negatedX, negatedY );
|
||||
}
|
||||
|
||||
static VALUE Vector2_Add( VALUE self, VALUE aRightOperand )
|
||||
{
|
||||
VALUE rightVector = Vector2_ForceType( aRightOperand );
|
||||
// Get values
|
||||
VALUE leftX = rb_funcall( self, rb_intern( "x" ), 0 );
|
||||
VALUE leftY = rb_funcall( self, rb_intern( "y" ), 0 );
|
||||
VALUE rightX = rb_funcall( rightVector, rb_intern( "x" ), 0 );
|
||||
VALUE rightY = rb_funcall( rightVector, rb_intern( "y" ), 0 );
|
||||
|
||||
// Do calculation
|
||||
VALUE newX = rb_funcall( leftX, rb_intern( "+" ), 1, rightX );
|
||||
VALUE newY = rb_funcall( leftY, rb_intern( "+" ), 1, rightY );
|
||||
|
||||
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, newX, newY );
|
||||
}
|
||||
|
||||
static VALUE Vector2_Subtract( VALUE self, VALUE aRightOperand )
|
||||
{
|
||||
VALUE rightVector = Vector2_ForceType( aRightOperand );
|
||||
// Get values
|
||||
VALUE leftX = rb_funcall( self, rb_intern( "x" ), 0 );
|
||||
VALUE leftY = rb_funcall( self, rb_intern( "y" ), 0 );
|
||||
VALUE rightX = rb_funcall( rightVector, rb_intern( "x" ), 0 );
|
||||
VALUE rightY = rb_funcall( rightVector, rb_intern( "y" ), 0 );
|
||||
|
||||
// Do calculation
|
||||
VALUE newX = rb_funcall( leftX, rb_intern( "-" ), 1, rightX );
|
||||
VALUE newY = rb_funcall( leftY, rb_intern( "-" ), 1, rightY );
|
||||
|
||||
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, newX, newY );
|
||||
}
|
||||
|
||||
static VALUE Vector2_Multiply( VALUE self, VALUE aRightOperand )
|
||||
{
|
||||
VALUE rightVector = Vector2_ForceType( aRightOperand );
|
||||
// Get values
|
||||
VALUE leftX = rb_funcall( self, rb_intern( "x" ), 0 );
|
||||
VALUE leftY = rb_funcall( self, rb_intern( "y" ), 0 );
|
||||
VALUE rightX = rb_funcall( rightVector, rb_intern( "x" ), 0 );
|
||||
VALUE rightY = rb_funcall( rightVector, rb_intern( "y" ), 0 );
|
||||
|
||||
// Do calculation
|
||||
VALUE newX = rb_funcall( leftX, rb_intern( "*" ), 1, rightX );
|
||||
VALUE newY = rb_funcall( leftY, rb_intern( "*" ), 1, rightY );
|
||||
|
||||
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, newX, newY );
|
||||
}
|
||||
|
||||
static VALUE Vector2_Divide( VALUE self, VALUE aRightOperand )
|
||||
{
|
||||
VALUE rightVector = Vector2_ForceType( aRightOperand );
|
||||
// Get values
|
||||
VALUE leftX = rb_funcall( self, rb_intern( "x" ), 0 );
|
||||
VALUE leftY = rb_funcall( self, rb_intern( "y" ), 0 );
|
||||
VALUE rightX = rb_funcall( rightVector, rb_intern( "x" ), 0 );
|
||||
VALUE rightY = rb_funcall( rightVector, rb_intern( "y" ), 0 );
|
||||
|
||||
// Do calculation
|
||||
VALUE newX = rb_funcall( leftX, rb_intern( "/" ), 1, rightX );
|
||||
VALUE newY = rb_funcall( leftY, rb_intern( "/" ), 1, rightY );
|
||||
|
||||
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, newX, newY );
|
||||
}
|
||||
|
||||
static VALUE Vector2_Equal( VALUE self, VALUE anArgument )
|
||||
{
|
||||
VALUE aVector = Vector2_ForceType( anArgument );
|
||||
VALUE leftX = rb_funcall( self, rb_intern( "x" ), 0 );
|
||||
VALUE leftY = rb_funcall( self, rb_intern( "y" ), 0 );
|
||||
VALUE rightX = rb_funcall( aVector, rb_intern( "x" ), 0 );
|
||||
VALUE rightY = rb_funcall( aVector, rb_intern( "y" ), 0 );
|
||||
|
||||
if( rb_funcall( leftX, rb_intern( "==" ), 1, rightX ) == Qtrue &&
|
||||
rb_funcall( leftY, rb_intern( "==" ), 1, rightY ) == Qtrue )
|
||||
{
|
||||
return Qtrue;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Qfalse;
|
||||
}
|
||||
}
|
||||
|
||||
static VALUE Vector2_StrictEqual( VALUE self, VALUE anArgument )
|
||||
{
|
||||
VALUE aVector = Vector2_ForceType( anArgument );
|
||||
VALUE leftX = rb_funcall( self, rb_intern( "x" ), 0 );
|
||||
VALUE leftY = rb_funcall( self, rb_intern( "y" ), 0 );
|
||||
VALUE rightX = rb_funcall( aVector, rb_intern( "x" ), 0 );
|
||||
VALUE rightY = rb_funcall( aVector, rb_intern( "y" ), 0 );
|
||||
|
||||
if( rb_funcall( leftX, rb_intern( "eql?" ), 1, rightX ) == Qtrue &&
|
||||
rb_funcall( leftY, rb_intern( "eql?" ), 1, rightY ) == Qtrue )
|
||||
{
|
||||
return Qtrue;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Qfalse;
|
||||
}
|
||||
}
|
||||
|
||||
static VALUE Vector2_Initialize( VALUE self, VALUE someArgs )
|
||||
{
|
||||
long arrayLength = RARRAY_LEN( someArgs );
|
||||
rb_iv_set( self, "@x", INT2NUM( 0 ) );
|
||||
rb_iv_set( self, "@y", INT2NUM( 0 ) );
|
||||
|
||||
if( arrayLength == 0 )
|
||||
{
|
||||
// Nothing needs to be done
|
||||
}
|
||||
else if( arrayLength == 1 )
|
||||
{
|
||||
Vector2_internal_CopyFrom( self, rb_ary_entry( someArgs, 0 ) );
|
||||
}
|
||||
else if( arrayLength == 2 )
|
||||
{
|
||||
VALUE arg1 = rb_ary_entry( someArgs, 0 );
|
||||
VALUE arg2 = rb_ary_entry( someArgs, 1 );
|
||||
Vector2_internal_ValidateTypes( arg1, arg2 );
|
||||
|
||||
rb_iv_set( self, "@x", arg1 );
|
||||
rb_iv_set( self, "@y", arg2 );
|
||||
}
|
||||
|
||||
rb_iv_set( self, "@dataType", CLASS_OF( rb_iv_get( self, "@x" ) ) );
|
||||
return self;
|
||||
}
|
||||
|
||||
VALUE Vector2_New( int anArgCount, VALUE * someArgs, VALUE aKlass )
|
||||
{
|
||||
return rb_call_super( anArgCount, someArgs );
|
||||
}
|
||||
|
||||
void Init_Vector2( void )
|
||||
{
|
||||
globalVector2Class = rb_define_class_under( GetNamespace(), "Vector2", rb_cObject );
|
||||
|
||||
// Class methods
|
||||
rb_define_singleton_method( globalVector2Class, "new", FUNCPTR( Vector2_New ), -1 );
|
||||
|
||||
// Instance methods
|
||||
rb_define_method( globalVector2Class, "initialize", FUNCPTR( Vector2_Initialize ), -2 );
|
||||
rb_define_method( globalVector2Class, "eql?", FUNCPTR( Vector2_Initialize ), 1 );
|
||||
|
||||
// Instance operators
|
||||
rb_define_method( globalVector2Class, "-@", FUNCPTR( Vector2_Negate ), 0 );
|
||||
rb_define_method( globalVector2Class, "+", FUNCPTR( Vector2_Add ), 1 );
|
||||
rb_define_method( globalVector2Class, "-", FUNCPTR( Vector2_Subtract ), 1 );
|
||||
rb_define_method( globalVector2Class, "*", FUNCPTR( Vector2_Multiply ), 1 );
|
||||
rb_define_method( globalVector2Class, "/", FUNCPTR( Vector2_Divide ), 1 );
|
||||
rb_define_method( globalVector2Class, "==", FUNCPTR( Vector2_Divide ), 1 );
|
||||
|
||||
// Attribute accessors
|
||||
rb_define_attr( globalVector2Class, "x", 1, 1 );
|
||||
rb_define_attr( globalVector2Class, "y", 1, 1 );
|
||||
}
|
10
ruby/sfml-system/system/Vector2.hpp
Normal file
10
ruby/sfml-system/system/Vector2.hpp
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef SFML_RUBYEXT_VECTOR2_HEADER_
|
||||
#define SFML_RUBYEXT_VECTOR2_HEADER_
|
||||
|
||||
#include "ruby.h"
|
||||
|
||||
VALUE Vector2_ForceType( VALUE someValue );
|
||||
|
||||
void Init_Vector2( void );
|
||||
|
||||
#endif // SFML_RUBYEXT_VECTOR2_HEADER_
|
241
ruby/sfml-system/system/Vector3.cpp
Normal file
241
ruby/sfml-system/system/Vector3.cpp
Normal file
@ -0,0 +1,241 @@
|
||||
#include "Vector3.hpp"
|
||||
#include "System.hpp"
|
||||
|
||||
VALUE globalVector3Class;
|
||||
|
||||
VALUE Vector3_ForceType( VALUE someValue )
|
||||
{
|
||||
if( rb_obj_is_kind_of( someValue, rb_cArray ) == true )
|
||||
{
|
||||
VALUE arg1 = rb_ary_entry( someValue, 0 );
|
||||
VALUE arg2 = rb_ary_entry( someValue, 1 );
|
||||
VALUE arg3 = rb_ary_entry( someValue, 2 );
|
||||
return rb_funcall( globalVector3Class, rb_intern( "new" ), 3, arg1, arg2, arg3 );
|
||||
}
|
||||
else if( rb_obj_is_kind_of( someValue, globalVector3Class ) == true )
|
||||
{
|
||||
return someValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
rb_raise( rb_eRuntimeError, "expected Array or Vector3" );
|
||||
}
|
||||
}
|
||||
|
||||
static void Vector3_internal_CopyFrom( VALUE self, VALUE aSource )
|
||||
{
|
||||
VALUE vectorSource = Vector3_ForceType( aSource );
|
||||
VALUE x = rb_funcall( vectorSource, rb_intern( "x" ), 0 );
|
||||
VALUE y = rb_funcall( vectorSource, rb_intern( "y" ), 0 );
|
||||
VALUE z = rb_funcall( vectorSource, rb_intern( "z" ), 0 );
|
||||
|
||||
rb_funcall( self, rb_intern( "x=" ), 1, x );
|
||||
rb_funcall( self, rb_intern( "y=" ), 1, y );
|
||||
rb_funcall( self, rb_intern( "z=" ), 1, z );
|
||||
rb_iv_set( self, "@dataType", rb_iv_get( vectorSource, "@dataType" ) );
|
||||
}
|
||||
|
||||
static void Vector3_internal_ValidateTypes( VALUE aFirst, VALUE aSecond, VALUE aThird )
|
||||
{
|
||||
if( CLASS_OF( aFirst ) != CLASS_OF( aSecond ) && CLASS_OF( aFirst ) != CLASS_OF( aThird ) )
|
||||
{
|
||||
rb_raise( rb_eRuntimeError, "x, y and z must be of same type" );
|
||||
}
|
||||
|
||||
if( rb_obj_is_kind_of( aFirst, rb_cNumeric ) == Qfalse )
|
||||
{
|
||||
rb_raise( rb_eRuntimeError, "x, y and z must be numeric!" );
|
||||
}
|
||||
}
|
||||
|
||||
static VALUE Vector3_Negate( VALUE self )
|
||||
{
|
||||
VALUE x = rb_funcall( self, rb_intern( "x" ), 0 );
|
||||
VALUE y = rb_funcall( self, rb_intern( "y" ), 0 );
|
||||
VALUE z = rb_funcall( self, rb_intern( "y" ), 0 );
|
||||
VALUE negatedX = rb_funcall( x, rb_intern( "-@" ), 0 );
|
||||
VALUE negatedY = rb_funcall( y, rb_intern( "-@" ), 0 );
|
||||
VALUE negatedZ = rb_funcall( z, rb_intern( "-@" ), 0 );
|
||||
return rb_funcall( globalVector3Class, rb_intern( "new" ), 2, negatedX, negatedY, negatedZ );
|
||||
}
|
||||
|
||||
static VALUE Vector3_Add( VALUE self, VALUE aRightOperand )
|
||||
{
|
||||
VALUE rightVector = Vector3_ForceType( aRightOperand );
|
||||
// Get values
|
||||
VALUE leftX = rb_funcall( self, rb_intern( "x" ), 0 );
|
||||
VALUE leftY = rb_funcall( self, rb_intern( "y" ), 0 );
|
||||
VALUE leftZ = rb_funcall( self, rb_intern( "z" ), 0 );
|
||||
VALUE rightX = rb_funcall( rightVector, rb_intern( "x" ), 0 );
|
||||
VALUE rightY = rb_funcall( rightVector, rb_intern( "y" ), 0 );
|
||||
VALUE rightZ = rb_funcall( rightVector, rb_intern( "z" ), 0 );
|
||||
|
||||
// Do calculation
|
||||
VALUE newX = rb_funcall( leftX, rb_intern( "+" ), 1, rightX );
|
||||
VALUE newY = rb_funcall( leftY, rb_intern( "+" ), 1, rightY );
|
||||
VALUE newZ = rb_funcall( leftZ, rb_intern( "+" ), 1, rightZ );
|
||||
|
||||
return rb_funcall( globalVector3Class, rb_intern( "new" ), 2, newX, newY, newZ );
|
||||
}
|
||||
|
||||
static VALUE Vector3_Subtract( VALUE self, VALUE aRightOperand )
|
||||
{
|
||||
VALUE rightVector = Vector3_ForceType( aRightOperand );
|
||||
// Get values
|
||||
VALUE leftX = rb_funcall( self, rb_intern( "x" ), 0 );
|
||||
VALUE leftY = rb_funcall( self, rb_intern( "y" ), 0 );
|
||||
VALUE leftZ = rb_funcall( self, rb_intern( "z" ), 0 );
|
||||
VALUE rightX = rb_funcall( rightVector, rb_intern( "x" ), 0 );
|
||||
VALUE rightY = rb_funcall( rightVector, rb_intern( "y" ), 0 );
|
||||
VALUE rightZ = rb_funcall( rightVector, rb_intern( "z" ), 0 );
|
||||
|
||||
// Do calculation
|
||||
VALUE newX = rb_funcall( leftX, rb_intern( "-" ), 1, rightX );
|
||||
VALUE newY = rb_funcall( leftY, rb_intern( "-" ), 1, rightY );
|
||||
VALUE newZ = rb_funcall( leftZ, rb_intern( "-" ), 1, rightZ );
|
||||
|
||||
return rb_funcall( globalVector3Class, rb_intern( "new" ), 2, newX, newY, newZ );
|
||||
}
|
||||
|
||||
static VALUE Vector3_Multiply( VALUE self, VALUE aRightOperand )
|
||||
{
|
||||
VALUE rightVector = Vector3_ForceType( aRightOperand );
|
||||
// Get values
|
||||
VALUE leftX = rb_funcall( self, rb_intern( "x" ), 0 );
|
||||
VALUE leftY = rb_funcall( self, rb_intern( "y" ), 0 );
|
||||
VALUE leftZ = rb_funcall( self, rb_intern( "z" ), 0 );
|
||||
VALUE rightX = rb_funcall( rightVector, rb_intern( "x" ), 0 );
|
||||
VALUE rightY = rb_funcall( rightVector, rb_intern( "y" ), 0 );
|
||||
VALUE rightZ = rb_funcall( rightVector, rb_intern( "z" ), 0 );
|
||||
|
||||
// Do calculation
|
||||
VALUE newX = rb_funcall( leftX, rb_intern( "*" ), 1, rightX );
|
||||
VALUE newY = rb_funcall( leftY, rb_intern( "*" ), 1, rightY );
|
||||
VALUE newZ = rb_funcall( leftZ, rb_intern( "*" ), 1, rightZ );
|
||||
|
||||
return rb_funcall( globalVector3Class, rb_intern( "new" ), 2, newX, newY, newZ );
|
||||
}
|
||||
|
||||
static VALUE Vector3_Divide( VALUE self, VALUE aRightOperand )
|
||||
{
|
||||
VALUE rightVector = Vector3_ForceType( aRightOperand );
|
||||
// Get values
|
||||
VALUE leftX = rb_funcall( self, rb_intern( "x" ), 0 );
|
||||
VALUE leftY = rb_funcall( self, rb_intern( "y" ), 0 );
|
||||
VALUE leftZ = rb_funcall( self, rb_intern( "z" ), 0 );
|
||||
VALUE rightX = rb_funcall( rightVector, rb_intern( "x" ), 0 );
|
||||
VALUE rightY = rb_funcall( rightVector, rb_intern( "y" ), 0 );
|
||||
VALUE rightZ = rb_funcall( rightVector, rb_intern( "z" ), 0 );
|
||||
|
||||
// Do calculation
|
||||
VALUE newX = rb_funcall( leftX, rb_intern( "/" ), 1, rightX );
|
||||
VALUE newY = rb_funcall( leftY, rb_intern( "/" ), 1, rightY );
|
||||
VALUE newZ = rb_funcall( leftZ, rb_intern( "/" ), 1, rightZ );
|
||||
|
||||
return rb_funcall( globalVector3Class, rb_intern( "new" ), 2, newX, newY, newZ );
|
||||
}
|
||||
|
||||
static VALUE Vector3_Equal( VALUE self, VALUE anArgument )
|
||||
{
|
||||
VALUE aVector = Vector3_ForceType( anArgument );
|
||||
VALUE leftX = rb_funcall( self, rb_intern( "x" ), 0 );
|
||||
VALUE leftY = rb_funcall( self, rb_intern( "y" ), 0 );
|
||||
VALUE leftZ = rb_funcall( self, rb_intern( "z" ), 0 );
|
||||
VALUE rightX = rb_funcall( aVector, rb_intern( "x" ), 0 );
|
||||
VALUE rightY = rb_funcall( aVector, rb_intern( "y" ), 0 );
|
||||
VALUE rightZ = rb_funcall( aVector, rb_intern( "z" ), 0 );
|
||||
|
||||
if( rb_funcall( leftX, rb_intern( "==" ), 1, rightX ) == Qtrue &&
|
||||
rb_funcall( leftY, rb_intern( "==" ), 1, rightY ) == Qtrue &&
|
||||
rb_funcall( leftZ, rb_intern( "==" ), 1, rightZ ) == Qtrue )
|
||||
{
|
||||
return Qtrue;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Qfalse;
|
||||
}
|
||||
}
|
||||
|
||||
static VALUE Vector3_StrictEqual( VALUE self, VALUE anArgument )
|
||||
{
|
||||
VALUE aVector = Vector3_ForceType( anArgument );
|
||||
VALUE leftX = rb_funcall( self, rb_intern( "x" ), 0 );
|
||||
VALUE leftY = rb_funcall( self, rb_intern( "y" ), 0 );
|
||||
VALUE leftZ = rb_funcall( self, rb_intern( "z" ), 0 );
|
||||
VALUE rightX = rb_funcall( aVector, rb_intern( "x" ), 0 );
|
||||
VALUE rightY = rb_funcall( aVector, rb_intern( "y" ), 0 );
|
||||
VALUE rightZ = rb_funcall( aVector, rb_intern( "z" ), 0 );
|
||||
|
||||
if( rb_funcall( leftX, rb_intern( "eql?" ), 1, rightX ) == Qtrue &&
|
||||
rb_funcall( leftY, rb_intern( "eql?" ), 1, rightY ) == Qtrue &&
|
||||
rb_funcall( leftZ, rb_intern( "eql?" ), 1, rightZ ) == Qtrue )
|
||||
{
|
||||
return Qtrue;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Qfalse;
|
||||
}
|
||||
}
|
||||
|
||||
static VALUE Vector3_Initialize( VALUE self, VALUE someArgs )
|
||||
{
|
||||
long arrayLength = RARRAY_LEN( someArgs );
|
||||
rb_iv_set( self, "@x", INT2NUM( 0 ) );
|
||||
rb_iv_set( self, "@y", INT2NUM( 0 ) );
|
||||
rb_iv_set( self, "@z", INT2NUM( 0 ) );
|
||||
|
||||
if( arrayLength == 0 )
|
||||
{
|
||||
// Nothing needs to be done
|
||||
}
|
||||
else if( arrayLength == 1 )
|
||||
{
|
||||
Vector3_internal_CopyFrom( self, rb_ary_entry( someArgs, 0 ) );
|
||||
}
|
||||
else if( arrayLength == 3 )
|
||||
{
|
||||
VALUE arg1 = rb_ary_entry( someArgs, 0 );
|
||||
VALUE arg2 = rb_ary_entry( someArgs, 1 );
|
||||
VALUE arg3 = rb_ary_entry( someArgs, 1 );
|
||||
Vector3_internal_ValidateTypes( arg1, arg2, arg3 );
|
||||
|
||||
rb_iv_set( self, "@x", arg1 );
|
||||
rb_iv_set( self, "@y", arg2 );
|
||||
rb_iv_set( self, "@z", arg3 );
|
||||
}
|
||||
|
||||
rb_iv_set( self, "@dataType", CLASS_OF( rb_iv_get( self, "@x" ) ) );
|
||||
return self;
|
||||
}
|
||||
|
||||
VALUE Vector3_New( int anArgCount, VALUE * someArgs, VALUE aKlass )
|
||||
{
|
||||
return rb_call_super( anArgCount, someArgs );
|
||||
}
|
||||
|
||||
void Init_Vector3( void )
|
||||
{
|
||||
globalVector3Class = rb_define_class_under( GetNamespace(), "Vector3", rb_cObject );
|
||||
|
||||
// Class methods
|
||||
rb_define_singleton_method( globalVector3Class, "new", FUNCPTR( Vector3_New ), -1 );
|
||||
|
||||
// Instance methods
|
||||
rb_define_method( globalVector3Class, "initialize", FUNCPTR( Vector3_Initialize ), -2 );
|
||||
rb_define_method( globalVector3Class, "eql?", FUNCPTR( Vector3_Initialize ), 1 );
|
||||
|
||||
// Instance operators
|
||||
rb_define_method( globalVector3Class, "-@", FUNCPTR( Vector3_Negate ), 0 );
|
||||
rb_define_method( globalVector3Class, "+", FUNCPTR( Vector3_Add ), 1 );
|
||||
rb_define_method( globalVector3Class, "-", FUNCPTR( Vector3_Subtract ), 1 );
|
||||
rb_define_method( globalVector3Class, "*", FUNCPTR( Vector3_Multiply ), 1 );
|
||||
rb_define_method( globalVector3Class, "/", FUNCPTR( Vector3_Divide ), 1 );
|
||||
rb_define_method( globalVector3Class, "==", FUNCPTR( Vector3_Divide ), 1 );
|
||||
|
||||
// Attribute accessors
|
||||
rb_define_attr( globalVector3Class, "x", 1, 1 );
|
||||
rb_define_attr( globalVector3Class, "y", 1, 1 );
|
||||
rb_define_attr( globalVector3Class, "z", 1, 1 );
|
||||
}
|
10
ruby/sfml-system/system/Vector3.hpp
Normal file
10
ruby/sfml-system/system/Vector3.hpp
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef SFML_RUBYEXT_VECTOR3_HEADER_
|
||||
#define SFML_RUBYEXT_VECTOR3_HEADER_
|
||||
|
||||
#include "ruby.h"
|
||||
|
||||
VALUE Vector3_ForceType( VALUE someValue );
|
||||
|
||||
void Init_Vector3( void );
|
||||
|
||||
#endif // SFML_RUBYEXT_VECTOR3_HEADER_
|
77
ruby/testing/vector2.rb
Normal file
77
ruby/testing/vector2.rb
Normal file
@ -0,0 +1,77 @@
|
||||
class Vector2
|
||||
attr_accessor :x, :y
|
||||
|
||||
def initialize( *args )
|
||||
if args.size == 0
|
||||
@x = 0
|
||||
@y = 0
|
||||
elsif args.size == 1
|
||||
copyFrom( args[0] )
|
||||
elsif args.size == 2
|
||||
Vector2.valid? args[0], args[1]
|
||||
|
||||
@x = args[0]
|
||||
@y = args[1]
|
||||
else
|
||||
raise ArgumentError.new( "invalid argument list" )
|
||||
end
|
||||
|
||||
@dataType = x.class
|
||||
end
|
||||
|
||||
def copyFrom( source )
|
||||
unless source.is_a?( Array ) || source.is_a?( Vector2 )
|
||||
raise ArgumentError.new( "expected Array or Vector2" )
|
||||
end
|
||||
Vector2.valid? source[0], source[1]
|
||||
|
||||
@x = source[0]
|
||||
@y = source[1]
|
||||
end
|
||||
|
||||
def -@
|
||||
Vector2.new( -x, -y )
|
||||
end
|
||||
|
||||
def +( right )
|
||||
Vector2.new( x + right.x, y + right.y )
|
||||
end
|
||||
|
||||
def -( right )
|
||||
Vector2.new( x - right.x, y - right.y )
|
||||
end
|
||||
|
||||
def *( right )
|
||||
Vector2.new( x * right.x, y * right.y )
|
||||
end
|
||||
|
||||
def /( right )
|
||||
Vector2.new( x / right.x, y / right.y )
|
||||
end
|
||||
|
||||
def ==( right )
|
||||
x == right.x && y == right.y
|
||||
end
|
||||
|
||||
def []( index )
|
||||
if index == 0 || index == :x
|
||||
return x
|
||||
elsif index == 1 || index == :y
|
||||
return y
|
||||
end
|
||||
|
||||
raise ArgumentError.new( "Expected index to be either 0..1 or :x and :y" )
|
||||
end
|
||||
|
||||
def self.valid?( x, y )
|
||||
if x.class != y.class
|
||||
raise RuntimeError.new( "x and y must be of same type" )
|
||||
end
|
||||
|
||||
if x.is_a?( Numeric ) == false
|
||||
raise RuntimeError.new( "x and y must be numeric!" )
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user