ray 0.0.1 → 0.1.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -26,12 +26,6 @@ module Ray
26
26
  # # Do drawing here
27
27
  # end
28
28
  #
29
- # Notice win is not filled with an empty color when render is called, i.e.
30
- # it still contains the frame which appears to the user.
31
- #
32
- # Also, scenes are rendered lazily: only once when the scene is created,
33
- # and then every time need_render! is called.
34
- #
35
29
  # Once your scene is loaded, you'll probably want to clean it up (set some
36
30
  # instance variables to nil so they can be garbaged collected for instance).
37
31
  # You can do that by passing a block to clean_up:
@@ -69,6 +63,13 @@ module Ray
69
63
  # == Limiting the loop rate
70
64
  # You can prevent a scene from always running by using #loops_per_second=:
71
65
  # self.loops_per_second = 30 # will sleep some time after each loop
66
+ # This defaults to 60.
67
+ #
68
+ # == Lazy rendering
69
+ # By default, the scene is always rendered. You can enable lazy rendering:
70
+ # self.lazy_rendering = true
71
+ # In this case, the scene will render in its own Image when requested (using
72
+ # #need_render!), and that image will be drawn in every loop.
72
73
  #
73
74
  # @see Ray::DSL::EventTranslator
74
75
  class Scene
@@ -94,22 +95,20 @@ module Ray
94
95
  # Creates a new scene. block will be instance evaluated when
95
96
  # this scene becomes the current one.
96
97
  def initialize(&block)
97
- @scene_register_block = block
98
- end
98
+ @scene_register_block = block
99
+ @scene_always_block = nil
100
+ @scene_render_block = nil
101
+ @scene_clean_block = nil
99
102
 
100
- def register_events
101
- @scene_held_keys = []
103
+ @scene_need_render = true
104
+ @scene_loops_per_second = 60
102
105
 
103
- on :key_press do |key, mod|
104
- @scene_held_keys << key
105
- end
106
-
107
- on :key_release do |key, mod|
108
- @scene_held_keys.reject! { |i| i == key }
109
- end
106
+ @scene_shader = nil
107
+ end
110
108
 
109
+ def register_events
111
110
  if @scene_register_block
112
- instance_eval(&@scene_register_block)
111
+ instance_exec(@scene_arguments, &@scene_register_block)
113
112
  else
114
113
  register
115
114
  end
@@ -126,21 +125,6 @@ module Ray
126
125
  def register
127
126
  end
128
127
 
129
- # @param [Symbol, Integer] val A symbol to find the key (its name)
130
- # or an integer (Ray::Event::KEY_*)
131
- #
132
- # @return [true, false] True if the user is holding key.
133
- def holding?(val)
134
- if val.is_a? Symbol
135
- val = key(val)
136
- @scene_held_keys.any? { |o| val === o }
137
- elsif val.is_a? DSL::Matcher
138
- @scene_held_keys.any? { |o| val === o }
139
- else
140
- @scene_held_keys.include? val
141
- end
142
- end
143
-
144
128
  # Runs until you exit the scene.
145
129
  # This will also raise events if the mouse moves, ... allowing you
146
130
  # to directly listen to a such event.
@@ -148,21 +132,43 @@ module Ray
148
132
  until @scene_exit
149
133
  loop_start = Time.now
150
134
 
151
- DSL::EventTranslator.translate_event(Ray::Event.new).each do |args|
152
- raise_event(*args)
135
+ ev = Ray::Event.new
136
+ until ev.type == Ray::Event::TYPE_NOEVENT
137
+ DSL::EventTranslator.translate_event(ev).each do |args|
138
+ raise_event(*args)
139
+ end
140
+
141
+ ev.poll!
153
142
  end
154
143
 
155
144
  @scene_always_block.call if @scene_always_block
156
145
 
157
146
  listener_runner.run
158
147
 
159
- if @scene_need_render
160
- @scene_need_render = false
148
+ @scene_window.fill(Ray::Color.none)
161
149
 
162
- render(@scene_window)
163
- @scene_window.flip
150
+ if lazy_rendering?
151
+ unless @scene_lazy_cache
152
+ @scene_lazy_cache = Ray::Image.new(:w => @scene_window.w,
153
+ :h => @scene_window.h)
154
+ end
155
+
156
+ if @scene_need_render
157
+ @scene_lazy_cache.fill Ray::Color.none
158
+
159
+ render @scene_lazy_cache
160
+ @scene_lazy_cache.update
161
+
162
+ @scene_need_render = false
163
+ end
164
+
165
+ @scene_lazy_cache.draw(:on => @scene_window, :shader => @scene_shader)
166
+ else
167
+ render @scene_window
164
168
  end
165
169
 
170
+ @scene_window.update
171
+
166
172
  if @scene_loops_per_second
167
173
  ellapsed_time = Time.now - loop_start
168
174
  time_per_loop = 1.0 / @scene_loops_per_second
@@ -174,6 +180,28 @@ module Ray
174
180
  clean_up
175
181
  end
176
182
 
183
+ # Runs another scene over the current one.
184
+ # This method will return when the scene is done running.
185
+ def run_scene(name, *args)
186
+ scene_list = SceneList.new(game)
187
+ scene_list.push(name, *args)
188
+
189
+ event_runner = DSL::EventRunner.new
190
+
191
+ old_event_runner = game.event_runner
192
+ old_scene_list = game.scenes
193
+
194
+ game.event_runner = event_runner
195
+ game.scenes = scene_list
196
+
197
+ begin
198
+ game.run
199
+ ensure
200
+ game.event_runner = old_event_runner
201
+ game.scenes = old_scene_list
202
+ end
203
+ end
204
+
177
205
  # Exits the scene, but does not pop the scene.
178
206
  #
179
207
  # You may want to call this if you pushed a new scene, to switch to
@@ -201,9 +229,8 @@ module Ray
201
229
 
202
230
  # Registers the block to draw the scene.
203
231
  #
204
- # Does nothing if no block is given, this method being called if you
205
- # didn't register any render block. You can thus override it in subclasses
206
- # instead of providing a block to it.
232
+ # If no block is given, renders the scen on the image passed as
233
+ # an argument.
207
234
  #
208
235
  # @yield [window] Block to render this scene.
209
236
  # @yieldparam [Ray::Image] window The window you should draw on
@@ -220,7 +247,7 @@ module Ray
220
247
  game.push_scene(scene, *args)
221
248
  exit
222
249
  end
223
-
250
+
224
251
  # @see Ray::Game#resize_window
225
252
  def resize_window(w, h)
226
253
  game.resize_window(w, h)
@@ -265,7 +292,36 @@ module Ray
265
292
  @scene_loops_per_second = val
266
293
  end
267
294
 
295
+ alias :frames_per_second :loops_per_second
296
+ alias :frames_per_second= :loops_per_second=
297
+
298
+ def lazy_rendering
299
+ @scene_lazy_rendering ||= false
300
+ end
301
+
302
+ alias :lazy_rendering? :lazy_rendering
303
+
304
+ def lazy_rendering=(val)
305
+ @scene_lazy_rendering = val
306
+
307
+ unless lazy_rendering?
308
+ @scene_lazy_cache = nil
309
+ end
310
+ end
311
+
268
312
  # The arguments passed to the scene with push_scene
269
313
  attr_accessor :scene_arguments
314
+
315
+ # @return [Ray::Shader] The shader used for this scene.
316
+ # It will be used when drawing the scene on the window.
317
+ # This won't do anything unless lazy_rendering is enabled.
318
+ def shader
319
+ @scene_shader
320
+ end
321
+
322
+ # Sets the shader used for this scene
323
+ def shader=(value)
324
+ @scene_shader = value
325
+ end
270
326
  end
271
327
  end
@@ -0,0 +1,67 @@
1
+ module Ray
2
+ # @api private
3
+ class SceneList
4
+ include Enumerable
5
+
6
+ def initialize(game)
7
+ @game = game
8
+ @scenes = []
9
+ @scene_args = []
10
+ end
11
+
12
+ def empty?
13
+ @scenes.empty?
14
+ end
15
+
16
+ def current
17
+ @scenes.last
18
+ end
19
+
20
+ def run_current
21
+ scene = @scenes.last
22
+
23
+ scene.scene_arguments = @scene_args.last
24
+
25
+ scene.setup(*@scene_args.last)
26
+ scene.register_events
27
+ scene.need_render!
28
+ scene.run
29
+ end
30
+
31
+ def exit_current
32
+ return if empty?
33
+ current.exit
34
+ end
35
+
36
+ def pop
37
+ @scenes.pop
38
+ @scene_args.pop
39
+ end
40
+
41
+ def clear
42
+ @scenes.clear
43
+ @scene_args.clear
44
+ end
45
+
46
+ # @param [Symbol] scene Name of the scene
47
+ def push(scene, *args)
48
+ scene = @game.registred_scene(scene)
49
+ raise ArgumentError, "Unknown scene #{scene_name}" unless scene
50
+
51
+ @scenes << scene
52
+ @scene_args << args
53
+
54
+ self
55
+ end
56
+
57
+ alias :<< :push
58
+
59
+ def each(&block)
60
+ @scenes.each(&block)
61
+ end
62
+
63
+ def inspect
64
+ @scenes.inspect
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,132 @@
1
+ module Ray
2
+ class Shape
3
+ include Enumerable
4
+
5
+ # One of the points contained in a shape.
6
+ class Point
7
+ def initialize(shape, id)
8
+ @shape, @id = shape, id
9
+ end
10
+
11
+ # @return [Ray::Vector2] Position of the point.
12
+ def pos
13
+ @shape.position_of(@id)
14
+ end
15
+
16
+ # @return [Color] Color of the point
17
+ def color
18
+ @shape.color_of(@id)
19
+ end
20
+
21
+ # @return [Color] Outline color of the point
22
+ def outline
23
+ @shape.outline_of(@id)
24
+ end
25
+
26
+ # Sets the position of the point
27
+ def pos=(pos)
28
+ @shape.set_position_of(@id, pos)
29
+ end
30
+
31
+ # Sets the color of the point
32
+ def color=(color)
33
+ @shape.set_color_of(@id, color)
34
+ end
35
+
36
+ # Sets the outline color of the point
37
+ def outline=(outline)
38
+ @shape.set_outline_of(@id, outline)
39
+ end
40
+
41
+ def inspect
42
+ "#<#{self.class} shape=#{shape} id=#{id} pos=#{pos} color=#{color} outline=#{outline}>"
43
+ end
44
+
45
+ attr_reader :shape, :id
46
+ end
47
+
48
+ # Creates an ellipse.
49
+ #
50
+ # @param [Ray::Vector2, #to_vector2] center Center of the ellipse
51
+ # @param [Float] rx x radius
52
+ # @param [Float] ry y radius
53
+ # @param [Ray::Color] color Color of the ellipse
54
+ # @param [Float] outline_width Width of the ouline
55
+ # @param [Ray::Color] outline_color Color of the outline
56
+ def self.ellipse(center, rx, ry, color, outline_width = 0.0,
57
+ outline_color = Ray::Color.none)
58
+ rx, ry = rx.to_f, ry.to_f
59
+ min_radius = x_factor = y_factor = 1
60
+
61
+ min_radius = rx
62
+
63
+ if rx <= ry
64
+ min_radius = rx
65
+ y_factor = ry / rx
66
+ else
67
+ min_radius = ry
68
+ x_factor = rx / ry
69
+ end
70
+
71
+ shape = circle([0, 0], min_radius, color, outline_width, outline_color)
72
+ shape.scale = [x_factor, y_factor]
73
+ shape.pos = center
74
+
75
+ shape
76
+ end
77
+
78
+ alias :size :point_count
79
+
80
+ # @param [Integer, nil] size Size of the shape. If non nil, size points will
81
+ # be added to the shape and then yielded.
82
+ # @example
83
+ # shape = Ray::Shape.new
84
+ #
85
+ # other_shape = Ray::Shape.new(10) do |point|
86
+ # point.pos = [point.id, point.id]
87
+ # point.color = Ray::Color.new(point.id * 10, 0, 0)
88
+ # end
89
+ def initialize(size = nil)
90
+ if size
91
+ 0.upto(size - 1) do |n|
92
+ add_point [0, 0]
93
+ yield self[n]
94
+ end
95
+ end
96
+ end
97
+
98
+ def scale_x; scale.x; end
99
+ def scale_y; scale.y; end
100
+
101
+ def scale_x=(val); self.scale = [val, scale_y]; end
102
+ def scale_y=(val); self.scale = [scale_x, val]; end
103
+
104
+ # @yieldparam [Ray::Shape::Point] point Each point of the shape
105
+ def each
106
+ 0.upto(size - 1) { |n| yield self[n] }
107
+ end
108
+
109
+ # @return [Ray::Shape::Point] idth point of the shape (id should be less than size)
110
+ def [](id)
111
+ Point.new(self, id)
112
+ end
113
+
114
+ # Draws the shape on an image, optionally using a shader and a color
115
+ # to multiply each pixel.
116
+ def draw_on(image, shader = nil, color = Ray::Color.white)
117
+ image.draw_shape(self, shader, color)
118
+ end
119
+
120
+ # Draws the shape
121
+ #
122
+ # @option opts [Ray::Image] :on (Ray.screen) The object to draw the shape.
123
+ # on.
124
+ # @option opts [Ray::Shader] :shader (nil) The shader to use to draw the shape.
125
+ # @option opts [Ray::Color] :color (Ray::Color.white) Color multiplying each
126
+ # pixel of the shape.
127
+ def draw(opts = {})
128
+ draw_on(opts[:on] || Ray.screen, opts[:shader],
129
+ opts[:color] || Ray::Color.white)
130
+ end
131
+ end
132
+ end
@@ -1,7 +1,7 @@
1
1
  module Ray
2
2
  module SoundSet
3
3
  extend Ray::ResourceSet
4
-
4
+
5
5
  class << self
6
6
  def missing_pattern(string)
7
7
  Ray::Sound[string]
@@ -20,9 +20,11 @@ module Ray
20
20
  # @yield [*args] Block returning the sound
21
21
  #
22
22
  # @yieldparam args Regex captures
23
- def self.sound_set(regex, &block)
23
+ def sound_set(regex, &block)
24
24
  Ray::SoundSet.add_set(regex, &block)
25
25
  end
26
+
27
+ module_function :sound_set
26
28
  end
27
29
 
28
30
  begin
@@ -1,103 +1,57 @@
1
1
  module Ray
2
2
  class Sprite
3
- # Creates a sprite.
4
- #
5
- # @param [String, Ray::Image] The image this object will wrap;
6
- # @option opts [Ray::Rect, Array<Integer>] :at Position of the sprite
7
- # (defaults to 0,0)
8
- # @option opts [Ray::Rect, Array<Integer>] :rect Rect of the image which will
9
- # be drawn (defaults to the
10
- # whole image)
11
- # @option opts [Float] :angle The angle which will be used to draw the image
12
- # in degrees. Defaults to 0.
13
- # @option opts [Float] :zoom The zoom level which will be used to draw the image.
14
- # Defaults to 1.
15
- def initialize(image, opts = {})
16
- opts = {
17
- :rect => Ray::Rect.new(0, 0, 0, 0),
18
- :at => [0, 0],
19
- :angle => 0.0,
20
- :zoom => 1
21
- }.merge(opts)
22
-
23
- @from_rect = opts[:rect].to_rect
24
-
25
- rect = opts[:at].to_rect
26
- @x, @y = rect.x, rect.y
27
-
28
- self.angle = opts[:angle]
29
- self.zoom = opts[:zoom]
30
-
31
- @image = image.to_image
32
- end
33
-
34
3
  # Sets the size of the sprite sheet. For instance,
35
4
  # sprite.sheet_size = [3, 4]
36
5
  # would mean there are 4 rows and 3 columns in the sprite (and each cell
37
6
  # has the same size).
38
7
  def sheet_size=(ary)
39
- w, h = ary
40
-
41
8
  @uses_sprite_sheet = true
42
-
43
- @sprite_sheet_w = w
44
- @sprite_sheet_h = h
45
-
9
+ @sprite_sheet_size = ary.to_vector2
10
+
46
11
  self.sheet_pos = [0, 0]
47
12
  end
48
-
13
+
49
14
  # Sets which cell of the sprite sheet should be displayed.
50
15
  # sprite.sheet_pos = [0, 1] # Uses the first cell of the second line.
51
- def sheet_pos=(ary)
52
- x, y = ary
53
-
54
- self.from_rect = Ray::Rect.new(x * sprite_width, y * sprite_height,
16
+ def sheet_pos=(pos)
17
+ pos = pos.to_vector2
18
+
19
+ self.from_rect = Ray::Rect.new(pos.x * sprite_width,
20
+ pos.y * sprite_height,
55
21
  sprite_width, sprite_height)
56
-
57
- @sheet_pos_x = x
58
- @sheet_pos_y = y
59
- end
60
-
61
- # Returns the position of the cell which is being used.
62
- def sheet_pos
63
- [@sheet_pos_x, @sheet_pos_y]
22
+
23
+ @sheet_pos = pos
64
24
  end
65
-
25
+
26
+ # @return [Ray::Vector2] the position of the cell which is being used.
27
+ attr_reader :sheet_pos
28
+
66
29
  # @return [Integer, nil] The width of each cell in the sprite sheet
67
30
  def sprite_width
68
31
  if uses_sprite_sheet?
69
- @image.w / @sprite_sheet_w
32
+ image.w / @sprite_sheet_size.w
70
33
  end
71
34
  end
72
-
35
+
73
36
  # @return [Integer, nil] The height of each cell in the sprite sheet
74
37
  def sprite_height
75
38
  if uses_sprite_sheet?
76
- @image.h / @sprite_sheet_h
39
+ image.h / @sprite_sheet_size.h
77
40
  end
78
41
  end
79
-
42
+
80
43
  # Disables the sprite sheet
81
44
  def disable_sprite_sheet
82
- self.from_rect = Ray::Rect.new(0, 0, @image.w, @image.h)
83
-
84
- @sprite_sheet_w = nil
85
- @sprite_sheet_h = nil
86
-
45
+ self.from_rect = Ray::Rect.new(0, 0, image.w, image.h)
46
+
47
+ @sprite_sheet_size = nil
87
48
  @uses_sprite_sheet = false
88
49
  end
89
-
50
+
90
51
  def uses_sprite_sheet?
91
52
  @uses_sprite_sheet
92
53
  end
93
54
 
94
- # Draws the sprite on an image.
95
- # @param [Ray::Image] screen The image on which the sprite will be drawn
96
- def draw_on(screen)
97
- @image.blit(:rect => @from_rect, :on => screen, :at => [@x, @y],
98
- :angle => @angle, :zoom => @zoom)
99
- end
100
-
101
55
  # Draws the sprite on the screen or on another image
102
56
  # @option opts [Ray::Image] :on The image we should draw on. Defaults to
103
57
  # Ray.screen.
@@ -107,29 +61,29 @@ module Ray
107
61
 
108
62
  # @return [true, false] True if the sprite is located at (x, y)
109
63
  def is_at?(x, y)
110
- @x == x && @y == y
64
+ pos == [x, y]
111
65
  end
112
66
 
113
67
  # @return [Ray::Rect] The rect where this sprite will be drawn.
114
68
  def rect
115
- Ray::Rect.new(@x, @y, @from_rect.w == 0 ? @image.w : @from_rect.w,
116
- @from_rect.h == 0 ? @image.h : @from_rect.h)
69
+ pos = self.pos
70
+ from_rect = self.from_rect
71
+ zoom = self.zoom
72
+
73
+ Ray::Rect.new(pos.x, pos.y, from_rect.w * zoom.w, from_rect.h * zoom.h)
117
74
  end
118
75
 
119
- # @param [Ray::Rect, #rect] An object with which the receiver may collide
120
- # @return [true, false]
76
+ # @param [Ray::Rect, #to_rect] An object with which the receiver may collide
121
77
  def collide?(obj)
122
78
  rect.collide?(obj.to_rect)
123
79
  end
124
80
 
125
- # @param [Ray::Rect, #rect] (See #collide?)
126
- # @return [true, false]
81
+ # @param [Ray::Rect, #to_rect] (See #collide?)
127
82
  def inside?(obj)
128
83
  rect.inside?(obj.to_rect)
129
84
  end
130
85
 
131
- # @param [Ray::Rect, #rect] (See #collide?)
132
- # @return [true, false]
86
+ # @param [Ray::Rect, #to_rect] (See #collide?)
133
87
  def outside?(obj)
134
88
  rect.outside?(obj.to_rect)
135
89
  end
@@ -138,47 +92,31 @@ module Ray
138
92
  rect
139
93
  end
140
94
 
141
- # @return [Ray::Image]
142
- attr_reader :image
143
-
144
- # @return [Integer] position of the sprite
145
- attr_accessor :x, :y
146
-
147
- # @return [Ray::Rect] the part of the image which will be drawn. An empty
148
- # rect means the whole image.
149
- attr_accessor :from_rect
150
-
151
- # @return [Ray::Rect] the position of the sprite
152
- def pos
153
- [x, y]
154
- end
155
-
156
- # Sets the position of the sprite
157
- def pos=(ary)
158
- rect = ary.to_rect
159
-
160
- self.x = rect.x
161
- self.y = rect.y
95
+ # @return [Integer] x position of the sprite
96
+ def x
97
+ pos.x
162
98
  end
163
99
 
164
- # @return [Float] The angle used when the image is drawn.
165
- def angle
166
- @angle ? @angle : 0
100
+ # @return [Integer] y position of the sprite
101
+ def y
102
+ pos.y
167
103
  end
168
104
 
169
- # Sets the angle.
170
- def angle=(val)
171
- @angle = (val % 360).zero? ? nil : val
105
+ def x=(value)
106
+ self.pos = [value, y]
172
107
  end
173
108
 
174
- # @return [Float] the zoom applied to the image when it is drawn.
175
- def zoom
176
- @zoom ? @zoom : 1
109
+ def y=(value)
110
+ self.pos = [x, value]
177
111
  end
178
112
 
179
- # Sets the zoom level.
180
- def zoom=(val)
181
- @zoom = val == 1 ? nil : val
182
- end
113
+ def scale_x; scale.x; end
114
+ def scale_y; scale.y; end
115
+
116
+ def scale_x=(val); self.scale = [val, scale_y]; end
117
+ def scale_y=(val); self.scale = [scale_x, val]; end
118
+
119
+ alias :zoom :scale
120
+ alias :zoom= :scale=
183
121
  end
184
122
  end