Class: SDL2::Rect

Inherits:
Struct
  • Object
show all
Defined in:
lib/sdl2/rect.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Struct

#==, create, #free, #initialize, release, #to_s, #update_members

Methods included from StructHelper

#member_readers, #member_writers

Constructor Details

This class inherits a constructor from SDL2::Struct

Class Method Details

.cast(something) ⇒ Object

Automatically construct a Rect out of something.

  • Accepts an array of &:to_i with length 2 or 4 as [x, y, [w, h]]



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sdl2/rect.rb', line 16

def self.cast(something)

  if something.kind_of?(Array)
    result = Rect.new
    case something.count
      when 4
        result.x, result.y, result.w, result.h = something.map(&:to_i)
      when 2
        result.x, result.y = something.map(&:to_i)
      else
        raise "#{self}#cast cannot convert array length #{something.count} of: #{something.inspect}"
    end
    return result
  else
    return super
  end
end

Instance Method Details

#emptyObject Also known as: empty?

Returns true if the rectangle has no area.



35
36
37
# File 'lib/sdl2/rect.rb', line 35

def empty
  return ((!self.null?) || (self[:w] <= 0) || (self[:h] <= 0))
end

#enclose_points(points, count = nil, clip = self) ⇒ Object

Calculate a minimal rectangle enclosing a set of points



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sdl2/rect.rb', line 94

def enclose_points(points, count = nil, clip = self)
  clip = Rect.cast(clip)
  points, count = Point.cast_array(points, count)
  result = Rect.new
  if SDL2.enclose_points?(points, count, clip, result)
    return result
  else
    result.free
    return nil
  end
end

#has_intersection?(rect) ⇒ Boolean

Determine whether two rectangles intersect.

*  Another rectangle to test against.

Returns:

  • (Boolean)

    True when they touch, false when they don’t



64
65
66
67
# File 'lib/sdl2/rect.rb', line 64

def has_intersection?(rect)
  rect = Rect.cast(rect)
  SDL2.has_intersection?(self, rect)
end

#intersect_line(x1, y1, x2, y2) ⇒ Object

Calculate the intersection of a rectangle and line segment.



107
108
109
110
111
112
113
114
115
# File 'lib/sdl2/rect.rb', line 107

def intersect_line(x1, y1, x2, y2)
  result = Rect.new
  if SDL2.intersect_rect_and_line(self, x1, y1, x2, y2)
    return result
  else
    result.free
    return nil
  end
end

#intersection(rect) ⇒ Object Also known as: intersect_rect

Calculate the intersection of two rectangles.

*  Another rectangle to find intersection with self.

Returns:

  • Rect or Nil if no intersection found.



72
73
74
75
76
77
78
79
80
81
# File 'lib/sdl2/rect.rb', line 72

def intersection(rect)
  rect = Rect.cast(rect)
  result = Rect.new
  if SDL2.intersect_rect?(self, rect, result)
    return result
  else
    result.free
    return nil
  end
end

#union(rect) ⇒ Object

Calculate the union of two rectangles.



86
87
88
89
90
91
# File 'lib/sdl2/rect.rb', line 86

def union(rect)
  rect = Rect.cast(rect)
  result = Rect.new
  SDL2.union_rect(self, rect, result)
  return result
end