@@ -86,11 +86,14 @@ static VALUE event_type_to_class[SDL_LASTEVENT];
8686 * timestamp of the event
8787 * @return [Integer]
8888 */
89+ DEFINE_DATA_TYPE (SDL_Event , free );
90+
8991static VALUE Event_new (SDL_Event * ev )
9092{
91- SDL_Event * e = ALLOC (SDL_Event );
93+ SDL_Event * e ;
94+ VALUE obj = TypedData_Make_Struct (event_type_to_class [ev -> type ], SDL_Event , & SDL_Event_data_type , e );
9295 * e = * ev ;
93- return Data_Wrap_Struct ( event_type_to_class [ ev -> type ], 0 , free , e ) ;
96+ return obj ;
9497}
9598
9699static VALUE Event_s_allocate (VALUE klass )
@@ -99,11 +102,12 @@ static VALUE Event_s_allocate(VALUE klass)
99102 VALUE event_type = rb_iv_get (klass , "event_type" );
100103 if (event_type == Qnil )
101104 rb_raise (rb_eArgError , "Cannot allocate %s" , rb_class2name (klass ));
102-
103- e = ALLOC (SDL_Event );
104- memset (e , 0 , sizeof (SDL_Event ));
105- e -> common .type = NUM2INT (event_type );
106- return Data_Wrap_Struct (klass , 0 , free , e );
105+
106+ {
107+ VALUE obj = TypedData_Make_Struct (klass , SDL_Event , & SDL_Event_data_type , e );
108+ e -> common .type = NUM2INT (event_type );
109+ return obj ;
110+ }
107111}
108112
109113/*
@@ -176,15 +180,15 @@ static void set_string(char* field, VALUE str, int maxlength)
176180 static VALUE Ev##classname##_##name(VALUE self) \
177181 { \
178182 SDL_Event* ev; \
179- Data_Get_Struct (self, SDL_Event, ev); \
183+ TypedData_Get_Struct (self, SDL_Event, &SDL_Event_data_type, ev); \
180184 return c2ruby(ev->field); \
181185 } \
182186
183187#define EVENT_WRITER (classname , name , field , ruby2c ) \
184188 static VALUE Ev##classname##_set_##name(VALUE self, VALUE val) \
185189 { \
186190 SDL_Event* ev; \
187- Data_Get_Struct (self, SDL_Event, ev); \
191+ TypedData_Get_Struct (self, SDL_Event, &SDL_Event_data_type, ev); \
188192 ev->field = ruby2c(val); \
189193 return Qnil; \
190194 }
@@ -214,7 +218,7 @@ EVENT_ACCESSOR_UINT(Event, timestamp, common.timestamp);
214218/* @return [String] inspection string */
215219static VALUE Event_inspect (VALUE self )
216220{
217- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
221+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
218222 return rb_sprintf ("<%s: type=%u timestamp=%u>" ,
219223 rb_obj_classname (self ), ev -> common .type , ev -> common .timestamp );
220224}
@@ -303,7 +307,7 @@ EVENT_ACCESSOR_INT(Window, data2, window.data2);
303307/* @return [String] inspection string */
304308static VALUE EvWindow_inspect (VALUE self )
305309{
306- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
310+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
307311 return rb_sprintf ("<%s: type=%u timestamp=%u window_id=%u event=%u data1=%d data2=%d>" ,
308312 rb_obj_classname (self ), ev -> common .type , ev -> common .timestamp ,
309313 ev -> window .windowID , ev -> window .event ,
@@ -357,7 +361,7 @@ EVENT_ACCESSOR_UINT(Keyboard, mod, key.keysym.mod);
357361/* @return [String] inspection string */
358362static VALUE EvKeyboard_inspect (VALUE self )
359363{
360- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
364+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
361365 return rb_sprintf ("<%s: type=%u timestamp=%u"
362366 " window_id=%u state=%u repeat=%u"
363367 " scancode=%u sym=%u mod=%u>" ,
@@ -407,15 +411,15 @@ EVENT_READER(TextEditing, text, edit.text, utf8str_new_cstr);
407411static VALUE EvTextEditing_set_text (VALUE self , VALUE str )
408412{
409413 SDL_Event * ev ;
410- Data_Get_Struct (self , SDL_Event , ev );
414+ TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
411415 set_string (ev -> edit .text , str , 30 );
412416 return str ;
413417}
414418
415419/* @return [String] inspection string */
416420static VALUE EvTextEditing_inspect (VALUE self )
417421{
418- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
422+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
419423 return rb_sprintf ("<%s: type=%u timestamp=%u"
420424 " window_id=%u text=%s start=%d length=%d>" ,
421425 rb_obj_classname (self ), ev -> common .type , ev -> common .timestamp ,
@@ -441,15 +445,15 @@ EVENT_READER(TextInput, text, text.text, utf8str_new_cstr);
441445static VALUE EvTextInput_set_text (VALUE self , VALUE str )
442446{
443447 SDL_Event * ev ;
444- Data_Get_Struct (self , SDL_Event , ev );
448+ TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
445449 set_string (ev -> text .text , str , 30 );
446450 return str ;
447451}
448452
449453/* @return [String] inspection string */
450454static VALUE EvTextInput_inspect (VALUE self )
451455{
452- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
456+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
453457 return rb_sprintf ("<%s: type=%u timestamp=%u window_id=%u text=%s>" ,
454458 rb_obj_classname (self ), ev -> common .type , ev -> common .timestamp ,
455459 ev -> text .windowID , ev -> text .text );
@@ -507,7 +511,7 @@ EVENT_ACCESSOR_INT(MouseButton, y, button.y);
507511/* @return [String] inspection string */
508512static VALUE EvMouseButton_inspect (VALUE self )
509513{
510- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
514+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
511515 return rb_sprintf ("<%s: type=%u timestamp=%u"
512516 " window_id=%u which=%u button=%hhu pressed=%s"
513517#if SDL_VERSION_ATLEAST (2 ,0 ,2 )
@@ -576,7 +580,7 @@ EVENT_ACCESSOR_INT(MouseMotion, yrel, motion.yrel);
576580/* @return [String] inspection string */
577581static VALUE EvMouseMotion_inspect (VALUE self )
578582{
579- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
583+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
580584 return rb_sprintf ("<%s: type=%u timestamp=%u"
581585 " window_id=%u which=%u state=%u"
582586 " x=%d y=%d xrel=%d yrel=%d>" ,
@@ -615,7 +619,7 @@ EVENT_ACCESSOR_INT(MouseWheel, y, wheel.y);
615619/* @return [String] inspection string */
616620static VALUE EvMouseWheel_inspect (VALUE self )
617621{
618- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
622+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
619623 return rb_sprintf ("<%s: type=%u timestamp=%u"
620624 " window_id=%u which=%u x=%d y=%d>" ,
621625 rb_obj_classname (self ), ev -> common .type , ev -> common .timestamp ,
@@ -651,7 +655,7 @@ EVENT_ACCESSOR_BOOL(JoyButton, pressed, jbutton.state);
651655/* @return [String] inspection string */
652656static VALUE EvJoyButton_inspect (VALUE self )
653657{
654- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
658+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
655659 return rb_sprintf ("<%s: type=%u timestamp=%u"
656660 " which=%d button=%u pressed=%s>" ,
657661 rb_obj_classname (self ), ev -> common .type , ev -> common .timestamp ,
@@ -693,7 +697,7 @@ EVENT_ACCESSOR_INT(JoyAxisMotion, value, jaxis.value);
693697/* @return [String] inspection string */
694698static VALUE EvJoyAxisMotion_inspect (VALUE self )
695699{
696- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
700+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
697701 return rb_sprintf ("<%s: type=%u timestamp=%u"
698702 " which=%d axis=%u value=%d>" ,
699703 rb_obj_classname (self ), ev -> common .type , ev -> common .timestamp ,
@@ -728,7 +732,7 @@ EVENT_ACCESSOR_INT(JoyBallMotion, yrel, jball.yrel);
728732/* @return [String] inspection string */
729733static VALUE EvJoyBallMotion_inspect (VALUE self )
730734{
731- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
735+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
732736 return rb_sprintf ("<%s: type=%u timestamp=%u"
733737 " which=%d ball=%u xrel=%d yrel=%d>" ,
734738 rb_obj_classname (self ), ev -> common .type , ev -> common .timestamp ,
@@ -758,7 +762,7 @@ EVENT_ACCESSOR_UINT8(JoyHatMotion, value, jhat.value);
758762/* @return [String] inspection string */
759763static VALUE EvJoyHatMotion_inspect (VALUE self )
760764{
761- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
765+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
762766 return rb_sprintf ("<%s: type=%u timestamp=%u which=%d hat=%u value=%u>" ,
763767 rb_obj_classname (self ), ev -> common .type , ev -> common .timestamp ,
764768 ev -> jhat .which , ev -> jhat .hat , ev -> jhat .value );
@@ -775,7 +779,7 @@ EVENT_ACCESSOR_INT(JoyDevice, which, jdevice.which);
775779/* @return [String] inspection string */
776780static VALUE EvJoyDevice_inspect (VALUE self )
777781{
778- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
782+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
779783 return rb_sprintf ("<%s: type=%u timestamp=%u which=%d>" ,
780784 rb_obj_classname (self ), ev -> common .type , ev -> common .timestamp ,
781785 ev -> jdevice .which );
@@ -816,7 +820,7 @@ EVENT_ACCESSOR_INT(ControllerAxis, value, caxis.value);
816820/* @return [String] inspection string */
817821static VALUE ControllerAxis_inspect (VALUE self )
818822{
819- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
823+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
820824 return rb_sprintf ("<%s: type=%u timestamp=%u"
821825 " which=%d axis=%s value=%d>" ,
822826 rb_obj_classname (self ), ev -> common .type , ev -> common .timestamp ,
@@ -853,7 +857,7 @@ EVENT_ACCESSOR_BOOL(ControllerButton, pressed, cbutton.state);
853857/* @return [String] inspection string */
854858static VALUE ControllerButton_inspect (VALUE self )
855859{
856- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
860+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
857861 return rb_sprintf ("<%s: type=%u timestamp=%u"
858862 " which=%d button=%s state=%s>" ,
859863 rb_obj_classname (self ), ev -> common .type , ev -> common .timestamp ,
@@ -893,7 +897,7 @@ EVENT_ACCESSOR_INT(ControllerDevice, which, cdevice.which);
893897/* @return [String] inspection string */
894898static VALUE ControllerDevice_inspect (VALUE self )
895899{
896- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
900+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
897901 return rb_sprintf ("<%s: type=%u timestamp=%u which=%d>" ,
898902 rb_obj_classname (self ), ev -> common .type , ev -> common .timestamp ,
899903 ev -> cdevice .which );
@@ -953,7 +957,7 @@ EVENT_ACCESSOR_DBL(TouchFinger, pressure, tfinger.pressure);
953957/* @return [String] inspection string */
954958static VALUE EvTouchFinger_inspect (VALUE self )
955959{
956- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
960+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
957961 return rb_sprintf ("<%s: type=%u timestamp=%u"
958962 " touch_id=%d finger_id=%d"
959963 " x=%f y=%f pressure=%f>" ,
@@ -990,7 +994,7 @@ EVENT_ACCESSOR_DBL(FingerMotion, dy, tfinger.dy);
990994/* @return [String] inspection string */
991995static VALUE EvFingerMotion_inspect (VALUE self )
992996{
993- SDL_Event * ev ; Data_Get_Struct (self , SDL_Event , ev );
997+ SDL_Event * ev ; TypedData_Get_Struct (self , SDL_Event , & SDL_Event_data_type , ev );
994998 return rb_sprintf ("<%s: type=%u timestamp=%u"
995999 " touch_id=%d finger_id=%d"
9961000 " x=%f y=%f pressure=%f"
0 commit comments