Method: Snow::Mat4Array#store

Defined in:
ext/snow-math/snow-math.c

#store(sm_index, sm_value) ⇒ Object Also known as: []=

Stores a Mat4 at the given index. If the provided Mat4 is a member of the array and stored at the index, then no copy is done, otherwise the Mat4 is copied to the array.

If the value stored is a Mat3, it will be converted to a Mat4 for storage, though this will not modify the value directly.

call-seq: store(index, value) -> value



1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
# File 'ext/snow-math/snow-math.c', line 1481

static VALUE sm_mat4_array_store(VALUE sm_self, VALUE sm_index, VALUE sm_value)
{
  mat4_t *arr;
  size_t length = NUM2SIZET(sm_mathtype_array_length(sm_self));
  size_t index = NUM2SIZET(sm_index);
  int is_mat4 = 0;

  rb_check_frozen(sm_self);

  if (index >= length) {
    rb_raise(rb_eRangeError,
      "Index %zu out of bounds for array with length %zu",
      index, length);
  } else if (!(is_mat4 = SM_IS_A(sm_value, mat4)) && !SM_IS_A(sm_value, mat3)) {
    rb_raise(rb_eTypeError,
      "Invalid value to store: expected Mat3 or Mat4, got %s",
      rb_obj_classname(sm_value));
  }

  Data_Get_Struct(sm_self, mat4_t, arr);

  if (is_mat4) {
    mat4_t *value = sm_unwrap_mat4(sm_value, NULL);
    if (value == &arr[index]) {
      /* The object's part of the array, don't bother copying */
      return sm_value;
    }
    mat4_copy(*value, arr[index]);
  } else {
    mat3_to_mat4(*sm_unwrap_mat3(sm_value, NULL), arr[index]);
  }
  return sm_value;
}