sfml3.rb

Class: SFML::Rect

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
ext/graphics/rect.c,
ext/graphics/rect.c

Overview

An axis-aligned rectangle, stored as a position (left, top) and a size (width, height). Used both for float rects (bounds, viewports) and int rects (texture sub-regions) -- the underlying storage is always Float.

Includes Enumerable and behaves like a 4-element sequence.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#new ⇒ Rect(0, 0, 0, 0) #new(left, top) ⇒ Rect(left, 0, 0) #new(left, top, width, height) ⇒ Rect(left] #new([left, top, width, height]) ⇒ Rect(left] #new(other_rect) ⇒ Rect

Used for both float rects (positions/sizes) and int rects (pixel sub-regions of a texture); the underlying storage is always Float.

Overloads:

  • #new ⇒ Rect(0, 0, 0, 0)
  • #new(left, top) ⇒ Rect(left, 0, 0)
  • #new(left, top, width, height) ⇒ Rect(left]

    Returns Rect(left].

  • #new([left, top, width, height]) ⇒ Rect(left]

    Returns Rect(left].

Raises:

  • (ArgumentError) —

    if given an Array shorter than 4 elements, or an argument count other than 0, 1, 2 or 4



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'ext/graphics/rect.c', line 59

static VALUE Rect_initialize(int argc, VALUE* argv, VALUE self) {
    sfFloatRect rect = {{0, 0}, {0, 0}};

    if (argc == 1 && rb_obj_is_kind_of(argv[0], rb_cRect)) {
        rect = ((Rect*)Get_Rect_Struct(argv[0]))->rect;
    } else if (argc == 1 && RB_TYPE_P(argv[0], T_ARRAY)) {
        rect = rect_from_rb(argv[0]);
    } else if (argc == 2) {
        rect = (sfFloatRect){{(float)NUM2DBL(argv[0]), (float)NUM2DBL(argv[1])}, {0, 0}};
    } else if (argc == 4) {
        rect = (sfFloatRect){{(float)NUM2DBL(argv[0]), (float)NUM2DBL(argv[1])},
                             {(float)NUM2DBL(argv[2]), (float)NUM2DBL(argv[3])}};
    } else if (argc != 0) {
        raise_invalid_arguments_excepted(4, argc);
    }

    ((Rect*)Get_Rect_Struct(self))->rect = rect;

    return self;
}

Instance Attribute Details

#height ⇒ Float

Returns the height.

Returns:

  • (Float)

Returns:

  • (Float)


341
342
343
# File 'ext/graphics/rect.c', line 341

static VALUE Rect_get_height(VALUE self) {
    return DBL2NUM(((Rect*)Get_Rect_Struct(self))->rect.size.y);
}

#left ⇒ Float

Returns the X coordinate of the left edge.

Returns:

  • (Float)

Returns:

  • (Float)


341
342
343
# File 'ext/graphics/rect.c', line 341

static VALUE Rect_get_left(VALUE self) {
    return DBL2NUM(((Rect*)Get_Rect_Struct(self))->rect.position.x);
}

#position ⇒ Vector2

Returns the object's position.

Returns:

Returns:

  • (Vector2) —

    the (left, top) corner



341
342
343
344
345
# File 'ext/graphics/rect.c', line 341

static VALUE Rect_get_position(VALUE self) {
    sfFloatRect rect = ((Rect*)Get_Rect_Struct(self))->rect;

    return vec2f_to_rb(rect.position);
}

#size ⇒ Vector2

Returns the object's size.

Returns:

Returns:

  • (Vector2) —

    the (width, height) dimensions



341
342
343
344
345
# File 'ext/graphics/rect.c', line 341

static VALUE Rect_get_size(VALUE self) {
    sfFloatRect rect = ((Rect*)Get_Rect_Struct(self))->rect;

    return vec2f_to_rb(rect.size);
}

#top ⇒ Float

Returns the Y coordinate of the top edge.

Returns:

  • (Float)

Returns:

  • (Float)


341
342
343
# File 'ext/graphics/rect.c', line 341

static VALUE Rect_get_top(VALUE self) {
    return DBL2NUM(((Rect*)Get_Rect_Struct(self))->rect.position.y);
}

#width ⇒ Float

Returns the width.

Returns:

  • (Float)

Returns:

  • (Float)


341
342
343
# File 'ext/graphics/rect.c', line 341

static VALUE Rect_get_width(VALUE self) {
    return DBL2NUM(((Rect*)Get_Rect_Struct(self))->rect.size.x);
}

Instance Method Details

#==(other) ⇒ Boolean

other may be a Rect or a 4-element Array.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'ext/graphics/rect.c', line 309

static VALUE Rect_eql(VALUE self, VALUE rb_other) {
    sfFloatRect a = ((Rect*)Get_Rect_Struct(self))->rect;
    sfFloatRect b;

    if (rb_obj_is_kind_of(rb_other, rb_cRect)) {
        b = ((Rect*)Get_Rect_Struct(rb_other))->rect;
    } else if (RB_TYPE_P(rb_other, T_ARRAY) && RARRAY_LEN(rb_other) >= 4) {
        b = rect_from_rb(rb_other);
    } else {
        return Qfalse;
    }

    return BOOL2RB(a.position.x == b.position.x && a.position.y == b.position.y &&
                   a.size.x == b.size.x && a.size.y == b.size.y);
}

#contains?(point) ⇒ Boolean #contains?(x, y) ⇒ Boolean

Returns true if the rectangle contains the given point.

Overloads:

  • #contains?(point) ⇒ Boolean

    Returns:

    • (Boolean)
  • #contains?(x, y) ⇒ Boolean

    Returns:

    • (Boolean)

Returns:

  • (Boolean)

Raises:

  • (ArgumentError) —

    if given an argument count other than 1 or 2



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'ext/graphics/rect.c', line 225

static VALUE Rect_contains(int argc, VALUE* argv, VALUE self) {
    sfFloatRect rect = ((Rect*)Get_Rect_Struct(self))->rect;
    sfVector2f point;

    if (argc == 1) {
        point = vec2f_from_rb(argv[0]);
    } else if (argc == 2) {
        point = (sfVector2f){(float)NUM2DBL(argv[0]), (float)NUM2DBL(argv[1])};
    } else {
        raise_invalid_arguments_excepted(2, argc);
        return Qfalse;
    }

    return BOOL2RB(sfFloatRect_contains(&rect, point));
}

#each {|component| ... } ⇒ self

Yields left, top, width, height in that order.

Yields:

  • (component)

Returns:

  • (self)

Returns:

  • (self)


291
292
293
294
295
296
297
298
299
300
# File 'ext/graphics/rect.c', line 291

static VALUE Rect_each(VALUE self) {
    VALUE array = Rect_to_a(self);
    long i;

    for (i = 0; i < 4; i++) {
        rb_yield(rb_ary_entry(array, i));
    }

    return self;
}

#intersection(other) ⇒ Rect?

Returns the overlapping region of two rectangles.

Returns:

Returns:

  • (Rect, nil) —

    the overlapping region, or nil if the rects don't intersect



249
250
251
252
253
254
255
256
257
258
259
# File 'ext/graphics/rect.c', line 249

static VALUE Rect_intersection(VALUE self, VALUE rb_other) {
    sfFloatRect a = ((Rect*)Get_Rect_Struct(self))->rect;
    sfFloatRect b = rect_from_rb(rb_other);
    sfFloatRect intersection;

    if (sfFloatRect_intersects(&a, &b, &intersection)) {
        return Rect_wrap(intersection);
    }

    return Qnil;
}

#intersects?(other) ⇒ Boolean

Returns true if the two rectangles overlap.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


268
269
270
# File 'ext/graphics/rect.c', line 268

static VALUE Rect_intersects(VALUE self, VALUE rb_other) {
    return BOOL2RB(Rect_intersection(self, rb_other) != Qnil);
}

#to_a ⇒ Array, Float

Returns the object as an Array.

Returns ].

Returns:

  • (Array, Float, Float, Float) —

    ]

Returns:

  • (Array<Float>) —

    [left, top, width, height]



278
279
280
281
282
283
# File 'ext/graphics/rect.c', line 278

static VALUE Rect_to_a(VALUE self) {
    sfFloatRect rect = ((Rect*)Get_Rect_Struct(self))->rect;

    return rb_ary_new_from_args(4, DBL2NUM(rect.position.x), DBL2NUM(rect.position.y),
                                DBL2NUM(rect.size.x), DBL2NUM(rect.size.y));
}

#to_a ⇒ Array, Float

Returns the object as an Array.

Returns ].

Returns:

  • (Array, Float, Float, Float) —

    ]

Returns:

  • (Array<Float>) —

    [left, top, width, height]



278
279
280
281
282
283
# File 'ext/graphics/rect.c', line 278

static VALUE Rect_to_a(VALUE self) {
    sfFloatRect rect = ((Rect*)Get_Rect_Struct(self))->rect;

    return rb_ary_new_from_args(4, DBL2NUM(rect.position.x), DBL2NUM(rect.position.y),
                                DBL2NUM(rect.size.x), DBL2NUM(rect.size.y));
}

#to_s ⇒ String

Returns a human-readable representation of the object.

Returns:

  • (String)

Returns:

  • (String) —

    "(left, top, width, height)"



331
332
333
334
335
336
337
338
339
# File 'ext/graphics/rect.c', line 331

static VALUE Rect_to_s(VALUE self) {
    sfFloatRect rect = ((Rect*)Get_Rect_Struct(self))->rect;
    char buffer[96];

    snprintf(buffer, sizeof(buffer), "(%g, %g, %g, %g)", rect.position.x, rect.position.y,
             rect.size.x, rect.size.y);

    return rb_str_new2(buffer);
}