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
This commit is contained in:
groogy 2011-02-27 14:07:10 +00:00
parent 0a2abc6933
commit 7d7c4c999f
26 changed files with 235 additions and 301 deletions

View file

@ -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

View file

@ -25,7 +25,6 @@
#include <SFML/Window/ContextSettings.hpp>
#include <iostream>
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 );

View file

@ -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

View file

@ -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 );

View file

@ -19,7 +19,7 @@
* 3. This notice may not be removed or altered from any
* source distribution.
*/
#include "VideoMode.hpp"
#include "main.hpp"
#include <SFML/Window/VideoMode.hpp>
@ -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" );
}

View file

@ -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 );