This message will be more like a note to self.

I've added so that a ruby instance of each event type is created when an event is allocated and initialized. Left to do is to keep reference to these objects inside the event instance and to force the event type instances to keep a reference back to their original event. This is to make sure that the garbage collector keep them together in memory at all times. Also something I must do is to check the event's type in the initialize method so that we will always only create ONE of these "child" instances.

This way we won't need to allocate any unnecessary memory except if the developer specifically request a new event type instance. For example: SFML::Event::Key.new

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1606 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
groogy 2010-11-04 14:41:47 +00:00
parent 31d438dd87
commit dec0928c4e

View File

@ -85,8 +85,32 @@ static void Event_Free( sf::Event *anObject )
delete anObject; delete anObject;
} }
static VALUE Event_Initialize( VALUE self )
{
sf::Event * object = NULL;
Data_Get_Struct( self, sf::Event, object );
VALUE joyButton = Data_Wrap_Struct( globalJoyButtonEventClass, 0, 0, &object->JoyButton );
VALUE joyMove = Data_Wrap_Struct( globalJoyMoveEventClass, 0, 0, &object->JoyMove );
VALUE key = Data_Wrap_Struct( globalKeyEventClass, 0, 0, &object->Key );
VALUE mouseButton = Data_Wrap_Struct( globalMouseButtonEventClass, 0, 0, &object->MouseButton );
VALUE mouseMove = Data_Wrap_Struct( globalMouseMoveEventClass, 0, 0, &object->MouseMove );
VALUE mouseWheel = Data_Wrap_Struct( globalMouseWheelEventClass, 0, 0, &object->MouseWheel );
VALUE size = Data_Wrap_Struct( globalSizeEventClass, 0, 0, &object->Size );
VALUE text = Data_Wrap_Struct( globalTextEventClass, 0, 0, &object->Text );
rb_obj_call_init( joyButton, 0, 0 );
rb_obj_call_init( joyMove, 0, 0 );
rb_obj_call_init( key, 0, 0 );
rb_obj_call_init( mouseButton, 0, 0 );
rb_obj_call_init( mouseMove, 0, 0 );
rb_obj_call_init( mouseWheel, 0, 0 );
rb_obj_call_init( size, 0, 0 );
rb_obj_call_init( text, 0, 0 );
}
/* call-seq: /* call-seq:
* Event.new() -> event * Event.new() -> event
* *
* The constructor creates a new event. * The constructor creates a new event.
*/ */
@ -101,14 +125,14 @@ static VALUE Event_New( VALUE aKlass )
void Init_Event( void ) void Init_Event( void )
{ {
globalEventClass = rb_define_class_under( GetNamespace(), "Event", rb_cObject ); globalEventClass = rb_define_class_under( GetNamespace(), "Event", rb_cObject );
globalJoyButtonEventClass = rb_define_class_under( globalEventClass, "JoyButton" ); globalJoyButtonEventClass = rb_define_class_under( globalEventClass, "JoyButton", rb_cObject );
globalJoyMoveEventClass = rb_define_class_under( globalEventClass, "JoyMove" ); globalJoyMoveEventClass = rb_define_class_under( globalEventClass, "JoyMove", rb_cObject );
globalKeyEventClass = rb_define_class_under( globalEventClass, "Key" ); globalKeyEventClass = rb_define_class_under( globalEventClass, "Key", rb_cObject );
globalMouseButtonEventClass = rb_define_class_under( globalEventClass, "MouseButton" ); globalMouseButtonEventClass = rb_define_class_under( globalEventClass, "MouseButton", rb_cObject );
globalMouseMoveEventClass = rb_define_class_under( globalEventClass, "MouseMove" ); globalMouseMoveEventClass = rb_define_class_under( globalEventClass, "MouseMove", rb_cObject );
globalMouseWheelEventClass = rb_define_class_under( globalEventClass, "MouseWheel" ); globalMouseWheelEventClass = rb_define_class_under( globalEventClass, "MouseWheel", rb_cObject );
globalSizeEventEventClass = rb_define_class_under( globalEventClass, "Size" ); globalSizeEventClass = rb_define_class_under( globalEventClass, "Size", rb_cObject );
globalTextEventEventClass = rb_define_class_under( globalEventClass, "Text" ); globalTextEventClass = rb_define_class_under( globalEventClass, "Text", rb_cObject );
// Class methods // Class methods
rb_define_singleton_method( globalEventClass, "new", FUNCPTR( Event_New ), 0 ); rb_define_singleton_method( globalEventClass, "new", FUNCPTR( Event_New ), 0 );