From dec0928c4e521b9031e84c9cf22e3a8ae80892e9 Mon Sep 17 00:00:00 2001 From: groogy Date: Thu, 4 Nov 2010 14:41:47 +0000 Subject: [PATCH] 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 --- ruby/sfml-window/window/Event.cpp | 42 ++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/ruby/sfml-window/window/Event.cpp b/ruby/sfml-window/window/Event.cpp index 09e0685c..83d7e408 100644 --- a/ruby/sfml-window/window/Event.cpp +++ b/ruby/sfml-window/window/Event.cpp @@ -85,8 +85,32 @@ static void Event_Free( sf::Event *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: - * Event.new() -> event + * Event.new() -> event * * The constructor creates a new event. */ @@ -101,14 +125,14 @@ static VALUE Event_New( VALUE aKlass ) void Init_Event( void ) { globalEventClass = rb_define_class_under( GetNamespace(), "Event", rb_cObject ); - globalJoyButtonEventClass = rb_define_class_under( globalEventClass, "JoyButton" ); - globalJoyMoveEventClass = rb_define_class_under( globalEventClass, "JoyMove" ); - globalKeyEventClass = rb_define_class_under( globalEventClass, "Key" ); - globalMouseButtonEventClass = rb_define_class_under( globalEventClass, "MouseButton" ); - globalMouseMoveEventClass = rb_define_class_under( globalEventClass, "MouseMove" ); - globalMouseWheelEventClass = rb_define_class_under( globalEventClass, "MouseWheel" ); - globalSizeEventEventClass = rb_define_class_under( globalEventClass, "Size" ); - globalTextEventEventClass = rb_define_class_under( globalEventClass, "Text" ); + 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 ); // Class methods rb_define_singleton_method( globalEventClass, "new", FUNCPTR( Event_New ), 0 );