Class: SDL2::Joystick

Inherits:
Object
  • Object
show all
Defined in:
joystick.c,
joystick.c

Overview

This class represents a joystick connected to the machine.

In order to use joystick subsystem, init must have been called with the SDL2::INIT_JOYSTICK flag.

Defined Under Namespace

Modules: Hat Classes: DeviceInfo

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.devicesArray<SDL2::Joystick::DeviceInfo>

Get the information of connected joysticks

Returns:



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'joystick.c', line 65

static VALUE Joystick_s_devices(VALUE self)
{
    int num_joysticks = SDL_NumJoysticks();
    int i;
    VALUE devices = rb_ary_new2(num_joysticks);
    for (i=0; i<num_joysticks; ++i) {
        VALUE device = rb_obj_alloc(cDeviceInfo);
        rb_iv_set(device, "@GUID", GUID_to_String(SDL_JoystickGetDeviceGUID(i)));
        rb_iv_set(device, "@name", utf8str_new_cstr(SDL_JoystickNameForIndex(i)));
        rb_ary_push(devices, device);
    }
    return devices;
}

.game_controller?(index) ⇒ Boolean

Return true if the joystick of given index supports the game controller interface.

Parameters:

  • index (Integer)

    the joystick device index

See Also:

Returns:

  • (Boolean)


105
106
107
108
# File 'joystick.c', line 105

static VALUE Joystick_s_game_controller_p(VALUE self, VALUE index)
{
    return INT2BOOL(SDL_IsGameController(NUM2INT(index)));
}

.num_connected_joysticksInteger

Get the number of connected joysticks.

Returns:

  • (Integer)


48
49
50
51
# File 'joystick.c', line 48

static VALUE Joystick_s_num_connected_joysticks(VALUE self)
{
    return INT2FIX(HANDLE_ERROR(SDL_NumJoysticks()));
}

.open(device_index) ⇒ SDL2::Joystick

Open a joystick for use.

Parameters:

  • device_index (Integer)

    device index

Returns:

Raises:

  • (SDL2::Error)

    raised when device open is failed. for exmaple, device_index is out of range.



88
89
90
91
92
93
94
# File 'joystick.c', line 88

static VALUE Joystick_s_open(VALUE self, VALUE device_index)
{
    SDL_Joystick* joystick = SDL_JoystickOpen(NUM2INT(device_index));
    if (!joystick)
        SDL_ERROR();
    return Joystick_new(joystick);
}

Instance Method Details

#attached?Boolean

Return true a joystick has been opened and currently connected.

Returns:

  • (Boolean)


113
114
115
116
117
118
119
# File 'joystick.c', line 113

static VALUE Joystick_attached_p(VALUE self)
{
    Joystick* j = Get_Joystick(self);
    if (!j->joystick)
        return Qfalse;
    return INT2BOOL(SDL_JoystickGetAttached(j->joystick));
}

#axis(which) ⇒ Integer

Get the current state of an axis control on a joystick.

Parameters:

  • which (Integer)

    an index of an axis, started at index 0

Returns:

  • (Integer)

    state value, ranging from -32768 to 32767.

See Also:



218
219
220
221
# File 'joystick.c', line 218

static VALUE Joystick_axis(VALUE self, VALUE which)
{
    return INT2FIX(SDL_JoystickGetAxis(Get_SDL_Joystick(self), NUM2INT(which)));
}

#ball(which) ⇒ [Integer,Integer]

Get the current state of a trackball on a joystick.

Parameters:

  • which (Integer)

    an index of a trackball, started at index 0

Returns:

  • ([Integer,Integer])

    dx and dy

See Also:



231
232
233
234
235
236
# File 'joystick.c', line 231

static VALUE Joystick_ball(VALUE self, VALUE which)
{
    int dx, dy;
    HANDLE_ERROR(SDL_JoystickGetBall(Get_SDL_Joystick(self), NUM2INT(which), &dx, &dy));
    return rb_ary_new3(2, INT2NUM(dx), INT2NUM(dy));
}

#button(which) ⇒ Boolean

Get the current state of a button on a joystick.

Parameters:

  • which (Integer)

    an index of a button, started at index 0

Returns:

  • (Boolean)

    true if the button is pressed

See Also:



246
247
248
249
# File 'joystick.c', line 246

static VALUE Joystick_button(VALUE self, VALUE which)
{
    return INT2BOOL(SDL_JoystickGetButton(Get_SDL_Joystick(self), NUM2INT(which)));
}

#destroynil Also known as: close

Close a joystick device.

Returns:

  • (nil)

See Also:



151
152
153
154
155
156
157
158
# File 'joystick.c', line 151

static VALUE Joystick_destroy(VALUE self)
{
    Joystick* j = Get_Joystick(self);
    if (j->joystick)
        SDL_JoystickClose(j->joystick);
    j->joystick = NULL;
    return Qnil;
}

#destroy?Boolean Also known as: close?

Return true if the device is alread closed.

Returns:

  • (Boolean)

See Also:

#GUIDString

Get the joystick GUID

Returns:

  • (String)

    GUID string



126
127
128
129
130
131
132
133
# File 'joystick.c', line 126

static VALUE Joystick_GUID(VALUE self)
{
    SDL_JoystickGUID guid;
    char buf[128];
    guid = SDL_JoystickGetGUID(Get_SDL_Joystick(self));
    SDL_JoystickGetGUIDString(guid, buf, sizeof(buf));
    return rb_usascii_str_new_cstr(buf);
}

#hat(which) ⇒ Integer

Get the current state of a POV hat on a joystick.

Parameters:

  • which (Integer)

    an index of a hat, started at index 0

Returns:

  • (Integer)

    hat state

See Also:



259
260
261
262
# File 'joystick.c', line 259

static VALUE Joystick_hat(VALUE self, VALUE which)
{
    return UINT2NUM(SDL_JoystickGetHat(Get_SDL_Joystick(self), NUM2INT(which)));
}

#indexInteger

Get the index of a joystick

Returns:

  • (Integer)

    index



140
141
142
143
# File 'joystick.c', line 140

static VALUE Joystick_index(VALUE self)
{
    return INT2NUM(HANDLE_ERROR(SDL_JoystickInstanceID(Get_SDL_Joystick(self))));
}

#nameString

Get the name of a joystick

Returns:

  • (String)

    name



165
166
167
168
# File 'joystick.c', line 165

static VALUE Joystick_name(VALUE self)
{
    return utf8str_new_cstr(SDL_JoystickName(Get_SDL_Joystick(self)));
}

#num_axesInteger

Get the number of general axis controls on a joystick.

Returns:

  • (Integer)

See Also:



175
176
177
178
# File 'joystick.c', line 175

static VALUE Joystick_num_axes(VALUE self)
{
    return INT2FIX(SDL_JoystickNumAxes(Get_SDL_Joystick(self)));
}

#num_ballsInteger

Get the number of trackball on a joystick

Returns:

  • (Integer)

See Also:



185
186
187
188
# File 'joystick.c', line 185

static VALUE Joystick_num_balls(VALUE self)
{
    return INT2FIX(SDL_JoystickNumBalls(Get_SDL_Joystick(self)));
}

#num_buttonsInteger

Get the number of button on a joystick

Returns:

  • (Integer)

See Also:



195
196
197
198
# File 'joystick.c', line 195

static VALUE Joystick_num_buttons(VALUE self)
{
    return INT2FIX(SDL_JoystickNumButtons(Get_SDL_Joystick(self)));
}

#num_hatsInteger

Get the number of POV hats on a joystick

Returns:

  • (Integer)

See Also:



205
206
207
208
# File 'joystick.c', line 205

static VALUE Joystick_num_hats(VALUE self)
{
    return INT2FIX(SDL_JoystickNumHats(Get_SDL_Joystick(self)));
}