From 7d7c4c999fd1a14d9d6381076f876cf05d8febe1 Mon Sep 17 00:00:00 2001 From: groogy Date: Sun, 27 Feb 2011 14:07:10 +0000 Subject: [PATCH] Some refactoring, moved C++ allocation into the *_Alloc function instead of directly in new, removed new in most of classes too. Cloning should work on all copyable classes now. git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1802 4e206d99-4929-0410-ac5d-dfc041789085 --- bindings/ruby/sfml-audio/audio/Music.cpp | 9 +- bindings/ruby/sfml-audio/audio/Sound.cpp | 20 +++-- .../ruby/sfml-audio/audio/SoundBuffer.cpp | 9 +- .../sfml-audio/audio/SoundBufferRecorder.cpp | 9 +- .../ruby/sfml-audio/audio/SoundRecorder.cpp | 9 +- .../ruby/sfml-audio/audio/SoundSource.cpp | 9 +- .../ruby/sfml-audio/audio/SoundStream.cpp | 6 +- .../ruby/sfml-graphics/graphics/Drawable.cpp | 17 +++- bindings/ruby/sfml-graphics/graphics/Font.cpp | 14 +--- .../ruby/sfml-graphics/graphics/Glyph.cpp | 10 --- .../ruby/sfml-graphics/graphics/Image.cpp | 18 ++-- .../sfml-graphics/graphics/RenderImage.cpp | 14 +--- .../sfml-graphics/graphics/RenderWindow.cpp | 25 +----- .../ruby/sfml-graphics/graphics/Shader.cpp | 9 +- .../ruby/sfml-graphics/graphics/Shape.cpp | 9 +- .../ruby/sfml-graphics/graphics/Sprite.cpp | 73 ++++++++-------- bindings/ruby/sfml-graphics/graphics/Text.cpp | 9 +- bindings/ruby/sfml-graphics/graphics/View.cpp | 9 +- bindings/ruby/sfml-system/system/Clock.cpp | 15 ++-- bindings/ruby/sfml-window/window/Context.cpp | 14 +--- .../sfml-window/window/ContextSettings.cpp | 64 +++++++------- bindings/ruby/sfml-window/window/Event.cpp | 55 +++++------- bindings/ruby/sfml-window/window/Input.cpp | 15 +--- .../ruby/sfml-window/window/VideoMode.cpp | 84 ++++++++++--------- bindings/ruby/sfml-window/window/Window.cpp | 9 +- bindings/ruby/shared/global.hpp | 2 +- 26 files changed, 235 insertions(+), 301 deletions(-) diff --git a/bindings/ruby/sfml-audio/audio/Music.cpp b/bindings/ruby/sfml-audio/audio/Music.cpp index a97cdd1b..f3debeb2 100644 --- a/bindings/ruby/sfml-audio/audio/Music.cpp +++ b/bindings/ruby/sfml-audio/audio/Music.cpp @@ -86,12 +86,10 @@ static VALUE Music_GetDuration( VALUE self ) return rb_float_new( object->GetDuration() ); } -static VALUE Music_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE Music_Alloc( VALUE aKlass ) { sf::Music *object = new sf::Music(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, Music_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, Music_Free, object ); } void Init_Music( void ) @@ -134,7 +132,8 @@ void Init_Music( void ) globalMusicClass = rb_define_class_under( sfml, "Music", globalSoundStreamClass ); // Class methods - rb_define_singleton_method( globalMusicClass, "new", Music_New, -1 ); + //rb_define_singleton_method( globalMusicClass, "new", Music_New, -1 ); + rb_define_alloc_func( globalMusicClass, Music_Alloc ); // Instance methods rb_define_method( globalMusicClass, "initialize", Music_Initialize, -1 ); diff --git a/bindings/ruby/sfml-audio/audio/Sound.cpp b/bindings/ruby/sfml-audio/audio/Sound.cpp index de66db77..0c763192 100644 --- a/bindings/ruby/sfml-audio/audio/Sound.cpp +++ b/bindings/ruby/sfml-audio/audio/Sound.cpp @@ -91,6 +91,16 @@ static VALUE Sound_Initialize( int argc, VALUE *args, VALUE self ) return self; } +static VALUE Sound_InitializeCopy( VALUE self, VALUE aSource ) +{ + sf::Sound *selfObject = NULL; + Data_Get_Struct( self, sf::Sound, selfObject ); + sf::Sound *sourceObject = NULL; + Data_Get_Struct( aSource, sf::Sound, sourceObject ); + *selfObject = *sourceObject; + return self; +} + /* call-seq: * sound.play() * @@ -269,12 +279,10 @@ static VALUE Sound_ResetBuffer( VALUE self ) return Qnil; } -static VALUE Sound_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE Sound_Alloc( VALUE aKlass ) { sf::Sound *object = new sf::Sound(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, Sound_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, Sound_Free, object ); } void Init_Sound( void ) @@ -312,10 +320,12 @@ void Init_Sound( void ) globalSoundClass = rb_define_class_under( sfml, "Sound", globalSoundSourceClass ); // Class methods - rb_define_singleton_method( globalSoundClass, "new", Sound_New, -1 ); + //rb_define_singleton_method( globalSoundClass, "new", Sound_New, -1 ); + rb_define_alloc_func( globalSoundClass, Sound_Alloc ); // Instance methods rb_define_method( globalSoundClass, "initialize", Sound_Initialize, 0 ); + rb_define_method( globalSoundClass, "initialize_copy", Sound_InitializeCopy, 1 ); rb_define_method( globalSoundClass, "play", Sound_Play, 0 ); rb_define_method( globalSoundClass, "pause", Sound_Pause, 0 ); rb_define_method( globalSoundClass, "stop", Sound_Stop, 0 ); diff --git a/bindings/ruby/sfml-audio/audio/SoundBuffer.cpp b/bindings/ruby/sfml-audio/audio/SoundBuffer.cpp index d296885b..3f10710a 100644 --- a/bindings/ruby/sfml-audio/audio/SoundBuffer.cpp +++ b/bindings/ruby/sfml-audio/audio/SoundBuffer.cpp @@ -231,12 +231,10 @@ static VALUE SoundBuffer_InitializeCopy( VALUE self, VALUE aSource ) * * Creates an sound buffer instance for us. */ -static VALUE SoundBuffer_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE SoundBuffer_Alloc( VALUE aKlass ) { sf::SoundBuffer *object = new sf::SoundBuffer(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, SoundBuffer_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, SoundBuffer_Free, object ); } void Init_SoundBuffer( void ) @@ -294,7 +292,8 @@ void Init_SoundBuffer( void ) globalSoundBufferClass = rb_define_class_under( sfml, "SoundBuffer", rb_cObject ); // Class methods - rb_define_singleton_method( globalSoundBufferClass, "new", SoundBuffer_New, -1 ); + //rb_define_singleton_method( globalSoundBufferClass, "new", SoundBuffer_New, -1 ); + rb_define_alloc_func( globalSoundBufferClass, SoundBuffer_Alloc ); // Instance methods rb_define_method( globalSoundBufferClass, "initialize", SoundBuffer_Initialize, -1 ); diff --git a/bindings/ruby/sfml-audio/audio/SoundBufferRecorder.cpp b/bindings/ruby/sfml-audio/audio/SoundBufferRecorder.cpp index 786f409e..310e910a 100644 --- a/bindings/ruby/sfml-audio/audio/SoundBufferRecorder.cpp +++ b/bindings/ruby/sfml-audio/audio/SoundBufferRecorder.cpp @@ -126,12 +126,10 @@ static VALUE SoundBufferRecorder_GetBuffer( VALUE self ) * * Creates a sound buffer recorder instance for us. */ -static VALUE SoundBufferRecorder_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE SoundBufferRecorder_Alloc( VALUE aKlass ) { rbSoundBufferRecorder *object = new rbSoundBufferRecorder(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, SoundBufferRecorder_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, SoundBufferRecorder_Free, object ); } void Init_SoundBufferRecorder( void ) @@ -201,7 +199,8 @@ void Init_SoundBufferRecorder( void ) globalSoundBufferRecorderClass = rb_define_class_under( sfml, "SoundBufferRecorder", globalSoundRecorderClass ); // Class methods - rb_define_singleton_method( globalSoundBufferRecorderClass, "new", SoundBufferRecorder_New, -1 ); + //rb_define_singleton_method( globalSoundBufferRecorderClass, "new", SoundBufferRecorder_New, -1 ); + rb_define_alloc_func( globalSoundBufferRecorderClass, SoundBufferRecorder_Alloc ); // Instance methods rb_define_method( globalSoundRecorderClass, "getBuffer", SoundBufferRecorder_GetBuffer, 0 ); diff --git a/bindings/ruby/sfml-audio/audio/SoundRecorder.cpp b/bindings/ruby/sfml-audio/audio/SoundRecorder.cpp index 583f13c8..e07a0bcd 100644 --- a/bindings/ruby/sfml-audio/audio/SoundRecorder.cpp +++ b/bindings/ruby/sfml-audio/audio/SoundRecorder.cpp @@ -161,12 +161,10 @@ static VALUE SoundRecorder_GetSampleRate( VALUE self ) * * Creates a sound recorder instance for us. */ -static VALUE SoundRecorder_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE SoundRecorder_Alloc( VALUE aKlass ) { rbSoundRecorder *object = new rbSoundRecorder(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, SoundRecorder_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, SoundRecorder_Free, object ); } /* call-seq: @@ -250,7 +248,8 @@ void Init_SoundRecorder( void ) rb_include_module( globalSoundRecorderClass, globalNonCopyableModule ); // Class methods - rb_define_singleton_method( globalSoundRecorderClass, "new", SoundRecorder_New, -1 ); + //rb_define_singleton_method( globalSoundRecorderClass, "new", SoundRecorder_New, -1 ); + rb_define_alloc_func( globalSoundRecorderClass, SoundRecorder_Alloc ); rb_define_singleton_method( globalSoundRecorderClass, "isAvailable", SoundRecorder_IsAvailable, 0 ); // Instance methods diff --git a/bindings/ruby/sfml-audio/audio/SoundSource.cpp b/bindings/ruby/sfml-audio/audio/SoundSource.cpp index e4d34a69..83755c66 100644 --- a/bindings/ruby/sfml-audio/audio/SoundSource.cpp +++ b/bindings/ruby/sfml-audio/audio/SoundSource.cpp @@ -237,13 +237,9 @@ static VALUE SoundSource_SetVolume( VALUE self, VALUE aValue ) return Qnil; } -static VALUE SoundSource_InitializeCopy( VALUE self, VALUE aSource ) +static VALUE SoundSource_Initialize( VALUE self ) { - sf::SoundSource *object = NULL; - Data_Get_Struct( self, sf::SoundSource, object ); - sf::SoundSource *source = NULL; - Data_Get_Struct( aSource, sf::SoundSource, source ); - *object = *source; + rb_raise( rb_eNotImpError, "Trying to construct instance of abstract class" ); } static void DefineStatusEnum( void ) @@ -269,7 +265,6 @@ void Init_SoundSource( void ) DefineStatusEnum(); // Instance methods - rb_define_method( globalSoundSourceClass, "initialize_copy", SoundSource_InitializeCopy, 1 ); rb_define_method( globalSoundSourceClass, "getAttenuation", SoundSource_GetAttenuation, 0 ); rb_define_method( globalSoundSourceClass, "getMinDistance", SoundSource_GetMinDistance, 0 ); rb_define_method( globalSoundSourceClass, "getPitch", SoundSource_GetPitch, 0 ); diff --git a/bindings/ruby/sfml-audio/audio/SoundStream.cpp b/bindings/ruby/sfml-audio/audio/SoundStream.cpp index 50cdcf8b..25861fa1 100644 --- a/bindings/ruby/sfml-audio/audio/SoundStream.cpp +++ b/bindings/ruby/sfml-audio/audio/SoundStream.cpp @@ -287,12 +287,11 @@ static VALUE SoundStream_Initialize( VALUE self, VALUE channelsCount, VALUE samp return self; } -static VALUE SoundStream_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE SoundStream_Alloc( VALUE aKlass ) { rbSoundStream *object = new rbSoundStream(); VALUE rbData = Data_Wrap_Struct( aKlass, 0, SoundStream_Free, object ); object->Init( rbData ); - rb_obj_call_init( rbData, argc, args ); return rbData; } @@ -361,7 +360,8 @@ void Init_SoundStream( void ) rb_include_module( globalSoundStreamClass, globalNonCopyableModule ); // Class methods - rb_define_singleton_method( globalSoundStreamClass, "new", SoundStream_New, -1 ); + //rb_define_singleton_method( globalSoundStreamClass, "new", SoundStream_New, -1 ); + rb_define_alloc_func( globalSoundStreamClass, SoundStream_Alloc ); // Instance methods rb_define_method( globalSoundStreamClass, "initialize", SoundStream_Initialize, 2 ); diff --git a/bindings/ruby/sfml-graphics/graphics/Drawable.cpp b/bindings/ruby/sfml-graphics/graphics/Drawable.cpp index 38efe6f9..96b2bc7d 100644 --- a/bindings/ruby/sfml-graphics/graphics/Drawable.cpp +++ b/bindings/ruby/sfml-graphics/graphics/Drawable.cpp @@ -529,18 +529,28 @@ static VALUE Drawable_Initialize( int argc, VALUE *args, VALUE self ) return rb_call_super( argc, args ); } -static VALUE Drawable_New( int argc, VALUE *args, VALUE aKlass ) + +static VALUE Drawable_InitializeCopy( VALUE self, VALUE aSource ) +{ + sf::Drawable *selfDrawable = NULL; + Data_Get_Struct( self, sf::Drawable, selfDrawable ); + sf::Drawable *sourceDrawable = NULL; + Data_Get_Struct( aSource, sf::Drawable, sourceDrawable ); + *selfDrawable = *sourceDrawable; + return self; +} + +static VALUE Drawable_Allocate( VALUE aKlass ) { rbDrawable *object = new rbDrawable(); VALUE rbData = Data_Wrap_Struct( aKlass, 0, Drawable_Free, object ); object->Init( rbData ); - rb_obj_call_init( rbData, argc, args ); return rbData; } static VALUE Drawable_Included( VALUE aModule, VALUE aBase ) { - rb_define_singleton_method( aBase, "new", Drawable_New, -1 ); + rb_define_singleton_method( aBase, "allocate", Drawable_Allocate, 0 ); return Qnil; } @@ -615,6 +625,7 @@ void Init_Drawable( void ) // Instance methods rb_define_method( globalDrawableModule, "initialize", Drawable_Initialize, -1 ); + rb_define_method( globalDrawableModule, "initialize_copy", Drawable_Initialize, 1 ); rb_define_method( globalDrawableModule, "setPosition", Drawable_SetPosition, -1 ); rb_define_method( globalDrawableModule, "setX", Drawable_SetX, 1 ); rb_define_method( globalDrawableModule, "setY", Drawable_SetY, 1 ); diff --git a/bindings/ruby/sfml-graphics/graphics/Font.cpp b/bindings/ruby/sfml-graphics/graphics/Font.cpp index c434c2c5..8ee72c7e 100644 --- a/bindings/ruby/sfml-graphics/graphics/Font.cpp +++ b/bindings/ruby/sfml-graphics/graphics/Font.cpp @@ -156,17 +156,10 @@ static VALUE Font_InitializeCopy( VALUE self, VALUE aSource ) *object = *source; } -/* call-seq: - * Font.new() -> font - * - * Creates an empty font - */ -static VALUE Font_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE Font_Alloc( VALUE aKlass ) { sf::Font *object = new sf::Font(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, Font_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, Font_Free, object ); } /* call-seq: @@ -241,7 +234,8 @@ void Init_Font( void ) globalFontClass = rb_define_class_under( sfml, "Font", rb_cObject ); // Class methods - rb_define_singleton_method( globalFontClass, "new", Font_New, -1 ); + //rb_define_singleton_method( globalFontClass, "new", Font_New, -1 ); + rb_define_alloc_func( globalFontClass, Font_Alloc ); rb_define_singleton_method( globalFontClass, "getDefaultFont", Font_GetDefaultFont, 0 ); // Instance methods diff --git a/bindings/ruby/sfml-graphics/graphics/Glyph.cpp b/bindings/ruby/sfml-graphics/graphics/Glyph.cpp index a201f2c4..5ea1df26 100644 --- a/bindings/ruby/sfml-graphics/graphics/Glyph.cpp +++ b/bindings/ruby/sfml-graphics/graphics/Glyph.cpp @@ -43,15 +43,6 @@ static VALUE Glyph_Initialize( VALUE self ) return self; } -static VALUE Glyph_InitializeCopy( VALUE self, VALUE aSource ) -{ - sf::Glyph *object = NULL; - Data_Get_Struct( self, sf::Glyph, object ); - sf::Glyph *source = NULL; - Data_Get_Struct( aSource, sf::Glyph, source ); - *object = *source; -} - void Init_Glyph( void ) { /* SFML namespace which contains the classes of this module. */ @@ -70,7 +61,6 @@ void Init_Glyph( void ) // Instance methods rb_define_method( globalGlyphClass, "initialize", Glyph_Initialize, 0 ); - rb_define_method( globalGlyphClass, "initialize_copy", Glyph_InitializeCopy, 1 ); // Attribute accessors rb_define_attr( globalGlyphClass, "advance", 1, 1 ); diff --git a/bindings/ruby/sfml-graphics/graphics/Image.cpp b/bindings/ruby/sfml-graphics/graphics/Image.cpp index 15cf7427..670de267 100644 --- a/bindings/ruby/sfml-graphics/graphics/Image.cpp +++ b/bindings/ruby/sfml-graphics/graphics/Image.cpp @@ -524,8 +524,8 @@ static VALUE Image_GetTexCoords( VALUE self, VALUE aRectangle ) * * Will create a new image instance. * - * If a filename argument is specified then image#loadFromFile will be called on the created instance. If width, height - * and pixels are specified then image#loadFromPixels will be called on the created instance. + * If a filename argument is specified then Image#loadFromFile will be called on the created instance. If width, height + * and pixels are specified then Image#loadFromPixels will be called on the created instance. */ static VALUE Image_Initialize( int argc, VALUE *args, VALUE self ) { @@ -549,17 +549,10 @@ static VALUE Image_InitializeCopy( VALUE self, VALUE aSource ) *object = *source; } -/* call-seq: - * Image.new() -> image - * - * Creates an image instance for us. - */ -static VALUE Image_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE Image_Alloc( VALUE aKlass ) { sf::Image *object = new sf::Image(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, Image_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, Image_Free, object ); } /* call-seq: @@ -624,7 +617,8 @@ void Init_Image( void ) globalImageClass = rb_define_class_under( sfml, "Image", rb_cObject ); // Class methods - rb_define_singleton_method( globalImageClass, "new", Image_New, -1 ); + //rb_define_singleton_method( globalImageClass, "new", Image_New, -1 ); + rb_define_alloc_func( globalImageClass, Image_Alloc ); rb_define_singleton_method( globalImageClass, "getMaximumSize", Image_GetMaximumSize, 0 ); // Instance methods diff --git a/bindings/ruby/sfml-graphics/graphics/RenderImage.cpp b/bindings/ruby/sfml-graphics/graphics/RenderImage.cpp index b566194c..29f6dc19 100644 --- a/bindings/ruby/sfml-graphics/graphics/RenderImage.cpp +++ b/bindings/ruby/sfml-graphics/graphics/RenderImage.cpp @@ -300,17 +300,10 @@ static VALUE RenderImage_Initialize( int argc, VALUE *args, VALUE self ) return self; } -/* call-seq: - * RenderImage.new() -> render_image - * - * Constructs an empty, invalid render-image. You must call create() to have a valid render-image. - */ -static VALUE RenderImage_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE RenderImage_Alloc( VALUE aKlass ) { sf::RenderImage *object = new sf::RenderImage(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, RenderImage_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, RenderImage_Free, object ); } /* call-seq: @@ -396,7 +389,8 @@ void Init_RenderImage( void ) rb_include_module( globalRenderImageClass, globalRenderTargetModule ); // Class methods - rb_define_singleton_method( globalRenderImageClass, "new", RenderImage_New, 0 ); + //rb_define_singleton_method( globalRenderImageClass, "new", RenderImage_New, 0 ); + rb_define_alloc_func( globalRenderImageClass, RenderImage_Alloc ); rb_define_singleton_method( globalRenderImageClass, "isAvailable", RenderImage_IsAvailable, 0 ); // Instance methods diff --git a/bindings/ruby/sfml-graphics/graphics/RenderWindow.cpp b/bindings/ruby/sfml-graphics/graphics/RenderWindow.cpp index 28f16359..c1ac12ee 100644 --- a/bindings/ruby/sfml-graphics/graphics/RenderWindow.cpp +++ b/bindings/ruby/sfml-graphics/graphics/RenderWindow.cpp @@ -121,28 +121,10 @@ static VALUE RenderWindow_GetHeight( VALUE self ) return INT2FIX( object->GetHeight() ); } -/* call-seq: - * Window.new() -> render_window - * Window.new( mode, title, style = SFML::Style::Default, settings = SFML::ContextSettings.new ) -> render_window - * - * Construct a new window. - * - * The first form of new doesn't actually create the visual window, use the other form of new or call - * SFML::Window#create to do so. - * - * The second form of new creates the window with the size and pixel depth defined in mode. An optional style can be passed - * to customize the look and behaviour of the window (borders, title bar, resizable, closable, ...). If style contains - * Style::Fullscreen, then mode must be a valid video mode. - * - * The fourth parameter is an optional structure specifying advanced OpenGL context settings such as antialiasing, - * depth-buffer bits, etc. You shouldn't care about these parameters for a regular usage of the graphics module. - */ -static VALUE RenderWindow_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE RenderWindow_Alloc( VALUE aKlass ) { sf::RenderWindow *object = new sf::RenderWindow(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, RenderWindow_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, RenderWindow_Free, object ); } void Init_RenderWindow( void ) @@ -236,7 +218,8 @@ void Init_RenderWindow( void ) rb_include_module( globalRenderWindowClass, globalRenderTargetModule ); // Class methods - rb_define_singleton_method( globalRenderWindowClass, "new", RenderWindow_New, -1 ); + //rb_define_singleton_method( globalRenderWindowClass, "new", RenderWindow_New, -1 ); + rb_define_alloc_func( globalRenderWindowClass, RenderWindow_Alloc ); // Instance methods rb_define_method( globalRenderWindowClass, "draw", RenderWindow_Draw, -1 ); diff --git a/bindings/ruby/sfml-graphics/graphics/Shader.cpp b/bindings/ruby/sfml-graphics/graphics/Shader.cpp index 92c1f859..a9aa0b9c 100644 --- a/bindings/ruby/sfml-graphics/graphics/Shader.cpp +++ b/bindings/ruby/sfml-graphics/graphics/Shader.cpp @@ -246,12 +246,10 @@ static VALUE Shader_InitializeCopy( VALUE self, VALUE aSource ) * * Create a new shader. */ -static VALUE Shader_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE Shader_Alloc( VALUE aKlass ) { sf::Shader *object = new sf::Shader(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, Shader_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, Shader_Free, object ); } /* call-seq: @@ -335,7 +333,8 @@ void Init_Shader( void ) globalShaderClass = rb_define_class_under( sfml, "Shader", rb_cObject ); // Class methods - rb_define_singleton_method( globalShaderClass, "new", Shader_New, -1 ); + //rb_define_singleton_method( globalShaderClass, "new", Shader_New, -1 ); + rb_define_alloc_func( globalShaderClass, Shader_Alloc ); rb_define_singleton_method( globalShaderClass, "isAvailable", Shader_IsAvailable, 0 ); // Class Constants diff --git a/bindings/ruby/sfml-graphics/graphics/Shape.cpp b/bindings/ruby/sfml-graphics/graphics/Shape.cpp index 8b3a2bb5..9eaf96a2 100644 --- a/bindings/ruby/sfml-graphics/graphics/Shape.cpp +++ b/bindings/ruby/sfml-graphics/graphics/Shape.cpp @@ -344,12 +344,10 @@ static VALUE Shape_InitializeCopy( VALUE self, VALUE aSource ) * * Create an empty shape. */ -static VALUE Shape_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE Shape_Alloc( VALUE aKlass ) { sf::Shape *object = new sf::Shape(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, Shape_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, Shape_Free, object ); } /* call-seq: @@ -646,7 +644,8 @@ void Init_Shape( void ) rb_include_module( globalShapeClass, globalDrawableModule ); // Class methods - rb_define_singleton_method( globalShapeClass, "new", Shape_New, -1 ); + //rb_define_singleton_method( globalShapeClass, "new", Shape_New, -1 ); + rb_define_alloc_func( globalShapeClass, Shape_Alloc ); rb_define_singleton_method( globalShapeClass, "line", Shape_Line, -1 ); rb_define_singleton_method( globalShapeClass, "rectangle", Shape_Rectangle, -1 ); rb_define_singleton_method( globalShapeClass, "circle", Shape_Circle, -1 ); diff --git a/bindings/ruby/sfml-graphics/graphics/Sprite.cpp b/bindings/ruby/sfml-graphics/graphics/Sprite.cpp index d0f76f39..f732e162 100644 --- a/bindings/ruby/sfml-graphics/graphics/Sprite.cpp +++ b/bindings/ruby/sfml-graphics/graphics/Sprite.cpp @@ -19,7 +19,7 @@ * 3. This notice may not be removed or altered from any * source distribution. */ - + #include "Sprite.hpp" #include "Vector2.hpp" #include "Rect.hpp" @@ -44,7 +44,7 @@ static void Sprite_Free( sf::Sprite *anObject ) * Sprite.new() -> sprite * Sprite.new( image, position = [0, 0], scale = [1, 1], rotation = 0.0, color = SFML::Color::White ) -> sprite * - * Construct the sprite from a source image. + * Construct the sprite from a source image. */ static VALUE Sprite_Initialize( int argc, VALUE *args, VALUE self ) { @@ -54,7 +54,7 @@ static VALUE Sprite_Initialize( int argc, VALUE *args, VALUE self ) sf::Vector2f scale = sf::Vector2f( 1, 1 ); float rotation = 0; sf::Color color = sf::Color::White; - + sf::Sprite *object = NULL; Data_Get_Struct( self, sf::Sprite, object ); switch( argc ) @@ -102,17 +102,17 @@ static VALUE Sprite_InitializeCopy( VALUE self, VALUE aSource ) * * Change the source image of the sprite. * - * The image argument refers to an image that must exist as long as the sprite uses it. Indeed, the sprite doesn't - * store its own copy of the image, but rather keeps a pointer to the one that you passed to this function. If the + * The image argument refers to an image that must exist as long as the sprite uses it. Indeed, the sprite doesn't + * store its own copy of the image, but rather keeps a pointer to the one that you passed to this function. If the * source image is destroyed and the sprite tries to use it, it may appear as a white rectangle. If adjustToNewSize is - * true, the SubRect property of the sprite is adjusted to the size of the new image. If it is false, the SubRect + * true, the SubRect property of the sprite is adjusted to the size of the new image. If it is false, the SubRect * is unchanged. */ static VALUE Sprite_SetImage( int argc, VALUE *args, VALUE self ) { sf::Image *image = NULL; bool adjustToNewSize = false; - + sf::Sprite *object = NULL; Data_Get_Struct( self, sf::Sprite, object ); rb_iv_set( self, "@__image_ref", Qnil ); @@ -202,7 +202,7 @@ static VALUE Sprite_Resize( int argc, VALUE *args, VALUE self ) /* call-seq: * sprite.flipX( flipped ) * - * Flip the sprite horizontally. + * Flip the sprite horizontally. */ static VALUE Sprite_FlipX( VALUE self, VALUE aFlippedFlag ) { @@ -226,7 +226,7 @@ static VALUE Sprite_FlipX( VALUE self, VALUE aFlippedFlag ) /* call-seq: * sprite.flipY( flipped ) * - * Flip the sprite vertically. + * Flip the sprite vertically. */ static VALUE Sprite_FlipY( VALUE self, VALUE aFlippedFlag ) { @@ -262,15 +262,15 @@ static VALUE Sprite_GetImage( VALUE self ) /* call-seq: * sprite.getSubRect() -> rectangle * - * Get the region of the image displayed by the sprite. + * Get the region of the image displayed by the sprite. */ static VALUE Sprite_GetSubRect( VALUE self ) { sf::Sprite *object = NULL; Data_Get_Struct( self, sf::Sprite, object ); const sf::IntRect &rect = object->GetSubRect(); - return rb_funcall( globalRectClass, rb_intern( "new" ), 4, - INT2FIX( rect.Left ), INT2FIX( rect.Top ), + return rb_funcall( globalRectClass, rb_intern( "new" ), 4, + INT2FIX( rect.Left ), INT2FIX( rect.Top ), INT2FIX( rect.Width ), INT2FIX( rect.Height ) ); } @@ -286,7 +286,7 @@ static VALUE Sprite_GetSize( VALUE self ) sf::Sprite *object = NULL; Data_Get_Struct( self, sf::Sprite, object ); const sf::Vector2f size = object->GetSize(); - return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, rb_float_new( size.x ), rb_float_new( size.y ) ); + return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, rb_float_new( size.x ), rb_float_new( size.y ) ); } /* call-seq: @@ -294,8 +294,8 @@ static VALUE Sprite_GetSize( VALUE self ) * * Get the color of a given pixel in the sprite. * - * This function returns the source image pixel, multiplied by the global color of the sprite. The input point must - * be in local coordinates. If you have a global point, you can use the TransformToLocal function to make it local. + * This function returns the source image pixel, multiplied by the global color of the sprite. The input point must + * be in local coordinates. If you have a global point, you can use the TransformToLocal function to make it local. * This function doesn't perform any check, you must ensure that the x and y coordinates are not out of bounds. */ static VALUE Sprite_GetPixel( VALUE self, VALUE aX, VALUE aY ) @@ -303,17 +303,15 @@ static VALUE Sprite_GetPixel( VALUE self, VALUE aX, VALUE aY ) sf::Sprite *object = NULL; Data_Get_Struct( self, sf::Sprite, object ); const sf::Color color = object->GetPixel( FIX2UINT( aX ), FIX2UINT( aY ) ); - return rb_funcall( globalColorClass, rb_intern( "new" ), 4, - INT2FIX( color.r ), INT2FIX( color.g ), + return rb_funcall( globalColorClass, rb_intern( "new" ), 4, + INT2FIX( color.r ), INT2FIX( color.g ), INT2FIX( color.b ), INT2FIX( color.a ) ); } -static VALUE Sprite_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE Sprite_Alloc( VALUE aKlass ) { sf::Sprite *object = new sf::Sprite(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, Sprite_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, Sprite_Free, object ); } void Init_Sprite( void ) @@ -324,20 +322,20 @@ void Init_Sprite( void ) * * SFML::Sprite is a drawable class that allows to easily display an image (or a part of it) on a render target. * - * It inherits all the functions from SFML::Drawable: position, rotation, scale, origin, global color and blend mode. - * It also adds sprite-specific properties such as the image to use, the part of it to display, and some convenience + * It inherits all the functions from SFML::Drawable: position, rotation, scale, origin, global color and blend mode. + * It also adds sprite-specific properties such as the image to use, the part of it to display, and some convenience * functions to flip or resize the sprite. * - * SFML::Sprite works in combination with the SFML::Image class, which loads and provides the pixel data of a + * SFML::Sprite works in combination with the SFML::Image class, which loads and provides the pixel data of a * given image. * - * The separation of SFML::Sprite and SFML::Image allows more flexibility and better performances: indeed a SFML::Image - * is a heavy resource, and any operation on it is slow (often too slow for real-time applications). On the other side, - * a SFML::Sprite is a lightweight object which can use the pixel data of a SFML::Image and draw it with its own + * The separation of SFML::Sprite and SFML::Image allows more flexibility and better performances: indeed a SFML::Image + * is a heavy resource, and any operation on it is slow (often too slow for real-time applications). On the other side, + * a SFML::Sprite is a lightweight object which can use the pixel data of a SFML::Image and draw it with its own * transformation / color / blending attributes. * - * It is important to note that the SFML::Sprite instance doesn't copy the image that it uses, it only keeps a reference - * to it. Thus, a SFML::Image must not be destructed while it is used by a SFML::Sprite (i.e. never write a function that + * It is important to note that the SFML::Sprite instance doesn't copy the image that it uses, it only keeps a reference + * to it. Thus, a SFML::Image must not be destructed while it is used by a SFML::Sprite (i.e. never write a function that * uses a local SFML::Image instance for creating a sprite). * * NOTE: This is the ruby bindings so the images will be managed by the ruby garbage collector and thus the image won't @@ -361,10 +359,11 @@ void Init_Sprite( void ) */ globalSpriteClass = rb_define_class_under( sfml, "Sprite", rb_cObject ); rb_include_module( globalSpriteClass, globalDrawableModule ); - + // Class methods - rb_define_singleton_method( globalSpriteClass, "new", Sprite_New, -1 ); - + //rb_define_singleton_method( globalSpriteClass, "new", Sprite_New, -1 ); + rb_define_alloc_func( globalSpriteClass, Sprite_Alloc ); + // Instance methods rb_define_method( globalSpriteClass, "initialize", Sprite_Initialize, -1 ); rb_define_method( globalSpriteClass, "initialize_copy", Sprite_InitializeCopy, 1 ); @@ -377,27 +376,27 @@ void Init_Sprite( void ) rb_define_method( globalSpriteClass, "getSubRect", Sprite_GetSubRect, 0 ); rb_define_method( globalSpriteClass, "getSize", Sprite_GetSize, 0 ); rb_define_method( globalSpriteClass, "getPixel", Sprite_GetPixel, 2 ); - + // Instance Aliases rb_define_alias( globalSpriteClass, "image=", "setImage" ); rb_define_alias( globalSpriteClass, "set_image", "setImage" ); rb_define_alias( globalSpriteClass, "image", "getImage" ); rb_define_alias( globalSpriteClass, "get_image", "getImage" ); - + rb_define_alias( globalSpriteClass, "subRect=", "setSubRect" ); rb_define_alias( globalSpriteClass, "sub_rect=", "setSubRect" ); rb_define_alias( globalSpriteClass, "subRect", "getSubRect" ); rb_define_alias( globalSpriteClass, "sub_rect", "getSubRect" ); - + rb_define_alias( globalSpriteClass, "flip_x", "flipX" ); rb_define_alias( globalSpriteClass, "flip_y", "flipY" ); rb_define_alias( globalSpriteClass, "flip_x=", "flipX" ); rb_define_alias( globalSpriteClass, "flip_y=", "flipY" ); rb_define_alias( globalSpriteClass, "flipX=", "flipX" ); rb_define_alias( globalSpriteClass, "flipY=", "flipY" ); - + rb_define_alias( globalSpriteClass, "get_size", "getSize" ); rb_define_alias( globalSpriteClass, "size", "getSize" ); - + rb_define_alias( globalSpriteClass, "get_pixel", "getPixel" ); } diff --git a/bindings/ruby/sfml-graphics/graphics/Text.cpp b/bindings/ruby/sfml-graphics/graphics/Text.cpp index 7d546930..65f7cd1d 100644 --- a/bindings/ruby/sfml-graphics/graphics/Text.cpp +++ b/bindings/ruby/sfml-graphics/graphics/Text.cpp @@ -229,12 +229,10 @@ static VALUE Text_GetRect( VALUE self ) rb_float_new( rect.Width ), rb_float_new( rect.Height ) ); } -static VALUE Text_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE Text_Alloc( VALUE aKlass ) { sf::Text *object = new sf::Text(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, Text_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, Text_Free, object ); } static void CreateStyleEnum() @@ -293,7 +291,8 @@ void Init_Text( void ) CreateStyleEnum(); // Class methods - rb_define_singleton_method( globalTextClass, "new", Text_New, -1 ); + //rb_define_singleton_method( globalTextClass, "new", Text_New, -1 ); + rb_define_alloc_func( globalTextClass, Text_Alloc ); // Instance methods rb_define_method( globalTextClass, "initialize", Text_Initialize, -1 ); diff --git a/bindings/ruby/sfml-graphics/graphics/View.cpp b/bindings/ruby/sfml-graphics/graphics/View.cpp index 4bb17476..441fed12 100644 --- a/bindings/ruby/sfml-graphics/graphics/View.cpp +++ b/bindings/ruby/sfml-graphics/graphics/View.cpp @@ -349,12 +349,10 @@ static VALUE View_Zoom( VALUE self, VALUE aFactor ) return Qnil; } -static VALUE View_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE View_Alloc( VALUE aKlass ) { sf::View *object = new sf::View(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, View_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, View_Free, object ); } void Init_View( void ) @@ -408,7 +406,8 @@ void Init_View( void ) globalViewClass = rb_define_class_under( sfml, "View", rb_cObject ); // Class methods - rb_define_singleton_method( globalViewClass, "new", View_New, -1 ); + //rb_define_singleton_method( globalViewClass, "new", View_New, -1 ); + rb_define_alloc_func( globalViewClass, View_Alloc ); // Instance methods rb_define_method( globalViewClass, "initialize", View_Initialize, -1 ); diff --git a/bindings/ruby/sfml-system/system/Clock.cpp b/bindings/ruby/sfml-system/system/Clock.cpp index 9b279178..6ba25d99 100644 --- a/bindings/ruby/sfml-system/system/Clock.cpp +++ b/bindings/ruby/sfml-system/system/Clock.cpp @@ -75,12 +75,10 @@ static VALUE Clock_InitializeCopy( VALUE self, VALUE aSource ) * * The clock starts automatically after being constructed. */ -static VALUE Clock_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE Clock_Allocate( VALUE aKlass ) { sf::Clock *object = new sf::Clock(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, Clock_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, Clock_Free, object ); } void Init_Clock( void ) @@ -94,16 +92,17 @@ void Init_Clock( void ) * Its resolution depends on the underlying OS, but you can generally expect a 1 ms resolution. */ globalClockClass = rb_define_class_under( sfml, "Clock", rb_cObject ); + rb_define_alloc_func( globalClockClass, Clock_Allocate ); // Class methods - rb_define_singleton_method( globalClockClass, "new", Clock_New, -1 ); + //rb_define_singleton_method( globalClockClass, "new", Clock_New, -1 ); // Instance methods rb_define_method( globalClockClass, "initialize_copy", Clock_InitializeCopy, 1 ); - rb_define_method( globalClockClass, "getElapsedTime", Clock_GetElapsedTime, 0 ); + rb_define_method( globalClockClass, "elapsed_time", Clock_GetElapsedTime, 0 ); rb_define_method( globalClockClass, "reset", Clock_Reset, 0 ); // Aliases - rb_define_alias( globalClockClass, "elapsedTime", "getElapsedTime" ); - rb_define_alias( globalClockClass, "elapsed_time", "getElapsedTime" ); + rb_define_alias( globalClockClass, "elapsedTime", "elapsed_time" ); + rb_define_alias( globalClockClass, "getElapsedTime", "elapsed_time" ); } diff --git a/bindings/ruby/sfml-window/window/Context.cpp b/bindings/ruby/sfml-window/window/Context.cpp index 6aa67fe5..9d606120 100644 --- a/bindings/ruby/sfml-window/window/Context.cpp +++ b/bindings/ruby/sfml-window/window/Context.cpp @@ -79,17 +79,10 @@ static VALUE Context_SetReferenceActive( VALUE aKlass ) } } -/* call-seq: - * Context.new() -> context - * - * The constructor creates and activates the context - */ -static VALUE Context_New( VALUE aKlass ) +static VALUE Context_Alloc( VALUE aKlass ) { sf::Context *object = new sf::Context(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, Context_Free, object ); - rb_obj_call_init( rbData, 0, 0 ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, Context_Free, object ); } void Init_Context( void ) @@ -115,7 +108,8 @@ void Init_Context( void ) rb_include_module( globalContextClass, globalNonCopyableModule ); // Class methods - rb_define_singleton_method( globalContextClass, "new", Context_New, 0 ); + //rb_define_singleton_method( globalContextClass, "new", Context_New, 0 ); + rb_define_alloc_func( globalContextClass, Context_Alloc ); rb_define_singleton_method( globalContextClass, "setReferenceActive", Context_SetReferenceActive, 0 ); // Instance methods diff --git a/bindings/ruby/sfml-window/window/ContextSettings.cpp b/bindings/ruby/sfml-window/window/ContextSettings.cpp index 6b9912a9..9ca2bb32 100644 --- a/bindings/ruby/sfml-window/window/ContextSettings.cpp +++ b/bindings/ruby/sfml-window/window/ContextSettings.cpp @@ -25,7 +25,6 @@ #include #include - VALUE globalContextSettingsClass; /* Free a heap allocated object @@ -171,43 +170,36 @@ static VALUE ContextSettings_InitializeCopy( VALUE self, VALUE aSource ) * * The constructor creates the settings */ -static VALUE ContextSettings_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE ContextSettings_Alloc( VALUE aKlass ) +{ + sf::ContextSettings *object = new sf::ContextSettings(); + return Data_Wrap_Struct( aKlass, 0, ContextSettings_Free, object );; +} + +static VALUE ContextSettings_Initialize( int argc, VALUE *args, VALUE self ) { sf::ContextSettings *object = NULL; - if( argc == 0 ) + Data_Get_Struct( self, sf::ContextSettings, object ); + switch( argc ) { - object = new sf::ContextSettings(); + case 0: + break; + case 5: + object->MinorVersion = NUM2UINT( args[4] ); + case 4: + object->MajorVersion = NUM2UINT( args[3] ); + case 3: + object->AntialiasingLevel = NUM2UINT( args[2] ); + case 2: + object->StencilBits = NUM2UINT( args[1] ); + case 1: + object->DepthBits = NUM2UINT( args[0] ); + break; + default: + rb_raise( rb_eArgError, "Expected 0..5 arguments but was given %d", argc ); + return Qnil; } - else if( argc == 1 ) - { - object = new sf::ContextSettings( NUM2UINT( args[0] ) ); - } - else if( argc == 2 ) - { - object = new sf::ContextSettings( NUM2UINT( args[0] ), NUM2UINT( args[1] ) ); - } - - else if( argc == 3 ) - { - object = new sf::ContextSettings( NUM2UINT( args[0] ), NUM2UINT( args[1] ), NUM2UINT( args[2] ) ); - } - else if( argc == 4 ) - { - object = new sf::ContextSettings( NUM2UINT( args[0] ), NUM2UINT( args[1] ), NUM2UINT( args[2] ), NUM2UINT( args[3] ) ); - } - else if( argc == 5 ) - { - object = new sf::ContextSettings( NUM2UINT( args[0] ), NUM2UINT( args[1] ), NUM2UINT( args[2] ), NUM2UINT( args[3] ), NUM2UINT( args[4] ) ); - } - else - { - rb_raise( rb_eArgError, "Expected 0..5 arguments but was given %d", argc ); - return Qnil; - } - - VALUE rbData = Data_Wrap_Struct( aKlass, 0, ContextSettings_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return self; } void Init_ContextSettings( void ) @@ -242,9 +234,11 @@ void Init_ContextSettings( void ) globalContextSettingsClass = rb_define_class_under( sfml, "ContextSettings", rb_cObject ); // Class methods - rb_define_singleton_method( globalContextSettingsClass, "new", ContextSettings_New, -1 ); + //rb_define_singleton_method( globalContextSettingsClass, "new", ContextSettings_New, -1 ); + rb_define_alloc_func( globalContextSettingsClass, ContextSettings_Alloc ); // Instance methods + rb_define_method( globalContextSettingsClass, "initialize", ContextSettings_Initialize, -1 ); rb_define_method( globalContextSettingsClass, "initialize_copy", ContextSettings_InitializeCopy, 1 ); rb_define_method( globalContextSettingsClass, "depthBits", ContextSettings_GetDepth, 0 ); diff --git a/bindings/ruby/sfml-window/window/Event.cpp b/bindings/ruby/sfml-window/window/Event.cpp index c3f62d2c..47584f47 100644 --- a/bindings/ruby/sfml-window/window/Event.cpp +++ b/bindings/ruby/sfml-window/window/Event.cpp @@ -145,7 +145,14 @@ EVENT_TYPE_ACCESSORS( Size, Height, INT2NUM ) static VALUE TextEvent_GetUnicode( VALUE self ) EVENT_TYPE_ACCESSORS( Text, Unicode, INT2NUM ) -/* */ +/* call-seq: + * Event.new(type) -> event + * + * You should never call this function directly. You should only aquire event's trough + * SFML::Window#getEvent or SFML::Window#waitEvent, if you need to pass data to a method + * that takes an event instance then make a proxy instance to simulate an event. + * NOTE: Using this method works but it will act constant as you can't access any values. + */ static VALUE Event_Initialize( VALUE self, VALUE aType ) { sf::Event * object = NULL; @@ -212,6 +219,7 @@ static VALUE Event_Initialize( VALUE self, VALUE aType ) rb_iv_set( eventType, "@internal__parent_ref", self ); rb_iv_set( self, name, eventType ); } + return self; } static VALUE Event_InitializeCopy( VALUE self, VALUE aSource ) @@ -224,20 +232,10 @@ static VALUE Event_InitializeCopy( VALUE self, VALUE aSource ) return Event_Initialize( self, INT2FIX( object->Type ) ); } -/* call-seq: - * Event.new(type) -> event - * - * You should never call this function directly. You should only aquire event's trough - * SFML::Window#getEvent or SFML::Window#waitEvent, if you need to pass data to a method - * that takes an event instance then make a proxy instance to simulate an event. - * NOTE: Using this method works but it will act constant as you can't access any values. - */ -static VALUE Event_New( int argc, VALUE * args, VALUE aKlass ) +static VALUE Event_Alloc( VALUE aKlass ) { sf::Event *object = new sf::Event(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, Event_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, Event_Free, object ); } void Init_Event( void ) @@ -279,14 +277,14 @@ void Init_Event( void ) * end */ globalEventClass = rb_define_class_under( sfml, "Event", rb_cObject ); - globalJoyButtonEventClass = rb_define_class_under( globalEventClass, "JoyButton", rb_cObject ); - globalJoyMoveEventClass = rb_define_class_under( globalEventClass, "JoyMove", rb_cObject ); - globalKeyEventClass = rb_define_class_under( globalEventClass, "Key", rb_cObject ); - globalMouseButtonEventClass = rb_define_class_under( globalEventClass, "MouseButton", rb_cObject ); - globalMouseMoveEventClass = rb_define_class_under( globalEventClass, "MouseMove", rb_cObject ); - globalMouseWheelEventClass = rb_define_class_under( globalEventClass, "MouseWheel", rb_cObject ); - globalSizeEventClass = rb_define_class_under( globalEventClass, "Size", rb_cObject ); - globalTextEventClass = rb_define_class_under( globalEventClass, "Text", rb_cObject ); + globalJoyButtonEventClass = rb_define_class_under( globalEventClass, "JoyButton", rb_cObject ); + globalJoyMoveEventClass = rb_define_class_under( globalEventClass, "JoyMove", rb_cObject ); + globalKeyEventClass = rb_define_class_under( globalEventClass, "Key", rb_cObject ); + globalMouseButtonEventClass = rb_define_class_under( globalEventClass, "MouseButton", rb_cObject ); + globalMouseMoveEventClass = rb_define_class_under( globalEventClass, "MouseMove", rb_cObject ); + globalMouseWheelEventClass = rb_define_class_under( globalEventClass, "MouseWheel", rb_cObject ); + globalSizeEventClass = rb_define_class_under( globalEventClass, "Size", rb_cObject ); + globalTextEventClass = rb_define_class_under( globalEventClass, "Text", rb_cObject ); rb_define_const( globalEventClass, "Closed", INT2NUM( sf::Event::Closed ) ); rb_define_const( globalEventClass, "Resized", INT2NUM( sf::Event::Resized ) ); @@ -307,7 +305,8 @@ void Init_Event( void ) rb_define_const( globalEventClass, "Count", INT2NUM( sf::Event::Count ) ); // Class methods - rb_define_singleton_method( globalEventClass, "new", Event_New, -1 ); + //rb_define_singleton_method( globalEventClass, "new", Event_New, -1 ); + rb_define_alloc_func( globalEventClass, Event_Alloc ); // Instance methods rb_define_method( globalEventClass, "initialize", Event_Initialize, 1 ); @@ -325,47 +324,35 @@ void Init_Event( void ) // JoyButton methods rb_define_method( globalJoyButtonEventClass, "joystickId", JoyButtonEvent_GetJoystickId, 0 ); - rb_define_method( globalJoyButtonEventClass, "button", JoyButtonEvent_GetButton, 0 ); // JoyMove methods rb_define_method( globalJoyMoveEventClass, "joystickId", JoyMoveEvent_GetJoystickId, 0 ); - rb_define_method( globalJoyMoveEventClass, "axis", JoyMoveEvent_GetAxis, 0 ); - rb_define_method( globalJoyMoveEventClass, "position", JoyMoveEvent_GetPosition, 0 ); // Key methods rb_define_method( globalKeyEventClass, "code", KeyEvent_GetCode, 0 ); - rb_define_method( globalKeyEventClass, "alt", KeyEvent_GetAlt, 0 ); - rb_define_method( globalKeyEventClass, "control", KeyEvent_GetControl, 0 ); - rb_define_method( globalKeyEventClass, "shift", KeyEvent_GetShift, 0 ); // MouseButton methods rb_define_method( globalMouseButtonEventClass, "button", MouseButtonEvent_GetButton, 0 ); - rb_define_method( globalMouseButtonEventClass, "x", MouseButtonEvent_GetX, 0 ); - rb_define_method( globalMouseButtonEventClass, "y", MouseButtonEvent_GetY, 0 ); // MouseMove methods rb_define_method( globalMouseMoveEventClass, "x", MouseMoveEvent_GetX, 0 ); - rb_define_method( globalMouseMoveEventClass, "y", MouseMoveEvent_GetY, 0 ); // MouseWheel methods rb_define_method( globalMouseWheelEventClass, "delta", MouseWheelEvent_GetDelta, 0 ); - rb_define_method( globalMouseWheelEventClass, "x", MouseWheelEvent_GetX, 0 ); - rb_define_method( globalMouseWheelEventClass, "y", MouseWheelEvent_GetY, 0 ); // Size methods rb_define_method( globalSizeEventClass, "width", SizeEvent_GetWidth, 0 ); - rb_define_method( globalSizeEventClass, "height", SizeEvent_GetWidth, 0 ); // Text methods diff --git a/bindings/ruby/sfml-window/window/Input.cpp b/bindings/ruby/sfml-window/window/Input.cpp index 0a52b69b..56f52214 100644 --- a/bindings/ruby/sfml-window/window/Input.cpp +++ b/bindings/ruby/sfml-window/window/Input.cpp @@ -136,18 +136,10 @@ static VALUE Input_GetJoystickAxis( VALUE self, VALUE aJoystick, VALUE anAxis ) return rb_float_new( object->GetJoystickAxis( rawJoystick, rawAxis ) ); } -/* call-seq: - * Input.new() -> input - * - * This will create a new input object. though it will not receive any updates of events. - * You should aquire an input object from the SFML::Window#input method. - */ -static VALUE Input_New( int argc, VALUE * args, VALUE aKlass ) +static VALUE Input_Alloc( VALUE aKlass ) { sf::Input *object = new sf::Input(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, Input_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, Input_Free, object ); } void Init_Input( void ) @@ -184,7 +176,8 @@ void Init_Input( void ) rb_include_module( globalInputClass, globalNonCopyableModule ); // Class methods - rb_define_singleton_method( globalInputClass, "new", Input_New, -1 ); + //rb_define_singleton_method( globalInputClass, "new", Input_New, -1 ); + rb_define_alloc_func( globalInputClass, Input_Alloc ); // Instance methods rb_define_method( globalInputClass, "isKeyDown", Input_IsKeyDown, 1 ); diff --git a/bindings/ruby/sfml-window/window/VideoMode.cpp b/bindings/ruby/sfml-window/window/VideoMode.cpp index b90d6006..c7562416 100644 --- a/bindings/ruby/sfml-window/window/VideoMode.cpp +++ b/bindings/ruby/sfml-window/window/VideoMode.cpp @@ -19,7 +19,7 @@ * 3. This notice may not be removed or altered from any * source distribution. */ - + #include "VideoMode.hpp" #include "main.hpp" #include @@ -40,7 +40,7 @@ VALUE VideoMode_ForceType( VALUE someValue ) if( FIX2INT( rb_funcall( someValue, rb_intern( "size" ), 0 ) ) == 3 ) { VALUE arg3 = rb_ary_entry( someValue, 2 ); - return rb_funcall( globalVideoModeClass, rb_intern( "new" ), 3, arg1, arg2, arg3 ); + return rb_funcall( globalVideoModeClass, rb_intern( "new" ), 3, arg1, arg2, arg3 ); } else { @@ -57,7 +57,7 @@ VALUE VideoMode_ForceType( VALUE someValue ) } } -/* Free a heap allocated object +/* Free a heap allocated object * Not accessible trough ruby directly! */ static void VideoMode_Free( sf::VideoMode *anObject ) @@ -116,7 +116,7 @@ static VALUE VideoMode_SetHeight( VALUE self, VALUE aValue ) /* call-seq: * mode.bitsPerPixel -> bpp * - * Video mode pixel depth, in bits per pixels. + * Video mode pixel depth, in bits per pixels. */ static VALUE VideoMode_GetBitsPerPixel( VALUE self ) { @@ -128,7 +128,7 @@ static VALUE VideoMode_GetBitsPerPixel( VALUE self ) /* call-seq: * mode.bitsPerPixel=(new_bpp) -> new_bpp * - * Video mode pixel depth, in bits per pixels. + * Video mode pixel depth, in bits per pixels. */ static VALUE VideoMode_SetBitsPerPixel( VALUE self, VALUE aValue ) { @@ -142,7 +142,7 @@ static VALUE VideoMode_SetBitsPerPixel( VALUE self, VALUE aValue ) * * Tell whether or not the video mode is valid. * - * The validity of video modes is only relevant when using fullscreen windows; otherwise any video mode can be used + * The validity of video modes is only relevant when using fullscreen windows; otherwise any video mode can be used * with no restriction. */ static VALUE VideoMode_IsValid( VALUE self ) @@ -164,7 +164,7 @@ static VALUE VideoMode_InitializeCopy( VALUE self, VALUE aSource ) /* call-seq: * VideoMode.getDesktopMode -> desktop_mode * - * Get the current desktop video mode. + * Get the current desktop video mode. */ static VALUE VideoMode_GetDesktopMode( VALUE aKlass ) { @@ -179,8 +179,8 @@ static VALUE VideoMode_GetDesktopMode( VALUE aKlass ) * * Retrieve all the video modes supported in fullscreen mode. * - * When creating a fullscreen window, the video mode is restricted to be compatible with what the graphics driver and - * monitor support. This function returns the complete list of all video modes that can be used in fullscreen mode. + * When creating a fullscreen window, the video mode is restricted to be compatible with what the graphics driver and + * monitor support. This function returns the complete list of all video modes that can be used in fullscreen mode. * The returned array is sorted from best to worst, so that the first element will always give the best mode * (higher width, height and bits-per-pixel). */ @@ -198,33 +198,37 @@ static VALUE VideoMode_GetFullscreenModes( VALUE aKlass ) return array; } +static VALUE VideoMode_Alloc( VALUE aKlass ) +{ + sf::VideoMode *object = new sf::VideoMode(); + return Data_Wrap_Struct( aKlass, 0, VideoMode_Free, object ); +} + /* call-seq: - * VideoMode.new() -> mode + * VideoMode.new() -> mode * VideoMode.new( width, height, bpp = 32 ) -> mode * * Create a new mode. */ -static VALUE VideoMode_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE VideoMode_Initialize( int argc, VALUE *args, VALUE self ) { sf::VideoMode *object = NULL; + Data_Get_Struct( self, sf::VideoMode, object ); switch( argc ) { case 0: - object = new sf::VideoMode(); - break; - case 2: - object = new sf::VideoMode( FIX2UINT( args[0] ), FIX2UINT( args[1] ) ); break; case 3: - object = new sf::VideoMode( FIX2UINT( args[0] ), FIX2UINT( args[1] ),FIX2UINT( args[2] ) ); + object->BitsPerPixel = NUM2UINT( args[2] ); + case 2: + object->Height = NUM2UINT( args[1] ); + object->Width = NUM2UINT( args[0] ); break; default: - rb_raise( rb_eArgError, "Expected 0, 2 or 3 arguments but was given %d", argc ); - break; + rb_raise( rb_eArgError, "Expected 0..3 arguments but was given %d", argc ); + return Qnil; } - VALUE rbData = Data_Wrap_Struct( aKlass, 0, VideoMode_Free, object ); - rb_obj_call_init( rbData, 0, 0 ); - return rbData; + return self; } void Init_VideoMode( void ) @@ -234,21 +238,21 @@ void Init_VideoMode( void ) /* A video mode is defined by a width and a height (in pixels) and a depth (in bits per pixel). * * Video modes are used to setup windows (SFML::Window) at creation time. - * - * The main usage of video modes is for fullscreen mode: indeed you must use one of the valid - * video modes allowed by the OS (which are defined by what the monitor and the graphics card support), + * + * The main usage of video modes is for fullscreen mode: indeed you must use one of the valid + * video modes allowed by the OS (which are defined by what the monitor and the graphics card support), * otherwise your window creation will just fail. - * + * * SFML::VideoMode provides a static function for retrieving the list of all the video modes supported by * the system: getFullscreenModes(). - * + * * A custom video mode can also be checked directly for fullscreen compatibility with its isValid() function. - * - * Additionnally, SFML::VideoMode provides a static function to get the mode currently used by the desktop: + * + * Additionnally, SFML::VideoMode provides a static function to get the mode currently used by the desktop: * getDesktopMode(). This allows to build windows with the same size or pixel depth as the current resolution. - * + * * Usage example: - * + * * # Display the list of all the video modes available for fullscreen * modes = SFML::VideoMode.getFullscreenModes() * i = 0 @@ -262,38 +266,40 @@ void Init_VideoMode( void ) * window.create( SFML::VideoMode.new( 1024, 768, desktop.BitsPerPixel ), "SFML window" ) */ globalVideoModeClass = rb_define_class_under( sfml, "VideoMode", rb_cObject ); - + // Class methods - rb_define_singleton_method( globalVideoModeClass, "new", VideoMode_New, -1 ); + //rb_define_singleton_method( globalVideoModeClass, "new", VideoMode_New, -1 ); + rb_define_alloc_func( globalVideoModeClass, VideoMode_Alloc ); rb_define_singleton_method( globalVideoModeClass, "getDesktopMode", VideoMode_GetDesktopMode, 0 ); rb_define_singleton_method( globalVideoModeClass, "getFullscreenModes", VideoMode_GetFullscreenModes, 0 ); - + // Instance methods + rb_define_method( globalVideoModeClass, "initialize", VideoMode_Initialize, -1 ); rb_define_method( globalVideoModeClass, "initialize_copy", VideoMode_InitializeCopy, 1 ); - + rb_define_method( globalVideoModeClass, "width", VideoMode_GetWidth, 0 ); rb_define_method( globalVideoModeClass, "width=", VideoMode_SetWidth, 1 ); - + rb_define_method( globalVideoModeClass, "height", VideoMode_GetHeight, 0 ); rb_define_method( globalVideoModeClass, "height=", VideoMode_SetHeight, 1 ); rb_define_method( globalVideoModeClass, "bitsPerPixel", VideoMode_GetBitsPerPixel, 0 ); rb_define_method( globalVideoModeClass, "bitsPerPixel=", VideoMode_SetBitsPerPixel, 1 ); - + rb_define_method( globalVideoModeClass, "isValid", VideoMode_IsValid, 0 ); - + // Class aliases rb_define_alias( CLASS_OF( globalVideoModeClass ), "desktopMode", "getDesktopMode" ); rb_define_alias( CLASS_OF( globalVideoModeClass ), "desktop_mode", "getDesktopMode" ); rb_define_alias( CLASS_OF( globalVideoModeClass ), "fullscreenModes", "getFullscreenModes" ); rb_define_alias( CLASS_OF( globalVideoModeClass ), "fullscreen_modes", "getFullscreenModes" ); - + // Aliases rb_define_alias( globalVideoModeClass, "bits_per_pixel", "bitsPerPixel" ); rb_define_alias( globalVideoModeClass, "bits_per_pixel=", "bitsPerPixel=" ); rb_define_alias( globalVideoModeClass, "bpp", "bitsPerPixel" ); rb_define_alias( globalVideoModeClass, "bpp=", "bitsPerPixel=" ); - + rb_define_alias( globalVideoModeClass, "is_valid", "isValid" ); rb_define_alias( globalVideoModeClass, "valid?", "isValid" ); } diff --git a/bindings/ruby/sfml-window/window/Window.cpp b/bindings/ruby/sfml-window/window/Window.cpp index c753ce68..429f101f 100644 --- a/bindings/ruby/sfml-window/window/Window.cpp +++ b/bindings/ruby/sfml-window/window/Window.cpp @@ -620,12 +620,10 @@ static VALUE Window_Initialize( int argc, VALUE *args, VALUE self ) return self; } -static VALUE Window_New( int argc, VALUE *args, VALUE aKlass ) +static VALUE Window_Alloc( VALUE aKlass ) { sf::Window *object = new sf::Window(); - VALUE rbData = Data_Wrap_Struct( aKlass, 0, Window_Free, object ); - rb_obj_call_init( rbData, argc, args ); - return rbData; + return Data_Wrap_Struct( aKlass, 0, Window_Free, object ); } void Init_Window( void ) @@ -680,7 +678,8 @@ void Init_Window( void ) rb_include_module( globalWindowClass, globalNonCopyableModule ); // Class methods - rb_define_singleton_method( globalWindowClass, "new", Window_New , -1 ); + //rb_define_singleton_method( globalWindowClass, "new", Window_New , -1 ); + rb_define_alloc_func( globalWindowClass, Window_Alloc ); // Instance methods rb_define_method( globalWindowClass, "initialize", Window_Initialize, -1 ); diff --git a/bindings/ruby/shared/global.hpp b/bindings/ruby/shared/global.hpp index b38577b5..5331e1d9 100644 --- a/bindings/ruby/shared/global.hpp +++ b/bindings/ruby/shared/global.hpp @@ -25,7 +25,7 @@ #include "ruby.h" -#define SFML_STATIC +#define SFML_DYNAMIC extern VALUE globalSFMLNamespace;