sfml3.rb

Class: SFML::Window

Inherits:
Get_Klass_WindowBase()
  • Object
show all
Defined in:
ext/window/window.c,
ext/window/window.c

Overview

A renderable OS window with an OpenGL context attached. It derives from SFML::WindowBase; SFML::RenderWindow is the same object under the name that includes SFML::RenderTarget.

view

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(video_mode, title, style = :default, state = :windowed, settings = nil) ⇒ Window

Creates a window with an OpenGL context from video_mode and title.

Raises:

  • (RuntimeError) —

    if window creation fails



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'ext/window/window.c', line 43

static VALUE Window_initialize(int argc, VALUE* argv, VALUE self) {
    VALUE rb_video_mode, rb_title, rb_style, rb_state, rb_settings, title_buffer;
    sfWindowState state = sfWindowed;
    sfContextSettings settings;
    const sfContextSettings* settings_ptr = NULL;
    uint32_t style = sfDefaultStyle;
    sfRenderWindow* window;

    rb_scan_args(argc, argv, "23", &rb_video_mode, &rb_title, &rb_style, &rb_state, &rb_settings);

    if (!rb_obj_is_kind_of(rb_video_mode, Get_Klass_Mode())) {
        raise_invalid_argument_class(Get_Klass_Mode());
    }

    if (!NIL_P(rb_style)) {
        style = window_style_from_rb(rb_style);
    }

    if (!NIL_P(rb_state)) {
        state = window_state_from_rb(rb_state);
    }

    if (!NIL_P(rb_settings)) {
        settings = context_settings_from_rb(rb_settings);
        settings_ptr = &settings;
    }

    title_buffer = utf32_from_rb(rb_title);
    window = sfRenderWindow_createUnicode(*Get_Mode_Struct(rb_video_mode), UTF32_PTR(title_buffer),
                                          style, state, settings_ptr);
    RB_GC_GUARD(title_buffer);

    if (window == NULL) {
        rb_raise(rb_eRuntimeError, "failed to create window");
    }

    Get_Window_Data(self)->handle = window;
    Get_Window_Data(self)->kind = SFML_WINDOW_KIND_WINDOW;

    return self;
}

Class Method Details

.from_handle(handle, settings = nil) ⇒ Window

Returns:

Returns:

Raises:

  • (RuntimeError) —

    if window creation fails



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'ext/window/window.c', line 95

static VALUE Window_s_from_handle(int argc, VALUE* argv, VALUE klass) {
    VALUE rb_handle, rb_settings;
    sfContextSettings settings;
    const sfContextSettings* settings_ptr = NULL;
    sfRenderWindow* window;

    rb_scan_args(argc, argv, "11", &rb_handle, &rb_settings);

    if (!NIL_P(rb_settings)) {
        settings = context_settings_from_rb(rb_settings);
        settings_ptr = &settings;
    }

    window = sfRenderWindow_createFromHandle((sfWindowHandle)(uintptr_t)NUM2ULL(rb_handle),
                                             settings_ptr);

    if (window == NULL) {
        rb_raise(rb_eRuntimeError, "failed to create window from handle");
    }

    return Window_wrap_handle(klass, window, SFML_WINDOW_KIND_WINDOW);
}

Instance Method Details

#active=(value) ⇒ Boolean

Activates or deactivates this window's OpenGL context as the current one on the calling thread.

Returns:

  • (Boolean)

Returns:

  • (Boolean) —

    whether activation succeeded



174
175
176
# File 'ext/window/window.c', line 174

static VALUE Window_set_active(VALUE self, VALUE rb_active) {
    return BOOL2RB(sfRenderWindow_setActive(Get_Window_Struct(self), RTEST(rb_active)));
}

#clear(color = Color::BLACK) ⇒ self

Clears the window to color (black by default).

Returns:

  • (self)

Returns:

  • (self)

Raises:

  • (ArgumentError) —

    if given more than one argument



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'ext/window/window.c', line 126

static VALUE Window_clear(int argc, VALUE* argv, VALUE self) {
    sfColor color = sfBlack;

    if (argc > 1) {
        raise_invalid_arguments_excepted(1, argc);
    }

    if (argc == 1) {
        color_check(argv[0]);
        color = color_new_from_rb(argv[0]);
    }

    sfRenderWindow_clear(Get_Window_Struct(self), color);

    return self;
}

#clear_color_and_stencil(color, stencil) ⇒ self

Clears the color buffer to color and the stencil buffer to stencil.

Returns:

  • (self)

#clear_stencil(value) ⇒ self

Clears the stencil buffer to value.

Returns:

  • (self)

#close! ⇒ self

Closes the window.

Returns:

  • (self)

#create_vulkan_surface(instance, allocator = nil) ⇒ Integer?

Creates a Vulkan surface for this window.

Returns:

  • (Integer, nil) —

    the new VkSurfaceKHR, or nil if creation failed

#cursor=(value) ⇒ Cursor

Sets the window's mouse cursor.

Returns:

#cursor_grabbed=(value) ⇒ self

Confines or releases the mouse cursor to the window's client area.

Returns:

  • (self)

#cursor_visible=(value) ⇒ self

Shows or hides the mouse cursor over the window.

Returns:

  • (self)

#default_view ⇒ View

Returns a copy of the window's default view.

Returns:

Returns:

  • (View) —

    a copy of the window's default view



256
257
258
# File 'ext/window/window.c', line 256

static VALUE Window_get_default_view(VALUE self) {
    return Get_Casting_View(sfView_copy((sfRenderWindow_getDefaultView(Get_Window_Struct(self)))));
}

#display ⇒ self

Presents everything drawn since the last call to the screen.

Returns:

  • (self)

Returns:

  • (self)


149
150
151
152
# File 'ext/window/window.c', line 149

static VALUE Window_display(VALUE self) {
    sfRenderWindow_display(Get_Window_Struct(self));
    return self;
}

#draw(drawable, state = nil) ⇒ self

Draws drawable into the window.

Returns:

  • (self)

Returns:

  • (self)

Raises:

  • (ArgumentError) —

    if given no arguments or more than 2



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'ext/window/window.c', line 208

static VALUE Window_draw(int argc, VALUE* argv, VALUE self) {
    VALUE rb_drawable, rb_state;

    if (argc == 0 || argc > 2) {
        raise_invalid_arguments_excepted(-1, argc);
    }

    rb_drawable = argv[0];
    rb_state = (argc == 2) ? argv[1] : Get_New_RenderState();

    rb_funcall(Get_New_Target(self), rb_intern("draw"), 2, rb_drawable, rb_state);

    return self;
}

#draw_primitives(vertices, primitive, state = nil) ⇒ self

Draws raw vertex data as the given primitive type.

Returns:

  • (self)

#draw_vertex_buffer_range(buffer, first, count, state = nil) ⇒ self

Draws a range of vertices from a vertex buffer.

Returns:

  • (self)

#focus? ⇒ Boolean

Returns true if the window currently has focus.

Returns:

  • (Boolean)

#frame_rate=(value) ⇒ self

Limits the framerate to value frames per second; 0 disables the limit.

Returns:

  • (self)

Returns:

  • (self)


161
162
163
164
# File 'ext/window/window.c', line 161

static VALUE Window_set_frame_rate(VALUE self, VALUE rb_limit) {
    sfRenderWindow_setFramerateLimit(Get_Window_Struct(self), NUM2INT(rb_limit));
    return self;
}

#frame_rate=(value) ⇒ self

Limits the framerate to value frames per second; 0 disables the limit.

Returns:

  • (self)

Returns:

  • (self)


161
162
163
164
# File 'ext/window/window.c', line 161

static VALUE Window_set_frame_rate(VALUE self, VALUE rb_limit) {
    sfRenderWindow_setFramerateLimit(Get_Window_Struct(self), NUM2INT(rb_limit));
    return self;
}

#is_open? ⇒ Boolean

Returns true while the window is open.

Returns:

  • (Boolean)

#joystick_threshold=(value) ⇒ Float

Sets the minimum joystick axis change that generates a move event.

Returns:

  • (Float) —

    value

#key_repeat_enabled=(value) ⇒ self

Enables or disables key-repeat events.

Returns:

  • (self)

#map_coords_to_pixel(point, view = nil) ⇒ Vector2

Converts a world position to pixel coordinates.

Returns:

#map_pixel_to_coords(point, view = nil) ⇒ Vector2

Converts a pixel position to world coordinates, using the inverse of the view transform.

Returns:

#maximum_size=(value) ⇒ Vector2

Sets the window's maximum allowed client size.

Returns:

#minimum_size=(value) ⇒ Vector2

Sets the window's minimum allowed client size.

Returns:

#mouse_cursor=(value) ⇒ Cursor

Sets the window's mouse cursor.

Returns:

#native_handle ⇒ Integer

Returns the OS-specific window handle.

Returns:

  • (Integer)

#poll_event!(event) ⇒ Boolean

Pops the next pending event into event, if any, without blocking.

Returns:

  • (Boolean) —

    whether an event was popped

#pop_gl_states ⇒ self

Restores the OpenGL state saved by #push_gl_states.

Returns:

  • (self)

#position ⇒ Vector2

Returns the window's position in desktop coordinates.

Returns:

#position=(value) ⇒ Vector2

Moves the window to value.

Returns:

#push_gl_states ⇒ self

Saves the current OpenGL state.

Returns:

  • (self)

#request_focus ⇒ self

Requests focus for this window.

Returns:

  • (self)

#reset_gl_states ⇒ self

Resets the OpenGL state to SFML's defaults.

Returns:

  • (self)

#scissor(view = nil) ⇒ Rect

Returns the current scissor rectangle in pixels.

Returns:

  • (Rect) —

    the current scissor rectangle in pixels; view defaults to the target's current

#set_icon(size, pixels) ⇒ String

Sets the window's icon from RGBA32 pixel data.

Returns:

  • (String) —

    pixels

#settings ⇒ ContextSettings

Returns the context settings this window was created with.

Returns:

Returns:

  • (ContextSettings) —

    the settings this window's context was created with



196
197
198
# File 'ext/window/window.c', line 196

static VALUE Window_get_settings(VALUE self) {
    return context_settings_to_rb(sfRenderWindow_getSettings(Get_Window_Struct(self)));
}

#size ⇒ Vector2

Returns the client area size in pixels.

Returns:

#size=(value) ⇒ self

Resizes the window's client area.

Returns:

  • (self)

#srgb? ⇒ Boolean

Returns true if the target's framebuffer is sRGB-capable.

Returns:

  • (Boolean)

#title=(value) ⇒ self

Sets the window title.

Returns:

  • (self)

#vertical_sync_enabled=(value) ⇒ self

Enables or disables vertical synchronization.

Returns:

  • (self)

Returns:

  • (self)


185
186
187
188
# File 'ext/window/window.c', line 185

static VALUE Window_set_vertical_sync_enabled(VALUE self, VALUE rb_enable) {
    sfRenderWindow_setVerticalSyncEnabled(Get_Window_Struct(self), RTEST(rb_enable));
    return self;
}

#view ⇒ View

Returns a copy of the window's currently active view.

Returns:

Returns:

  • (View) —

    a copy of the window's currently active view



246
247
248
# File 'ext/window/window.c', line 246

static VALUE Window_get_view(VALUE self) {
    return Get_Casting_View(sfView_copy(sfRenderWindow_getView(Get_Window_Struct(self))));
}

#view=(value) ⇒ self

Sets the window's active view to value.

Returns:

  • (self)

Returns:

  • (self)

Raises:

  • (ArgumentError) —

    if value is not a View



230
231
232
233
234
235
236
237
238
# File 'ext/window/window.c', line 230

static VALUE Window_set_view(VALUE self, VALUE rb_view) {
    if (!rb_obj_is_kind_of(rb_view, Get_Klass_View())) {
        rb_raise(rb_eArgError, "invalid object, expected a View object");
    }

    sfRenderWindow_setView(Get_Window_Struct(self), Get_View_Struct(rb_view));

    return self;
}

#viewport(view = nil) ⇒ Rect

Returns the current viewport rectangle in pixels.

Returns:

  • (Rect) —

    the current viewport in pixels; view defaults to the target's current view

#visible=(value) ⇒ self

Shows or hides the window.

Returns:

  • (self)

#wait_event!(event) ⇒ Boolean

Blocks until an event is available and pops it into event.

Returns:

  • (Boolean) —

    whether an event was popped