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

Constant Summary collapse

CIRCLE_STEP =

Draws an unfilled circle, thanks shawn24!

10

Instance Method Summary collapse

Instance Method Details

#draw_circle(cx, cy, r, color) ⇒ Object



80
81
82
83
84
85
# File 'lib/chingu/helpers/gfx.rb', line 80

def draw_circle(cx,cy,r,color)      
  0.step(360, CIRCLE_STEP) do |a1|
    a2 = a1 + CIRCLE_STEP
    $window.draw_line cx + Gosu.offset_x(a1, r), cy + Gosu.offset_y(a1, r), color, cx + Gosu.offset_x(a2, r), cy + Gosu.offset_y(a2, r), color, 9999
  end
end

#draw_rect(rect, color, zorder) ⇒ Object

Draws an unfilled rect in given color



68
69
70
71
72
73
# File 'lib/chingu/helpers/gfx.rb', line 68

def draw_rect(rect, color, zorder)
  $window.draw_line(rect.x, rect.y, color, rect.right, rect.y, color, zorder)
  $window.draw_line(rect.right, rect.y, color, rect.right, rect.bottom, color, zorder)
  $window.draw_line(rect.right, rect.bottom, color, rect.x, rect.bottom, color, zorder)
  $window.draw_line(rect.x, rect.bottom, color, rect.x, rect.y, color, zorder)
end

#fill(options, zorder = 0) ⇒ Object

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
62
63
# File 'lib/chingu/helpers/gfx.rb', line 51

def fill(options, zorder = 0)
  #
  # if only 1 color-argument is given, assume fullscreen simple color fill.
  #
  if options.is_a?(Gosu::Color)
    $window.draw_quad(0, 0, options,
                    $window.width, 0, options,
                    $window.width, $window.height, options,
                    0, $window.height, options, zorder, :default)
  else
    fill_gradient(options)
  end
end

#fill_gradient(options) ⇒ Object

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)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/chingu/helpers/gfx.rb', line 107

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

#fill_rect(rect, color, zorder = 0) ⇒ Object

Fills a given Rect ‘rect’ with Color ‘color’, drawing with zorder ‘zorder’



90
91
92
93
94
95
96
97
# File 'lib/chingu/helpers/gfx.rb', line 90

def fill_rect(rect, color, zorder = 0)
  rect = Rect.new(rect)     # Make sure it's a rect
  $window.draw_quad(  rect.x, rect.y, color,
                      rect.right, rect.y, color,
                      rect.right, rect.bottom, color,
                      rect.x, rect.bottom, color,
                      zorder, :default)
end