Class: Ray::Drawable

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

Overview

A drawable object represents anything that can be rendered on a target. This class allows to define transformations for any drawable and to set states to create your own drawable.

A subclass of Ray::Drawable must implement “render“ to draw the object, using OpenGL calls:

# vertex_id is the index of the first vertex of this object; index_id is
# the index of the first index of this object in the element buffer. One
# of those variables at least will be ignored in the implementation
def render(vertex_id, index_id)
  # do work
end

Also, it will often use Ray's system to store vertices and sometimes indices:

def fill_vertices
  # returns an array of vertices
end

Same with indices:

def fill_indices(vertex_id)
  # returns a Ray::GL::IntArray
end

Instance Attribute Summary collapse

Transformations collapse

Instance Method Summary collapse

Methods included from PP

#pretty_print_attributes

Constructor Details

#initialize(vertex_class = Ray::Vertex) ⇒ Object

Parameters:

  • vertex_class (Class) (defaults to: Ray::Vertex)

    Class of the vertices.


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'ext/drawable.c', line 138

static
VALUE ray_drawable_init(int argc, VALUE *argv, VALUE self) {
  if (rb_obj_is_kind_of(self, rb_path2class("Ray::Text"))   ||
      rb_obj_is_kind_of(self, rb_path2class("Ray::Sprite")) ||
      rb_obj_is_kind_of(self, rb_path2class("Ray::Polygon"))) {
    rb_raise(rb_eTypeError, "can't get drawable pointer from %s",
             RAY_OBJ_CLASSNAME(self));
  }

  ray_drawable *obj = NULL;
  Data_Get_Struct(self, ray_drawable, obj);

  VALUE arg = Qnil;
  rb_scan_args(argc, argv, "01", &arg);

  size_t id = NIL_P(arg) ? 0 : ray_get_vtype(arg);

  obj->drawable = say_drawable_create(id);
  say_drawable_set_custom_data(obj->drawable, obj);
  say_drawable_set_other_data(obj->drawable, (void*)self);
  say_drawable_set_fill_proc(obj->drawable, ray_drawable_fill_proc);
  say_drawable_set_shader_proc(obj->drawable, ray_drawable_shader_proc);
  say_drawable_set_render_proc(obj->drawable, ray_drawable_render_proc);
  say_drawable_set_index_fill_proc(obj->drawable,
                                   ray_drawable_indices_fill_proc);
  say_drawable_set_changed(obj->drawable);

  rb_iv_set(self, "@vertex_type_class", NIL_P(arg) ?
            rb_path2class("Ray::Vertex") : arg);

  obj->vsize = say_vertex_type_get_size(say_get_vertex_type(id));

  return self;
}

Instance Attribute Details

#shader_attributesHash?

Returns Attributes passed to the shader when the object is drawn.

Returns:

  • (Hash, nil)

    Attributes passed to the shader when the object is drawn.


56
57
58
# File 'lib/ray/drawable.rb', line 56

def shader_attributes
  @shader_attributes
end

Instance Method Details

#angleFloat

Angle is a rotation applied to a drawable. It is expressed in degrees, in the counter-clockwise direction.

Returns:

  • (Float)

    The rotation applied to the drawable


308
309
310
311
# File 'ext/drawable.c', line 308

static
VALUE ray_drawable_angle(VALUE self) {
  return rb_float_new(say_drawable_get_angle(ray_rb2drawable(self)));
}

#angle=(val) ⇒ Object

Sets the rotation applied to the drawable.

Parameters:

  • val (Float)

    The rotation applied to the drawable.

See Also:


319
320
321
322
323
# File 'ext/drawable.c', line 319

static
VALUE ray_drawable_set_angle(VALUE self, VALUE val) {
  say_drawable_set_angle(ray_rb2drawable(self), NUM2DBL(val));
  return val;
}

#blend_modeObject

See Also:


524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'ext/drawable.c', line 524

static
VALUE ray_drawable_blend_mode(VALUE self) {
  say_blend_mode mode = say_drawable_get_blend_mode(ray_rb2drawable(self));

  switch (mode) {
  case SAY_BLEND_NO: return RAY_SYM("none");
  case SAY_BLEND_ALPHA: return RAY_SYM("alpha");
  case SAY_BLEND_ADD: return RAY_SYM("add");
  case SAY_BLEND_MULTIPLY: return RAY_SYM("multiply");
  }

  return Qnil; /* should never happen */
}

#blend_mode=(mode) ⇒ Object

Sets blend mode

Ray supports 3 differents blend mode: :add, :multiply, and :alpha (also the default). Blending can also be disabled using :none.

Parameters:

  • mode (Symbol)

    New blend mode


509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'ext/drawable.c', line 509

static
VALUE ray_drawable_set_blend_mode(VALUE self, VALUE mode) {
  say_blend_mode c_mode = SAY_BLEND_NO;
  if (mode == RAY_SYM("alpha"))
    c_mode = SAY_BLEND_ALPHA;
  else if (mode == RAY_SYM("add"))
    c_mode = SAY_BLEND_ADD;
  else if (mode == RAY_SYM("multiply"))
    c_mode = SAY_BLEND_MULTIPLY;

  say_drawable_set_blend_mode(ray_rb2drawable(self), c_mode);
  return mode;
}

#changed!Object

Marks the object as changed, meaning vertices must be updated.


481
482
483
484
485
# File 'ext/drawable.c', line 481

static
VALUE ray_drawable_set_changed(VALUE self) {
  say_drawable_set_changed(ray_rb2drawable(self));
  return self;
}

#changed?true, flase

Returns true if the drawable has changed, and vertices must be updated.

Returns:

  • (true, flase)

    true if the drawable has changed, and vertices must be updated.


475
476
477
478
# File 'ext/drawable.c', line 475

static
VALUE ray_drawable_has_changed(VALUE self) {
  return say_drawable_has_changed(ray_rb2drawable(self)) ? Qtrue : Qfalse;
}

#default_matrixRay::Matrix

Default transformation matrix

This is useful when matrix is set to a custom matrix or matrix_proc is non-nil. It allows to retrieve the matrix that would be used otherwise, for instance to multiply it by another matrix to apply more transformations.

Returns:


404
405
406
407
408
409
# File 'ext/drawable.c', line 404

static
VALUE ray_drawable_default_matrix(VALUE self) {
  say_matrix *mat = say_drawable_get_default_matrix(ray_rb2drawable(self));
  return Data_Wrap_Struct(rb_path2class("Ray::Matrix"), NULL, say_matrix_free,
                          mat);
}

#index_countInteger

Returns Amount of indices used by this drawable.

Returns:

  • (Integer)

    Amount of indices used by this drawable


465
466
467
468
469
470
# File 'ext/drawable.c', line 465

static
VALUE ray_drawable_index_count(VALUE self) {
  say_drawable *drawable = ray_rb2drawable(self);
  size_t count = say_drawable_get_index_count(drawable);
  return ULONG2NUM(count);
}

#index_count=(val) ⇒ Object


457
458
459
460
461
462
# File 'ext/drawable.c', line 457

static
VALUE ray_drawable_set_index_count(VALUE self, VALUE val) {
  ray_drawable *drawable = ray_rb2full_drawable(self);
  say_drawable_set_index_count(drawable->drawable, NUM2ULONG(val));
  return val;
}

#initialize_copy(orig) ⇒ Object


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'ext/drawable.c', line 179

static
VALUE ray_drawable_init_copy(VALUE self, VALUE orig) {
  if (rb_obj_is_kind_of(self, rb_path2class("Ray::Text"))   ||
      rb_obj_is_kind_of(self, rb_path2class("Ray::Sprite")) ||
      rb_obj_is_kind_of(self, rb_path2class("Ray::Polygon"))) {
    rb_raise(rb_eTypeError, "can't get drawable pointer from %s",
             RAY_OBJ_CLASSNAME(self));
  }

  ray_drawable *obj = NULL;
  Data_Get_Struct(self, ray_drawable, obj);

  ray_drawable *other = ray_rb2full_drawable(orig);

  size_t vid = say_drawable_get_vertex_type(other->drawable);
  obj->drawable = say_drawable_create(vid);
  say_drawable_set_custom_data(obj->drawable, obj);
  say_drawable_set_other_data(obj->drawable, (void*)self);
  say_drawable_copy(obj->drawable, other->drawable);
  say_drawable_set_changed(obj->drawable);

  rb_iv_set(self, "@vertex_type_class", rb_iv_get(orig, "@vertex_type_class"));
  obj->vsize = other->vsize;

  ray_drawable_copy_attr(self, orig);

  return self;
}

#matrixRay::Matrix

Returns The transformation matrix used by this object.

Returns:

  • (Ray::Matrix)

    The transformation matrix used by this object.


367
368
369
370
# File 'ext/drawable.c', line 367

static
VALUE ray_drawable_matrix(VALUE self) {
  return ray_matrix2rb(say_drawable_get_matrix(ray_rb2drawable(self)));
}

#matrix=(val) ⇒ Object

Sets the current matrix to a custom one, making Ray ignore attributes setting that change the transformation matrix.

Setting this to nil will cause Ray to start using the actual transformation matrix.

Parameters:


382
383
384
385
386
387
388
389
390
391
392
# File 'ext/drawable.c', line 382

static
VALUE ray_drawable_set_matrix(VALUE self, VALUE val) {
  say_drawable *drawable = ray_rb2drawable(self);

  if (NIL_P(val))
    say_drawable_set_matrix(drawable, NULL);
  else
    say_drawable_set_matrix(drawable, ray_rb2matrix(val));

  return val;
}

#matrix_changed!Object

Marks the drawable's matrix as changed. This is meant to be called when you want your matrix proc to be called the next time the matrix is accessed.


358
359
360
361
362
# File 'ext/drawable.c', line 358

static
VALUE ray_drawable_matrix_changed(VALUE self) {
  say_drawable_set_matrix_changed(ray_rb2drawable(self));
  return self;
}

#matrix_procProc?

Returns Proc used to generate matrices, if any.

Returns:

  • (Proc, nil)

    Proc used to generate matrices, if any.


328
329
330
331
# File 'ext/drawable.c', line 328

static
VALUE ray_drawable_matrix_proc(VALUE self) {
  return rb_iv_get(self, "@matrix_proc");
}

#matrix_proc=(val) ⇒ Object

Sets the proc used to generate matrices. When set to nil, disables custom proc. The proc is called with the drawable to generate a matrix for, and should return that matrix.

Parameters:

  • val (Proc, nil)

341
342
343
344
345
346
347
348
349
350
351
352
# File 'ext/drawable.c', line 341

static
VALUE ray_drawable_set_matrix_proc(VALUE self, VALUE val) {
  say_drawable *drawable = ray_rb2drawable(self);

  if (NIL_P(val))
    say_drawable_set_matrix_proc(drawable, NULL);
  else
    say_drawable_set_matrix_proc(drawable, ray_drawable_actual_matrix_proc);

  rb_iv_set(self, "@matrix_proc", val);
  return val;
}

#originRay::Vector2

The origin is used as the origin for translations, rotations, and scalings.

Returns:


214
215
216
217
# File 'ext/drawable.c', line 214

static
VALUE ray_drawable_origin(VALUE self) {
  return ray_vector2_to_rb(say_drawable_get_origin(ray_rb2drawable(self)));
}

#origin=(val) ⇒ Object

Sets the origin of the drawable.

Parameters:

See Also:


225
226
227
228
229
# File 'ext/drawable.c', line 225

static
VALUE ray_drawable_set_origin(VALUE self, VALUE val) {
  say_drawable_set_origin(ray_rb2drawable(self), ray_convert_to_vector2(val));
  return val;
}

#posRay::Vector2 Also known as: position

Position of the drawable. This is thus the translation applied to it.

Returns:


258
259
260
261
# File 'ext/drawable.c', line 258

static
VALUE ray_drawable_pos(VALUE self) {
  return ray_vector2_to_rb(say_drawable_get_pos(ray_rb2drawable(self)));
}

#pos=(val) ⇒ Object Also known as: position=

Sets the position of the drawable.

Parameters:

See Also:


269
270
271
272
273
# File 'ext/drawable.c', line 269

static
VALUE ray_drawable_set_pos(VALUE self, VALUE val) {
  say_drawable_set_pos(ray_rb2drawable(self), ray_convert_to_vector2(val));
  return val;
}

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


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ray/drawable.rb', line 41

def pretty_print(q, other_attributes = [])
  # Note: This doesn't use %w[...] arrays because YARD can't parse them
  attributes = [
                "origin", "pos", "z", "scale", "angle",
                "matrix", "matrix_proc",
                "shader", "shader_attributes",
                "vertex_count", "index_count", "changed?", "textured?",
                "blend_mode"
               ]

  pretty_print_attributes q, attributes + other_attributes
end

#scaleRay::Vector2 Also known as: zoom

The scaling factor multiplies the size of the object. If it is set to (3,0.5), then te object is 3 times wider and 2 times shorter.

Returns:


237
238
239
240
# File 'ext/drawable.c', line 237

static
VALUE ray_drawable_scale(VALUE self) {
  return ray_vector2_to_rb(say_drawable_get_scale(ray_rb2drawable(self)));
}

#scale=(val) ⇒ Object Also known as: zoom=

Sets the scaling factor of the drawable.

Parameters:

See Also:


248
249
250
251
# File 'ext/drawable.c', line 248

VALUE ray_drawable_set_scale(VALUE self, VALUE val) {
  say_drawable_set_scale(ray_rb2drawable(self), ray_convert_to_vector2(val));
  return val;
}

#scale_xObject


23
24
25
# File 'lib/ray/drawable.rb', line 23

def scale_x
  scale.x
end

#scale_x=(val) ⇒ Object


31
32
33
# File 'lib/ray/drawable.rb', line 31

def scale_x=(val)
  self.scale = [val, scale_y]
end

#scale_yObject


27
28
29
# File 'lib/ray/drawable.rb', line 27

def scale_y
  scale.y
end

#scale_y=(val) ⇒ Object


35
36
37
# File 'lib/ray/drawable.rb', line 35

def scale_y=(val)
  self.scale = [scale_x, val]
end

#shaderRay::Shader

Returns:


426
427
428
429
# File 'ext/drawable.c', line 426

static
VALUE ray_drawable_shader(VALUE self) {
  return rb_iv_get(self, "@shader");
}

#shader=(val) ⇒ Object


431
432
433
434
435
436
437
438
439
440
# File 'ext/drawable.c', line 431

static
VALUE ray_drawable_set_shader(VALUE self, VALUE val) {
  if (NIL_P(val))
    say_drawable_set_shader(ray_rb2drawable(self), NULL);
  else
    say_drawable_set_shader(ray_rb2drawable(self), ray_rb2shader(val));

  rb_iv_set(self, "@shader", val);
  return val;
}

#textured=(val) ⇒ Object

Marks the object as changed, meaning vertices must be updated.


494
495
496
497
498
# File 'ext/drawable.c', line 494

static
VALUE ray_drawable_set_textured(VALUE self, VALUE val) {
  say_drawable_set_textured(ray_rb2drawable(self), RTEST(val));
  return self;
}

#textured?true, flase

Returns true if the drawable is textured.

Returns:

  • (true, flase)

    true if the drawable is textured


488
489
490
491
# File 'ext/drawable.c', line 488

static
VALUE ray_drawable_is_textured(VALUE self) {
  return say_drawable_is_textured(ray_rb2drawable(self)) ? Qtrue : Qfalse;
}

#transform(point) ⇒ Ray::Vector3

Applies the transformations to a point

Parameters:

Returns:


418
419
420
421
422
423
# File 'ext/drawable.c', line 418

static
VALUE ray_drawable_transform(VALUE self, VALUE point) {
  say_vector3 res = say_drawable_transform(ray_rb2drawable(self),
                                           ray_convert_to_vector3(point));
  return ray_vector3_to_rb(res);
}

#vertex_countInteger

Returns Amount of vertices used by this drawable.

Returns:

  • (Integer)

    Amount of vertices used by this drawable


450
451
452
453
454
455
# File 'ext/drawable.c', line 450

static
VALUE ray_drawable_vertex_count(VALUE self) {
  say_drawable *drawable = ray_rb2drawable(self);
  size_t count = say_drawable_get_vertex_count(drawable);
  return ULONG2NUM(count);
}

#vertex_count=(val) ⇒ Object


442
443
444
445
446
447
# File 'ext/drawable.c', line 442

static
VALUE ray_drawable_set_vertex_count(VALUE self, VALUE val) {
  ray_drawable *drawable = ray_rb2full_drawable(self);
  say_drawable_set_vertex_count(drawable->drawable, NUM2ULONG(val));
  return val;
}

#xObject


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

def x
  pos.x
end

#x=(val) ⇒ Object


19
20
21
# File 'lib/ray/drawable.rb', line 19

def x=(val)
  self.pos = [val, y]
end

#yObject


7
8
9
# File 'lib/ray/drawable.rb', line 7

def y
  pos.y
end

#y=(val) ⇒ Object


15
16
17
# File 'lib/ray/drawable.rb', line 15

def y=(val)
  self.pos = [x, val]
end

#zFloat

The Z order is a number between 1 and -1. Numbers with a smaller Z order are drawn behind those with a higher one.

Notice that this is only true without transparency. When transparency is enabled, transparent parts of a drawable would hide what's behind it if the scene isn't drawn in pixel order.

Returns:

  • (Float)

    The z order.


285
286
287
288
# File 'ext/drawable.c', line 285

static
VALUE ray_drawable_z(VALUE self) {
  return rb_float_new(say_drawable_get_z(ray_rb2drawable(self)));
}

#z=(val) ⇒ Object

Sets the z order.

Parameters:

  • val (Float)

    The z order.

See Also:


296
297
298
299
300
# File 'ext/drawable.c', line 296

static
VALUE ray_drawable_set_z(VALUE self, VALUE val) {
  say_drawable_set_z(ray_rb2drawable(self), NUM2DBL(val));
  return val;
}