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



115
116
117
118
119
120
# File 'lib/chingu/helpers/gfx.rb', line 115

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



103
104
105
106
107
108
# File 'lib/chingu/helpers/gfx.rb', line 103

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 = 99) ⇒ 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/chingu/helpers/gfx.rb', line 51

def fill(options, zorder = 99)
  #
  # 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, 0, :default)
    return
  end
  
  default_options = { :colors => [Gosu::Color.new(0xFFFFFFFF), Gosu::Color.new(0xFF000000)],
                      :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])
  
  if options[:orientation] == :vertical
    x = rect.x
    y = rect.y
    right = rect.right
    bottom = rect.bottom
    
    step = (rect.x + rect.right) / options[:colors].size
    1.upto(options[:colors].size).each do |nr|
      from = options[:colors]
      to = 
    
      $window.draw_quad(  x, y, options[:from],
                        x + step, rect.y, options[:from],
                        x + step, rect.bottom, options[:to],
                        x, rect.bottom, options[:to],
                        options[:zorder], options[:mode]
                      )
    end
  else
    $window.draw_quad(  rect.x, rect.y, options[:from],
                        rect.x, rect.bottom, options[:from],
                        rect.right, rect.bottom, options[:to],
                        rect.right, rect.y, options[:to],
                        options[:zorder], options[:mode]
                      )        
  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)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/chingu/helpers/gfx.rb', line 142

def fill_gradient(options)
  default_options = { :from => Gosu::Color.new(255,0,0,0),
                      :to => Gosu::Color.new(255,255,255,255), 
                      :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])
  
  if options[:orientation] == :vertical
    $window.draw_quad(  rect.x, rect.y, options[:from],
                        rect.right, rect.y, options[:from],
                        rect.right, rect.bottom, options[:to],
                        rect.x, rect.bottom, options[:to],
                        options[:zorder], options[:mode]
                      )
  else
    $window.draw_quad(  rect.x, rect.y, options[:from],
                        rect.x, rect.bottom, options[:from],
                        rect.right, rect.bottom, options[:to],
                        rect.right, rect.y, options[:to],
                        options[:zorder], options[:mode]
                      )        
  end
end

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

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



125
126
127
128
129
130
131
132
# File 'lib/chingu/helpers/gfx.rb', line 125

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