Module: SDL2

Defined in:
main.c,
lib/sdl2/version.rb,
main.c

Overview

Namespace module for Ruby/SDL2.

Defined Under Namespace

Modules: BlendMode, Clipboard, GL, Hints, IMG, Key, MessageBox, Mixer, Mouse, ScreenSaver, TextInput Classes: Display, Error, Event, GameController, Joystick, PixelFormat, Point, Rect, Renderer, Surface, TTF, Texture, Window

Constant Summary

VERSION =

Version string of Ruby/SDL2

"0.3.2"
VERSION_NUMBER =

Version of Ruby/SDL2, [major, minor, patch level]

VERSION.split(".").map(&:to_i)
LIBSDL_VERSION =

SDL’s version string

libsdl_version()
LIBSDL_VERSION_NUMBER =

SDL’s version array of numbers

libsdl_version_number()
LIBSDL_REVISION =

SDL’s revision (from VCS) string

libsdl_revision()
LIBSDL_REVISION_NUMBER =

SDL’s revision (from VCS) array of numbers

libsdl_revision_number()
LIBSDL_IMAGE_VERSION =

SDL_image’s version string, only available if SDL_image is linked

SDL_version_to_String(version)
LIBSDL_IMAGE_VERSION_NUMBER =

SDL_image’s version array of numbers

SDL_version_to_Array(version)
LIBSDL_TTF_VERSION =

SDL_ttf’s version string, only available if SDL_ttf is linked

SDL_version_to_String(version)
LIBSDL_TTF_VERSION_NUMBER =

SDL_ttf’s version array of numbers

SDL_version_to_Array(version)
LIBSDL_MIXER_VERSION =

SDL_mixer’s version string , only available if SDL_mixer is linked

SDL_version_to_String(version)
LIBSDL_MIXER_VERSION_NUMBER =

SDL_mixer’s version array of numbers

SDL_version_to_Array(version)

Class Method Summary collapse

Class Method Details

.base_pathString

Note:

This method is available since SDL 2.0.1.

Get the directory where the application was run from.

In almost all cases, using this method, you will get the directory where ruby executable is in. Probably what you hope is $0 or FILE.

Returns:

  • (String)

    an absolute path in UTF-8 encoding to the application data directory

Raises:

  • (SDL2::Error)

    raised if your platform doesn’t implement this functionality.



15
16
17
18
19
20
21
22
23
24
# File 'filesystem.c', line 15

static VALUE SDL2_s_base_path(VALUE self)
{
    char* path = SDL_GetBasePath();
    VALUE str;
    if (!path)
        SDL_ERROR();
    str = utf8str_new_cstr(path);
    SDL_free(path);
    return str;
}

.current_video_driverString?

Get the name of current video driver

Returns:

  • (String)

    the name of the current video driver

  • (nil)

    when the video is not initialized



315
316
317
318
319
320
321
322
# File 'video.c', line 315

static VALUE SDL2_s_current_video_driver(VALUE self)
{
    const char* name = SDL_GetCurrentVideoDriver();
    if (name)
        return utf8str_new_cstr(name);
    else
        return Qnil;
}

.delay(ms) ⇒ nil

Wait a specified milliseconds.

This function stops all ruby threads. If you want to keep running other threads, you should use Kernel.sleep instead of this function.

Parameters:

  • ms (Integer)

    the number of milliseconds to delay

Returns:

  • (nil)


15
16
17
18
19
# File 'timer.c', line 15

static VALUE SDL_s_delay(VALUE self, VALUE ms)
{
    SDL_Delay(NUM2UINT(ms));
    return Qnil;
}

.get_performance_counterInteger

Return the current value of the high resolution counter.

This method is typically used for profiling.

Returns:

  • (Integer)

    the current counter value

See Also:



39
40
41
42
# File 'timer.c', line 39

static VALUE SDL_s_get_performance_counter(VALUE self)
{
    return ULL2NUM(SDL_GetPerformanceCounter());
}

.get_performance_frequencyInteger

Return the frequency (count per second) of the high resolution counter.

Returns:

  • (Integer)

    a platform-specific count per second.

See Also:



50
51
52
53
# File 'timer.c', line 50

static VALUE SDL_s_get_performance_frequency(VALUE self)
{
    return ULL2NUM(SDL_GetPerformanceFrequency());
}

.get_ticksInteger

Return the number of milliseconds since init is called.

Returns:

  • (Integer)

    milliseconds in 32bit unsigned value.



26
27
28
29
# File 'timer.c', line 26

static VALUE SDL_s_get_ticks(VALUE self)
{
    return UINT2NUM(SDL_GetTicks());
}

.init(flags) ⇒ nil

Initialize SDL. You must call this function before using any other Ruby/SDL2 methods.

You can specify initialized subsystem by flags which is bitwise OR of the following constants:

  • SDL2::INIT_TIMER - timer subsystem
  • SDL2::INIT_AUDIO - audio subsystem
  • SDL2::INIT_VIDEO - video subsystem
  • SDL2::INIT_JOYSTICK - joystick subsystem
  • SDL2::INIT_HAPTIC - haptic (force feedback) subsystem (interface is not implemented yet)
  • SDL2::INIT_GAMECONTROLLER - controller subsystem
  • SDL2::INIT_EVENTS - events subsystem
  • SDL2::INIT_EVERYTHING - all of the above flags
  • SDL2::INIT_NOPARACHUTE - this flag is ignored; for compatibility

Parameters:

  • flags (Integer)

    initializing subsystems

Returns:

  • (nil)


116
117
118
119
120
121
122
# File 'main.c', line 116

static VALUE SDL2_s_init(VALUE self, VALUE flags)
{
    SDL_SetMainReady();
    HANDLE_ERROR(SDL_Init(NUM2UINT(flags)));
    state = INITIALIZDED;
    return Qnil;
}

.preference_path(org, app) ⇒ String

Note:

This method is available since SDL 2.0.1.

Get the “pref dir”. You can use the directory to write personal files such as preferences and save games.

The directory is unique per user and per application.

Examples:

SDL2.preference_path("foo", "bar") # => "/home/ohai/.local/share/foo/bar/" (on Linux)

Parameters:

  • org (String)

    the name of your organization

  • app (String)

    the name of your application

Returns:

  • (String)

    a UTF-8 string of the user directory in platform-depnedent notation.

Raises:

  • (SDL2::Error)

    raised if your platform doesn’t implement this functionality.



43
44
45
46
47
48
49
50
51
52
# File 'filesystem.c', line 43

static VALUE SDL2_s_preference_path(VALUE self, VALUE org, VALUE app)
{
    char* path = SDL_GetPrefPath(StringValueCStr(org), StringValueCStr(app));
    VALUE str;
    if (!path)
        SDL_ERROR();
    str = utf8str_new_cstr(path);
    SDL_free(path);
    return str;
}

.video_driversArray<String>

Get the names of all video drivers.

You can use the name as an argument of video_init.

Returns:

  • (Array<String>)


299
300
301
302
303
304
305
306
307
# File 'video.c', line 299

static VALUE SDL2_s_video_drivers(VALUE self)
{
    int num_drivers = HANDLE_ERROR(SDL_GetNumVideoDrivers());
    int i;
    VALUE drivers = rb_ary_new();
    for (i=0; i<num_drivers; ++i)
        rb_ary_push(drivers, rb_usascii_str_new_cstr(SDL_GetVideoDriver(i)));
    return drivers;
}

.video_init(driver_name) ⇒ nil

Initialize the video subsystem, specifying a video driver.

init cannot specify a video driver, so you need to use this method to specify a driver.

Parameters:

  • driver_name (String)

Returns:

  • (nil)

See Also:



336
337
338
339
340
# File 'video.c', line 336

static VALUE SDL2_s_video_init(VALUE self, VALUE driver_name)
{
    HANDLE_ERROR(SDL_VideoInit(StringValueCStr(driver_name)));
    return Qnil;
}