ray 0.0.1 → 0.1.0.pre1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. data/.gemtest +0 -0
  2. data/.yardopts +4 -0
  3. data/README.md +17 -21
  4. data/Rakefile +18 -139
  5. data/VERSION +1 -1
  6. data/ext/audio.cpp +723 -0
  7. data/ext/{color.c → color.cpp} +25 -13
  8. data/ext/drawable.cpp +91 -0
  9. data/ext/event.cpp +460 -0
  10. data/ext/extconf.rb +5 -104
  11. data/ext/font.cpp +190 -0
  12. data/ext/image.cpp +733 -0
  13. data/ext/input.cpp +74 -0
  14. data/ext/ray.cpp +168 -0
  15. data/ext/ray.hpp +356 -0
  16. data/ext/{rect.c → rect.cpp} +51 -37
  17. data/ext/shader.cpp +169 -0
  18. data/ext/shape.cpp +409 -0
  19. data/ext/sprite.cpp +306 -0
  20. data/ext/text.cpp +181 -0
  21. data/ext/vector.cpp +215 -0
  22. data/guide.md +619 -0
  23. data/lib/ray/audio.rb +0 -41
  24. data/lib/ray/color.rb +32 -10
  25. data/lib/ray/drawable.rb +16 -0
  26. data/lib/ray/dsl/event_listener.rb +25 -2
  27. data/lib/ray/dsl/event_runner.rb +33 -5
  28. data/lib/ray/dsl/event_translator.rb +66 -30
  29. data/lib/ray/dsl/handler.rb +3 -2
  30. data/lib/ray/dsl/matcher.rb +58 -14
  31. data/lib/ray/font.rb +38 -96
  32. data/lib/ray/font_set.rb +8 -8
  33. data/lib/ray/game.rb +87 -66
  34. data/lib/ray/helper.rb +105 -10
  35. data/lib/ray/image.rb +150 -24
  36. data/lib/ray/image_set.rb +3 -1
  37. data/lib/ray/input.rb +10 -0
  38. data/lib/ray/music_set.rb +5 -3
  39. data/lib/ray/ray.rb +21 -9
  40. data/lib/ray/rect.rb +48 -7
  41. data/lib/ray/rmagick.rb +41 -0
  42. data/lib/ray/scene.rb +99 -43
  43. data/lib/ray/scene_list.rb +67 -0
  44. data/lib/ray/shape.rb +132 -0
  45. data/lib/ray/sound_set.rb +4 -2
  46. data/lib/ray/sprite.rb +49 -111
  47. data/lib/ray/text.rb +101 -0
  48. data/lib/ray/text_helper.rb +37 -0
  49. data/lib/ray/turtle.rb +215 -0
  50. data/lib/ray/vector.rb +226 -0
  51. data/samples/audio/spacial.rb +44 -0
  52. data/samples/hello_world/hello.rb +9 -13
  53. data/samples/hello_world/hello_dsl.rb +8 -12
  54. data/samples/hello_world/text.rb +15 -0
  55. data/samples/opengl/binding.rb +38 -0
  56. data/samples/opengl/image.rb +32 -0
  57. data/samples/opengl/opengl.rb +34 -0
  58. data/samples/opengl/shader.rb +42 -0
  59. data/samples/pong/pong.rb +14 -10
  60. data/samples/run_scene.rb +53 -0
  61. data/samples/shaders/scene.rb +40 -0
  62. data/samples/shaders/shaders.rb +42 -0
  63. data/samples/shaders/shape.rb +34 -0
  64. data/samples/sokoban/sokoban.rb +18 -18
  65. data/samples/test/actual_scene.rb +41 -0
  66. data/samples/test/scene_riot.rb +39 -0
  67. data/samples/test/scene_spec.rb +32 -0
  68. data/samples/test/scene_test_unit.rb +25 -0
  69. data/samples/turtle/byzantium.rb +45 -0
  70. data/samples/turtle/hilbert.rb +48 -0
  71. data/samples/turtle/koch.rb +55 -0
  72. data/samples/turtle/mandala.rb +61 -0
  73. data/samples/turtle/tree.rb +57 -0
  74. data/test/audio_test.rb +69 -0
  75. data/test/color_test.rb +77 -0
  76. data/test/drawable_test.rb +19 -0
  77. data/test/dsl_test.rb +93 -0
  78. data/test/font_test.rb +57 -0
  79. data/test/helpers.rb +94 -0
  80. data/test/image_test.rb +82 -0
  81. data/test/ray_test.rb +25 -0
  82. data/test/rect_test.rb +121 -0
  83. data/{spec → test}/res/VeraMono.ttf +0 -0
  84. data/{spec → test}/res/aqua.bmp +0 -0
  85. data/{spec → test}/res/aqua.png +0 -0
  86. data/{spec → test}/res/aqua2.bmp +0 -0
  87. data/{spec → test}/res/not_a_jpeg.jpeg +0 -0
  88. data/{spec → test}/res/pop.wav +0 -0
  89. data/test/resource_set_test.rb +99 -0
  90. data/test/run_all.rb +7 -0
  91. data/test/shape_test.rb +101 -0
  92. data/test/sprite_test.rb +89 -0
  93. data/test/text_test.rb +78 -0
  94. data/test/turtle_test.rb +176 -0
  95. data/test/vector_test.rb +111 -0
  96. data/yard_ext.rb +0 -28
  97. metadata +95 -139
  98. data/.gitignore +0 -23
  99. data/.gitmodules +0 -3
  100. data/.rspec +0 -3
  101. data/ext/audio.c +0 -473
  102. data/ext/event.c +0 -557
  103. data/ext/font.c +0 -287
  104. data/ext/image.c +0 -933
  105. data/ext/joystick.c +0 -145
  106. data/ext/ray.c +0 -489
  107. data/ext/ray.h +0 -245
  108. data/ext/ray_osx.m +0 -161
  109. data/lib/ray/joystick.rb +0 -30
  110. data/psp/SDL_psp_main.c +0 -84
  111. data/psp/bigdecimal/README +0 -60
  112. data/psp/bigdecimal/bigdecimal.c +0 -4697
  113. data/psp/bigdecimal/bigdecimal.h +0 -216
  114. data/psp/bigdecimal/lib/bigdecimal/jacobian.rb +0 -85
  115. data/psp/bigdecimal/lib/bigdecimal/ludcmp.rb +0 -84
  116. data/psp/bigdecimal/lib/bigdecimal/math.rb +0 -235
  117. data/psp/bigdecimal/lib/bigdecimal/newton.rb +0 -77
  118. data/psp/bigdecimal/lib/bigdecimal/util.rb +0 -65
  119. data/psp/digest/bubblebabble/bubblebabble.c +0 -142
  120. data/psp/digest/defs.h +0 -20
  121. data/psp/digest/digest.c +0 -643
  122. data/psp/digest/digest.h +0 -32
  123. data/psp/digest/lib/digest.rb +0 -50
  124. data/psp/digest/lib/md5.rb +0 -27
  125. data/psp/digest/lib/sha1.rb +0 -27
  126. data/psp/digest/md5/md5.c +0 -420
  127. data/psp/digest/md5/md5.h +0 -80
  128. data/psp/digest/md5/md5init.c +0 -40
  129. data/psp/digest/rmd160/rmd160.c +0 -457
  130. data/psp/digest/rmd160/rmd160.h +0 -56
  131. data/psp/digest/rmd160/rmd160init.c +0 -40
  132. data/psp/digest/sha1/sha1.c +0 -269
  133. data/psp/digest/sha1/sha1.h +0 -39
  134. data/psp/digest/sha1/sha1init.c +0 -40
  135. data/psp/digest/sha2/lib/sha2.rb +0 -73
  136. data/psp/digest/sha2/sha2.c +0 -919
  137. data/psp/digest/sha2/sha2.h +0 -109
  138. data/psp/digest/sha2/sha2init.c +0 -52
  139. data/psp/enumerator/enumerator.c +0 -298
  140. data/psp/etc/etc.c +0 -559
  141. data/psp/ext.c +0 -289
  142. data/psp/fcntl/fcntl.c +0 -187
  143. data/psp/lib/rbconfig.rb +0 -178
  144. data/psp/nkf/lib/kconv.rb +0 -367
  145. data/psp/nkf/nkf-utf8/config.h +0 -88
  146. data/psp/nkf/nkf-utf8/nkf.c +0 -6040
  147. data/psp/nkf/nkf-utf8/utf8tbl.c +0 -8500
  148. data/psp/nkf/nkf-utf8/utf8tbl.h +0 -34
  149. data/psp/nkf/nkf.c +0 -654
  150. data/psp/socket/addrinfo.h +0 -173
  151. data/psp/socket/getaddrinfo.c +0 -676
  152. data/psp/socket/getnameinfo.c +0 -270
  153. data/psp/socket/pspsocket.c +0 -71
  154. data/psp/socket/pspsocket.h +0 -28
  155. data/psp/socket/socket.c +0 -4662
  156. data/psp/socket/sockport.h +0 -76
  157. data/psp/stringio/stringio.c +0 -1306
  158. data/psp/strscan/strscan.c +0 -1320
  159. data/psp/syck/bytecode.c +0 -1166
  160. data/psp/syck/emitter.c +0 -1242
  161. data/psp/syck/gram.c +0 -1894
  162. data/psp/syck/gram.h +0 -79
  163. data/psp/syck/handler.c +0 -174
  164. data/psp/syck/implicit.c +0 -2990
  165. data/psp/syck/node.c +0 -408
  166. data/psp/syck/rubyext.c +0 -2367
  167. data/psp/syck/syck.c +0 -504
  168. data/psp/syck/syck.h +0 -456
  169. data/psp/syck/token.c +0 -2725
  170. data/psp/syck/yaml2byte.c +0 -257
  171. data/psp/syck/yamlbyte.h +0 -170
  172. data/psp/thread/thread.c +0 -1175
  173. data/psp/zlib/zlib.c +0 -3547
  174. data/script.rb +0 -10
  175. data/spec/ray/audio_spec.rb +0 -146
  176. data/spec/ray/color_spec.rb +0 -57
  177. data/spec/ray/event_spec.rb +0 -80
  178. data/spec/ray/font_spec.rb +0 -93
  179. data/spec/ray/image_set_spec.rb +0 -48
  180. data/spec/ray/image_spec.rb +0 -162
  181. data/spec/ray/joystick_spec.rb +0 -21
  182. data/spec/ray/matcher_spec.rb +0 -50
  183. data/spec/ray/ray_spec.rb +0 -88
  184. data/spec/ray/rect_spec.rb +0 -154
  185. data/spec/ray/resource_set_spec.rb +0 -105
  186. data/spec/ray/sprite_spec.rb +0 -163
  187. data/spec/spec.opts +0 -4
  188. data/spec/spec_helper.rb +0 -8
@@ -4,7 +4,6 @@ module Ray
4
4
  "#<#{self.class} w=#{w} h=#{h}>"
5
5
  end
6
6
 
7
- alias :bits_per_pixel :bpp
8
7
  alias :w :width
9
8
  alias :h :height
10
9
 
@@ -14,13 +13,151 @@ module Ray
14
13
  extend Ray::ResourceSet
15
14
  add_set(/^(.*)$/) { |filename| new(filename) }
16
15
 
16
+ # @return [Ray::Vector2] Size of the image
17
+ def size
18
+ Ray::Vector2[w, h]
19
+ end
20
+
21
+ # Draws a point.
22
+ #
23
+ # @param [Ray::Vector2, #to_vector2] point The point which should be drawn.
24
+ # @param [Ray::Color] color Its new color.
25
+ def draw_pixel(point, color)
26
+ point = point.to_vector2
27
+ draw_filled_rect(Ray::Rect.new(point.x, point.y, 1, 1), color)
28
+ end
29
+
30
+ # Draws a line.
31
+ #
32
+ # @param [Ray::Vector2, #to_vector2] p1 First point of the line.
33
+ # @param [Ray::Vector2, #to_vector2] p2 Second point of the line.
34
+ # @param [Ray::Color] color Color of the line.
35
+ # @param [Integer] size Thickness of the line.
36
+ def draw_line(p1, p2, color, size = 1)
37
+ draw_shape Ray::Shape.line(p1, p2, size, color)
38
+ end
39
+
40
+ # Draws a rect.
41
+ #
42
+ # @param [Ray::Rect, Array<Integer>] rect Rect to draw.
43
+ # @param [Ray::Color] color Color of the rect.
44
+ def draw_rect(rect, color)
45
+ shape = Ray::Shape.rectangle(rect, Ray::Color.none, 1, color)
46
+ shape.filled = false
47
+
48
+ draw_shape shape
49
+ end
50
+
51
+ # Draws a filled rect.
52
+ #
53
+ # @param [Ray::Rect, Array<Integer>] rect Rect to draw.
54
+ # @param [Ray::Color] color color of the rect.
55
+ def draw_filled_rect(rect, color)
56
+ draw_shape Ray::Shape.rectangle(rect, color)
57
+ end
58
+
59
+ # Draws a circle.
60
+ #
61
+ # @param [Ray::Vector2, #to_vector2] center Center of the circle.
62
+ # @param [Integer] radius Radius of the circle.
63
+ # @param [Ray::Color] color Color of the circle.
64
+ def draw_circle(center, radius, color)
65
+ shape = Ray::Shape.circle(center, radius, Ray::Color.none, 1, color)
66
+ shape.filled = false
67
+
68
+ draw_shape shape
69
+ end
70
+
71
+ # Draws a filled circle.
72
+ #
73
+ # @param [Ray::Vector2, #to_vector2] center Center of the circle.
74
+ # @param [Integer] radius Radius of the circle.
75
+ # @param [Ray::Color] color Color of the circle.
76
+ def draw_filled_circle(center, radius, color)
77
+ draw_shape Ray::Shape.circle(center, radius, color)
78
+ end
79
+
80
+ # Draws an ellipse.
81
+ #
82
+ # @param [Ray::Vector2, #to_vector2] center Center of the ellipse
83
+ # @param [Float] rx x radius (half of the width)
84
+ # @param [Float] ry y radius (half of the height)
85
+ # @param [Ray::Color] color Color in which the ellipse should be drawn.
86
+ def draw_ellipse(center, rx, ry, color)
87
+ ellipse = Ray::Shape.ellipse(center, rx, ry, Ray::Color.none, 1, color)
88
+ ellipse.filled = false
89
+
90
+ draw_shape ellipse
91
+ end
92
+
93
+ # Draws a filled ellipse.
94
+ #
95
+ # @param [Ray::Vector2, #to_vector2] center Center of the ellipse
96
+ # @param [Float] rx x radius (half of the width)
97
+ # @param [Float] ry y radius (half of the height)
98
+ # @param [Ray::Color] color Color in which the ellipse should be drawn.
99
+ def draw_filled_ellipse(center, rx, ry, color)
100
+ draw_shape Ray::Shape.ellipse(center, rx, ry, color)
101
+ end
102
+
103
+ # Draws a triangle.
104
+ def draw_triangle(p1, p2, p3, color)
105
+ shape = Ray::Shape.new
106
+
107
+ shape.add_point(p1, Ray::Color.none, color)
108
+ shape.add_point(p2, Ray::Color.none, color)
109
+ shape.add_point(p3, Ray::Color.none, color)
110
+
111
+ shape.filled = false
112
+ shape.outline_width = 1.0
113
+
114
+ draw_shape shape
115
+ end
116
+
117
+ # Draws a filled triangle.
118
+ def draw_filled_triangle(p1, p2, p3, color)
119
+ shape = Ray::Shape.new
120
+
121
+ shape.add_point(p1, color)
122
+ shape.add_point(p2, color)
123
+ shape.add_point(p3, color)
124
+
125
+ draw_shape shape
126
+ end
127
+
128
+ # Draws a polygon.
129
+ #
130
+ # @param [Array<Ray::Vector2, #to_vector2>] points Points which should be joined
131
+ # to draw the polygon.
132
+ # @param [Ray::Color] color The color in which the polygon should be drawn.
133
+ def draw_polygon(points, color)
134
+ shape = Ray::Shape.new
135
+ points.each { |p| shape.add_point(p, Ray::Color.none, color) }
136
+
137
+ shape.filled = false
138
+ shape.outline_width = 1.0
139
+
140
+ draw_shape shape
141
+ end
142
+
143
+ # Draws a filled polygon.
144
+ #
145
+ # @param [Array<Ray::Vector2, #to_vector2>] points Points which should be joined
146
+ # to draw the polygon.
147
+ # @param [Ray::Color] color The color in which the polygon should be drawn.
148
+ def draw_filled_polygon(points, color)
149
+ shape = Ray::Shape.new
150
+ points.each { |p| shape.add_point(p, color) }
151
+ draw_shape shape
152
+ end
153
+
17
154
  # @yield [pixel]
18
155
  # @yieldparam [Ray::Color] pixel Color of a point
19
156
  def each
20
157
  return Enumerator.new(self, :each) unless block_given?
21
158
 
22
- (0...w).each do |x|
23
- (0...h).each do |y|
159
+ (0...h).each do |y|
160
+ (0...w).each do |x|
24
161
  yield self[x, y]
25
162
  end
26
163
  end
@@ -29,13 +166,13 @@ module Ray
29
166
  end
30
167
 
31
168
  # Same as each, but also yields the position of each point.
32
- # @yield [x, y, pixel]
169
+ # @yield [pixel, x, y]
33
170
  def each_with_pos
34
171
  return Enumerator.new(self, :each_with_pos) unless block_given?
35
172
 
36
- (0...w).each do |x|
37
- (0...h).each do |y|
38
- yield x, y, self[x, y]
173
+ (0...h).each do |y|
174
+ (0...w).each do |x|
175
+ yield self[x, y], x, y
39
176
  end
40
177
  end
41
178
 
@@ -47,8 +184,8 @@ module Ray
47
184
  return Enumerator.new(self, :map!) unless block_given?
48
185
 
49
186
  lock do
50
- (0...w).each do |x|
51
- (0...h).each do |y|
187
+ (0...h).each do |y|
188
+ (0...w).each do |x|
52
189
  self[x, y] = yield self[x, y]
53
190
  end
54
191
  end
@@ -57,14 +194,14 @@ module Ray
57
194
  self
58
195
  end
59
196
 
60
- # @yield [x, y, pixel] Block returning the new color of this pixel
197
+ # @yield [pixel, x, y] Block returning the new color of this pixel
61
198
  def map_with_pos!
62
199
  return Enumerator.new(self, :map_with_pos!) unless block_given?
63
200
 
64
201
  lock do
65
- (0...w).each do |x|
66
- (0...h).each do |y|
67
- self[x, y] = yield x, y, self[x, y]
202
+ (0...h).each do |y|
203
+ (0...w).each do |x|
204
+ self[x, y] = yield self[x, y], x, y
68
205
  end
69
206
  end
70
207
  end
@@ -82,17 +219,6 @@ module Ray
82
219
  dup.map_with_pos!(&block)
83
220
  end
84
221
 
85
- def to_image
86
- self
87
- end
88
-
89
222
  include Enumerable
90
223
  end
91
224
  end
92
-
93
- class String
94
- # Converts the string to an image using Ray::ImageSet.[]
95
- def to_image
96
- Ray::ImageSet[self]
97
- end
98
- end
@@ -19,9 +19,11 @@ module Ray
19
19
  # @param [Regexp] regex Regular expression used to match file
20
20
  # @yield [*args] Block returning the image
21
21
  # @yieldparam args Regex captures
22
- def self.image_set(regex, &block)
22
+ def image_set(regex, &block)
23
23
  Ray::ImageSet.add_set(regex, &block)
24
24
  end
25
+
26
+ module_function :image_set
25
27
  end
26
28
 
27
29
  begin
@@ -0,0 +1,10 @@
1
+ module Ray
2
+ module Input
3
+ module_function
4
+
5
+ # @return [Ray::Vector2] Position of the mouse
6
+ def mouse_pos
7
+ Ray::Vector2[mouse_x, mouse_y]
8
+ end
9
+ end
10
+ end
@@ -1,10 +1,10 @@
1
1
  module Ray
2
2
  module MusicSet
3
3
  extend Ray::ResourceSet
4
-
4
+
5
5
  class << self
6
6
  def missing_pattern(string)
7
- Ray::Music[string, size]
7
+ Ray::Music[string]
8
8
  end
9
9
 
10
10
  def select!(&block)
@@ -20,9 +20,11 @@ module Ray
20
20
  # @yield [*args] Block returning the music
21
21
  #
22
22
  # @yieldparam args Regex captures
23
- def self.music_set(regex, &block)
23
+ def music_set(regex, &block)
24
24
  Ray::MusicSet.add_set(regex, &block)
25
25
  end
26
+
27
+ module_function :music_set
26
28
  end
27
29
 
28
30
  begin
@@ -1,22 +1,33 @@
1
1
  require 'enumerator'
2
+ require 'iconv'
2
3
 
3
- # 'ray_ext' doesn't exist on the PSP.
4
- # We assume that Ray isn't defined in other cases.
5
- require 'ray_ext' unless defined? Ray
4
+ require 'ray_ext'
5
+
6
+ module Ray
7
+ BIG_ENDIAN = ([1].pack("I") == [1].pack("N"))
8
+ INTERNAL_ENCODING = BIG_ENDIAN ? "UTF-32BE" : "UTF-32LE"
9
+ end
6
10
 
7
11
  require 'ray/resource_set'
8
12
 
9
13
  require 'ray/color'
10
14
  require 'ray/rect'
15
+ require 'ray/vector'
11
16
  require 'ray/image'
12
- require 'ray/joystick'
13
- require 'ray/font' if Ray.has_font_support?
14
- require 'ray/audio' if Ray.has_audio_support?
17
+ require 'ray/shape'
18
+ require 'ray/drawable'
19
+ require 'ray/text_helper'
20
+ require 'ray/font'
21
+ require 'ray/text'
22
+ require 'ray/audio'
23
+ require 'ray/input'
15
24
 
16
25
  require 'ray/image_set'
17
- require 'ray/font_set' if Ray.has_font_support?
18
- require 'ray/sound_set' if Ray.has_audio_support?
19
- require 'ray/music_set' if Ray.has_audio_support?
26
+ require 'ray/font_set'
27
+ require 'ray/sound_set'
28
+ require 'ray/music_set'
29
+
30
+ require 'ray/turtle'
20
31
 
21
32
  require 'ray/dsl'
22
33
  require 'ray/helper'
@@ -24,4 +35,5 @@ require 'ray/helper'
24
35
  require 'ray/sprite'
25
36
 
26
37
  require 'ray/scene'
38
+ require 'ray/scene_list'
27
39
  require 'ray/game'
@@ -7,6 +7,7 @@ module Ray
7
7
  # @return [true, false] True if the receiver is inside the rect.
8
8
  # (false if they just collide)
9
9
  def inside?(rect)
10
+ rect = rect.to_rect
10
11
  (x >= rect.x) && (y >= rect.y) &&
11
12
  (x + w) <= (rect.x + rect.w) &&
12
13
  (y + h) <= (rect.y + rect.h)
@@ -14,10 +15,11 @@ module Ray
14
15
 
15
16
  # @return [true, false] True if the receiver is outside the rect.
16
17
  def outside?(rect)
17
- !rect.contain?(x, y) &&
18
- !rect.contain?(x, y + h) &&
19
- !rect.contain?(x + w, y) &&
20
- !rect.contain?(x + w, y + h)
18
+ rect = rect.to_rect
19
+ !rect.contain?(top_left) &&
20
+ !rect.contain?(bottom_left) &&
21
+ !rect.contain?(top_right) &&
22
+ !rect.contain?(bottom_right)
21
23
  end
22
24
 
23
25
  # @return [true, false] True if the receiver collides with the rect.
@@ -26,9 +28,30 @@ module Ray
26
28
  end
27
29
 
28
30
  # @return [true, false] True if the receiver contians this point
29
- def contain?(p_x, p_y)
30
- (p_x >= x) && (p_y >= y) &&
31
- (p_x < x + w) && (p_y < y + h)
31
+ def contain?(p)
32
+ p = p.to_vector2
33
+ (p.x >= x) && (p.y >= y) &&
34
+ (p.x < x + w) && (p.y < y + h)
35
+ end
36
+
37
+ # @return [Ray::Vector2] top left corner
38
+ def top_left
39
+ Ray::Vector2[x, y]
40
+ end
41
+
42
+ # @return [Ray::Vector2] top right corner
43
+ def top_right
44
+ Ray::Vector2[x + w, y]
45
+ end
46
+
47
+ # @return [Ray::Vector2] bottom left corner
48
+ def bottom_left
49
+ Ray::Vector2[x, y + h]
50
+ end
51
+
52
+ # @return [Ray::Vector2] bottom right corner
53
+ def bottom_right
54
+ Ray::Vector2[x + w, y + h]
32
55
  end
33
56
 
34
57
  # @return [true, false] True if the two rects are equal
@@ -37,10 +60,28 @@ module Ray
37
60
  x == rect.x && y == rect.y && w == rect.w && h == rect.h
38
61
  end
39
62
 
63
+ def eql?(obj)
64
+ self.class == obj.class && self == obj
65
+ end
66
+
67
+ def hash
68
+ [x, y, w, h].hash
69
+ end
70
+
40
71
  def to_rect
41
72
  self
42
73
  end
43
74
 
75
+ # @return [Ray::Vector2] The position of the rect
76
+ def pos
77
+ Vector2[x, y]
78
+ end
79
+
80
+ # @return [Ray::Vector2] The size of the rect
81
+ def size
82
+ Vector2[w, h]
83
+ end
84
+
44
85
  alias :w :width
45
86
  alias :h :height
46
87
 
@@ -0,0 +1,41 @@
1
+ require 'rmagick'
2
+
3
+ module Ray
4
+ class Color
5
+ # @return [Ray::Color] A color created from an Magick::Pixel
6
+ def self.from_rmagick(pix)
7
+ max = Magick::QuantumRange.to_f
8
+ new((pix.red * 255) / max, (pix.green * 255) / max, (pix.blue * 255) / max,
9
+ ((max - pix.opacity) * 255) / max)
10
+ end
11
+
12
+ # @return [Magick::Pixel] An equivalent color for RMagick
13
+ def to_rmagick
14
+ max = Magick::QuantumRange.to_f
15
+ Magick::Pixel.new((r * max) / 255, (g * max) / 255, (b * max) / 255,
16
+ ((255 - a) * max) / 255)
17
+ end
18
+ end
19
+
20
+ class Image
21
+ # @return [Ray::Image] An image created from a Magic::Image
22
+ def self.from_rmagick(input)
23
+ img = new(:w => input.columns, :h => input.rows)
24
+
25
+ img.map_with_pos! do |col, x, y|
26
+ Color.from_rmagick(input.pixel_color(x, y))
27
+ end
28
+ end
29
+
30
+ # @return [Magick::Image] An equivalant image for RMagick
31
+ def to_rmagick
32
+ img = Magick::Image.new(width, height)
33
+
34
+ each_with_pos do |col, x, y|
35
+ img.pixel_color(x, y, col.to_rmagick)
36
+ end
37
+
38
+ img
39
+ end
40
+ end
41
+ end