graphics 1.0.0b6 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/History.rdoc +88 -0
  5. data/Manifest.txt +2 -0
  6. data/Rakefile +7 -8
  7. data/examples/boid.rb +44 -63
  8. data/examples/bounce.rb +4 -4
  9. data/examples/canvas.rb +17 -16
  10. data/examples/collision.rb +1 -1
  11. data/examples/demo.rb +1 -1
  12. data/examples/editor.rb +1 -1
  13. data/examples/fluid.rb +6 -6
  14. data/examples/fluid2.rb +22 -9
  15. data/examples/gol.rb +1 -1
  16. data/examples/gol2.rb +14 -22
  17. data/examples/math.rb +1 -1
  18. data/examples/pi_polygon.rb +8 -3
  19. data/examples/radar.rb +3 -3
  20. data/examples/rainbow_fluid.rb +4 -3
  21. data/examples/tank.rb +7 -3
  22. data/examples/tank2.rb +3 -3
  23. data/examples/targeting.rb +3 -3
  24. data/examples/vants.rb +13 -4
  25. data/examples/walker.rb +1 -1
  26. data/examples/walker2.rb +1 -1
  27. data/examples/zombies.rb +13 -7
  28. data/ext/sdl/extconf.rb +12 -20
  29. data/ext/sdl/sdl.c +619 -360
  30. data/ext/sdl/sge/Makefile +36 -11
  31. data/ext/sdl/sge/Makefile.conf +15 -9
  32. data/ext/sdl/sge/sge_bm_text.cpp +7 -6
  33. data/ext/sdl/sge/sge_collision.cpp +3 -1
  34. data/ext/sdl/sge/sge_config.h +0 -2
  35. data/ext/sdl/sge/sge_internal.h +0 -8
  36. data/ext/sdl/sge/sge_primitives.cpp +0 -11
  37. data/ext/sdl/sge/sge_primitives.h +0 -3
  38. data/ext/sdl/sge/sge_rotation.cpp +1 -1
  39. data/ext/sdl/sge/sge_shape.cpp +18 -9
  40. data/ext/sdl/sge/sge_surface.cpp +10 -4
  41. data/ext/sdl/sge/sge_textpp.cpp +17 -13
  42. data/graphics_setup.sh +43 -13
  43. data/lib/graphics.rb +1 -1
  44. data/lib/graphics/body.rb +8 -0
  45. data/lib/graphics/decorators.rb +15 -3
  46. data/lib/graphics/extensions.rb +1 -1
  47. data/lib/graphics/rainbows.rb +17 -25
  48. data/lib/graphics/simulation.rb +265 -106
  49. data/lib/graphics/v.rb +8 -1
  50. data/resources/sounds/attribution.txt +2 -0
  51. data/resources/sounds/bullet.wav +0 -0
  52. data/test/test_graphics.rb +232 -107
  53. metadata +37 -43
  54. metadata.gz.sig +1 -2
@@ -39,6 +39,13 @@ class V
39
39
  V[x-v.x, y-v.y]
40
40
  end
41
41
 
42
+ ##
43
+ # Unary negation.
44
+
45
+ def -@
46
+ V[-x, -y]
47
+ end
48
+
42
49
  ##
43
50
  # Multiply a vector by a scalar, returning a new vector.
44
51
 
@@ -61,7 +68,7 @@ class V
61
68
  # Return the length of the vector from the origin.
62
69
 
63
70
  def magnitude
64
- Math.sqrt(x*x + y*y)
71
+ @magnitude ||= Math.sqrt(x*x + y*y)
65
72
  end
66
73
 
67
74
  def inspect # :nodoc:
@@ -0,0 +1,2 @@
1
+ bullet.wav by Mike Koenig, licensed under Attribution 3.0.
2
+ from http://soundbible.com/1875-Bullet-Whizzing-By.html
@@ -3,6 +3,13 @@
3
3
  require "minitest/autorun"
4
4
  require "graphics"
5
5
 
6
+ class FakeSimulation < Graphics::Simulation
7
+ def initialize
8
+ SDL.init SDL::INIT_VIDEO # HACK? Used to be in Simulation#initialize
9
+ super 300, 200
10
+ end
11
+ end
12
+
6
13
  class TestBody < Minitest::Test
7
14
  attr_accessor :w, :b
8
15
 
@@ -298,116 +305,167 @@ class TestNumeric < Minitest::Test
298
305
  end
299
306
 
300
307
  class TestSimulation < Minitest::Test
301
- # make_my_diffs_pretty!
308
+ class FakePixelFormat < SDL::PixelFormat
309
+ def initialize
310
+
311
+ end
312
+ end
302
313
 
303
- class FakeSimulation < Graphics::Simulation
314
+ class FakeSimulation < ::FakeSimulation
304
315
  def initialize
305
- super 100, 100, 16, "blah"
316
+ super
306
317
 
307
318
  s = []
308
319
 
320
+ sc = s.singleton_class
321
+
322
+ sc.send :undef_method, :clear
323
+ sc.send :undef_method, :[]=
324
+
309
325
  def s.method_missing *a
310
326
  @data ||= []
311
327
  @data << a
328
+ self
312
329
  end
313
330
 
314
331
  def s.data
315
332
  @data
316
333
  end
317
334
 
318
- self.screen = s
335
+ pf = self.renderer.format
336
+
337
+ s.instance_variable_set :@format, pf
338
+
339
+ self.renderer = s
319
340
  end
320
341
  end
321
342
 
322
- attr_accessor :t, :white, :exp
343
+ attr_accessor :t, :white, :black, :exp, :h
323
344
 
324
345
  def setup
325
346
  self.t = FakeSimulation.new
326
347
  self.white = t.color[:white]
348
+ self.black = t.color[:black]
327
349
  self.exp = []
350
+ self.h = t.h-1
328
351
  end
329
352
 
330
- def test_angle
331
- h = t.h-1
353
+ def assert_drawing *calls
354
+ exp.concat calls
332
355
 
333
- t.angle 50, 50, 0, 10, :white
334
- exp << [:draw_line, 50, h-50, 60.0, h-50.0, white]
356
+ assert_equal exp, t.renderer.data
357
+ end
335
358
 
336
- t.angle 50, 50, 90, 10, :white
337
- exp << [:draw_line, 50, 49, 50.0, h-60.0, white]
359
+ def test_angle
360
+ d45 = 10 * Math.sqrt(2) / 2
338
361
 
362
+ t.angle 50, 50, 0, 10, :white
363
+ t.angle 50, 50, 90, 10, :white
339
364
  t.angle 50, 50, 180, 10, :white
340
- exp << [:draw_line, 50, h-50, 40.0, h-50.0, white]
341
-
342
365
  t.angle 50, 50, 270, 10, :white
343
- exp << [:draw_line, 50, h-50, 50.0, h-40.0, white]
366
+ t.angle 50, 50, 45, 10, :white
344
367
 
345
- t.angle 50, 50, 45, 10, :white
346
- d45 = 10 * Math.sqrt(2) / 2
347
- exp << [:draw_line, 50, h-50, 50+d45, h-50-d45, white]
368
+ assert_drawing([:draw_line, 50, h-50, 60.0, h-50, white, true],
369
+ [:draw_line, 50, h-50, 50.0, h-60, white, true],
370
+ [:draw_line, 50, h-50, 40.0, h-50, white, true],
371
+ [:draw_line, 50, h-50, 50.0, h-40, white, true],
372
+ [:draw_line, 50, h-50, 50+d45, h-50-d45, white, true])
373
+ end
348
374
 
349
- assert_equal exp, t.screen.data
375
+ def test_bezier
376
+ t.bezier 50, 50, 25, 25, 100, 25, :white
377
+
378
+ assert_drawing [:draw_bezier, [50, 25, 100], [h-50, h-25, h-25], 5, white]
350
379
  end
351
380
 
352
- # def test_bezier
353
- # raise NotImplementedError, 'Need to write test_bezier'
354
- # end
355
- #
356
- # def test_blit
357
- # raise NotImplementedError, 'Need to write test_blit'
358
- # end
359
- #
360
- # def test_circle
361
- # raise NotImplementedError, 'Need to write test_circle'
362
- # end
363
- #
364
- # def test_clear
365
- # raise NotImplementedError, 'Need to write test_clear'
366
- # end
367
- #
368
- # def test_debug
369
- # raise NotImplementedError, 'Need to write test_debug'
370
- # end
371
- #
372
- # def test_draw
373
- # raise NotImplementedError, 'Need to write test_draw'
374
- # end
375
- #
376
- # def test_draw_and_flip
377
- # raise NotImplementedError, 'Need to write test_draw_and_flip'
378
- # end
381
+ def test_blit
382
+ s = t.render_text "blah", :white
383
+
384
+ assert_instance_of SDL::Surface, s
385
+ assert_equal [76, 38], [s.w, s.h]
386
+
387
+ t.blit s, 100, 10
388
+
389
+ assert_drawing [:blit, s, 100-(76/2), h-(38/2)-10+1, nil, nil, nil, :center]
390
+ end
391
+
392
+ def test_put
393
+ s = t.render_text "blah", :white
394
+
395
+ assert_instance_of SDL::Surface, s
396
+ assert_equal [76, 38], [s.w, s.h]
397
+
398
+ t.put s, 10, 10
399
+
400
+ # TODO: extra -1 / +1 doesn't make sense
401
+ assert_drawing [:blit, s, 10, h-38-10+1, nil, nil, nil, false]
402
+ end
403
+
404
+ def test_circle
405
+ t.circle 50, 50, 25, :white
406
+ t.circle 50, 50, 25, :white, :filled
407
+
408
+ t.circle 50, 50, 25, :white, false, false
409
+ t.circle 50, 50, 25, :white, :filled, false
410
+
411
+ assert_drawing([:draw_circle, 50, 149, 25, white, true, false],
412
+ [:draw_circle, 50, 149, 25, white, true, :filled],
413
+ [:draw_circle, 50, 149, 25, white, false, false],
414
+ [:draw_circle, 50, 149, 25, white, false, :filled])
415
+ end
416
+
417
+ def test_clear
418
+ t.clear
419
+ t.clear :white
420
+
421
+ assert_drawing([:clear, black],
422
+ [:clear, white])
423
+ end
424
+
425
+ def test_debug
426
+ skip "not yet, figure out test_text first"
427
+
428
+ raise NotImplementedError, 'Need to write test_debug'
429
+ end
379
430
 
380
431
  def test_ellipse
381
432
  t.ellipse 0, 0, 25, 25, :white
382
433
 
383
- h = t.h-1
384
- exp << [:draw_ellipse, 0, h, 25, 25, t.color[:white]]
434
+ assert_drawing [:draw_ellipse, 0, h, 25, 25, t.color[:white], true, false]
435
+ end
436
+
437
+ def test_fast_rect
438
+ t.fast_rect 25, 25, 10, 20, :white
439
+ t.fast_rect 0, 0, 100, 100, :white
385
440
 
386
- assert_equal exp, t.screen.data
441
+ assert_drawing([:fast_rect, 25, h+1-25-20, 10, 20, white],
442
+ [:fast_rect, 0, h+1-0-100, 100, 100, white])
443
+ end
444
+
445
+ def test_fps
446
+ skip "not yet, figure out test_text first"
447
+
448
+ t.start_time = Time.now - 1
449
+
450
+ t.fps 42
451
+
452
+ assert_drawing
387
453
  end
388
454
 
389
- # def test_fast_rect
390
- # raise NotImplementedError, 'Need to write test_fast_rect'
391
- # end
392
- #
393
- # def test_fps
394
- # raise NotImplementedError, 'Need to write test_fps'
395
- # end
396
- #
397
455
  # def test_handle_event
398
456
  # raise NotImplementedError, 'Need to write test_handle_event'
399
457
  # end
400
- #
458
+
401
459
  # def test_handle_keys
402
460
  # raise NotImplementedError, 'Need to write test_handle_keys'
403
461
  # end
404
462
 
405
463
  def test_hline
406
464
  t.hline 42, :white
407
- h = t.h - 1
408
- exp << [:draw_line, 0, h-42, 100, h-42, t.color[:white]]
465
+ t.hline 0, :white
409
466
 
410
- assert_equal exp, t.screen.data
467
+ assert_drawing([:draw_line, 0, h-42, 300, h-42, t.color[:white], true],
468
+ [:draw_line, 0, h- 0, 300, h- 0, t.color[:white], true])
411
469
  end
412
470
 
413
471
  # def test_image
@@ -415,68 +473,135 @@ class TestSimulation < Minitest::Test
415
473
  # end
416
474
 
417
475
  def test_line
476
+ t.line 0, 0, 25, 0, :white
477
+ t.line 0, 0, 0, 25, :white
418
478
  t.line 0, 0, 25, 25, :white
419
- h = t.h - 1
420
- exp << [:draw_line, 0, h, 25, h-25, t.color[:white]]
479
+ t.line 0, 0, 25, 25, :white, false
480
+
481
+ t.line 0, 200-1, 25, 200-1, :white
482
+ t.line 0, 200-1, 0, 200-1-25, :white
483
+ t.line 0, 200-1, 25, 200-1-25, :white
421
484
 
422
- assert_equal exp, t.screen.data
485
+ assert_drawing([:draw_line, 0, h, 25, h- 0, t.color[:white], true],
486
+ [:draw_line, 0, h, 0, h-25, t.color[:white], true],
487
+ [:draw_line, 0, h, 25, h-25, t.color[:white], true],
488
+ [:draw_line, 0, h, 25, h-25, t.color[:white], false],
489
+ [:draw_line, 0, 0, 25, 0, t.color[:white], true],
490
+ [:draw_line, 0, 0, 0, 25, t.color[:white], true],
491
+ [:draw_line, 0, 0, 25, 25, t.color[:white], true])
423
492
  end
424
493
 
425
494
  def test_point
426
- skip "not yet"
427
495
  t.point 2, 10, :white
428
496
 
429
- exp = [nil, nil, t.color[:white]]
430
- assert_equal exp, t.screen
497
+ assert_drawing [:[]=, 2, h-10, white]
498
+ end
431
499
 
432
- skip "This test isn't sufficient"
500
+ def test_populate
501
+ skip "not done yet"
433
502
  end
434
503
 
435
- # def test_populate
436
- # raise NotImplementedError, 'Need to write test_populate'
437
- # end
438
- #
439
- # def test_rect
440
- # raise NotImplementedError, 'Need to write test_rect'
441
- # end
442
- #
443
- # def test_register_color
444
- # raise NotImplementedError, 'Need to write test_register_color'
445
- # end
446
- #
447
- # def test_render_text
448
- # raise NotImplementedError, 'Need to write test_render_text'
449
- # end
450
- #
451
- # def test_run
452
- # raise NotImplementedError, 'Need to write test_run'
453
- # end
454
- #
455
- # def test_sprite
456
- # raise NotImplementedError, 'Need to write test_sprite'
457
- # end
458
- #
459
- # def test_text
460
- # raise NotImplementedError, 'Need to write test_text'
461
- # end
462
- #
463
- # def test_text_size
464
- # raise NotImplementedError, 'Need to write test_text_size'
465
- # end
466
- #
467
- # def test_update
468
- # raise NotImplementedError, 'Need to write test_update'
469
- # end
470
- #
471
- # def test_vline
472
- # raise NotImplementedError, 'Need to write test_vline'
473
- # end
504
+ make_my_diffs_pretty!
505
+
506
+
507
+ def test_rect
508
+ t.rect 25, 25, 10, 20, :white
509
+ t.rect 0, 0, 100, 100, :white
510
+ t.rect 25, 25, 10, 20, :white, :filled
511
+
512
+ assert_drawing([:draw_rect, 25, h+1-25-20, 10, 20, white, false],
513
+ [:draw_rect, 0, h+1-0-100, 100, 100, white, false],
514
+ [:draw_rect, 25, h+1-25-20, 10, 20, white, :filled])
515
+ end
516
+
517
+ def test_register_color
518
+ skip "not done yet"
519
+ end
520
+
521
+ def test_render_text
522
+ s = t.render_text("blah", :black)
523
+
524
+ assert_instance_of SDL::Surface, s
525
+ assert_equal [76, 38], [s.w, s.h]
526
+ end
527
+
528
+ def test_run
529
+ skip "not done yet"
530
+ end
531
+
532
+ def test_sprite
533
+ skip "not done yet"
534
+ end
535
+
536
+ def test_text
537
+ skip "um... not sure how to test this yet"
538
+
539
+ t.text "woot", 25, 25, :white
540
+
541
+ assert_drawing
542
+ end
543
+
544
+ def test_text_size
545
+ assert_equal [76, 38], t.text_size("blah")
546
+ assert_equal [ 0, 38], t.text_size("")
547
+ assert_equal [76, 38], t.text_size(:blah)
548
+ end
549
+
550
+ def test_update
551
+ skip "not done yet"
552
+ end
553
+
554
+ def test_vline
555
+ t.vline 0, :white
556
+ t.vline 42, :white
557
+
558
+ assert_drawing([:draw_line, 0, 0, 0, h, t.color[:white], true],
559
+ [:draw_line, 42, 0, 42, h, t.color[:white], true])
560
+ end
561
+
562
+ def test_from_hsl
563
+ assert_equal [ 0, 0, 0], t.from_hsl( 0, 0, 0) # Black
564
+ assert_equal [255, 255, 255], t.from_hsl( 0, 0, 1) # White
565
+ assert_equal [255, 0, 0], t.from_hsl( 0, 1, 0.50) # Red
566
+ assert_equal [ 0, 255, 0], t.from_hsl(120, 1, 0.50) # Lime
567
+ assert_equal [ 0, 0, 255], t.from_hsl(240, 1, 0.50) # Blue
568
+ assert_equal [255, 255, 0], t.from_hsl( 60, 1, 0.50) # Yellow
569
+ assert_equal [ 0, 255, 255], t.from_hsl(180, 1, 0.50) # Cyan
570
+ assert_equal [255, 0, 255], t.from_hsl(300, 1, 0.50) # Magenta
571
+ assert_equal [191, 191, 191], t.from_hsl( 0, 0, 0.75) # Silver
572
+ assert_equal [128, 128, 128], t.from_hsl( 0, 0, 0.50) # Gray
573
+ assert_equal [128, 0, 0], t.from_hsl( 0, 1, 0.25) # Maroon
574
+ assert_equal [128, 128, 0], t.from_hsl( 60, 1, 0.25) # Olive
575
+ assert_equal [ 0, 128, 0], t.from_hsl(120, 1, 0.25) # Green
576
+ assert_equal [128, 0, 128], t.from_hsl(300, 1, 0.25) # Purple
577
+ assert_equal [ 0, 128, 128], t.from_hsl(180, 1, 0.25) # Teal
578
+ assert_equal [ 0, 0, 128], t.from_hsl(240, 1, 0.25) # Navy
579
+ end
580
+
581
+ def test_from_hsv
582
+ assert_equal [ 0, 0, 0], t.from_hsv( 0, 0, 0) # Black
583
+ assert_equal [255, 255, 255], t.from_hsv( 0, 0, 1) # White
584
+ assert_equal [255, 0, 0], t.from_hsv( 0, 1, 1) # Red
585
+ assert_equal [ 0, 255, 0], t.from_hsv(120, 1, 1) # Lime
586
+ assert_equal [ 0, 0, 255], t.from_hsv(240, 1, 1) # Blue
587
+ assert_equal [255, 255, 0], t.from_hsv( 60, 1, 1) # Yellow
588
+ assert_equal [ 0, 255, 255], t.from_hsv(180, 1, 1) # Cyan
589
+ assert_equal [255, 0, 255], t.from_hsv(300, 1, 1) # Magenta
590
+ assert_equal [191, 191, 191], t.from_hsv( 0, 0, 0.75) # Silver
591
+ assert_equal [128, 128, 128], t.from_hsv( 0, 0, 0.50) # Gray
592
+ assert_equal [128, 0, 0], t.from_hsv( 0, 1, 0.50) # Maroon
593
+ assert_equal [128, 128, 0], t.from_hsv( 60, 1, 0.50) # Olive
594
+ assert_equal [ 0, 128, 0], t.from_hsv(120, 1, 0.50) # Green
595
+ assert_equal [128, 0, 128], t.from_hsv(300, 1, 0.50) # Purple
596
+ assert_equal [ 0, 128, 128], t.from_hsv(180, 1, 0.50) # Teal
597
+ assert_equal [ 0, 0, 128], t.from_hsv(240, 1, 0.50) # Navy
598
+ end
474
599
  end
475
600
 
476
601
  require 'graphics/rainbows'
477
602
  class TestGraphics < Minitest::Test
478
603
  def setup
479
- @t = Graphics::Simulation.new 100, 100, 16, ""
604
+ @t = FakeSimulation.new
480
605
  end
481
606
 
482
607
  def test_registering_rainbows