Class: Ray::Target

Inherits:
Object
  • Object
show all
Includes:
PP
Defined in:
lib/ray/target.rb,
lib/ray/turtle.rb,
ext/target.c

Direct Known Subclasses

ImageTarget, Window

Manipulating views collapse

Instance Method Summary collapse

Methods included from PP

#pretty_print_attributes

Instance Method Details

#[](x, y) ⇒ Ray::Color

Color of the pixel at a given position

This operation is slow. It is much faster to access a rect or the whole image than to read each pixel from that rect.

Parameters:

  • x (Integer)
  • y (Integer)

Returns:

  • (Ray::Color)

    Color of the pixel at that position


162
163
164
165
166
167
# File 'ext/target.c', line 162

static
VALUE ray_target_get(VALUE self, VALUE x, VALUE y) {
  return ray_col2rb(say_target_get(ray_rb2target(self),
                                   NUM2ULONG(x),
                                   NUM2ULONG(y)));
}

#clear(color) ⇒ Object

Clears the target in a given color

This method should usually be called before performing drawing.

Parameters:

  • color (Ray::Color)

    Color to clear the target with.


128
129
130
131
132
# File 'ext/target.c', line 128

static
VALUE ray_target_clear(VALUE self, VALUE color) {
  say_target_clear(ray_rb2target(self), ray_rb2col(color));
  return self;
}

#clipRay::Rect

Clipping rectangle when using the current view

Returns:

  • (Ray::Rect)

    Part of the target that's used by the view

See Also:


85
86
87
88
# File 'ext/target.c', line 85

static
VALUE ray_target_clip(VALUE self) {
  return ray_rect2rb(say_target_get_clip(ray_rb2target(self)));
}

#default_viewRay::View

Default view of the target

The default view is made so that no scaling will be performed. It has the following properties:

  1. size is the size of the target;

  2. center is the center of the target (half of the target);

  3. viewport is the whole target (that is, [0, 0, 1, 1]).

Returns:


63
64
65
66
67
68
# File 'ext/target.c', line 63

static
VALUE ray_target_default_view(VALUE self) {
  say_view *view = say_target_get_default_view(ray_rb2target(self));
  return Data_Wrap_Struct(rb_path2class("Ray::View"), NULL, say_view_free,
                          view);
}

#draw(obj) ⇒ Object

Draws an object on the target

Parameters:


139
140
141
142
143
144
145
146
147
148
# File 'ext/target.c', line 139

static
VALUE ray_target_draw(VALUE self, VALUE obj) {
  if (RAY_IS_A(obj, rb_path2class("Ray::BufferRenderer"))) {
    say_target_draw_buffer(ray_rb2target(self),
                           ray_rb2buf_renderer(obj));
  }
  else
    say_target_draw(ray_rb2target(self), ray_rb2drawable(obj));
  return self;
}

#make_currentObject

Makes a target become the current one

This method is only used when using low-level OpenGL access. Calling #draw will cause this method to be called anyway (and custom drawables also allow OpenGL calls)

Raises:

  • If a context could not be created or made current. This Would often mean the target was not initialized properly (e.g. window is not open)


113
114
115
116
117
118
# File 'ext/target.c', line 113

static
VALUE ray_target_make_current(VALUE self) {
  if (!say_target_make_current(ray_rb2target(self)))
    rb_raise(rb_eRuntimeError, "%s", say_error_get_last());
  return self;
}

#pretty_print(q, other_attr = []) ⇒ Object


39
40
41
# File 'lib/ray/target.rb', line 39

def pretty_print(q, other_attr = [])
  pretty_print_attributes q, ["view", "shader", "size"] + other_attr
end

#rect(rect) ⇒ Ray::Image

Colors of pixels present in a rect

Parameters:

  • rect (Ray::Rect)

    Rect to read pixels from

Returns:

  • (Ray::Image)

    An image containing the pixels of that rect.


175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'ext/target.c', line 175

static
VALUE ray_target_rect(VALUE self, VALUE rect) {
  say_rect c_rect  = ray_convert_to_rect(rect);
  say_image *image = say_target_get_rect(ray_rb2target(self),
                                         c_rect.x, c_rect.y,
                                         c_rect.w, c_rect.h);

  if (!image) {
    rb_raise(rb_eRuntimeError, "%s", say_error_get_last());
  }

  return Data_Wrap_Struct(rb_path2class("Ray::Image"), NULL, say_image_free,
                          image);
}

#shaderRay::Shader

Shader used when drawing on this target

Notice this method will always return the same object, and that modifying this object will modify the shader used internally be the target.

Returns:

  • (Ray::Shader)

    Shader used when drawing on this target


11
12
13
# File 'lib/ray/target.rb', line 11

def shader
  @shader ||= simple_shader # must always remain the same object
end

#sizeRay::Vector2

Returns Size of the target, in pixels.

Returns:


23
24
25
26
# File 'ext/target.c', line 23

static
VALUE ray_target_size(VALUE self) {
  return ray_vector2_to_rb(say_target_get_size(ray_rb2target(self)));
}

#to_imageRay::Image

Colors of all the pixels in the target

Returns:

  • (Ray::Image)

    An image containing all the pixels of the target


194
195
196
197
198
199
200
201
202
203
204
# File 'ext/target.c', line 194

static
VALUE ray_target_to_image(VALUE self) {
  say_image *image = say_target_to_image(ray_rb2target(self));

  if (!image) {
    rb_raise(rb_eRuntimeError, "%s", say_error_get_last());
  }

  return Data_Wrap_Struct(rb_path2class("Ray::Image"), NULL, say_image_free,
                          image);
}

#turtle { ... } ⇒ Ray::Turtle

Creates a turtle operating on the receiver

If a block is passed, it is instance-evaluated (self becoming the turtle) and the receiver gets updated automatically.

Yields:

  • Optionally perform drawing in the block

Returns:

See Also:


222
223
224
225
226
227
228
229
230
# File 'lib/ray/turtle.rb', line 222

def turtle(&block)
  turtle = Ray::Turtle.new(self)
  if block
    turtle.instance_eval(&block)
    update
  end

  turtle
end

#viewObject

See Also:


29
30
31
32
# File 'ext/target.c', line 29

static
VALUE ray_target_view(VALUE self) {
  return ray_view2rb(say_target_get_view(ray_rb2target(self)));
}

#view=(view) ⇒ Object

Sets the view used by the target

Notice the view returned by #view is a copy of the view used internally.

Parameters:

See Also:


43
44
45
46
47
48
# File 'ext/target.c', line 43

static
VALUE ray_target_set_view(VALUE self, VALUE val) {
  rb_check_frozen(self);
  say_target_set_view(ray_rb2target(self), ray_rb2view(val));
  return val;
}

#viewport_for(rect) ⇒ Ray::Rect

Viewport for a view to make it draw on a given rect

Parameters:

Returns:

  • (Ray::Rect)

    Viewport to set in the view


97
98
99
100
101
# File 'ext/target.c', line 97

static
VALUE ray_target_viewport_for(VALUE self, VALUE rect) {
  return ray_rect2rb(say_target_get_viewport_for(ray_rb2target(self),
                                                 ray_convert_to_rect(rect)));
}

#with_view(view) { ... } ⇒ Object

Changes the view temporarily

Examples:

old_view = target.view
target.with_view new_view do
  target.view == new_view # => true
end

target.view == old_view # => true

Parameters:

Yields:

  • a block where the view has been changed


29
30
31
32
33
34
35
# File 'lib/ray/target.rb', line 29

def with_view(view)
  old_view = self.view
  self.view = view
  yield self
ensure
  self.view = old_view
end