rubygame 2.3.0-x86-linux

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. data/CREDITS +60 -0
  2. data/LICENSE +504 -0
  3. data/NEWS +252 -0
  4. data/README +123 -0
  5. data/ROADMAP +109 -0
  6. data/Rakefile +438 -0
  7. data/doc/extended_readme.rdoc +49 -0
  8. data/doc/getting_started.rdoc +47 -0
  9. data/doc/macosx_install.rdoc +70 -0
  10. data/doc/windows_install.rdoc +123 -0
  11. data/ext/rubygame/rubygame_core.so +0 -0
  12. data/ext/rubygame/rubygame_event.c +644 -0
  13. data/ext/rubygame/rubygame_event.h +48 -0
  14. data/ext/rubygame/rubygame_event.o +0 -0
  15. data/ext/rubygame/rubygame_gfx.c +942 -0
  16. data/ext/rubygame/rubygame_gfx.h +101 -0
  17. data/ext/rubygame/rubygame_gfx.o +0 -0
  18. data/ext/rubygame/rubygame_gfx.so +0 -0
  19. data/ext/rubygame/rubygame_gl.c +154 -0
  20. data/ext/rubygame/rubygame_gl.h +32 -0
  21. data/ext/rubygame/rubygame_gl.o +0 -0
  22. data/ext/rubygame/rubygame_image.c +252 -0
  23. data/ext/rubygame/rubygame_image.h +41 -0
  24. data/ext/rubygame/rubygame_image.o +0 -0
  25. data/ext/rubygame/rubygame_image.so +0 -0
  26. data/ext/rubygame/rubygame_joystick.c +247 -0
  27. data/ext/rubygame/rubygame_joystick.h +41 -0
  28. data/ext/rubygame/rubygame_joystick.o +0 -0
  29. data/ext/rubygame/rubygame_main.c +155 -0
  30. data/ext/rubygame/rubygame_main.h +36 -0
  31. data/ext/rubygame/rubygame_main.o +0 -0
  32. data/ext/rubygame/rubygame_mixer.c +1024 -0
  33. data/ext/rubygame/rubygame_mixer.h +36 -0
  34. data/ext/rubygame/rubygame_mixer.o +0 -0
  35. data/ext/rubygame/rubygame_mixer.so +0 -0
  36. data/ext/rubygame/rubygame_music.c +1017 -0
  37. data/ext/rubygame/rubygame_music.h +29 -0
  38. data/ext/rubygame/rubygame_music.o +0 -0
  39. data/ext/rubygame/rubygame_screen.c +448 -0
  40. data/ext/rubygame/rubygame_screen.h +43 -0
  41. data/ext/rubygame/rubygame_screen.o +0 -0
  42. data/ext/rubygame/rubygame_shared.c +272 -0
  43. data/ext/rubygame/rubygame_shared.h +68 -0
  44. data/ext/rubygame/rubygame_shared.o +0 -0
  45. data/ext/rubygame/rubygame_sound.c +863 -0
  46. data/ext/rubygame/rubygame_sound.h +29 -0
  47. data/ext/rubygame/rubygame_sound.o +0 -0
  48. data/ext/rubygame/rubygame_surface.c +1151 -0
  49. data/ext/rubygame/rubygame_surface.h +62 -0
  50. data/ext/rubygame/rubygame_surface.o +0 -0
  51. data/ext/rubygame/rubygame_time.c +183 -0
  52. data/ext/rubygame/rubygame_time.h +32 -0
  53. data/ext/rubygame/rubygame_time.o +0 -0
  54. data/ext/rubygame/rubygame_ttf.c +599 -0
  55. data/ext/rubygame/rubygame_ttf.h +69 -0
  56. data/ext/rubygame/rubygame_ttf.o +0 -0
  57. data/ext/rubygame/rubygame_ttf.so +0 -0
  58. data/lib/rubygame.rb +41 -0
  59. data/lib/rubygame/clock.rb +128 -0
  60. data/lib/rubygame/color.rb +79 -0
  61. data/lib/rubygame/color/models/base.rb +111 -0
  62. data/lib/rubygame/color/models/hsl.rb +153 -0
  63. data/lib/rubygame/color/models/hsv.rb +149 -0
  64. data/lib/rubygame/color/models/rgb.rb +78 -0
  65. data/lib/rubygame/color/palettes/css.rb +49 -0
  66. data/lib/rubygame/color/palettes/palette.rb +100 -0
  67. data/lib/rubygame/color/palettes/x11.rb +177 -0
  68. data/lib/rubygame/constants.rb +238 -0
  69. data/lib/rubygame/event.rb +313 -0
  70. data/lib/rubygame/ftor.rb +370 -0
  71. data/lib/rubygame/hotspot.rb +265 -0
  72. data/lib/rubygame/keyconstants.rb +237 -0
  73. data/lib/rubygame/mediabag.rb +94 -0
  74. data/lib/rubygame/named_resource.rb +254 -0
  75. data/lib/rubygame/queue.rb +288 -0
  76. data/lib/rubygame/rect.rb +612 -0
  77. data/lib/rubygame/sfont.rb +223 -0
  78. data/lib/rubygame/sprite.rb +511 -0
  79. data/samples/FreeSans.ttf +0 -0
  80. data/samples/GPL.txt +340 -0
  81. data/samples/README +40 -0
  82. data/samples/chimp.bmp +0 -0
  83. data/samples/chimp.rb +302 -0
  84. data/samples/demo_gl.rb +151 -0
  85. data/samples/demo_gl_tex.rb +197 -0
  86. data/samples/demo_music.rb +77 -0
  87. data/samples/demo_rubygame.rb +296 -0
  88. data/samples/demo_sfont.rb +52 -0
  89. data/samples/demo_ttf.rb +193 -0
  90. data/samples/demo_utf8.rb +53 -0
  91. data/samples/fist.bmp +0 -0
  92. data/samples/load_and_blit.rb +22 -0
  93. data/samples/panda.png +0 -0
  94. data/samples/punch.wav +0 -0
  95. data/samples/ruby.png +0 -0
  96. data/samples/song.ogg +0 -0
  97. data/samples/term16.png +0 -0
  98. data/samples/whiff.wav +0 -0
  99. data/test/audio_spec.rb +236 -0
  100. data/test/color_spec.rb +544 -0
  101. data/test/image.png +0 -0
  102. data/test/music_spec.rb +727 -0
  103. data/test/named_resource_spec.rb +211 -0
  104. data/test/short.ogg +0 -0
  105. data/test/sound_spec.rb +564 -0
  106. data/test/surface_spec.rb +219 -0
  107. data/test/test_crop.rb +45 -0
  108. data/test/test_rect.rb +841 -0
  109. metadata +174 -0
@@ -0,0 +1,219 @@
1
+ # This is mostly for regression testing and bugfix confirmation at the moment.
2
+
3
+ require 'rubygame'
4
+ include Rubygame
5
+
6
+ samples_dir = File.join( File.dirname(__FILE__), "..", "samples", "")
7
+ test_dir = File.join( File.dirname(__FILE__), "" )
8
+
9
+ test_image = test_dir + "image.png"
10
+ not_image = test_dir + "short.ogg"
11
+ panda = samples_dir + "panda.png"
12
+ dne = test_dir + "does_not_exist.png"
13
+
14
+
15
+ describe Surface, "(creation)" do
16
+ before(:each) do
17
+ Rubygame.init()
18
+ @screen = Screen.new([100,100])
19
+ end
20
+
21
+ after(:each) do
22
+ Rubygame.quit()
23
+ end
24
+
25
+ it "should raise TypeError when #new size is not an Array" do
26
+ lambda {
27
+ Surface.new("not an array")
28
+ }.should raise_error(TypeError)
29
+ end
30
+
31
+ it "should raise ArgumentError when #new size is an Array of non-Numerics" do
32
+ lambda {
33
+ Surface.new(["not", "numeric", "members"])
34
+ }.should raise_error(TypeError)
35
+ end
36
+
37
+ it "should raise ArgumentError when #new size is too short" do
38
+ lambda {
39
+ Surface.new([1])
40
+ }.should raise_error(ArgumentError)
41
+ end
42
+ end
43
+
44
+
45
+
46
+ describe Surface, "(loading)" do
47
+ before :each do
48
+ unless( Rubygame::VERSIONS[:sdl_image] )
49
+ raise "Can't test image loading, no SDL_image installed."
50
+ end
51
+ end
52
+
53
+ it "should load image to a new Surface" do
54
+ surface = Surface.load( test_image )
55
+ end
56
+
57
+ it "should raise an error if file is not an image" do
58
+ lambda{ Surface.load( not_image ) }.should raise_error( SDLError )
59
+ end
60
+
61
+ it "should raise an error if file doesn't exist" do
62
+ lambda{ Surface.load( dne ) }.should raise_error( SDLError )
63
+ end
64
+ end
65
+
66
+
67
+ describe Surface, "(loading from string)" do
68
+ before :each do
69
+ unless( Rubygame::VERSIONS[:sdl_image] )
70
+ raise "Can't test image loading, no SDL_image installed."
71
+ end
72
+
73
+ @data = "\x42\x4d\x3a\x00\x00\x00\x00\x00"+
74
+ "\x00\x00\x36\x00\x00\x00\x28\x00"+
75
+ "\x00\x00\x01\x00\x00\x00\x01\x00"+
76
+ "\x00\x00\x01\x00\x18\x00\x00\x00"+
77
+ "\x00\x00\x04\x00\x00\x00\x13\x0b"+
78
+ "\x00\x00\x13\x0b\x00\x00\x00\x00"+
79
+ "\x00\x00\x00\x00\x00\x00\x00\x00"+
80
+ "\xff\x00"
81
+ end
82
+
83
+ it "should be able to load from string" do
84
+ surf = Surface.load_from_string(@data)
85
+ surf.get_at(0,0).should == [255,0,0,255]
86
+ end
87
+
88
+ it "should be able to load from string (typed)" do
89
+ surf = Surface.load_from_string(@data,"BMP")
90
+ surf.get_at(0,0).should == [255,0,0,255]
91
+ end
92
+ end
93
+
94
+
95
+ describe Surface, "(named resource)" do
96
+ before :each do
97
+ Surface.autoload_dirs = [samples_dir]
98
+ end
99
+
100
+ after :each do
101
+ Surface.autoload_dirs = []
102
+ Surface.instance_eval { @resources = {} }
103
+ end
104
+
105
+ it "should include NamedResource" do
106
+ Surface.included_modules.should include(NamedResource)
107
+ end
108
+
109
+ it "should respond to :[]" do
110
+ Surface.should respond_to(:[])
111
+ end
112
+
113
+ it "should respond to :[]=" do
114
+ Surface.should respond_to(:[]=)
115
+ end
116
+
117
+ it "should allow setting resources" do
118
+ s = Surface.load(panda)
119
+ Surface["panda"] = s
120
+ Surface["panda"].should == s
121
+ end
122
+
123
+ it "should reject non-Surface resources" do
124
+ lambda { Surface["foo"] = "bar" }.should raise_error(TypeError)
125
+ end
126
+
127
+ it "should autoload images as Surface instances" do
128
+ unless( Rubygame::VERSIONS[:sdl_image] )
129
+ raise "Can't test image loading, no SDL_image installed."
130
+ end
131
+
132
+ Surface["panda.png"].should be_instance_of(Surface)
133
+ end
134
+
135
+ it "should return nil for nonexisting files" do
136
+ unless( Rubygame::VERSIONS[:sdl_image] )
137
+ raise "Can't test image loading, no SDL_image installed."
138
+ end
139
+
140
+ Surface["foobar.png"].should be_nil
141
+ end
142
+
143
+ it "should set names of autoload Surfaces" do
144
+ unless( Rubygame::VERSIONS[:sdl_image] )
145
+ raise "Can't test image loading, no SDL_image installed."
146
+ end
147
+
148
+ Surface["panda.png"].name.should == "panda.png"
149
+ end
150
+ end
151
+
152
+
153
+
154
+ describe Surface, "(blit)" do
155
+ before(:each) do
156
+ Rubygame.init()
157
+ @screen = Screen.new([100,100])
158
+ @surface = Surface.new([100,100])
159
+ end
160
+
161
+ after(:each) do
162
+ Rubygame.quit
163
+ end
164
+
165
+ it "should raise TypeError when #blit target is not a Surface" do
166
+ lambda {
167
+ @surface.blit("not a surface", [0,0])
168
+ }.should raise_error(TypeError)
169
+ end
170
+
171
+ it "should raise TypeError when #blit dest is not an Array" do
172
+ lambda {
173
+ @surface.blit(@screen, "foo")
174
+ }.should raise_error(TypeError)
175
+ end
176
+
177
+ it "should raise TypeError when #blit src is not an Array" do
178
+ lambda {
179
+ @surface.blit(@screen, [0,0], "foo")
180
+ }.should raise_error(TypeError)
181
+ end
182
+ end
183
+
184
+
185
+
186
+ describe Surface, "(fill)" do
187
+ before(:each) do
188
+ Rubygame.init()
189
+ @surface = Surface.new([100,100])
190
+ end
191
+
192
+ after(:each) do
193
+ Rubygame.quit
194
+ end
195
+
196
+ it "should raise TypeError when #fill color is not an Array" do
197
+ lambda {
198
+ @surface.fill(nil)
199
+ }.should raise_error(TypeError)
200
+ end
201
+
202
+ it "should raise TypeError when #fill color is an Array of non-Numerics" do
203
+ lambda {
204
+ @surface.fill(["non", "numeric", "members"])
205
+ }.should raise_error(TypeError)
206
+ end
207
+
208
+ it "should raise ArgumentError when #fill color is too short" do
209
+ lambda {
210
+ @surface.fill([0xff, 0xff])
211
+ }.should raise_error(TypeError)
212
+ end
213
+
214
+ it "should raise TypeError when #fill rect is not an Array" do
215
+ lambda {
216
+ @surface.fill([0xff, 0xff, 0xff], "not_an_array")
217
+ }.should raise_error(TypeError)
218
+ end
219
+ end
data/test/test_crop.rb ADDED
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ puts \
4
+ "This test is almost certainly not correct, and needs a lot of improvement.
5
+ Don't trust the results too much.
6
+ ~~~~~~~~~~"
7
+
8
+ require "rubygame"
9
+
10
+ screen = Rubygame::Screen.set_mode([200,200])
11
+ surf = Rubygame::Surface.new([100,100]).fill([0,0,255])
12
+ widesurf = Rubygame::Surface.new([250,100]).fill([0,0,255])
13
+ tallsurf = Rubygame::Surface.new([100,250]).fill([0,0,255])
14
+ bigsurf = Rubygame::Surface.new([250,250]).fill([0,0,255])
15
+
16
+ tests = [
17
+ [surf,[
18
+ [50,50], # fully inside
19
+ [-50,0], # off left
20
+ [0,-50], # off top
21
+ [150,0], # off right
22
+ [0,150], # off bottom
23
+ [-50,-50], # off top-left
24
+ [150,-50], # off top-right
25
+ [150,150], # off bottom-right
26
+ [-50,150], # off bottom-left
27
+ ]],
28
+ [widesurf,[[-25,50]],], # off left and right
29
+ [tallsurf,[[50,-25]],], # off top and bottom
30
+ [bigsurf,[[-25,-25]],], # off left, top, right, bottom
31
+ ]
32
+
33
+ tests.each{ |test|
34
+ test[1].each{ |pos|
35
+ x = [0,pos[0]].max
36
+ y = [0,pos[1]].max
37
+ w = [pos[0]+test[0].w,200].min - x
38
+ h = [pos[1]+test[0].h,200].min - y
39
+ if (rect = test[0].blit(screen,pos)) == [x,y,w,h]
40
+ puts "Correct! %s"%rect
41
+ else
42
+ puts "Incorrect! %s should have been [%d,%d,%d,%d]"%[rect,x,y,w,h]
43
+ end
44
+ }
45
+ }
data/test/test_rect.rb ADDED
@@ -0,0 +1,841 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # This program is PUBLIC DOMAIN.
4
+ # It is distributed in the hope that it will be useful,
5
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
6
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7
+
8
+
9
+ # This is a unit test suite for Rubygame Rects.
10
+ # However. it is BY NO MEANS COMPREHENSIVE.
11
+ #
12
+ # If you have the time and motivation, please consider contributing
13
+ # a few more tests to improve this suite.
14
+ #
15
+ # The following methods (of Rect if unspecified) have NO TESTS AT ALL:
16
+ # new_from_object
17
+ # collide_hash
18
+ # collide_hash_all
19
+ # collide_array
20
+ # collide_array_all
21
+ # collide_point?
22
+ # collide_rect?
23
+ # contain?
24
+ # inflate!
25
+ # move
26
+ # move!
27
+ # normalize
28
+ # normalize!
29
+ # union_all
30
+ # union_all!
31
+ # Surface#make_rect
32
+ #
33
+ # The following methods (of Rect if unspecified) have NOT ENOUGH TESTS:
34
+ # union
35
+ # union!
36
+ #
37
+ # The following methods (of Rect if unspecified) have ENOUGH TESTS:
38
+ # initialize
39
+ # to_s
40
+ # inspect
41
+ # []
42
+ # []=
43
+ # x
44
+ # y
45
+ # w
46
+ # h
47
+ # width
48
+ # height
49
+ # size
50
+ # left
51
+ # top
52
+ # right
53
+ # bottom
54
+ # center
55
+ # centerx
56
+ # centery
57
+ # topleft
58
+ # topright
59
+ # bottomleft
60
+ # bottomright
61
+ # midleft
62
+ # midtop
63
+ # midright
64
+ # midbottomX
65
+ # clip
66
+ # clip!
67
+ #
68
+
69
+ # --
70
+ # Table of Contents:
71
+ # INITIALIZATION
72
+ # CONVERSION
73
+ # PRIMARY ATTRIBUTES
74
+ # SIDES
75
+ # CENTERS
76
+ # CORNERS
77
+ # MIDPOINTS
78
+ # CLAMP
79
+ # INFLATE
80
+ # CLIP
81
+ # ++
82
+
83
+
84
+ require "test/unit"
85
+ require "rubygame"
86
+ Rect = Rubygame::Rect
87
+
88
+ # A class to test Rect.new_from_object()'s ability to extract a Rect from
89
+ # an object which responds to :rect
90
+ class RectParent
91
+ attr_reader :rect
92
+ def initialize(r)
93
+ @rect = r
94
+ end
95
+ end
96
+
97
+
98
+ class TC_Rect < Test::Unit::TestCase
99
+
100
+ # --
101
+ # INITALIZATION
102
+ # ++
103
+
104
+ # Test init'n from 1 Array with 4 entries
105
+ def test_new1_arr
106
+ rect = Rect.new([3,5,20,40])
107
+ assert_equal([3,5,20,40],rect)
108
+ end
109
+
110
+ # Test init'n from 1 Rect
111
+ def test_new1_rect
112
+ base = Rect.new([3,5,20,40])
113
+ rect = Rect.new(base)
114
+ assert_equal([3,5,20,40],rect)
115
+ end
116
+
117
+ # Test init'n from 2 Arrays with 2 entries each
118
+ def test_new2
119
+ rect = Rect.new([3,5],[20,40])
120
+ assert_equal([3,5,20,40],rect)
121
+ end
122
+
123
+ # Test init'n from 4 Numerics
124
+ def test_new4
125
+ rect = Rect.new(3,5,20,40)
126
+ assert_equal([3,5,20,40],rect)
127
+ end
128
+
129
+ # Test #new_from_object with a Rect
130
+ def test_nfo_rect
131
+ rect = Rect.new_from_object(Rect.new(3,5,20,40))
132
+ assert_equal([3,5,20,40],rect)
133
+ assert_kind_of(Rect,rect)
134
+ end
135
+
136
+ # Test #new_from_object with an Array
137
+ def test_nfo_rect
138
+ rect = Rect.new_from_object([3,5,20,40])
139
+ assert_equal([3,5,20,40],rect)
140
+ assert_kind_of(Rect,rect)
141
+ end
142
+
143
+ # Test #new_from_object with a .rect attribute which is a Rect
144
+ def test_nfo_rect_attr_rect
145
+
146
+ rect = Rect.new_from_object(RectParent.new(Rect.new(3,5,20,40)))
147
+ assert_equal([3,5,20,40],rect)
148
+ assert_kind_of(Rect,rect)
149
+ end
150
+
151
+ # Test #new_from_object with a .rect attribute which is an Array
152
+ def test_nfo_rect_attr_rect
153
+ rect = Rect.new_from_object(RectParent.new([3,5,20,40]))
154
+ assert_equal([3,5,20,40],rect)
155
+ assert_kind_of(Rect,rect)
156
+ end
157
+
158
+ # --
159
+ # CONVERSION
160
+ # ++
161
+
162
+ # Test that output from #to_s is proper with a Rect of Integers
163
+ def test_to_s_int
164
+ rect = Rect.new(3,5,20,40)
165
+ assert_equal("#<Rect [3,5,20,40]>",rect.to_s)
166
+ end
167
+
168
+ # Test that output from #to_s is proper with a Rect of Floats
169
+ def test_to_s_float
170
+ rect = Rect.new(3.15,5.2,20.3,40.5)
171
+ assert_equal("#<Rect [3.15,5.2,20.3,40.5]>",rect.to_s)
172
+ end
173
+
174
+ # Test that output from #inspect is proper with a Rect of Integers
175
+ def test_inspect_int
176
+ rect = Rect.new(3,5,20,40)
177
+ assert_equal("#<Rect:#{rect.object_id} [3,5,20,40]>",rect.inspect)
178
+ end
179
+
180
+ # Test that output from #inspect is proper with a Rect of Floats
181
+ def test_inspect_float
182
+ rect = Rect.new(3.15,5.2,20.3,40.5)
183
+ assert_equal("#<Rect:#{rect.object_id} [3.15,5.2,20.3,40.5]>",rect.inspect)
184
+ end
185
+
186
+ # --
187
+ # PRIMARY ATTRIBUTES
188
+ # ++
189
+
190
+ def test_x
191
+ rect = Rect.new(3,5,20,40)
192
+ assert_equal(3,rect.x)
193
+ end
194
+
195
+ def test_set_x
196
+ rect = Rect.new(3,5,20,40)
197
+ rect.x=9
198
+ assert_equal([9,5,20,40],rect)
199
+ end
200
+
201
+ def test_index_0
202
+ rect = Rect.new(3,5,20,40)
203
+ assert_equal(3,rect[0])
204
+ end
205
+
206
+ def test_set_index_0
207
+ rect = Rect.new(3,5,20,40)
208
+ rect[0]=9
209
+ assert_equal([9,5,20,40],rect)
210
+ end
211
+
212
+
213
+ def test_y
214
+ rect = Rect.new(3,5,20,40)
215
+ assert_equal(5,rect.y)
216
+ end
217
+
218
+ def test_set_y
219
+ rect = Rect.new(3,5,20,40)
220
+ rect.y=23
221
+ assert_equal([3,23,20,40],rect)
222
+ end
223
+
224
+ def test_index_1
225
+ rect = Rect.new(3,5,20,40)
226
+ assert_equal(5,rect[1])
227
+ end
228
+
229
+ def test_set_index_1
230
+ rect = Rect.new(3,5,20,40)
231
+ rect[1]=9
232
+ assert_equal([3,9,20,40],rect)
233
+ end
234
+
235
+
236
+ def test_w
237
+ rect = Rect.new(3,5,20,40)
238
+ assert_equal(20,rect.w)
239
+ end
240
+
241
+ def test_set_w
242
+ rect = Rect.new(3,5,20,40)
243
+ rect.w=9
244
+ assert_equal([3,5,9,40],rect)
245
+ end
246
+
247
+ def test_width
248
+ rect = Rect.new(3,5,20,40)
249
+ assert_equal(20,rect.width)
250
+ end
251
+
252
+ def test_set_width
253
+ rect = Rect.new(3,5,20,40)
254
+ rect.width=9
255
+ assert_equal([3,5,9,40],rect)
256
+ end
257
+
258
+ def test_index_2
259
+ rect = Rect.new(3,5,20,40)
260
+ assert_equal(20,rect[2])
261
+ end
262
+
263
+ def test_set_index_2
264
+ rect = Rect.new(3,5,20,40)
265
+ rect[2]=9
266
+ assert_equal([3,5,9,40],rect)
267
+ end
268
+
269
+
270
+ def test_h
271
+ rect = Rect.new(3,5,20,40)
272
+ assert_equal(40,rect.h)
273
+ end
274
+
275
+ def test_set_h
276
+ rect = Rect.new(3,5,20,40)
277
+ rect.h=11
278
+ assert_equal([3,5,20,11],rect)
279
+ end
280
+
281
+ def test_height
282
+ rect = Rect.new(3,5,20,40)
283
+ assert_equal(40,rect.height)
284
+ end
285
+
286
+ def test_set_height
287
+ rect = Rect.new(3,5,20,40)
288
+ rect.height=9
289
+ assert_equal([3,5,20,9],rect)
290
+ end
291
+
292
+
293
+ def test_size
294
+ rect = Rect.new(3,5,20,40)
295
+ assert_equal([20,40],rect.size)
296
+ end
297
+
298
+ def test_set_size
299
+ rect = Rect.new(3,5,20,40)
300
+ rect.size = [9,5]
301
+ assert_equal([3,5,9,5],rect)
302
+ end
303
+
304
+ # --
305
+ # SIDES
306
+ # ++
307
+
308
+ def test_left
309
+ rect = Rect.new(3,5,20,40)
310
+ assert_equal(3,rect.left)
311
+ end
312
+
313
+ def test_set_left
314
+ rect = Rect.new(3,5,20,40)
315
+ rect.left=9
316
+ assert_equal([9,5,20,40],rect)
317
+ end
318
+
319
+
320
+ def test_top
321
+ rect = Rect.new(3,5,20,40)
322
+ assert_equal(5,rect.top)
323
+ end
324
+
325
+ def test_set_top
326
+ rect = Rect.new(3,5,20,40)
327
+ rect.top=9
328
+ assert_equal([3,9,20,40],rect)
329
+ end
330
+
331
+
332
+ def test_right
333
+ rect = Rect.new(3,5,20,40)
334
+ assert_equal(23,rect.right)
335
+ end
336
+
337
+ def test_set_right
338
+ rect = Rect.new(3,5,20,40)
339
+ rect.right = 42
340
+ assert_equal([22,5,20,40],rect)
341
+ end
342
+
343
+
344
+ def test_bottom
345
+ rect = Rect.new(3,5,20,40)
346
+ assert_equal(45,rect.bottom)
347
+ end
348
+
349
+ def test_set_bottom
350
+ rect = Rect.new(3,5,20,40)
351
+ rect.bottom = 42
352
+ assert_equal([3,2,20,40],rect)
353
+ end
354
+
355
+ # --
356
+ # CENTERS
357
+ # ++
358
+
359
+ def test_center
360
+ rect = Rect.new(3,5,20,40)
361
+ assert_equal([13,25],rect.center)
362
+ end
363
+
364
+ def test_set_center
365
+ rect = Rect.new(3,5,20,40)
366
+ rect.center = [16,42]
367
+ assert_equal([6,22,20,40],rect)
368
+ end
369
+
370
+ def test_centerx
371
+ rect = Rect.new(3,5,20,40)
372
+ assert_equal(13,rect.centerx)
373
+ end
374
+
375
+ def test_set_centerx
376
+ rect = Rect.new(3,5,20,40)
377
+ rect.centerx = 16
378
+ assert_equal([6,5,20,40],rect)
379
+ end
380
+
381
+ def test_centery
382
+ rect = Rect.new(3,5,20,40)
383
+ assert_equal(13,rect.centerx)
384
+ end
385
+
386
+ def test_set_centery
387
+ rect = Rect.new(3,5,20,40)
388
+ rect.centery = 43
389
+ assert_equal([3,23,20,40],rect)
390
+ end
391
+
392
+ # --
393
+ # CORNERS
394
+ # ++
395
+
396
+ def test_topleft
397
+ rect = Rect.new(3,5,20,40)
398
+ assert_equal([3,5],rect.topleft)
399
+ end
400
+
401
+ def test_set_topleft
402
+ rect = Rect.new(3,5,20,40)
403
+ rect.topleft = [6,2]
404
+ assert_equal([6,2,20,40],rect)
405
+ end
406
+
407
+ def test_tl
408
+ rect = Rect.new(3,5,20,40)
409
+ assert_equal([3,5],rect.tl)
410
+ end
411
+
412
+ def test_set_tl
413
+ rect = Rect.new(3,5,20,40)
414
+ rect.tl = [6,2]
415
+ assert_equal([6,2,20,40],rect)
416
+ end
417
+
418
+
419
+ def test_topright
420
+ rect = Rect.new(3,5,20,40)
421
+ assert_equal([23,5],rect.topright)
422
+ end
423
+
424
+ def test_set_topright
425
+ rect = Rect.new(3,5,20,40)
426
+ rect.topright = [26,2]
427
+ assert_equal([6,2,20,40],rect)
428
+ end
429
+
430
+ def test_tr
431
+ rect = Rect.new(3,5,20,40)
432
+ assert_equal([23,5],rect.tr)
433
+ end
434
+
435
+ def test_set_tr
436
+ rect = Rect.new(3,5,20,40)
437
+ rect.tr = [26,2]
438
+ assert_equal([6,2,20,40],rect)
439
+ end
440
+
441
+
442
+ def test_bottomright
443
+ rect = Rect.new(3,5,20,40)
444
+ assert_equal([23,45],rect.bottomright)
445
+ end
446
+
447
+ def test_set_bottomright
448
+ rect = Rect.new(3,5,20,40)
449
+ rect.bottomright = [26,42]
450
+ assert_equal([6,2,20,40],rect)
451
+ end
452
+
453
+ def test_br
454
+ rect = Rect.new(3,5,20,40)
455
+ assert_equal([23,45],rect.br)
456
+ end
457
+
458
+ def test_set_br
459
+ rect = Rect.new(3,5,20,40)
460
+ rect.br = [26,42]
461
+ assert_equal([6,2,20,40],rect)
462
+ end
463
+
464
+
465
+ def test_bottomleft
466
+ rect = Rect.new(3,5,20,40)
467
+ assert_equal([3,45],rect.bottomleft)
468
+ end
469
+
470
+ def test_set_bottomleft
471
+ rect = Rect.new(3,5,20,40)
472
+ rect.bottomleft = [6,42]
473
+ assert_equal([6,2,20,40],rect)
474
+ end
475
+
476
+ def test_bl
477
+ rect = Rect.new(3,5,20,40)
478
+ assert_equal([3,45],rect.bl)
479
+ end
480
+
481
+ def test_set_bl
482
+ rect = Rect.new(3,5,20,40)
483
+ rect.bl = [6,42]
484
+ assert_equal([6,2,20,40],rect)
485
+ end
486
+
487
+ # --
488
+ # MIDPOINTS
489
+ # ++
490
+
491
+ def test_midleft
492
+ rect = Rect.new(3,5,20,40)
493
+ assert_equal([3,25],rect.midleft)
494
+ end
495
+
496
+ def test_set_midleft
497
+ rect = Rect.new(3,5,20,40)
498
+ rect.midleft = [6,22]
499
+ assert_equal([6,2,20,40],rect)
500
+ end
501
+
502
+ def test_ml
503
+ rect = Rect.new(3,5,20,40)
504
+ assert_equal([3,25],rect.ml)
505
+ end
506
+
507
+ def test_set_ml
508
+ rect = Rect.new(3,5,20,40)
509
+ rect.ml = [6,22]
510
+ assert_equal([6,2,20,40],rect)
511
+ end
512
+
513
+
514
+ def test_midtop
515
+ rect = Rect.new(3,5,20,40)
516
+ assert_equal([13,5],rect.midtop)
517
+ end
518
+
519
+ def test_set_midtop
520
+ rect = Rect.new(3,5,20,40)
521
+ rect.midtop = [16,2]
522
+ assert_equal([6,2,20,40],rect)
523
+ end
524
+
525
+ def test_mt
526
+ rect = Rect.new(3,5,20,40)
527
+ assert_equal([13,5],rect.mt)
528
+ end
529
+
530
+ def test_set_mt
531
+ rect = Rect.new(3,5,20,40)
532
+ rect.mt = [16,2]
533
+ assert_equal([6,2,20,40],rect)
534
+ end
535
+
536
+
537
+ def test_midright
538
+ rect = Rect.new(3,5,20,40)
539
+ assert_equal([23,25],rect.midright)
540
+ end
541
+
542
+ def test_set_midright
543
+ rect = Rect.new(3,5,20,40)
544
+ rect.midright = [26,22]
545
+ assert_equal([6,2,20,40],rect)
546
+ end
547
+
548
+ def test_mr
549
+ rect = Rect.new(3,5,20,40)
550
+ assert_equal([23,25],rect.mr)
551
+ end
552
+
553
+ def test_set_mr
554
+ rect = Rect.new(3,5,20,40)
555
+ rect.mr = [26,22]
556
+ assert_equal([6,2,20,40],rect)
557
+ end
558
+
559
+
560
+ def test_midbottom
561
+ rect = Rect.new(3,5,20,40)
562
+ assert_equal([13,45],rect.midbottom)
563
+ end
564
+
565
+ def test_set_midbottom
566
+ rect = Rect.new(3,5,20,40)
567
+ rect.midbottom = [16,42]
568
+ assert_equal([6,2,20,40],rect)
569
+ end
570
+
571
+ def test_mb
572
+ rect = Rect.new(3,5,20,40)
573
+ assert_equal([13,45],rect.mb)
574
+ end
575
+
576
+ def test_set_mb
577
+ rect = Rect.new(3,5,20,40)
578
+ rect.mb = [16,42]
579
+ assert_equal([6,2,20,40],rect)
580
+ end
581
+
582
+
583
+ # --
584
+ # CLAMP
585
+ # ++
586
+
587
+ # Clamp a rect that fits.
588
+ def test_clamp_fits
589
+ rect_a = Rect.new(0,0,20,20)
590
+ rect_b = Rect.new(40,10,6,6)
591
+ assert_equal([14,10,6,6],rect_b.clamp(rect_a))
592
+ end
593
+
594
+ # Clamp a rect that fits, but is already contained (no effect)
595
+ def test_clamp_contained
596
+ rect_a = Rect.new(0,0,20,20)
597
+ rect_b = Rect.new(10,10,6,6)
598
+ assert_equal([10,10,6,6],rect_b.clamp(rect_a))
599
+ end
600
+
601
+ # Clamp a rect that is too wide.
602
+ def test_clamp_wide
603
+ rect_a = Rect.new(0,0,20,20)
604
+ rect_b = Rect.new(40,10,30,6)
605
+ assert_equal([-5,10,30,6],rect_b.clamp(rect_a))
606
+ end
607
+
608
+ # Clamp a rect that is too tall.
609
+ def test_clamp_tall
610
+ rect_a = Rect.new(0,0,20,20)
611
+ rect_b = Rect.new(10,40,6,30)
612
+ assert_equal([10,-5,6,30],rect_b.clamp(rect_a))
613
+ end
614
+
615
+ # Clamp a rect that is both too wide and too tall.
616
+ def test_clamp_nofit
617
+ rect_a = Rect.new(0,0,20,20)
618
+ rect_b = Rect.new(10,40,30,30)
619
+ assert_equal([-5,-5,30,30],rect_b.clamp(rect_a))
620
+ end
621
+
622
+
623
+ # --
624
+ # INFLATE
625
+ # ++
626
+
627
+ # Inflate a rect by zero on x and y
628
+ def test_inflate_zero_x_zero_y
629
+ assert_equal([0,0,20,20],
630
+ Rect.new(0,0,20,20).inflate(0,0))
631
+ end
632
+
633
+ # Inflate a rect by an even positive integer on x and y
634
+ def test_inflate_evenposint_x_evenposint_y
635
+ assert_equal([-2,-3,24,26],
636
+ Rect.new(0,0,20,20).inflate(4,6))
637
+ end
638
+
639
+ # Inflate a rect by an odd negative integer on x and y
640
+ # NOTE: Pygame would give a result of [-1,-3,23,25].
641
+ def test_inflate_oddposint_x_oddposint_y
642
+ assert_equal([-1,-2,23,25],
643
+ Rect.new(0,0,20,20).inflate(3,5))
644
+ end
645
+
646
+ # Inflate a rect by an even negative integer on x and y
647
+ def test_inflate_evennegint_x_evennegint_y
648
+ assert_equal([2,3,16,14],
649
+ Rect.new(0,0,20,20).inflate(-4,-6))
650
+ end
651
+
652
+ # Inflate a rect by an odd negative integer on x and y
653
+ # NOTE: Pygame would give a result of [1,2,17,15].
654
+ def test_inflate_oddnegint_x_oddnegint_y
655
+ assert_equal([2,3,17,15],
656
+ Rect.new(0,0,20,20).inflate(-3,-5))
657
+ end
658
+
659
+ # --
660
+ # COLLIDE_RECT
661
+ # ++
662
+
663
+ # Test collision between rects which do not collide on either axis.
664
+ def test_collide_rect_nox_noy
665
+ r1 = Rect.new(0,0,10,10)
666
+ r2 = Rect.new(20,20,10,10)
667
+ assert(!(r1.collide_rect?(r2)), "r1 should not collide with r2")
668
+ assert(!(r2.collide_rect?(r1)), "r2 should not collide with r1")
669
+ end
670
+
671
+ # Test collision between rects which overlap on the X axis only
672
+ # (i.e. they would intersect if they were at the same vertical position).
673
+ def test_collide_rect_overlapx_noy
674
+ r1 = Rect.new(0,0,10,10)
675
+ r2 = Rect.new(5,30,10,10)
676
+ assert(!(r1.collide_rect?(r2)), "r1 should not collide with r2")
677
+ assert(!(r2.collide_rect?(r1)), "r2 should not collide with r1")
678
+ end
679
+
680
+ # Test collision between rects which overlap on the Y axis only
681
+ # (i.e. they would intersect if they were at the same horizontal position).
682
+ def test_collide_rect_nox_overlapy
683
+ r1 = Rect.new(0,0,10,10)
684
+ r2 = Rect.new(20,5,10,10)
685
+ assert(!(r1.collide_rect?(r2)), "r1 should not collide with r2")
686
+ assert(!(r2.collide_rect?(r1)), "r2 should not collide with r1")
687
+ end
688
+
689
+ # Test collision between rects which overlap on both axes.
690
+ def test_collide_rect_overlapx_overlapy
691
+ r1 = Rect.new(0,0,10,10)
692
+ r2 = Rect.new(5,5,10,10)
693
+ assert(r1.collide_rect?(r2), "r1 should collide with r2")
694
+ assert(r2.collide_rect?(r1), "r2 should collide with r1")
695
+ end
696
+
697
+ # Test collision between rects, one containing the other on both axes.
698
+ def test_collide_rect_conx_cony
699
+ r1 = Rect.new(0,0,10,10)
700
+ r2 = Rect.new(5,5,4,4)
701
+ assert(r1.collide_rect?(r2), "r1 should collide with r2")
702
+ assert(r2.collide_rect?(r1), "r2 should collide with r1")
703
+ end
704
+
705
+ # Test collision between rects touching but not overlapping on X axis only.
706
+ def test_collide_rect_touchx_noy
707
+ r1 = Rect.new(0,0,10,10)
708
+ r2 = Rect.new(10,0,10,10)
709
+ assert(r1.collide_rect?(r2), "r1 should collide with r2")
710
+ assert(r2.collide_rect?(r1), "r2 should collide with r1")
711
+ end
712
+
713
+ # Test collision between rects touching but not overlapping on Y axis only.
714
+ def test_collide_rect_nox_touchy
715
+ r1 = Rect.new(0,0,10,10)
716
+ r2 = Rect.new(0,10,10,10)
717
+ assert(r1.collide_rect?(r2), "r1 should collide with r2")
718
+ assert(r2.collide_rect?(r1), "r2 should collide with r1")
719
+ end
720
+
721
+ # Test collision between rects touching but not overlapping on both axes.
722
+ # (i.e. corners are touching)
723
+ def test_collide_rect_touchx_touchy
724
+ r1 = Rect.new(0,0,10,10)
725
+ r2 = Rect.new(10,10,10,10)
726
+ assert(r1.collide_rect?(r2), "r1 should collide with r2")
727
+ assert(r2.collide_rect?(r1), "r2 should collide with r1")
728
+ end
729
+
730
+ # --
731
+ # UNION
732
+ # ++
733
+
734
+ # Test union between two non-overlapping rects.
735
+ def test_union_separate
736
+ r1 = Rect.new([0,0,10,10])
737
+ r2 = Rect.new([20,20,10,10])
738
+ assert_equal([0,0,30,30], r1.union(r2))
739
+ assert_equal([0,0,30,30], r2.union(r1))
740
+ assert_equal(r1.union(r2), r2.union(r1))
741
+ end
742
+
743
+ # Test union between two overlapping rects.
744
+ def test_union_overlap
745
+ r1 = Rect.new([0,0,10,10])
746
+ r2 = Rect.new([5,5,10,10])
747
+ assert_equal([0,0,15,15], r1.union(r2))
748
+ assert_equal([0,0,15,15], r2.union(r1))
749
+ end
750
+
751
+ # Test union between a rect and another rect which it contains.
752
+ def test_union_contained
753
+ r1 = Rect.new([0,0,20,20])
754
+ r2 = Rect.new([5,5,10,10])
755
+ assert_equal([0,0,20,20], r1.union(r2))
756
+ assert_equal([0,0,20,20], r2.union(r1))
757
+ end
758
+
759
+ # Test union between two identical rects.
760
+ def test_union_identical
761
+ r1 = Rect.new([0,0,20,20])
762
+ r2 = Rect.new([0,0,20,20])
763
+ assert_equal([0,0,20,20], r1.union(r2))
764
+ assert_equal([0,0,20,20], r2.union(r1))
765
+ end
766
+
767
+ # --
768
+ # CLIP
769
+ # ++
770
+
771
+ def test_clip_offleft
772
+ r1 = Rect.new([2,5,10,10])
773
+ r2 = Rect.new([4,1,20,20])
774
+ should_be = Rect.new([4,5,8,10])
775
+ assert_equal(should_be, r1.clip(r2))
776
+ assert_equal(should_be, r2.clip(r1))
777
+ end
778
+
779
+ def test_clip_offright
780
+ r1 = Rect.new([15,5,10,10])
781
+ r2 = Rect.new([4,1,20,20])
782
+ should_be = Rect.new([15,5,9,10])
783
+ assert_equal(should_be, r1.clip(r2))
784
+ assert_equal(should_be, r2.clip(r1))
785
+ end
786
+
787
+ def test_clip_offtop
788
+ r1 = Rect.new([5,2,10,10])
789
+ r2 = Rect.new([1,4,20,20])
790
+ should_be = Rect.new([5,4,10,8])
791
+ assert_equal(should_be, r1.clip(r2))
792
+ assert_equal(should_be, r2.clip(r1))
793
+ end
794
+
795
+ def test_clip_offbottom
796
+ r1 = Rect.new([5,15,10,10])
797
+ r2 = Rect.new([1,4,20,20])
798
+ should_be = Rect.new([5,15,10,9])
799
+ assert_equal(should_be, r1.clip(r2))
800
+ assert_equal(should_be, r2.clip(r1))
801
+ end
802
+
803
+ def test_clip_offleftandright
804
+ r1 = Rect.new([2,5,30,10])
805
+ r2 = Rect.new([4,1,20,20])
806
+ should_be = Rect.new([4,5,20,10])
807
+ assert_equal(should_be, r1.clip(r2))
808
+ assert_equal(should_be, r2.clip(r1))
809
+ end
810
+
811
+ def test_clip_offtopandbottom
812
+ r1 = Rect.new([2,5,10,30])
813
+ r2 = Rect.new([4,1,20,20])
814
+ should_be = Rect.new([4,5,8,16])
815
+ assert_equal(should_be, r1.clip(r2))
816
+ assert_equal(should_be, r2.clip(r1))
817
+ end
818
+
819
+ def test_clip_offallsides
820
+ r1 = Rect.new([2,5,30,30])
821
+ r2 = Rect.new([4,1,20,20])
822
+ should_be = Rect.new([4,5,20,16])
823
+ assert_equal(should_be, r1.clip(r2))
824
+ assert_equal(should_be, r2.clip(r1))
825
+ end
826
+
827
+ def test_clip_contained
828
+ r1 = Rect.new([6,3,5,5])
829
+ r2 = Rect.new([4,1,20,20])
830
+ should_be = r1
831
+ assert_equal(should_be, r1.clip(r2))
832
+ assert_equal(should_be, r2.clip(r1))
833
+ end
834
+
835
+ def test_clip_nooverlap
836
+ r1 = Rect.new([6,3,5,5])
837
+ r2 = Rect.new([20,1,20,20])
838
+ assert_equal(Rect.new([6,3,0,0]), r1.clip(r2))
839
+ assert_equal(Rect.new([20,1,0,0]), r2.clip(r1))
840
+ end
841
+ end