Class: SDL2::Window

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

Overview

This class represents a window.

If you want to create graphical application using Ruby/SDL, first you need to create a window.

All of methods/class methods are available only after initializing video subsystem by init.

Defined Under Namespace

Modules: Flags

Constant Summary collapse

POS_CENTERED =

Returns Indicate that you don’t care what the window position is.

Returns:

  • (Integer)

    Indicate that you don’t care what the window position is

INT2NUM(SDL_WINDOWPOS_CENTERED)
POS_UNDEFINED =

Returns Indicate that the window position should be centered.

Returns:

  • (Integer)

    Indicate that the window position should be centered

INT2NUM(SDL_WINDOWPOS_UNDEFINED)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_windowsHash<Integer => SDL2::Window>

Get all windows under SDL.

Returns:



412
413
414
415
# File 'video.c', line 412

static VALUE Window_s_all_windows(VALUE self)
{
    return rb_hash_dup(hash_windowid_to_window);
}

.create(title, x, y, w, h, flags) ⇒ SDL2::Window

Create a window with the specified position (x,y), dimensions (w,h) and flags.

Parameters:

  • title (String)

    title of the created window

  • x (Integer)

    the x position of the left-top of the window

  • y (Integer)

    the y position of the left-top of the window

  • w (Integer)

    the width of the window

  • h (Integer)

    the height of the window

  • flags (Integer)

    0, or one or more Flags OR’d together

Returns:



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'video.c', line 389

static VALUE Window_s_create(VALUE self, VALUE title, VALUE x, VALUE y, VALUE w, VALUE h,
                             VALUE flags)
{
    SDL_Window* window;
    VALUE win;
    title = rb_str_export_to_enc(title, rb_utf8_encoding());
    window = SDL_CreateWindow(StringValueCStr(title),
                              NUM2INT(x), NUM2INT(y), NUM2INT(w), NUM2INT(h),
                              NUM2UINT(flags));
    if (window == NULL)
        HANDLE_ERROR(-1);

    win = Window_new(window);
    rb_hash_aset(hash_windowid_to_window, UINT2NUM(SDL_GetWindowID(window)), win);
    return win;
}

.find_by_id(id) ⇒ SDL2::Window?

Get the window from ID.

Parameters:

  • id (Integer)

    the window id you want to find

Returns:

  • (SDL2::Window)

    the window associated with id

  • (nil)

    when no window is associated with id



426
427
428
429
# File 'video.c', line 426

static VALUE Window_s_find_by_id(VALUE self, VALUE id)
{
    return rb_hash_aref(hash_windowid_to_window, id);
}

Instance Method Details

#borderedBoolean

Return true if the window is bordered

Returns:

  • (Boolean)

See Also:



774
775
776
777
# File 'video.c', line 774

static VALUE Window_bordered(VALUE self)
{
    return INT2BOOL(!(SDL_GetWindowFlags(Get_SDL_Window(self)) & SDL_WINDOW_BORDERLESS));
}

#bordered=(bordered) ⇒ void

This method returns an undefined value.

Set the border state of the window.

Parameters:

  • bordered (Boolean)

    true for bordered window, anad false for borderless window

See Also:



790
791
792
793
794
# File 'video.c', line 790

static VALUE Window_set_bordered(VALUE self, VALUE bordered)
{
    SDL_SetWindowBordered(Get_SDL_Window(self), RTEST(bordered));
    return bordered;
}

#brightnessFloat

Get the brightness (gamma correction) of the window.

Returns:

  • (Float)

    the brightness

See Also:



529
530
531
532
# File 'video.c', line 529

static VALUE Window_brightness(VALUE self)
{
    return DBL2NUM(SDL_GetWindowBrightness(Get_SDL_Window(self)));
}

#brightness=(brightness) ⇒ void

This method returns an undefined value.

Set the brightness (gamma correction) of the window.

Parameters:

  • brightness (Float)

    the brightness, 0.0 means complete dark and 1.0 means normal brightness.

See Also:



544
545
546
547
548
# File 'video.c', line 544

static VALUE Window_set_brightness(VALUE self, VALUE brightness)
{
    HANDLE_ERROR(SDL_SetWindowBrightness(Get_SDL_Window(self), NUM2DBL(brightness)));
    return brightness;
}

#create_renderer(index, flags) ⇒ SDL2::Renderer

Create a 2D rendering context for a window.

Parameters:

  • index (Integer)

    the index of the rendering driver to initialize, or -1 to initialize the first one supporting the requested flags

  • flags (Integer)

    0, or one or more Flags OR’d together

Returns:



464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'video.c', line 464

static VALUE Window_create_renderer(VALUE self, VALUE index, VALUE flags)
{
    SDL_Renderer* sdl_renderer;
    VALUE renderer;
    sdl_renderer = SDL_CreateRenderer(Get_SDL_Window(self), NUM2INT(index), NUM2UINT(flags));

    if (sdl_renderer == NULL)
        HANDLE_ERROR(-1);

    renderer = Renderer_new(sdl_renderer, Get_Window(self));
    rb_iv_set(self, "renderer", renderer);
    return renderer;
}

#debug_infoHash

Returns (GC) debug information.

Returns:

  • (Hash)

    (GC) debug information



954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
# File 'video.c', line 954

static VALUE Window_debug_info(VALUE self)
{
    Window* w = Get_Window(self);
    VALUE info = rb_hash_new();
    int num_active_renderers = 0;
    int i;
    rb_hash_aset(info, rb_str_new2("destroy?"), INT2BOOL(w->window == NULL));
    rb_hash_aset(info, rb_str_new2("max_renderers"), INT2NUM(w->max_renderers));
    rb_hash_aset(info, rb_str_new2("num_renderers"), INT2NUM(w->num_renderers));
    for (i=0; i<w->num_renderers; ++i)
        if (w->renderers[i]->renderer)
            ++num_active_renderers;
    rb_hash_aset(info, rb_str_new2("num_active_renderers"), INT2NUM(num_active_renderers));

    return info;
}

#destroyvoid

This method returns an undefined value.

Destroy window.

You cannot call almost all methods after calling this method. The exception is #destroy?.



445
446
447
448
449
450
451
452
# File 'video.c', line 445

static VALUE Window_destroy(VALUE self)
{
    Window* w = Get_Window(self);
    Window_destroy_internal(w);
    SDL_DestroyWindow(w->window);
    w->window = NULL;
    return Qnil;
}

#destroy?Boolean

Returns true if the window is already destroyed.

Returns:

  • (Boolean)

    true if the window is already destroyed.

#displaySDL2::Display

Get the display associated with the window.

Returns:



517
518
519
520
521
# File 'video.c', line 517

static VALUE Window_display(VALUE self)
{
    int display_index = HANDLE_ERROR(SDL_GetWindowDisplayIndex(Get_SDL_Window(self)));
    return Display_new(display_index);
}

#display_modeSDL2::Display::Mode

Get information about the window.

Returns:



505
506
507
508
509
510
# File 'video.c', line 505

static VALUE Window_display_mode(VALUE self)
{
    SDL_DisplayMode mode;
    HANDLE_ERROR(SDL_GetWindowDisplayMode(Get_SDL_Window(self), &mode));
    return DisplayMode_new(&mode);
}

#flagsInteger

Get the Window flag masks of the window.

Returns:

  • (Integer)

    flags

See Also:



556
557
558
559
# File 'video.c', line 556

static VALUE Window_flags(VALUE self)
{
    return UINT2NUM(SDL_GetWindowFlags(Get_SDL_Window(self)));
}

#fullscreen_modeInteger

Get the fullscreen stete of the window

Returns:

See Also:



893
894
895
896
897
# File 'video.c', line 893

static VALUE Window_fullscreen_mode(VALUE self)
{
    Uint32 flags = SDL_GetWindowFlags(Get_SDL_Window(self));
    return UINT2NUM(flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_FULLSCREEN_DESKTOP));
}

#fullscreen_mode=(flag) ⇒ void

This method returns an undefined value.

Set the fullscreen state of the window

Parameters:

  • flag (Integer)

    0 for window mode, SDL2::Window::Flags::FULLSCREEN for fullscreen mode, and Flags::Window::FULLSCREEN_DESKTOP for fullscreen at the current desktop resolution.

See Also:



910
911
912
913
914
# File 'video.c', line 910

static VALUE Window_set_fullscreen_mode(VALUE self, VALUE flags)
{
    HANDLE_ERROR(SDL_SetWindowFullscreen(Get_SDL_Window(self), NUM2UINT(flags)));
    return flags;
}

#gamma_rampArray<Array<Integer>>

Get the gamma ramp for a window

Returns:

  • (Array<Array<Integer>>)

    the gamma ramp, return value is red, green, and blue gamma tables and each gamma table has 256 Integers of 0-65535.



578
579
580
581
582
583
584
585
586
# File 'video.c', line 578

static VALUE Window_gamma_ramp(VALUE self)
{
    Uint16 r[256], g[256], b[256];
    HANDLE_ERROR(SDL_GetWindowGammaRamp(Get_SDL_Window(self), r, g, b));
    return rb_ary_new3(3,
                       gamma_table_to_Array(r),
                       gamma_table_to_Array(g),
                       gamma_table_to_Array(b));
}

#gl_drawable_sizeArray(Integer, Integer)

Get the size of the drawable region.

Returns:

  • (Array(Integer, Integer))

    the width and height of the region



922
923
924
925
926
927
# File 'video.c', line 922

static VALUE Window_gl_drawable_size(VALUE self)
{
    int w, h;
    SDL_GL_GetDrawableSize(Get_SDL_Window(self), &w, &h);
    return rb_ary_new3(2, INT2NUM(w), INT2NUM(h));
}

#gl_swapnil

Swap the OpenGL buffers for the window, if double buffering is supported.

Returns:

  • (nil)


936
937
938
939
940
# File 'video.c', line 936

static VALUE Window_gl_swap(VALUE self)
{
    SDL_GL_SwapWindow(Get_SDL_Window(self));
    return Qnil;
}

#hidenil

Hide the window.

Returns:

  • (nil)

See Also:



832
833
834
835
# File 'video.c', line 832

static VALUE Window_hide(VALUE self)
{
    SDL_HideWindow(Get_SDL_Window(self)); return Qnil;
};

#icon=(icon) ⇒ void

This method returns an undefined value.

Set the window icon.

Parameters:



596
597
598
599
600
# File 'video.c', line 596

static VALUE Window_set_icon(VALUE self, VALUE icon)
{
    SDL_SetWindowIcon(Get_SDL_Window(self), Get_SDL_Surface(icon));
    return icon;
}

#input_is_grabbed=(grabbed) ⇒ void

This method returns an undefined value.

Set the window’s input grab mode.

Parameters:

  • grabbed (Boolean)

    true to grub input, and false to release input

See Also:



621
622
623
624
625
# File 'video.c', line 621

static VALUE Window_set_input_is_grabbed(VALUE self, VALUE grabbed)
{
    SDL_SetWindowGrab(Get_SDL_Window(self), RTEST(grabbed));
    return grabbed;
}

#input_is_grabbed?Boolean

Return true if the input is grabbed to the window.

Returns:

  • (Boolean)

See Also:



607
608
609
610
# File 'video.c', line 607

static VALUE Window_input_is_grabbed_p(VALUE self)
{
    return INT2BOOL(SDL_GetWindowGrab(Get_SDL_Window(self)));
}

#inspectString

Returns inspection string.

Returns:

  • (String)

    inspection string



943
944
945
946
947
948
949
950
951
# File 'video.c', line 943

static VALUE Window_inspect(VALUE self)
{
    Window* w = Get_Window(self);
    if (w->window)
        return rb_sprintf("<%s:%p window_id=%d>",
                          rb_obj_classname(self), (void*)self, SDL_GetWindowID(w->window));
    else
        return rb_sprintf("<%s:%p (destroyed)>", rb_obj_classname(self), (void*)self);
}

#maximizenil

Maximize the window.

Returns:

  • (nil)

See Also:



844
845
846
847
# File 'video.c', line 844

static VALUE Window_maximize(VALUE self)
{
    SDL_MaximizeWindow(Get_SDL_Window(self)); return Qnil;
};

#maximum_sizeArray(Integer,Integer)

Get the maximum size of the window’s client area.

Returns:

  • (Array(Integer,Integer))

    maximum width and maximum height.

See Also:



650
651
652
653
# File 'video.c', line 650

static VALUE Window_maximum_size(VALUE self)
{
    return Window_get_int_int(SDL_GetWindowMaximumSize, self);
}

#maximum_size=(size) ⇒ void

This method returns an undefined value.

Set the maximum size of the window’s client area.

Parameters:

  • size (Array(Integer, Integer))

    maximum width and maximum height, the both must be positive.

See Also:



666
667
668
669
# File 'video.c', line 666

static VALUE Window_set_maximum_size(VALUE self, VALUE max_size)
{
    return Window_set_int_int(SDL_SetWindowMaximumSize, self, max_size);
}

#minimizenil

Minimize the window.

Returns:

  • (nil)

See Also:



856
857
858
859
# File 'video.c', line 856

static VALUE Window_minimize(VALUE self)
{
    SDL_MinimizeWindow(Get_SDL_Window(self)); return Qnil;
};

#minimum_sizeArray(Integer,Integer)

Get the minimum size of the window’s client area.

Returns:

  • (Array(Integer,Integer))

    minimum width and minimum height.

See Also:



678
679
680
681
# File 'video.c', line 678

static VALUE Window_minimum_size(VALUE self)
{
    return Window_get_int_int(SDL_GetWindowMinimumSize, self);
}

#minimum_size=(size) ⇒ void

This method returns an undefined value.

Set the minimum size of the window’s client area.

Parameters:

  • size (Array(Integer, Integer))

    minimum width and minimum height, the both must be positive.

See Also:



694
695
696
697
# File 'video.c', line 694

static VALUE Window_set_minimum_size(VALUE self, VALUE min_size)
{
    return Window_set_int_int(SDL_SetWindowMinimumSize, self, min_size);
}

#positionArray(Integer,Integer)

Get the position of the window.

Returns:

  • (Array(Integer,Integer))

    the x position and the y position

See Also:



706
707
708
709
# File 'video.c', line 706

static VALUE Window_position(VALUE self)
{
    return Window_get_int_int(SDL_GetWindowPosition, self);
}

#position=(xy) ⇒ void

This method returns an undefined value.

Set the position of the window

Parameters:

See Also:



723
724
725
726
# File 'video.c', line 723

static VALUE Window_set_position(VALUE self, VALUE xy)
{
    return Window_set_int_int(SDL_SetWindowPosition, self, xy);
}

#raisenil

Raise the window above other windows and set the input focus.

Returns:

  • (nil)


866
867
868
869
# File 'video.c', line 866

static VALUE Window_raise(VALUE self)
{
    SDL_RaiseWindow(Get_SDL_Window(self)); return Qnil;
};

#rendererSDL2::Renderer?

Return the renderer associate with the window

Returns:

  • (SDL2::Renderer)

    the associated renderer

  • (nil)

    if no renderer is created yet



485
486
487
488
# File 'video.c', line 485

static VALUE Window_renderer(VALUE self)
{
    return rb_iv_get(self, "renderer");
}

#restorenil

Restore the size and position of a minimized or maixmized window.

Returns:

  • (nil)

See Also:



878
879
880
881
# File 'video.c', line 878

static VALUE Window_restore(VALUE self)
{
    SDL_RestoreWindow(Get_SDL_Window(self)); return Qnil;
};

#shownil

Show the window.

Returns:

  • (nil)

See Also:



821
822
823
824
# File 'video.c', line 821

static VALUE Window_show(VALUE self)
{
    SDL_ShowWindow(Get_SDL_Window(self)); return Qnil;
};

#sizeArray(Integer, Integer)

Get the size of the window.

Returns:

  • (Array(Integer, Integer))

    the width and the height

See Also:



735
736
737
738
# File 'video.c', line 735

static VALUE Window_size(VALUE self)
{
    return Window_get_int_int(SDL_GetWindowSize, self);
}

#size=(size) ⇒ void

This method returns an undefined value.

Set the size of the window.

Parameters:

  • size (Array(Integer, Integer))

    new width and new height

See Also:



750
751
752
753
# File 'video.c', line 750

static VALUE Window_set_size(VALUE self, VALUE size)
{
    return Window_set_int_int(SDL_SetWindowSize, self, size);
}

#titleString

Get the title of the window

Returns:

  • (String)

    the title, in UTF-8 encoding

See Also:



762
763
764
765
# File 'video.c', line 762

static VALUE Window_title(VALUE self)
{
    return utf8str_new_cstr(SDL_GetWindowTitle(Get_SDL_Window(self)));
}

#title=(title) ⇒ void

This method returns an undefined value.

Set the title of the window.

Parameters:

  • title (String)

    the title

See Also:



805
806
807
808
809
810
# File 'video.c', line 805

static VALUE Window_set_title(VALUE self, VALUE title)
{
    title = rb_str_export_to_enc(title, rb_utf8_encoding());
    SDL_SetWindowTitle(Get_SDL_Window(self), StringValueCStr(title));
    return Qnil;
}

#window_idInteger

Get the numeric ID of the window.

Returns:

  • (Integer)


495
496
497
498
# File 'video.c', line 495

static VALUE Window_window_id(VALUE self)
{
    return UINT2NUM(SDL_GetWindowID(Get_SDL_Window(self)));
}