Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions event.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,13 @@ static VALUE event_type_to_class[SDL_LASTEVENT];
* timestamp of the event
* @return [Integer]
*/
DEFINE_DATA_TYPE(SDL_Event, free);

static VALUE Event_new(SDL_Event* ev)
{
SDL_Event* e = ALLOC(SDL_Event);
*e = *ev;
return Data_Wrap_Struct(event_type_to_class[ev->type], 0, free, e);
return TypedData_Wrap_Struct(event_type_to_class[ev->type], &SDL_Event_data_type, e);
}

static VALUE Event_s_allocate(VALUE klass)
Expand All @@ -99,11 +101,11 @@ static VALUE Event_s_allocate(VALUE klass)
VALUE event_type = rb_iv_get(klass, "event_type");
if (event_type == Qnil)
rb_raise(rb_eArgError, "Cannot allocate %s", rb_class2name(klass));

e = ALLOC(SDL_Event);
memset(e, 0, sizeof(SDL_Event));
e->common.type = NUM2INT(event_type);
return Data_Wrap_Struct(klass, 0, free, e);
return TypedData_Wrap_Struct(klass, &SDL_Event_data_type, e);
}

/*
Expand Down Expand Up @@ -176,15 +178,15 @@ static void set_string(char* field, VALUE str, int maxlength)
static VALUE Ev##classname##_##name(VALUE self) \
{ \
SDL_Event* ev; \
Data_Get_Struct(self, SDL_Event, ev); \
TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev); \
return c2ruby(ev->field); \
} \

#define EVENT_WRITER(classname, name, field, ruby2c) \
static VALUE Ev##classname##_set_##name(VALUE self, VALUE val) \
{ \
SDL_Event* ev; \
Data_Get_Struct(self, SDL_Event, ev); \
TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev); \
ev->field = ruby2c(val); \
return Qnil; \
}
Expand Down Expand Up @@ -214,7 +216,7 @@ EVENT_ACCESSOR_UINT(Event, timestamp, common.timestamp);
/* @return [String] inspection string */
static VALUE Event_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u>",
rb_obj_classname(self), ev->common.type, ev->common.timestamp);
}
Expand Down Expand Up @@ -303,7 +305,7 @@ EVENT_ACCESSOR_INT(Window, data2, window.data2);
/* @return [String] inspection string */
static VALUE EvWindow_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u window_id=%u event=%u data1=%d data2=%d>",
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
ev->window.windowID, ev->window.event,
Expand Down Expand Up @@ -357,7 +359,7 @@ EVENT_ACCESSOR_UINT(Keyboard, mod, key.keysym.mod);
/* @return [String] inspection string */
static VALUE EvKeyboard_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u"
" window_id=%u state=%u repeat=%u"
" scancode=%u sym=%u mod=%u>",
Expand Down Expand Up @@ -407,15 +409,15 @@ EVENT_READER(TextEditing, text, edit.text, utf8str_new_cstr);
static VALUE EvTextEditing_set_text(VALUE self, VALUE str)
{
SDL_Event* ev;
Data_Get_Struct(self, SDL_Event, ev);
TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
set_string(ev->edit.text, str, 30);
return str;
}

/* @return [String] inspection string */
static VALUE EvTextEditing_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u"
" window_id=%u text=%s start=%d length=%d>",
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
Expand All @@ -441,15 +443,15 @@ EVENT_READER(TextInput, text, text.text, utf8str_new_cstr);
static VALUE EvTextInput_set_text(VALUE self, VALUE str)
{
SDL_Event* ev;
Data_Get_Struct(self, SDL_Event, ev);
TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
set_string(ev->text.text, str, 30);
return str;
}

/* @return [String] inspection string */
static VALUE EvTextInput_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u window_id=%u text=%s>",
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
ev->text.windowID, ev->text.text);
Expand Down Expand Up @@ -507,7 +509,7 @@ EVENT_ACCESSOR_INT(MouseButton, y, button.y);
/* @return [String] inspection string */
static VALUE EvMouseButton_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u"
" window_id=%u which=%u button=%hhu pressed=%s"
#if SDL_VERSION_ATLEAST(2,0,2)
Expand Down Expand Up @@ -576,7 +578,7 @@ EVENT_ACCESSOR_INT(MouseMotion, yrel, motion.yrel);
/* @return [String] inspection string */
static VALUE EvMouseMotion_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u"
" window_id=%u which=%u state=%u"
" x=%d y=%d xrel=%d yrel=%d>",
Expand Down Expand Up @@ -615,7 +617,7 @@ EVENT_ACCESSOR_INT(MouseWheel, y, wheel.y);
/* @return [String] inspection string */
static VALUE EvMouseWheel_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u"
" window_id=%u which=%u x=%d y=%d>",
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
Expand Down Expand Up @@ -651,7 +653,7 @@ EVENT_ACCESSOR_BOOL(JoyButton, pressed, jbutton.state);
/* @return [String] inspection string */
static VALUE EvJoyButton_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u"
" which=%d button=%u pressed=%s>",
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
Expand Down Expand Up @@ -693,7 +695,7 @@ EVENT_ACCESSOR_INT(JoyAxisMotion, value, jaxis.value);
/* @return [String] inspection string */
static VALUE EvJoyAxisMotion_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u"
" which=%d axis=%u value=%d>",
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
Expand Down Expand Up @@ -728,7 +730,7 @@ EVENT_ACCESSOR_INT(JoyBallMotion, yrel, jball.yrel);
/* @return [String] inspection string */
static VALUE EvJoyBallMotion_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u"
" which=%d ball=%u xrel=%d yrel=%d>",
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
Expand Down Expand Up @@ -758,7 +760,7 @@ EVENT_ACCESSOR_UINT8(JoyHatMotion, value, jhat.value);
/* @return [String] inspection string */
static VALUE EvJoyHatMotion_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u which=%d hat=%u value=%u>",
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
ev->jhat.which, ev->jhat.hat, ev->jhat.value);
Expand All @@ -775,7 +777,7 @@ EVENT_ACCESSOR_INT(JoyDevice, which, jdevice.which);
/* @return [String] inspection string */
static VALUE EvJoyDevice_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u which=%d>",
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
ev->jdevice.which);
Expand Down Expand Up @@ -816,7 +818,7 @@ EVENT_ACCESSOR_INT(ControllerAxis, value, caxis.value);
/* @return [String] inspection string */
static VALUE ControllerAxis_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u"
" which=%d axis=%s value=%d>",
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
Expand Down Expand Up @@ -853,7 +855,7 @@ EVENT_ACCESSOR_BOOL(ControllerButton, pressed, cbutton.state);
/* @return [String] inspection string */
static VALUE ControllerButton_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u"
" which=%d button=%s state=%s>",
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
Expand Down Expand Up @@ -893,7 +895,7 @@ EVENT_ACCESSOR_INT(ControllerDevice, which, cdevice.which);
/* @return [String] inspection string */
static VALUE ControllerDevice_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u which=%d>",
rb_obj_classname(self), ev->common.type, ev->common.timestamp,
ev->cdevice.which);
Expand Down Expand Up @@ -953,7 +955,7 @@ EVENT_ACCESSOR_DBL(TouchFinger, pressure, tfinger.pressure);
/* @return [String] inspection string */
static VALUE EvTouchFinger_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u"
" touch_id=%d finger_id=%d"
" x=%f y=%f pressure=%f>",
Expand Down Expand Up @@ -990,7 +992,7 @@ EVENT_ACCESSOR_DBL(FingerMotion, dy, tfinger.dy);
/* @return [String] inspection string */
static VALUE EvFingerMotion_inspect(VALUE self)
{
SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
SDL_Event* ev; TypedData_Get_Struct(self, SDL_Event, &SDL_Event_data_type, ev);
return rb_sprintf("<%s: type=%u timestamp=%u"
" touch_id=%d finger_id=%d"
" x=%f y=%f pressure=%f"
Expand Down
4 changes: 3 additions & 1 deletion gamecontroller.c.m4
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ static void GameController_free(GameController* g)
free(g);
}

DEFINE_DATA_TYPE(GameController, GameController_free);

/*
* Document-class: SDL2::GameController
*
Expand Down Expand Up @@ -62,7 +64,7 @@ static VALUE GameController_new(SDL_GameController* controller)
{
GameController* g = ALLOC(GameController);
g->controller = controller;
return Data_Wrap_Struct(cGameController, 0, GameController_free, g);
return TypedData_Wrap_Struct(cGameController, &GameController_data_type, g);
}

DEFINE_WRAPPER(SDL_GameController, GameController, controller, cGameController,
Expand Down
8 changes: 5 additions & 3 deletions gl.c.m4
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ typedef struct GLContext {
SDL_GLContext context;
} GLContext;

DEFINE_WRAPPER(SDL_GLContext, GLContext, context, cGLContext, "SDL2::GL::Context");

static void GLContext_free(GLContext* c)
{
if (c->context)
SDL_GL_DeleteContext(c->context);
free(c);
}

DEFINE_DATA_TYPE(GLContext, GLContext_free);

DEFINE_WRAPPER(SDL_GLContext, GLContext, context, cGLContext, "SDL2::GL::Context");

static VALUE GLContext_new(SDL_GLContext context)
{
GLContext* c = ALLOC(GLContext);
c->context = context;
return Data_Wrap_Struct(cGLContext, 0, GLContext_free, c);
return TypedData_Wrap_Struct(cGLContext, &GLContext_data_type, c);
}

/*
Expand Down
4 changes: 3 additions & 1 deletion joystick.c.m4
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ static void Joystick_free(Joystick* j)
free(j);
}

DEFINE_DATA_TYPE(Joystick, Joystick_free);

static VALUE Joystick_new(SDL_Joystick* joystick)
{
Joystick* j = ALLOC(Joystick);
j->joystick = joystick;
return Data_Wrap_Struct(cJoystick, 0, Joystick_free, j);
return TypedData_Wrap_Struct(cJoystick, &Joystick_data_type, j);
}

DEFINE_WRAPPER(SDL_Joystick, Joystick, joystick, cJoystick, "SDL2::Joystick");
Expand Down
14 changes: 9 additions & 5 deletions mixer.c.m4
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,38 @@ typedef struct Music {

static void Chunk_free(Chunk* c)
{
if (rubysdl2_is_active() && c->chunk)
if (rubysdl2_is_active() && c->chunk)
Mix_FreeChunk(c->chunk);
free(c);
}

DEFINE_DATA_TYPE(Chunk, Chunk_free);

static VALUE Chunk_new(Mix_Chunk* chunk)
{
Chunk* c = ALLOC(Chunk);
c->chunk = chunk;
return Data_Wrap_Struct(cChunk, 0, Chunk_free, c);
return TypedData_Wrap_Struct(cChunk, &Chunk_data_type, c);
}

DEFINE_WRAPPER(Mix_Chunk, Chunk, chunk, cChunk, "SDL2::Mixer::Chunk");

static void Music_free(Music* m)
{
if (rubysdl2_is_active() && m->music)
if (rubysdl2_is_active() && m->music)
Mix_FreeMusic(m->music);
free(m);
}

DEFINE_DATA_TYPE(Music, Music_free);

static VALUE Music_new(Mix_Music* music)
{
Music* c = ALLOC(Music);
c->music = music;
return Data_Wrap_Struct(cMusic, 0, Music_free, c);
return TypedData_Wrap_Struct(cMusic, &Music_data_type, c);
}

DEFINE_WRAPPER(Mix_Music, Music, music, cMusic, "SDL2::Mixer::Music");

/*
Expand Down
13 changes: 12 additions & 1 deletion rubysdl2_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,25 @@ void rubysdl2_init_gamecontorller(void);
#define UCHAR2NUM UINT2NUM
#define rb_str_export_to_utf8(str) rb_str_export_to_enc((str), rb_utf8_encoding())

/* Helper macro to define a rb_data_type_t for TypedData.
* Usage: DEFINE_DATA_TYPE(struct_name, free_func)
* Defines: static const rb_data_type_t struct_name##_data_type
*/
#define DEFINE_DATA_TYPE(struct_name, free_func) \
static const rb_data_type_t struct_name##_data_type = { \
#struct_name, \
{ NULL, (void (*)(void*))(free_func), NULL }, \
NULL, NULL, 0 \
};

#define DEFINE_GETTER(scope, ctype, var_class, classname) \
scope ctype* Get_##ctype(VALUE obj) \
{ \
ctype* s; \
if (!rb_obj_is_kind_of(obj, var_class)) \
rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)", \
rb_obj_classname(obj), classname); \
Data_Get_Struct(obj, ctype, s); \
TypedData_Get_Struct(obj, ctype, &ctype##_data_type, s); \
\
return s; \
}
Expand Down
4 changes: 3 additions & 1 deletion ttf.c.m4
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ static void TTF_free(TTF* f)
free(f);
}

DEFINE_DATA_TYPE(TTF, TTF_free);

static VALUE TTF_new(TTF_Font* font)
{
TTF* f = ALLOC(TTF);
f->font = font;
return Data_Wrap_Struct(cTTF, 0, TTF_free, f);
return TypedData_Wrap_Struct(cTTF, &TTF_data_type, f);
}

DEFINE_WRAPPER(TTF_Font, TTF, font, cTTF, "SDL2::TTF");
Expand Down
Loading