-
Notifications
You must be signed in to change notification settings - Fork 66
/
gfx.rb
233 lines (205 loc) · 7.62 KB
/
gfx.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
99
100
101
102
103
104
105
106
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#--
#
# Chingu -- OpenGL accelerated 2D game framework for Ruby
# Copyright (C) 2009 ippa / ippa@rubylicio.us
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#++
module Chingu
module Helpers
#
# 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
#
module GFX
#
# Fills whole window with specified 'color' and 'zorder'
#
#def fill(color, zorder = 0)
# $window.draw_quad(0, 0, color,
# $window.width, 0, color,
# $window.width, $window.height, color,
# 0, $window.height, color,
# zorder, :default)
#end
#
# 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)
#
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
#
# Draws an unfilled rect in given color
#
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
#
# Fills a given Rect 'rect' with Color 'color', drawing with zorder 'zorder'
#
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
#
# 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)
#
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
#
# Draws an unfilled circle, thanks shawn24!
#
def draw_circle(cx, cy, r, color, zorder = 0, mode = :default)
draw_arc(cx, cy, r, 0, 360, color, zorder, mode)
end
#
# Draws an unfilled arc from a1 to a2
#
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
#
# Draws a filled circle
#
def fill_circle(cx, cy, r, color, zorder = 0, mode = :default)
fill_arc(cx, cy, r, 0, 360, color, zorder, mode)
end
#
# Draws a filled arc from a1 to a2
#
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
private
def _fill_rect(rect, color_a, color_b, color_c, color_d, zorder, mode)
left, top = *rect.topleft
right, bottom = *rect.bottomright
$window.draw_quad(left, top, color_a,
left, bottom, color_b,
right, bottom, color_c,
right, top, color_d,
zorder, mode)
end
def _stroke_rect(rect, color_a, color_b, color_c, color_d, zorder, mode)
left, top = *rect.topleft
right, bottom = *rect.bottomright
$window.draw_line(left, top, color_a, left, bottom, color_b, zorder, mode)
$window.draw_line(left, bottom, color_b, right, bottom, color_c, zorder, mode)
$window.draw_line(right, bottom, color_c, right, top, color_d, zorder, mode)
$window.draw_line(right, top, color_d, left, top, color_a, zorder, mode)
end
#
# Calculates a reasonable number of segments for a circle of the given
# radius. Forgive the use of a magic number.
#
# Adapted from SiegeLord's "An Efficient Way to Draw Approximate Circles
# in OpenGL" <http://slabode.exofire.net/circle_draw.shtml>.
#
def _circle_segments(r)
10 * Math.sqrt(r)
end
#
# Appriximates an arc as a series of line segments and passes each segment
# to the block. Makes clever use of a transformation matrix to avoid
# repeated calls to sin and cos.
#
# Adapted from SiegeLord's "An Efficient Way to Draw Approximate Circles
# in OpenGL" <http://slabode.exofire.net/circle_draw.shtml>.
#
def _walk_arc(from, to, detail, &block)
walk_segments = ((to - from).to_f * (detail - 1) / 360).floor
theta = 2 * Math::PI / detail
c = Math.cos(theta)
s = Math.sin(theta)
x1 = Gosu.offset_x(from, 1)
y1 = Gosu.offset_y(from, 1)
0.upto(walk_segments) do
x2 = c * x1 - s * y1
y2 = s * x1 + c * y1
block[x1, y1, x2, y2]
x1, y1 = x2, y2
end
end
end
end
end