Module: Chingu::Helpers::GFX

Included in:
GameState, Window
Defined in:
lib/chingu/helpers/gfx.rb

Overview

Various helper-methods to manipulate the screen. All drawings depend on the global variable $window which should be an instance of Gosu::Window or Chingu::Window

Instance Method Summary (collapse)

Instance Method Details

- (Object) draw_arc(cx, cy, r, from, to, color, zorder = 0, mode = :default)

Draws an unfilled arc from a1 to a2



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/chingu/helpers/gfx.rb', line 131

def draw_arc(cx, cy, r, from, to, color, zorder = 0, mode = :default)
  from, to = to, from if from > to
  $window.translate(cx,  cy) do
    $window.scale(r) do
      detail = _circle_segments(r)
      _walk_arc(from, to, detail) do |x1, y1, x2, y2|
        $window.draw_line(x1, y1, color,
                          x2, y2, color,
                          zorder, mode)
      end
    end
  end
end

- (Object) draw_circle(cx, cy, r, color, zorder = 0, mode = :default)

Draws an unfilled circle, thanks shawn24!



124
125
126
# File 'lib/chingu/helpers/gfx.rb', line 124

def draw_circle(cx, cy, r, color, zorder = 0, mode = :default)
  draw_arc(cx, cy, r, 0, 360, color, zorder, mode)
end

- (Object) draw_rect(rect, color, zorder = 0, mode = :default)

Draws an unfilled rect in given color



66
67
68
69
# File 'lib/chingu/helpers/gfx.rb', line 66

def draw_rect(rect, color, zorder = 0, mode = :default)
  rect = Rect.new(rect) unless rect.is_a? Rect
  _stroke_rect(rect, color, color, color, color, zorder, mode)
end

- (Object) fill(material, zorder = 0, mode = :default)

Fills window or a given rect with a gradient between two colors.

:from         - Start with this color
:to           - End with this color
:rect         - Only fill rectangle :rect with the gradient, either a Rect-instance or [x,y,width,height] Array.
:orientation  - Either :vertical (top to bottom) or :horizontal (left to right)


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chingu/helpers/gfx.rb', line 51

def fill(material, zorder = 0, mode = :default)
  #
  # if only 1 color-argument is given, assume fullscreen simple color fill.
  #
  if material.is_a?(Gosu::Color)
    rect = Rect.new([0, 0, $window.width, $window.height])
    _fill_rect(rect, material, material, material, material, zorder, mode)
  else
    fill_gradient(material)
  end
end

- (Object) fill_arc(cx, cy, r, from, to, color, zorder = 0, mode = :default)

Draws a filled arc from a1 to a2



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/chingu/helpers/gfx.rb', line 155

def fill_arc(cx, cy, r, from, to, color, zorder = 0, mode = :default)
  from, to = to, from if from > to
  $window.translate(cx,  cy) do
    $window.scale(r) do
      detail = _circle_segments(r)
      _walk_arc(from, to, detail) do |x1, y1, x2, y2|
        $window.draw_triangle(0,  0,  color,
                              x1, y1, color,
                              x2, y2, color,
                              zorder, mode)
      end
    end
  end
end

- (Object) fill_circle(cx, cy, r, color, zorder = 0, mode = :default)

Draws a filled circle



148
149
150
# File 'lib/chingu/helpers/gfx.rb', line 148

def fill_circle(cx, cy, r, color, zorder = 0, mode = :default)
  fill_arc(cx, cy, r, 0, 360, color, zorder, mode)
end

- (Object) fill_gradient(options)

Fills window or a given rect with a gradient between two colors.

:from         - Start with this color
:to           - End with this color
:rect         - Only fill rectangle :rect with the gradient, either a Rect-instance or [x,y,width,height] Array.
:orientation  - Either :vertical (top to bottom) or :horizontal (left to right)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/chingu/helpers/gfx.rb', line 88

def fill_gradient(options)
  options = { :from => Gosu::Color::BLACK,
              :to => Gosu::Color::WHITE,
              :orientation => :vertical,
              :rect => [0, 0, $window.width, $window.height],
              :zorder => 0,
              :mode => :default
            }.merge!(options)
  
  rect   = Rect.new(options[:rect])
  colors = options[:colors] || options.values_at(:from, :to)
  zorder = options[:zorder]
  mode   = options[:mode]
  
  case options[:orientation]
  when :vertical
    rect.height /= colors.count - 1
    colors.each_cons(2) do |from, to|
      _fill_rect(rect, from, to, to, from, zorder, mode)
      rect.top += rect.height
    end
  when :horizontal
    rect.width /= colors.count - 1
    colors.each_cons(2) do |from, to|
      _fill_rect(rect, from, from, to, to, zorder, mode)
      rect.left += rect.width
    end
  else
    raise ArgumentError, "bad gradient orientation: #{options[:orientation]}"
  end
  
end

- (Object) fill_rect(rect, color, zorder = 0, mode = :default)

Fills a given Rect 'rect' with Color 'color', drawing with zorder 'zorder'



75
76
77
78
# File 'lib/chingu/helpers/gfx.rb', line 75

def fill_rect(rect, color, zorder = 0, mode = :default)
  rect = Rect.new(rect) unless rect.is_a? Rect
  _fill_rect(rect, color, color, color, color, zorder, mode)
end