gosling 2.1.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ describe MatrixCache do
2
+ describe '.get' do
3
+ context 'when there are no Mat3 objects available' do
4
+ before do
5
+ MatrixCache.instance.clear
6
+ end
7
+
8
+ it 'creates a new Mat3 and returns it' do
9
+ new_matrix = Snow::Vec3.new
10
+ expect(Snow::Mat3).to receive(:new).and_return(new_matrix)
11
+ m = MatrixCache.instance.get
12
+ expect(m).to equal(new_matrix)
13
+ end
14
+ end
15
+ end
16
+
17
+ describe '.recycle' do
18
+ it 'resets the Mat3' do
19
+ m = Snow::Mat3[1, 2, 3, 4, 5, 6, 7, 8, 9]
20
+ MatrixCache.instance.recycle(m)
21
+ [0, 4, 8].each { |i| expect(m[i]).to eq(1) }
22
+ [1, 2, 3, 5, 6, 7].each { |i| expect(m[i]).to eq(0) }
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,22 @@
1
+ describe Object do
2
+ before(:all) do
3
+ @test_object = []
4
+ end
5
+
6
+ describe '.unfreeze' do
7
+ it 'allows the object to be edited after being frozen' do
8
+ @test_object.freeze
9
+ expect { @test_object << 'a' }.to raise_error(RuntimeError)
10
+ @test_object.unfreeze
11
+ expect { @test_object << 'a' }.not_to raise_error
12
+ end
13
+
14
+ it 'works even after multiple freeze/unfreeze cycles' do
15
+ 3.times do
16
+ @test_object.freeze
17
+ @test_object.unfreeze
18
+ end
19
+ expect { @test_object << 'a' }.not_to raise_error
20
+ end
21
+ end
22
+ end
data/spec/polygon_spec.rb CHANGED
@@ -111,6 +111,33 @@ describe Gosling::Polygon do
111
111
  end
112
112
  end
113
113
 
114
+ describe '#set_vertices_rect' do
115
+ it 'accepts two positive integers, width and height' do
116
+ polygon = Gosling::Polygon.new(@window)
117
+
118
+ expect { polygon.set_vertices_rect(1, 1) }.not_to raise_error
119
+ expect { polygon.set_vertices_rect(2, 3) }.not_to raise_error
120
+ expect { polygon.set_vertices_rect(5, 8) }.not_to raise_error
121
+ expect { polygon.set_vertices_rect(100000, 100000) }.not_to raise_error
122
+
123
+ expect { polygon.set_vertices_rect('two', 2) }.to raise_error(ArgumentError)
124
+ expect { polygon.set_vertices_rect(3, :three) }.to raise_error(ArgumentError)
125
+ expect { polygon.set_vertices_rect(-1, 1) }.to raise_error(ArgumentError)
126
+ expect { polygon.set_vertices_rect(1, 0) }.to raise_error(ArgumentError)
127
+ end
128
+
129
+ it 'sets the vertices to form a square with the given width and height' do
130
+ polygon = Gosling::Polygon.new(@window)
131
+ polygon.set_vertices_rect(13, 21)
132
+ expect(polygon.get_vertices).to match_array([
133
+ Snow::Vec3[0, 0, 0],
134
+ Snow::Vec3[13, 0, 0],
135
+ Snow::Vec3[13, 21, 0],
136
+ Snow::Vec3[0, 21, 0]
137
+ ])
138
+ end
139
+ end
140
+
114
141
  describe '#get_global_vertices' do
115
142
  before(:all) do
116
143
  @global_polygon = Gosling::Polygon.new(@window)
data/spec/spec_helper.rb CHANGED
@@ -1 +1,3 @@
1
1
  require_relative '../lib/gosling.rb'
2
+
3
+ RSpec::Expectations.configuration.on_potential_false_positives = :nothing
@@ -1,5 +1,10 @@
1
1
  class TransformableThing
2
2
  include Gosling::Transformable
3
+
4
+ attr_reader :is_dirty
5
+ attr_reader :translation, :translate_is_dirty
6
+ attr_reader :center, :center_is_dirty
7
+ attr_reader :scale, :scale_is_dirty
3
8
  end
4
9
 
5
10
  describe Gosling::Transformable do
@@ -98,12 +103,40 @@ describe Gosling::Transformable do
98
103
  expect(tf.center[2]).to be == 1
99
104
  end
100
105
  end
106
+
107
+ context 'when given a block' do
108
+ it 'yields the center vector directly to the block' do
109
+ tf = TransformableThing.new
110
+ tf.center = [3, -7]
111
+
112
+ center_ref = nil
113
+ area = nil
114
+ tf.set_center do |c|
115
+ center_ref = c
116
+ area = c.x * c.y
117
+ end
118
+ expect(center_ref).to be_equal(tf.center)
119
+ expect(area).to eq(-21)
120
+ end
121
+
122
+ it 'marks the transformation as dirty' do
123
+ tf = TransformableThing.new
124
+ tf.set_center { |t| area = t.x * t.y }
125
+ expect(tf.center_is_dirty).to be true
126
+ expect(tf.is_dirty).to be true
127
+ end
128
+ end
101
129
  end
102
130
 
103
131
  describe '#set_scale' do
104
- it 'accepts a size 2 vector' do
132
+ it 'accepts a size 2 vector, an equivalent, or a single numeric value' do
105
133
  tf = TransformableThing.new
106
134
  expect { tf.scale = Snow::Vec2[1, 1] }.not_to raise_error
135
+ expect { tf.scale = [1, 1] }.not_to raise_error
136
+ expect { tf.scale = 1 }.not_to raise_error
137
+ expect { tf.set_scale([1, 1]) }.not_to raise_error
138
+ expect { tf.set_scale(1, 1) }.not_to raise_error
139
+ expect { tf.set_scale(1) }.not_to raise_error
107
140
  expect { tf.scale = :foo }.to raise_error(ArgumentError)
108
141
  end
109
142
 
@@ -113,6 +146,38 @@ describe Gosling::Transformable do
113
146
  tf.scale = v
114
147
  expect(tf.scale).to be == v
115
148
  end
149
+
150
+ context 'when given a single value' do
151
+ it 'sets the x and y scaling' do
152
+ tf = TransformableThing.new
153
+ tf.scale = 2.7
154
+ expect(tf.scale.x).to eq(2.7)
155
+ expect(tf.scale.y).to eq(2.7)
156
+ end
157
+ end
158
+
159
+ context 'when given a block' do
160
+ it 'yields the scaling vector directly to the block' do
161
+ tf = TransformableThing.new
162
+ tf.scale = [3, 2]
163
+
164
+ scale_ref = nil
165
+ area = nil
166
+ tf.set_scale do |s|
167
+ scale_ref = s
168
+ area = s.x * s.y
169
+ end
170
+ expect(scale_ref).to be_equal(tf.scale)
171
+ expect(area).to eq(6)
172
+ end
173
+
174
+ it 'marks the transformation as dirty' do
175
+ tf = TransformableThing.new
176
+ tf.set_scale { |s| area = s.x * s.y }
177
+ expect(tf.scale_is_dirty).to be true
178
+ expect(tf.is_dirty).to be true
179
+ end
180
+ end
116
181
  end
117
182
 
118
183
  describe '#set_rotation' do
@@ -159,6 +224,29 @@ describe Gosling::Transformable do
159
224
  expect(tf.translation[2]).to be == 1
160
225
  end
161
226
  end
227
+
228
+ context 'when given a block' do
229
+ it 'yields the transform vector directly to the block' do
230
+ tf = TransformableThing.new
231
+ tf.translation = [7, 11]
232
+
233
+ translation_ref = nil
234
+ area = nil
235
+ tf.set_translation do |t|
236
+ translation_ref = t
237
+ area = t.x * t.y
238
+ end
239
+ expect(translation_ref).to be_equal(tf.translation)
240
+ expect(area).to eq(77)
241
+ end
242
+
243
+ it 'marks the transformation as dirty' do
244
+ tf = TransformableThing.new
245
+ tf.set_translation { |t| area = t.x * t.y }
246
+ expect(tf.translate_is_dirty).to be true
247
+ expect(tf.is_dirty).to be true
248
+ end
249
+ end
162
250
  end
163
251
 
164
252
  describe '#to_matrix' do
@@ -244,17 +332,21 @@ describe Gosling::Transformable do
244
332
 
245
333
  describe '#transform_point' do
246
334
  it 'expects a length 3 vector' do
247
- expect { @read_only_tf.transform_point(Snow::Vec3[1, 0, 1]) }.not_to raise_error
248
- expect { @read_only_tf.transform_point(:foo) }.to raise_error(ArgumentError)
249
- expect { @read_only_tf.transform_point(nil) }.to raise_error(ArgumentError)
335
+ out = Snow::Vec3.new
336
+ expect { @read_only_tf.transform_point(Snow::Vec3[1, 0, 1], out) }.not_to raise_error
337
+ expect { @read_only_tf.transform_point(:foo, out) }.to raise_error
338
+ expect { @read_only_tf.transform_point(nil, out) }.to raise_error
250
339
  end
251
340
 
252
341
  it 'returns a length 3 vector' do
253
- result = @read_only_tf.transform_point(Snow::Vec3[1, 0, 1])
342
+ out = Snow::Vec3.new
343
+ result = @read_only_tf.transform_point(Snow::Vec3[1, 0, 1], out)
254
344
  expect(result).to be_instance_of(Snow::Vec3)
345
+ expect(result).to equal(out)
255
346
  end
256
347
 
257
348
  it 'always returns a z value of 0' do
349
+ out = Snow::Vec3.new
258
350
  [
259
351
  Snow::Vec3[1, 1, 1],
260
352
  Snow::Vec3[-1, -1, -1],
@@ -269,7 +361,8 @@ describe Gosling::Transformable do
269
361
  Snow::Vec3[33, -25, -20],
270
362
  Snow::Vec3[-46, -18, 48],
271
363
  ].each do |v|
272
- expect(@read_only_tf.transform_point(v)[2]).to be == 0
364
+ expect(@read_only_tf.transform_point(v, out)[2]).to be == 0
365
+ expect(out.z).to eq(0)
273
366
  end
274
367
  end
275
368
 
@@ -280,6 +373,7 @@ describe Gosling::Transformable do
280
373
  tf.rotation = Math::PI / 2
281
374
  tf.translation = Snow::Vec3[1024.to_r, 768.to_r, 0.to_r]
282
375
 
376
+ out = Snow::Vec3.new
283
377
  [
284
378
  [0, 0],
285
379
  [10, -20],
@@ -288,24 +382,30 @@ describe Gosling::Transformable do
288
382
  [5, 20]
289
383
  ].each do |pt|
290
384
  x, y = pt
291
- expect(tf.transform_point(Snow::Vec3[x, y, 0])).to be == Snow::Vec3[(y - 20) * 0.5 + 1024, (x - 5) * -2 + 768, 0]
385
+ expected = Snow::Vec3[(y - 20) * 0.5 + 1024, (x - 5) * -2 + 768, 0]
386
+ expect(tf.transform_point(Snow::Vec3[x, y, 0], out)).to be == expected
387
+ expect(out).to be == expected
292
388
  end
293
389
  end
294
390
  end
295
391
 
296
392
  describe '#untransform_point' do
297
393
  it 'expects a length 3 vector' do
298
- expect { @read_only_tf.untransform_point(Snow::Vec3[1, 0, 1]) }.not_to raise_error
299
- expect { @read_only_tf.untransform_point(:foo) }.to raise_error(ArgumentError)
300
- expect { @read_only_tf.untransform_point(nil) }.to raise_error(ArgumentError)
394
+ out = Snow::Vec3.new
395
+ expect { @read_only_tf.untransform_point(Snow::Vec3[1, 0, 1], out) }.not_to raise_error
396
+ expect { @read_only_tf.untransform_point(:foo, out) }.to raise_error
397
+ expect { @read_only_tf.untransform_point(nil, out) }.to raise_error
301
398
  end
302
399
 
303
400
  it 'returns a length 3 vector' do
304
- result = @read_only_tf.untransform_point(Snow::Vec3[1, 0, 1])
401
+ out = Snow::Vec3.new
402
+ result = @read_only_tf.untransform_point(Snow::Vec3[1, 0, 1], out)
305
403
  expect(result).to be_instance_of(Snow::Vec3)
404
+ expect(out).to equal(result)
306
405
  end
307
406
 
308
407
  it 'always returns a z value of 0' do
408
+ out = Snow::Vec3.new
309
409
  [
310
410
  Snow::Vec3[1, 1, 1],
311
411
  Snow::Vec3[-1, -1, -1],
@@ -320,7 +420,8 @@ describe Gosling::Transformable do
320
420
  Snow::Vec3[33, -25, -20],
321
421
  Snow::Vec3[-46, -18, 48],
322
422
  ].each do |v|
323
- expect(@read_only_tf.untransform_point(v)[2]).to be == 0
423
+ expect(@read_only_tf.untransform_point(v, out)[2]).to be == 0
424
+ expect(out.z).to be == 0
324
425
  end
325
426
  end
326
427
 
@@ -331,6 +432,7 @@ describe Gosling::Transformable do
331
432
  tf.rotation = Math::PI / 2
332
433
  tf.translation = Snow::Vec3[1024.to_r, 768.to_r, 0.to_r]
333
434
 
435
+ out = Snow::Vec3.new
334
436
  [
335
437
  [1014, 778],
336
438
  [1004, 758],
@@ -339,7 +441,9 @@ describe Gosling::Transformable do
339
441
  [768, 1024]
340
442
  ].each do |pt|
341
443
  x, y = pt
342
- expect(tf.untransform_point(Snow::Vec3[x, y, 0])).to be == Snow::Vec3[(y - 768) * -0.5 + 5, (x - 1024) * 2 + 20, 0]
444
+ expected = Snow::Vec3[(y - 768) * -0.5 + 5, (x - 1024) * 2 + 20, 0]
445
+ expect(tf.untransform_point(Snow::Vec3[x, y, 0], out)).to be == expected
446
+ expect(out).to be == expected
343
447
  end
344
448
  end
345
449
 
@@ -350,6 +454,7 @@ describe Gosling::Transformable do
350
454
  tf.rotation = Math::PI / 2
351
455
  tf.translation = Snow::Vec3[1024.to_r, 768.to_r, 0.to_r]
352
456
 
457
+ out = Snow::Vec3.new
353
458
  [
354
459
  Snow::Vec3[1, 1, 0],
355
460
  Snow::Vec3[-1, -1, 0],
@@ -364,8 +469,10 @@ describe Gosling::Transformable do
364
469
  Snow::Vec3[33, -25, 0],
365
470
  Snow::Vec3[-46, -18, 0],
366
471
  ].each do |v|
367
- expect(tf.untransform_point(tf.transform_point(v))).to be == v
368
- expect(tf.transform_point(tf.untransform_point(v))).to be == v
472
+ expect(tf.untransform_point(tf.transform_point(v, out), out)).to be == v
473
+ expect(out).to be == v
474
+ expect(tf.transform_point(tf.untransform_point(v, out), out)).to be == v
475
+ expect(out).to be == v
369
476
  end
370
477
  end
371
478
  end
@@ -0,0 +1,80 @@
1
+ class VectorCache
2
+ def cache
3
+ @cache
4
+ end
5
+ end
6
+
7
+ describe VectorCache do
8
+ describe '.get' do
9
+ context 'when there are no Vec3 objects available' do
10
+ before do
11
+ VectorCache.instance.clear
12
+ end
13
+
14
+ it 'creates a new Vec3 and returns it' do
15
+ new_vector = Snow::Vec3.new
16
+ expect(Snow::Vec3).to receive(:new).and_return(new_vector)
17
+ v = VectorCache.instance.get
18
+ expect(v).to equal(new_vector)
19
+ end
20
+ end
21
+
22
+ context 'when there are Vec3 objects available' do
23
+ before do
24
+ VectorCache.instance.clear
25
+ @recycled_vector = Snow::Vec3.new
26
+ VectorCache.instance.recycle(@recycled_vector)
27
+ end
28
+
29
+ it 'does not create a new Vec3' do
30
+ expect(Snow::Vec3).not_to receive(:new)
31
+ VectorCache.instance.get
32
+ end
33
+
34
+ it 'returns one of the cached Vec3' do
35
+ expect(VectorCache.instance.get).to eq(@recycled_vector)
36
+ end
37
+ end
38
+ end
39
+
40
+ describe '.recycle' do
41
+ it 'expects a Vec3' do
42
+ expect{ VectorCache.instance.recycle(Snow::Vec3.new) }.not_to raise_error
43
+ expect{ VectorCache.instance.recycle(:foo) }.to raise_error(ArgumentError)
44
+ end
45
+
46
+ it 'adds the Vec3 to the cache' do
47
+ v = Snow::Vec3.new
48
+ VectorCache.instance.recycle(v)
49
+ expect(VectorCache.instance.cache.values).to include(v)
50
+ end
51
+
52
+ it 'zeros out the Vec3' do
53
+ v = Snow::Vec3[1, 2, 3]
54
+ VectorCache.instance.recycle(v)
55
+ expect(v.x).to eq(0)
56
+ expect(v.y).to eq(0)
57
+ expect(v.z).to eq(0)
58
+ end
59
+ end
60
+
61
+ describe '.clear' do
62
+ it 'removes all Vec3 from the cache' do
63
+ VectorCache.instance.recycle(Snow::Vec3.new)
64
+ VectorCache.instance.recycle(Snow::Vec3.new)
65
+ VectorCache.instance.recycle(Snow::Vec3.new)
66
+ VectorCache.instance.clear
67
+ expect(VectorCache.instance.size).to eq(0)
68
+ end
69
+ end
70
+
71
+ describe '.size' do
72
+ it 'returns the number of Vec3 in the cache' do
73
+ VectorCache.instance.clear
74
+ VectorCache.instance.recycle(Snow::Vec3.new)
75
+ VectorCache.instance.recycle(Snow::Vec3.new)
76
+ VectorCache.instance.recycle(Snow::Vec3.new)
77
+ expect(VectorCache.instance.size).to eq(3)
78
+ end
79
+ end
80
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gosling
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Amos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-18 00:00:00.000000000 Z
11
+ date: 2019-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gosu
@@ -52,7 +52,9 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.5'
55
- description: A lightweight library for creating 2D apps in Ruby.
55
+ description: |2
56
+ A 2D app creation library. Includes basic collision testing, actor inheritance,
57
+ and easy-to-use, intuitive animation transforms. Powered by Gosu and SnowMath.
56
58
  email:
57
59
  - flashguardian13@gmail.com
58
60
  executables: []
@@ -65,12 +67,15 @@ files:
65
67
  - "./lib/gosling/collision.rb"
66
68
  - "./lib/gosling/image_library.rb"
67
69
  - "./lib/gosling/inheritance_error.rb"
70
+ - "./lib/gosling/matrix_cache.rb"
71
+ - "./lib/gosling/object_cache.rb"
68
72
  - "./lib/gosling/patches.rb"
69
73
  - "./lib/gosling/polygon.rb"
70
74
  - "./lib/gosling/rect.rb"
71
75
  - "./lib/gosling/sprite.rb"
72
76
  - "./lib/gosling/transformable.rb"
73
77
  - "./lib/gosling/utils.rb"
78
+ - "./lib/gosling/vector_cache.rb"
74
79
  - "./lib/gosling/version.rb"
75
80
  - "./spec/actor_spec.rb"
76
81
  - "./spec/circle_spec.rb"
@@ -82,11 +87,14 @@ files:
82
87
  - "./spec/images/full_icons_07_18.png"
83
88
  - "./spec/images/key.png"
84
89
  - "./spec/images/nil.png"
90
+ - "./spec/matrix_cache_spec.rb"
91
+ - "./spec/object_cache_spec.rb"
85
92
  - "./spec/polygon_spec.rb"
86
93
  - "./spec/rect_spec.rb"
87
94
  - "./spec/spec_helper.rb"
88
95
  - "./spec/sprite_spec.rb"
89
96
  - "./spec/transformable_spec.rb"
97
+ - "./spec/vector_cache_spec.rb"
90
98
  homepage: https://github.com/flashguardian13/gosling
91
99
  licenses:
92
100
  - CC-BY-4.0
@@ -110,5 +118,5 @@ rubyforge_project:
110
118
  rubygems_version: 2.5.2
111
119
  signing_key:
112
120
  specification_version: 4
113
- summary: Ruby 2D app creation
121
+ summary: A library for creating 2D apps
114
122
  test_files: []