Class: RedBird::Rect

Inherits:
Data
  • Object
show all
Defined in:
ext/red_bird/rect.c,
ext/red_bird/rect.c

Overview

An abstract rectangular object used mainly for collision detection.

Author:

  • Frederico Linhares

Instance Method Summary collapse

Constructor Details

#initialize(x, y, width, height) ⇒ Object

Parameters:

  • x (Integer)

    coordinate x for the rectangle position.

  • y (Integer)

    coordinate y for the rectangle position.

  • width (Integer)

    rectangle width.

  • height (Integer)

    rectangle height.

Author:

  • Frederico Linhares



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

VALUE
bird_cRect_initialize(VALUE self, VALUE x, VALUE y, VALUE width, VALUE height)
{
  struct bird_rect_data *ptr;

  RB_INTEGER_TYPE_P(x);
  RB_INTEGER_TYPE_P(y);
  RB_INTEGER_TYPE_P(width);
  RB_INTEGER_TYPE_P(height);

  TypedData_Get_Struct(self, struct bird_rect_data, &bird_rect_type, ptr);

  ptr->data.x = NUM2INT(x);
  ptr->data.y = NUM2INT(y);
  ptr->data.w = NUM2INT(width);
  ptr->data.h = NUM2INT(height);

  return self;
}

Instance Method Details

#collide?(rect) ⇒ Boolean

Test if this rectangle collided with another rectangle.

Parameters:

Returns:

  • (Boolean)

    true if there is a collision, false if there is no collision.

Author:

  • Frederico Linhares



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'ext/red_bird/rect.c', line 219

VALUE
bird_cRect_collide(VALUE self, VALUE rect)
{
  struct bird_rect_data *ptr1, *ptr2;

  if(!rb_obj_is_kind_of(rect, bird_cRect))
    rb_raise(rb_eTypeError, "%s",
             "rect must be an instance of RedBird::Rect");

  TypedData_Get_Struct(self, struct bird_rect_data, &bird_rect_type, ptr1);
  TypedData_Get_Struct(rect, struct bird_rect_data, &bird_rect_type, ptr2);

  if(ptr1->data.x <= ptr2->data.x + ptr2->data.w &&
     ptr1->data.x + ptr1->data.w >= ptr2->data.x &&
     ptr1->data.y <= ptr2->data.y + ptr2->data.h &&
     ptr1->data.y + ptr1->data.h >= ptr2->data.y)
    return Qtrue;
  else
    return Qfalse;
}

#heightInteger

Returns height of this rectangle.

Returns:

  • (Integer)

    height of this rectangle.

Author:

  • Frederico Linhares



163
164
165
166
167
168
169
170
171
# File 'ext/red_bird/rect.c', line 163

VALUE
bird_cRect_get_height(VALUE self)
{
  struct bird_rect_data *ptr;

  TypedData_Get_Struct(self, struct bird_rect_data, &bird_rect_type, ptr);

  return INT2FIX(ptr->data.h);
}

#height=(height) ⇒ Object

Set the height of this rectangle.

Parameters:

  • height (Integer)

Author:

  • Frederico Linhares



198
199
200
201
202
203
204
205
206
207
208
209
# File 'ext/red_bird/rect.c', line 198

VALUE
bird_cRect_set_height(VALUE self, VALUE height)
{
  struct bird_rect_data *ptr;

  RB_INTEGER_TYPE_P(height);
  TypedData_Get_Struct(self, struct bird_rect_data, &bird_rect_type, ptr);

  ptr->data.h = FIX2INT(height);

  return height;
}

#widthInteger

Returns width of this rectangle.

Returns:

  • (Integer)

    width of this rectangle.

Author:

  • Frederico Linhares



149
150
151
152
153
154
155
156
157
# File 'ext/red_bird/rect.c', line 149

VALUE
bird_cRect_get_width(VALUE self)
{
  struct bird_rect_data *ptr;

  TypedData_Get_Struct(self, struct bird_rect_data, &bird_rect_type, ptr);

  return INT2FIX(ptr->data.w);
}

#width=(width) ⇒ Object

Set the width of this rectangle.

Parameters:

  • width (Integer)

Author:

  • Frederico Linhares



179
180
181
182
183
184
185
186
187
188
189
190
# File 'ext/red_bird/rect.c', line 179

VALUE
bird_cRect_set_width(VALUE self, VALUE width)
{
  struct bird_rect_data *ptr;

  RB_INTEGER_TYPE_P(width);
  TypedData_Get_Struct(self, struct bird_rect_data, &bird_rect_type, ptr);

  ptr->data.w = FIX2INT(width);

  return width;
}

#xInteger

Returns coordinate x of this rectangle.

Returns:

  • (Integer)

    coordinate x of this rectangle.

Author:

  • Frederico Linhares



83
84
85
86
87
88
89
90
91
# File 'ext/red_bird/rect.c', line 83

VALUE
bird_cRect_get_x(VALUE self)
{
  struct bird_rect_data *ptr;

  TypedData_Get_Struct(self, struct bird_rect_data, &bird_rect_type, ptr);

  return INT2FIX(ptr->data.x);
}

#x=(x) ⇒ Object

Set the coordinate x of this rectangle.

Parameters:

  • x (Integer)

Author:

  • Frederico Linhares



113
114
115
116
117
118
119
120
121
122
123
124
# File 'ext/red_bird/rect.c', line 113

VALUE
bird_cRect_set_x(VALUE self, VALUE x)
{
  struct bird_rect_data *ptr;

  RB_INTEGER_TYPE_P(x);
  TypedData_Get_Struct(self, struct bird_rect_data, &bird_rect_type, ptr);

  ptr->data.x = FIX2INT(x);

  return x;
}

#yInteger

Returns coordinate y of this rectangle.

Returns:

  • (Integer)

    coordinate y of this rectangle.

Author:

  • Frederico Linhares



97
98
99
100
101
102
103
104
105
# File 'ext/red_bird/rect.c', line 97

VALUE
bird_cRect_get_y(VALUE self)
{
  struct bird_rect_data *ptr;

  TypedData_Get_Struct(self, struct bird_rect_data, &bird_rect_type, ptr);

  return INT2FIX(ptr->data.y);
}

#y=(y) ⇒ Object

Set the coordinate y of this rectangle.

Parameters:

  • y (Integer)

Author:

  • Frederico Linhares



132
133
134
135
136
137
138
139
140
141
142
143
# File 'ext/red_bird/rect.c', line 132

VALUE
bird_cRect_set_y(VALUE self, VALUE y)
{
  struct bird_rect_data *ptr;

  RB_INTEGER_TYPE_P(y);
  TypedData_Get_Struct(self, struct bird_rect_data, &bird_rect_type, ptr);

  ptr->data.y = FIX2INT(y);

  return y;
}