ray 0.0.0.pre2 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,48 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ Ray.image_set(/^test:(red|green|blue)$/) do |col|
4
+ %w(red green blue).include?(col).should == true
5
+ Ray::Image.new(:w => 50, :h => 50).fill(Ray::Color.send(col))
6
+ end
7
+
8
+ describe Ray::ImageSet do
9
+ before :all do
10
+ Ray.init
11
+ end
12
+
13
+ describe ".[]" do
14
+ it "should return a new image" do
15
+ Ray::ImageSet["test:red"].should be_a(Ray::Image)
16
+ end
17
+
18
+ it "should always return the same image" do
19
+ obj = Ray::ImageSet["test:green"]
20
+ obj.object_id.should == Ray::ImageSet["test:green"].object_id
21
+ end
22
+ end
23
+
24
+ it "should allow to remove images from cache" do
25
+ obj = Ray::ImageSet["test:red"]
26
+ other = Ray::ImageSet["test:blue"]
27
+ Ray::ImageSet.delete_if { |name, img| name == "test:red" }
28
+ obj.object_id.should_not == Ray::ImageSet["test:red"].object_id
29
+ other.object_id.should == Ray::ImageSet["test:blue"].object_id
30
+ end
31
+
32
+ it "should also use Ray::Image's cache" do
33
+ img = Ray::ImageSet[path_of("aqua.bmp")]
34
+ img.should == Ray::Image[path_of("aqua.bmp")]
35
+ end
36
+
37
+ it "should also handle Ray::Image's cache" do
38
+ img = Ray::ImageSet[path_of("aqua.bmp")]
39
+
40
+ Ray::ImageSet.delete_if { |name, img| name == path_of("aqua.bmp") }
41
+ img.should_not == Ray::Image[path_of("aqua.bmp")]
42
+ img.should_not == Ray::ImageSet[path_of("aqua.bmp")]
43
+ end
44
+
45
+ after :all do
46
+ Ray.stop
47
+ end
48
+ end
@@ -1,76 +1,162 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
1
3
  describe Ray::Image do
2
- before :each do
4
+ before :all do
3
5
  Ray.init
4
6
  @win = Ray.create_window(:w => 100, :h => 100)
5
7
  end
6
8
 
7
- describe "#blit" do
8
- context "when trying to blit on a non-surface" do
9
- it "should raise a type error" do
10
- img = Ray::Image.new(:w => 50, :h => 50)
11
- lambda {
12
- img.blit(:on => Ray::Color.new(10, 20, 30))
13
- }.should raise_exception(TypeError)
14
- end
15
- end
16
-
17
- context "when trying to use a non-rect as a position" do
18
- it "should raise a type error" do
19
- img = Ray::Image.new(:w => 50, :h => 50)
20
- lambda {
21
- img.blit(:from => Ray::Color.new(10, 20, 30), :on => @win)
22
- }.should raise_exception(TypeError)
23
-
24
- lambda {
25
- img.blit(:on => @win, :at => Ray::Color.new(10, 20, 30))
26
- }.should raise_exception(TypeError)
27
- end
28
- end
9
+ it "should raise a type error when blitting on something that isn't an image" do
10
+ img = Ray::Image.new(:w => 50, :h => 50)
11
+ lambda {
12
+ img.blit(:on => Ray::Color.new(10, 20, 30))
13
+ }.should raise_exception(TypeError)
29
14
  end
30
15
 
31
16
  describe "#initialize" do
32
- context "when the argument isn't a hash or a string" do
33
- it "should raise a type error" do
34
- lambda {
35
- Ray::Image.new(3)
36
- }.should raise_exception(TypeError)
37
- end
17
+ it "should raise a type error if the argument isn't a hash or a string " do
18
+ lambda {
19
+ Ray::Image.new(3)
20
+ }.should raise_exception(TypeError)
38
21
  end
39
22
 
40
- context "when loading an existing file" do
41
- it "should not raise an error" do
42
- lambda {
43
- Ray::Image.new(path_of("aqua.bmp"))
44
- }.should_not raise_exception
45
- end
23
+ it "should not raise an error when loading an existing image" do
24
+ lambda {
25
+ Ray::Image.new(path_of("aqua.bmp"))
26
+ }.should_not raise_exception
46
27
  end
47
28
 
48
- context "when loading an unexising file" do
49
- it "should raise a runtime error" do
29
+ it "should raise an error when loading an unexisting file " do
30
+ lambda {
31
+ Ray::Image.new("does_not_exist.bmp")
32
+ }.should raise_exception
33
+ end
34
+
35
+ it "should be able to load a file from an IO" do
36
+ File.open(path_of("aqua.bmp")) do |io|
50
37
  lambda {
51
- Ray::Image.new("does_not_exist.bmp")
52
- }.should raise_exception
38
+ img = Ray::Image.new(io)
39
+ }.should_not raise_exception
53
40
  end
54
41
  end
55
42
 
56
43
  if Ray.has_image_support?
57
- it "should be able to load other formats" do
44
+ it "should be able to load other kind of images" do
58
45
  lambda {
59
46
  Ray::Image.new(path_of("aqua.png"))
60
47
  }.should_not raise_exception
61
48
  end
62
49
 
63
- context "if the extension does not match the file type" do
64
- it "should still load it correctly" do
50
+ it "should be able to load other kind of images from an IO" do
51
+ File.open(path_of("aqua.png")) do |io|
65
52
  lambda {
66
- Ray::Image.new(path_of("not_a_jpeg.jpeg"))
53
+ Ray::Image.new(io)
67
54
  }.should_not raise_exception
68
55
  end
69
56
  end
57
+
58
+ it "should load images even if the extension is incorrect" do
59
+ lambda {
60
+ Ray::Image.new(path_of("not_a_jpeg.jpeg"))
61
+ }.should_not raise_exception
62
+ end
63
+ end
64
+ end
65
+
66
+ describe ".[]" do
67
+ it "should return a new image" do
68
+ Ray::Image[path_of("aqua.bmp")].should be_a(Ray::Image)
69
+ end
70
+
71
+ it "should always return the same image" do
72
+ obj = Ray::Image[path_of("aqua2.bmp")]
73
+ obj.object_id.should == Ray::Image[path_of("aqua2.bmp")].object_id
74
+ end
75
+ end
76
+
77
+ describe "#==" do
78
+ it "should be true if the parameter uses the same surface" do
79
+ @win.should == Ray.screen
80
+
81
+ first = Ray::Image.new(:w => 10, :h => 10).fill(Ray::Color.red)
82
+ sec = Ray::Image.new(:w => 10, :h => 10).fill(Ray::Color.red)
83
+
84
+ first.should_not == sec
85
+ end
86
+
87
+ it "should not raise an exception for non-images" do
88
+ lambda {
89
+ @win == 3
90
+ }.should_not raise_exception
91
+ end
92
+ end
93
+
94
+ describe "#dup" do
95
+ it "should create a new surface" do
96
+ first = Ray::Image.new(:w => 10, :h => 10).fill(Ray::Color.red)
97
+ sec = first.dup
98
+
99
+ first.should_not == sec
100
+ end
101
+ end
102
+
103
+ describe "#[]" do
104
+ it "should return nil for pixels outside the image" do
105
+ Ray::Image.new(:w => 10, :h => 10)[10, 10].should be_nil
106
+ end
107
+
108
+ it "should return a color for pixels inside the image" do
109
+ Ray::Image.new(:w => 10, :h => 10)[5, 5].should be_a(Ray::Color)
110
+ end
111
+ end
112
+
113
+ describe "#[]=" do
114
+ it "should change the color of the pixel" do
115
+ img = Ray::Image.new(:w => 10, :h => 10)
116
+ img.lock { img[5, 5] = Ray::Color.new(10, 15, 20) }
117
+
118
+ col = img[5, 5]
119
+ col.r.should == 10
120
+ col.g.should == 15
121
+ col.b.should == 20
122
+ end
123
+
124
+ it "should raise an error for pixels outside the image" do
125
+ img = Ray::Image.new(:w => 10, :h => 10)
126
+ lambda {
127
+ img.lock { img[11, 11] = Ray::Color.new(10, 15, 20) }
128
+ }.should raise_exception
129
+ end
130
+ end
131
+
132
+ describe "#clip" do
133
+ it "should return the clipping rect if there are no arguments" do
134
+ img = Ray::Image.new(:w => 10, :h => 10)
135
+ img.clip.should == Ray::Rect.new(0, 0, 10, 10)
136
+ end
137
+
138
+ it "should change the clip rect when called with a rect" do
139
+ img = Ray::Image.new(:w => 10, :h => 10)
140
+ img.clip([5, 5, 3, 3])
141
+ img.clip.should == Ray::Rect.new(5, 5, 3, 3)
142
+ end
143
+
144
+ it "should return the new rect" do
145
+ img = Ray::Image.new(:w => 10, :h => 10)
146
+ img.clip([5, 5, 3, 3]).should == Ray::Rect.new(5, 5, 3, 3)
147
+ end
148
+
149
+ it "should change the clipping rect only within a block" do
150
+ img = Ray::Image.new(:w => 10, :h => 10)
151
+ img.clip([5, 5, 3, 3]) do
152
+ img.clip.should == Ray::Rect.new(5, 5, 3, 3)
153
+ end
154
+
155
+ img.clip.should == Ray::Rect.new(0, 0, 10, 10)
70
156
  end
71
157
  end
72
158
 
73
- after :each do
159
+ after :all do
74
160
  Ray.stop
75
161
  end
76
162
  end
@@ -1,17 +1,21 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
1
3
  describe Ray::Joystick do
2
4
  describe ".handle_event" do
3
- context "when handle_event= is called" do
4
- before :all do Ray.init end
5
+ before :all do
6
+ Ray.init
7
+ end
5
8
 
6
- it "should change its value" do
7
- Ray::Joystick.handle_event = true
8
- Ray::Joystick.handle_event.should be_true
9
+ it "can be changed" do
10
+ Ray::Joystick.handle_event = true
11
+ Ray::Joystick.handle_event.should be_true
9
12
 
10
- Ray::Joystick.handle_event = false
11
- Ray::Joystick.handle_event.should be_false
12
- end
13
+ Ray::Joystick.handle_event = false
14
+ Ray::Joystick.handle_event.should be_false
15
+ end
13
16
 
14
- after :all do Ray.init end
17
+ after :all do
18
+ Ray.init
15
19
  end
16
20
  end
17
21
  end
@@ -1,73 +1,50 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
1
3
  describe Ray::Matchers do
2
- context "when a matcher is described" do
3
- it "should add it as a private method" do
4
- Ray.describe_matcher(:match, :string) do |reg|
4
+ it "should add matchers as public methods" do
5
+ lambda {
6
+ Ray.describe_matcher(:awesome_matcher) do |reg|
5
7
  lambda { |str| str =~ reg }
6
8
  end
7
-
8
- Ray::Matchers.private_instance_methods.should include(:match)
9
- end
10
-
11
- context "without a defined target" do
12
- it "should create one operate on anything" do
13
- Ray.describe_matcher(:foo_matcher) do |something|
14
- lambda { |x| x == something }
15
- end
16
-
17
- obj = Object.new
18
- obj.extend Ray::Matchers
19
-
20
- obj.instance_eval do
21
- matcher = foo_matcher("")
22
- matcher.can_match_on?(Object).should == true
23
- end
9
+ }.should change {
10
+ Ray::Matchers.instance_methods.any? do |i|
11
+ i == :awesome_matcher or i == "awesome_matcher"
24
12
  end
25
- end
13
+ }.from(false).to(true)
26
14
  end
27
15
 
28
- describe "'s private methods" do
29
- before :all do
30
- Ray.describe_matcher(:match, :string) do |reg|
31
- lambda { |str| str =~ reg }
32
- end
16
+ it "should return a Ray::DSL::Matchers for defined matchers" do
17
+ Ray.describe_matcher(:match) do |reg|
18
+ lambda { |str| str =~ reg }
33
19
  end
34
20
 
35
- before :each do
36
- @obj = Object.new
37
- @obj.extend Ray::Matchers
38
- end
21
+ @obj = Object.new
22
+ @obj.extend Ray::Matchers
39
23
 
40
- it "should return a matcher" do
41
- @obj.instance_eval { match("foo") }.should be_a(Ray::DSL::Matcher)
42
- end
24
+ @obj.instance_eval { match("foo") }.should be_a(Ray::DSL::Matcher)
43
25
  end
44
- end
45
26
 
46
- describe Ray::DSL::Matcher do
47
- it "should only be able to match on a given class or module" do
48
- matcher = Ray::DSL::Matcher.new(:string) { |foo| false }
49
- matcher.can_match_on?(Array).should be_false
50
- matcher.can_match_on?(Object).should be_false
27
+ describe "#where" do
28
+ it "should return a matcher matching if the block returns true" do
29
+ obj = Object.new
30
+ obj.extend Ray::Helper
51
31
 
52
- matcher.can_match_on?(String).should be_true
53
- matcher.can_match_on?(Class.new(String)).should be_true
54
- end
32
+ res = 0
33
+ obj.instance_eval do
34
+ self.event_runner = Ray::DSL::EventRunner.new
55
35
 
56
- describe "#can_match_on?" do
57
- it "should resolve types" do
58
- matcher = Ray::DSL::Matcher.new(:string) { |foo| false }
59
- matcher.can_match_on?(:string).should be_true
60
- end
61
- end
36
+ on :foo, where { |x| x > 10 } do |x|
37
+ x.should > 10
38
+ res += 1
39
+ end
62
40
 
63
- describe "#match?" do
64
- it "should return false if we can't match on the object's class" do
65
- matcher = Ray::DSL::Matcher.new(:string) { |foo| true }
66
- matcher.match?([]).should be_false
67
- matcher.match?(Object.new).should be_false
41
+ raise_event(:foo, 10)
42
+ raise_event(:foo, 15)
43
+
44
+ listener_runner.run
45
+ end
68
46
 
69
- matcher.match?("").should be_true
70
- matcher.match?(Class.new(String).new).should be_true
47
+ res.should == 1
71
48
  end
72
49
  end
73
50
  end
data/spec/ray/ray_spec.rb CHANGED
@@ -1,51 +1,44 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
1
2
  require 'rbconfig'
2
3
 
3
4
  describe Ray do
4
5
  describe ".create_window" do
5
- context "when given an unappropriate configuration" do
6
- it "should raise runtime error" do
7
- Ray.init
6
+ it "should raise runtime error for invalid configurations" do
7
+ Ray.init
8
8
 
9
- lambda {
10
- Ray.create_window(:w => 100, :h => 100, :bpp => 1)
11
- }.should raise_exception(RuntimeError)
9
+ lambda {
10
+ Ray.create_window(:w => 100, :h => 100, :bpp => 1)
11
+ }.should raise_exception(RuntimeError)
12
12
 
13
- Ray.stop
14
- end
13
+ Ray.stop
15
14
  end
16
15
 
17
- context "when given a correct configuration" do
18
- it "should create a window of the expected size" do
19
- Ray.init
16
+ it "should create a window of the expected size for valid configurations" do
17
+ Ray.init
20
18
 
21
- win = Ray.create_window(:w => 100, :h => 50)
22
- win.width.should == 100
23
- win.height.should == 50
19
+ win = Ray.create_window(:w => 100, :h => 50)
20
+ win.width.should == 100
21
+ win.height.should == 50
24
22
 
25
- win = Ray.create_window(:width => 50, :height => 100)
26
- win.width.should == 50
27
- win.height.should == 100
23
+ win = Ray.create_window(:width => 50, :height => 100)
24
+ win.width.should == 50
25
+ win.height.should == 100
28
26
 
29
- Ray.stop
30
- end
27
+ Ray.stop
31
28
  end
32
29
  end
33
30
 
34
31
  describe ".can_use_mode?" do
35
- context "when given an unappropriate configuration" do
36
- it "should return false" do
37
- Ray.init
38
- Ray.can_use_mode?(:w => 100, :h => 100, :bpp => 1).should be_false
39
- Ray.stop
40
- end
32
+ it "should return false for invalid configurations" do
33
+ Ray.init
34
+ Ray.can_use_mode?(:w => 100, :h => 100, :bpp => 1).should be_false
35
+ Ray.stop
41
36
  end
42
37
 
43
- context "when given a correct configuration" do
44
- it "should return true" do
45
- Ray.init
46
- Ray.can_use_mode?(:w => 100, :h => 100).should be_true
47
- Ray.stop
48
- end
38
+ it "should return true for valid configurations" do
39
+ Ray.init
40
+ Ray.can_use_mode?(:w => 480, :h => 272).should be_true
41
+ Ray.stop
49
42
  end
50
43
  end
51
44
 
@@ -83,4 +76,13 @@ describe Ray do
83
76
 
84
77
  after :all do Ray.stop end
85
78
  end
79
+
80
+ describe ".init" do
81
+ it "should allow to pass a hash to add options" do
82
+ lambda {
83
+ Ray.init({})
84
+ Ray.stop
85
+ }.should_not raise_exception
86
+ end
87
+ end
86
88
  end