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

Classes: DeviceInfo

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Array<SDL2::Joystick::DeviceInfo>) devices

Get the information of connected joysticks

Returns:



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

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;
}

+ (Boolean) game_controller?(index)

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

Parameters:

  • index (Integer)

    the joystick device index

See Also:

Returns:

  • (Boolean)


103
104
105
106
# File 'joystick.c', line 103

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

+ (Integer) num_connected_joysticks

Get the number of connected joysticks.

Returns:

  • (Integer)


46
47
48
49
# File 'joystick.c', line 46

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

+ (SDL2::Joystick) open(device_index)

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.



86
87
88
89
90
91
92
# File 'joystick.c', line 86

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

- (Boolean) attached?

Return true a joystick has been opened and currently connected.

Returns:

  • (Boolean)


111
112
113
114
115
116
117
# File 'joystick.c', line 111

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

- (Integer) axis(which)

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:



216
217
218
219
# File 'joystick.c', line 216

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

- ([Integer,Integer]) ball(which)

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:



229
230
231
232
233
234
# File 'joystick.c', line 229

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));
}

- (Boolean) button(which)

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:



244
245
246
247
# File 'joystick.c', line 244

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

- (nil) destroy Also known as: close

Close a joystick device.

Returns:

  • (nil)

See Also:



149
150
151
152
153
154
155
156
# File 'joystick.c', line 149

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

- (Boolean) destroy? Also known as: close?

Return true if the device is alread closed.

Returns:

  • (Boolean)

See Also:

- (String) GUID

Get the joystick GUID

Returns:

  • (String)

    GUID string



124
125
126
127
128
129
130
131
# File 'joystick.c', line 124

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);
}

- (Integer) hat(which)

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:



257
258
259
260
# File 'joystick.c', line 257

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

- (Integer) index

Get the index of a joystick

Returns:

  • (Integer)

    index



138
139
140
141
# File 'joystick.c', line 138

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

- (String) name

Get the name of a joystick

Returns:

  • (String)

    name



163
164
165
166
# File 'joystick.c', line 163

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

- (Integer) num_axes

Get the number of general axis controls on a joystick.

Returns:

  • (Integer)

See Also:



173
174
175
176
# File 'joystick.c', line 173

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

- (Integer) num_balls

Get the number of trackball on a joystick

Returns:

  • (Integer)

See Also:



183
184
185
186
# File 'joystick.c', line 183

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

- (Integer) num_buttons

Get the number of button on a joystick

Returns:

  • (Integer)

See Also:



193
194
195
196
# File 'joystick.c', line 193

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

- (Integer) num_hats

Get the number of POV hats on a joystick

Returns:

  • (Integer)

See Also:



203
204
205
206
# File 'joystick.c', line 203

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