ray 0.0.0.pre2 → 0.0.1

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 (76) hide show
  1. data/.gitignore +1 -0
  2. data/.rspec +3 -0
  3. data/README.md +62 -0
  4. data/Rakefile +33 -23
  5. data/VERSION +1 -1
  6. data/ext/audio.c +473 -0
  7. data/ext/color.c +4 -4
  8. data/ext/event.c +25 -3
  9. data/ext/extconf.rb +35 -22
  10. data/ext/font.c +287 -0
  11. data/ext/image.c +682 -33
  12. data/ext/joystick.c +9 -9
  13. data/ext/ray.c +166 -55
  14. data/ext/ray.h +120 -9
  15. data/ext/ray_osx.m +161 -0
  16. data/ext/rect.c +31 -4
  17. data/lib/ray/audio.rb +52 -0
  18. data/lib/ray/color.rb +16 -0
  19. data/lib/ray/dsl.rb +1 -3
  20. data/lib/ray/dsl/event.rb +1 -39
  21. data/lib/ray/dsl/event_listener.rb +38 -0
  22. data/lib/ray/dsl/event_runner.rb +3 -1
  23. data/lib/ray/dsl/event_translator.rb +74 -8
  24. data/lib/ray/dsl/handler.rb +3 -33
  25. data/lib/ray/dsl/matcher.rb +129 -23
  26. data/lib/ray/font.rb +108 -0
  27. data/lib/ray/font_set.rb +37 -0
  28. data/lib/ray/game.rb +171 -34
  29. data/lib/ray/helper.rb +43 -5
  30. data/lib/ray/image.rb +90 -3
  31. data/lib/ray/image_set.rb +35 -0
  32. data/lib/ray/joystick.rb +30 -0
  33. data/lib/ray/music_set.rb +35 -0
  34. data/lib/ray/ray.rb +17 -9
  35. data/lib/ray/rect.rb +51 -0
  36. data/lib/ray/resource_set.rb +92 -0
  37. data/lib/ray/scene.rb +220 -51
  38. data/lib/ray/sound_set.rb +35 -0
  39. data/lib/ray/sprite.rb +184 -0
  40. data/psp/ext.c +4 -0
  41. data/samples/hello_world/hello.rb +35 -0
  42. data/samples/hello_world/hello_dsl.rb +24 -0
  43. data/samples/pong/pong.rb +128 -0
  44. data/samples/sokoban/level_1 +7 -0
  45. data/samples/sokoban/sokoban.rb +370 -0
  46. data/spec/ray/audio_spec.rb +146 -0
  47. data/spec/ray/color_spec.rb +13 -0
  48. data/spec/ray/event_spec.rb +57 -168
  49. data/spec/ray/font_spec.rb +93 -0
  50. data/spec/ray/image_set_spec.rb +48 -0
  51. data/spec/ray/image_spec.rb +130 -44
  52. data/spec/ray/joystick_spec.rb +13 -9
  53. data/spec/ray/matcher_spec.rb +32 -55
  54. data/spec/ray/ray_spec.rb +33 -31
  55. data/spec/ray/rect_spec.rb +80 -0
  56. data/spec/ray/resource_set_spec.rb +105 -0
  57. data/spec/ray/sprite_spec.rb +163 -0
  58. data/spec/res/VeraMono.ttf +0 -0
  59. data/spec/res/aqua2.bmp +0 -0
  60. data/spec/res/pop.wav +0 -0
  61. data/spec/spec.opts +4 -0
  62. data/spec/spec_helper.rb +8 -0
  63. data/yard_ext.rb +91 -0
  64. metadata +104 -38
  65. data/bin/ray +0 -5
  66. data/bin/ray_irb +0 -4
  67. data/ext/SDLMain.h +0 -17
  68. data/ext/SDLMain.m +0 -381
  69. data/lib/ray/config.rb +0 -84
  70. data/lib/ray/dsl/converter.rb +0 -65
  71. data/lib/ray/dsl/listener.rb +0 -30
  72. data/lib/ray/dsl/type.rb +0 -58
  73. data/spec/ray/config_spec.rb +0 -90
  74. data/spec/ray/conversion_spec.rb +0 -43
  75. data/spec/ray/type_spec.rb +0 -17
  76. data/spec_runner.rb +0 -27
data/lib/ray/helper.rb CHANGED
@@ -1,17 +1,55 @@
1
1
  module Ray
2
- # This module includes the methods you may want to use in your code.
3
- # It allows you to play with events (listen to them, raise them) and
4
- # includes the matchers you described, which you may want to use when
5
- # you want to listen to the events matching a condition.
2
+ # Module including many helpful modules and methods.
6
3
  module Helper
7
4
  include Ray::DSL::EventRaiser
8
- include Ray::DSL::Listener
5
+ include Ray::DSL::EventListener
9
6
 
10
7
  include Ray::Matchers
11
8
 
9
+ # Sets the event runner for this object.
12
10
  def event_runner=(value)
13
11
  self.listener_runner = value
14
12
  self.raiser_runner = value
15
13
  end
14
+
15
+ # @return [DSL::EventRunner] The event runner used to handle event.
16
+ def event_runner
17
+ listener_runner
18
+ end
19
+
20
+ # Creates an event runner for this object
21
+ def create_event_runner
22
+ self.event_runner = Ray::DSL::EventRunner.new
23
+ end
24
+
25
+ # @see Ray::ImageSet.[]
26
+ def image(name)
27
+ Ray::ImageSet[name]
28
+ end
29
+
30
+ # @see Ray::Sprite#initialize
31
+ def sprite(image, opts = {})
32
+ Ray::Sprite.new(image, opts)
33
+ end
34
+
35
+ # @see Ray::Channel#initialize
36
+ def channel(id)
37
+ Ray::Channel.new(id)
38
+ end
39
+
40
+ # @see Ray::MusicSet.[]
41
+ def sound(file)
42
+ Ray::SoundSet[file]
43
+ end
44
+
45
+ # @see Ray::MusicSet.[]
46
+ def music(file)
47
+ Ray::MusicSet[file]
48
+ end
49
+
50
+ # @see Ray::FontSet.[]
51
+ def font(name, size)
52
+ Ray::FontSet[name, size]
53
+ end
16
54
  end
17
55
  end
data/lib/ray/image.rb CHANGED
@@ -1,11 +1,98 @@
1
1
  module Ray
2
2
  class Image
3
3
  def inspect
4
- "#<#{self} w=#{w} h=#{h} bpp=#{bpp}>"
4
+ "#<#{self.class} w=#{w} h=#{h}>"
5
5
  end
6
6
 
7
7
  alias :bits_per_pixel :bpp
8
- alias :width :w
9
- alias :height :h
8
+ alias :w :width
9
+ alias :h :height
10
+
11
+ alias :draw :blit
12
+ alias :update :flip
13
+
14
+ extend Ray::ResourceSet
15
+ add_set(/^(.*)$/) { |filename| new(filename) }
16
+
17
+ # @yield [pixel]
18
+ # @yieldparam [Ray::Color] pixel Color of a point
19
+ def each
20
+ return Enumerator.new(self, :each) unless block_given?
21
+
22
+ (0...w).each do |x|
23
+ (0...h).each do |y|
24
+ yield self[x, y]
25
+ end
26
+ end
27
+
28
+ self
29
+ end
30
+
31
+ # Same as each, but also yields the position of each point.
32
+ # @yield [x, y, pixel]
33
+ def each_with_pos
34
+ return Enumerator.new(self, :each_with_pos) unless block_given?
35
+
36
+ (0...w).each do |x|
37
+ (0...h).each do |y|
38
+ yield x, y, self[x, y]
39
+ end
40
+ end
41
+
42
+ self
43
+ end
44
+
45
+ # @yield [pixel] Block returning the new color of this pixel.
46
+ def map!
47
+ return Enumerator.new(self, :map!) unless block_given?
48
+
49
+ lock do
50
+ (0...w).each do |x|
51
+ (0...h).each do |y|
52
+ self[x, y] = yield self[x, y]
53
+ end
54
+ end
55
+ end
56
+
57
+ self
58
+ end
59
+
60
+ # @yield [x, y, pixel] Block returning the new color of this pixel
61
+ def map_with_pos!
62
+ return Enumerator.new(self, :map_with_pos!) unless block_given?
63
+
64
+ lock do
65
+ (0...w).each do |x|
66
+ (0...h).each do |y|
67
+ self[x, y] = yield x, y, self[x, y]
68
+ end
69
+ end
70
+ end
71
+
72
+ self
73
+ end
74
+
75
+ # @return [Ray::Image] New image created according to a block.
76
+ def map(&block)
77
+ dup.map!(&block)
78
+ end
79
+
80
+ # @return [Ray::Image] New image created according to a block.
81
+ def map_with_pos(&block)
82
+ dup.map_with_pos!(&block)
83
+ end
84
+
85
+ def to_image
86
+ self
87
+ end
88
+
89
+ include Enumerable
90
+ end
91
+ end
92
+
93
+ class String
94
+ # Converts the string to an image using Ray::ImageSet.[]
95
+ def to_image
96
+ Ray::ImageSet[self]
10
97
  end
11
98
  end
@@ -0,0 +1,35 @@
1
+ module Ray
2
+ module ImageSet
3
+ extend Ray::ResourceSet
4
+
5
+ class << self
6
+ def missing_pattern(string)
7
+ Ray::Image[string]
8
+ end
9
+
10
+ def select!(&block)
11
+ super(&block)
12
+ Ray::Image.select!(&block)
13
+ end
14
+ end
15
+ end
16
+
17
+ # Creates a new image set.
18
+ #
19
+ # @param [Regexp] regex Regular expression used to match file
20
+ # @yield [*args] Block returning the image
21
+ # @yieldparam args Regex captures
22
+ def self.image_set(regex, &block)
23
+ Ray::ImageSet.add_set(regex, &block)
24
+ end
25
+ end
26
+
27
+ begin
28
+ require 'open-uri'
29
+
30
+ Ray.image_set(/^(http|ftp):\/\/(\S+)$/) do |protocol, address|
31
+ open("#{protocol}://#{address}") { |io| Ray::Image.new(io) }
32
+ end
33
+ rescue LoadError
34
+ # that image set is not needed
35
+ end
@@ -0,0 +1,30 @@
1
+ module Ray
2
+ class Joystick
3
+ @@joysticks = Hash.new { |h, k| h[k] = new(k) }
4
+
5
+ class << self
6
+ # @return [Ray::Joystick] an opened joystick
7
+ def [](id)
8
+ joy = @@joysticks[id]
9
+ joy.open if joy.closed?
10
+ joy
11
+ end
12
+
13
+ # Enumerates through all the joysitcks.
14
+ #
15
+ # @yield [joystick]
16
+ # @yieldparam [Ray::Joystick] joystick
17
+ def each
18
+ return Enumerator.new(self, :each) unless block_given?
19
+
20
+ (0...count).each do |i|
21
+ yield self[i]
22
+ end
23
+
24
+ self
25
+ end
26
+
27
+ include Enumerable
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,35 @@
1
+ module Ray
2
+ module MusicSet
3
+ extend Ray::ResourceSet
4
+
5
+ class << self
6
+ def missing_pattern(string)
7
+ Ray::Music[string, size]
8
+ end
9
+
10
+ def select!(&block)
11
+ super(&block)
12
+ Ray::Music.select!(&block)
13
+ end
14
+ end
15
+ end
16
+
17
+ # Creates a new music set.
18
+ #
19
+ # @param [Regexp] regex Regular expression used to match file
20
+ # @yield [*args] Block returning the music
21
+ #
22
+ # @yieldparam args Regex captures
23
+ def self.music_set(regex, &block)
24
+ Ray::MusicSet.add_set(regex, &block)
25
+ end
26
+ end
27
+
28
+ begin
29
+ require 'open-uri'
30
+
31
+ Ray.music_set(/^(http|ftp):\/\/(\S+)$/) do |protocol, address|
32
+ open("#{protocol}://#{address}") { |io| Ray::Music.new(io) }
33
+ end
34
+ rescue LoadError
35
+ end
data/lib/ray/ray.rb CHANGED
@@ -1,19 +1,27 @@
1
- unless defined? Ray
2
- unless RUBY_PLATFORM =~ /darwin/
3
- require 'ray_ext'
4
- else
5
- $stderr.puts "Please run this program using ray: ray #$0"
6
- exit 1
7
- end
8
- end
1
+ require 'enumerator'
2
+
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
6
+
7
+ require 'ray/resource_set'
9
8
 
10
9
  require 'ray/color'
11
10
  require 'ray/rect'
11
+ 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?
12
15
 
13
- require 'ray/config'
16
+ 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?
14
20
 
15
21
  require 'ray/dsl'
16
22
  require 'ray/helper'
17
23
 
24
+ require 'ray/sprite'
25
+
18
26
  require 'ray/scene'
19
27
  require 'ray/game'
data/lib/ray/rect.rb CHANGED
@@ -4,6 +4,43 @@ module Ray
4
4
  "#<#{self.class} {{#{x}, #{y}}, {#{w}, #{h}}}>"
5
5
  end
6
6
 
7
+ # @return [true, false] True if the receiver is inside the rect.
8
+ # (false if they just collide)
9
+ def inside?(rect)
10
+ (x >= rect.x) && (y >= rect.y) &&
11
+ (x + w) <= (rect.x + rect.w) &&
12
+ (y + h) <= (rect.y + rect.h)
13
+ end
14
+
15
+ # @return [true, false] True if the receiver is outside the rect.
16
+ 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)
21
+ end
22
+
23
+ # @return [true, false] True if the receiver collides with the rect.
24
+ def collide?(rect)
25
+ !outside?(rect)
26
+ end
27
+
28
+ # @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)
32
+ end
33
+
34
+ # @return [true, false] True if the two rects are equal
35
+ def ==(rect)
36
+ return false unless rect.is_a? Rect
37
+ x == rect.x && y == rect.y && w == rect.w && h == rect.h
38
+ end
39
+
40
+ def to_rect
41
+ self
42
+ end
43
+
7
44
  alias :w :width
8
45
  alias :h :height
9
46
 
@@ -11,3 +48,17 @@ module Ray
11
48
  alias :h= :height=
12
49
  end
13
50
  end
51
+
52
+ class Array
53
+ # Converts an Array to a rect
54
+ def to_rect
55
+ Ray::Rect.new(*self)
56
+ end
57
+ end
58
+
59
+ class Hash
60
+ # Converts a Hash to a rect
61
+ def to_rect
62
+ Ray::Rect.new(self)
63
+ end
64
+ end
@@ -0,0 +1,92 @@
1
+ module Ray
2
+ # Exception raised when a pattern isn't found in ResourceSet
3
+ class NoPatternError < StandardError; end
4
+
5
+ # Mixin used to cache resources. It allows to register different block
6
+ # to create the resources using a regexp:
7
+ # add_set(/^http://(.+)$/) do |url| # captures are passed to the block
8
+ # ...
9
+ # end
10
+ #
11
+ # Custom arguments can also be specified:
12
+ # add_set(/^http://(.+)/) do |url, priority| # arguments are passed at the end
13
+ # ...
14
+ # end
15
+ #
16
+ # Notice you would usually use extend instead of include to use this module.
17
+ #
18
+ module ResourceSet
19
+ # Registers a block for handling some resources.
20
+ def add_set(regexp, &block)
21
+ set_hash[regexp] = Hash.new do |hash, args|
22
+ regexp =~ args.first
23
+
24
+ block_args = $~.captures + args[1..-1]
25
+ hash[args] = block.call(*block_args)
26
+ end
27
+ end
28
+
29
+ def set_hash
30
+ (@set_hash ||= {})
31
+ end
32
+
33
+ # @return The object corresponding to a given list of parameters.
34
+ def [](value, *params)
35
+ if params.size != required_argument_count
36
+ raise(ArgumentError, "wrong number of arguments (%d for %d)" %
37
+ [params.size, required_argument_count])
38
+ end
39
+
40
+ set_hash.each do |regexp, hash|
41
+ return hash[([value] + params)] if regexp =~ value
42
+ end
43
+
44
+ missing_pattern(value, *params)
45
+ end
46
+
47
+ # Selects objects matching a condition.
48
+ #
49
+ # @yield *arguments, object
50
+ # @yieldparam *arguments All the arguments used to access that object
51
+ # @yieldparam object the actual resource
52
+ def select!(&block)
53
+ return Enumerator.new(self, :select!) unless block_given?
54
+
55
+ set_hash.each do |regexp, hash|
56
+ hash.delete_if do |key, val|
57
+ !block.call(*(key + [val]))
58
+ end
59
+ end
60
+ end
61
+
62
+ # Removes all the objects from the array but those for which
63
+ # block returned true.
64
+ #
65
+ # @yield *arguments, object (See #select!)
66
+ def reject!(&block)
67
+ return Enumerator.new(self, :reject!) unless block_given?
68
+ select! { |*args| !block.call(*args) }
69
+ end
70
+
71
+ alias :delete_if :reject!
72
+
73
+ # Sets the required argument count. The number of arguments passed to []
74
+ # must be equal to this + 1 (the first argument isn't counted and is required)
75
+ #
76
+ # Defaulted to 0.
77
+ def need_argument_count(n)
78
+ @set_argument_count = n
79
+ end
80
+
81
+ # Returns the required argument count.
82
+ def required_argument_count
83
+ @set_argument_count ||= 0
84
+ end
85
+
86
+ # Method called when a string isn't matched by any pattern.
87
+ # Raises a NoPatternError excpetion by default.
88
+ def missing_pattern(string, *args)
89
+ raise NoPatternError, "#{string.inspect} not macthed by any pattern"
90
+ end
91
+ end
92
+ end