graphics 1.0.0b5 → 1.0.0b6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05606dab0e932b0e2a0244bfda441d75a5e075b4
4
- data.tar.gz: adbefb635747458fed5734fd2464578390376a90
3
+ metadata.gz: 37f59baac9923c3919719668065ee0c6371ea524
4
+ data.tar.gz: bd576413810e63a2e8e51f12669c42f5d8ba0dbb
5
5
  SHA512:
6
- metadata.gz: ec204d93283fd260d941d1851a2bd4425278754937f36a2495b84e7b319a0b4ee2455ac81b4dea3596819c5e7070add8f5c2e062fde7cacc66be2ce1b0ff31da
7
- data.tar.gz: b55da059453df3ab0358cbc278567e3546d389e1811843bf34e30cb9c2ff1424f9a8afb20aabc090c3fdcd4b130e76731a6e35affa1f0dd2110db5893f80d329
6
+ metadata.gz: 4519bbe92fc69ab4c4024cd3251afb68e87aa6f6100103d4b052c2d536b662b79448f392ff57a97e8a2f889aef5da414feb8196f65624195e28437d5ff52c476
7
+ data.tar.gz: 1a921bf4a04063aef66d0f8eb829405a61659df355dbc6c1f2258558541dd779eabfcc39760ee10524846cf65e6793b6098f54f58975d04e7e7676e0cba33b6e
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -1,3 +1,41 @@
1
+ === 1.0.0b6 / 2016-11-17
2
+
3
+ I'm still not happy with the color system. I'd love some help to make it more usable.
4
+
5
+ * 1 major enhancement:
6
+
7
+ * Added Graphics::Drawing: a simulation that doesn't double buffer or clear each tick.
8
+
9
+ * 20 minor enhancements:
10
+
11
+ * Added DrawGrid decorator.
12
+ * Added Graphics::Simulation#draw_collection
13
+ * Added Graphics::Simulation#register_bodies
14
+ * Added Graphics::Simulation#register_body
15
+ * Added Screen#save.
16
+ * Added ShowFPS decorator module
17
+ * Added Simulation#done that causes soft exit to allow profiling of apps.
18
+ * Added WhiteBackground decorator.
19
+ * Added cyan and magenta to color dictionary.
20
+ * Added sge_FastFilledRect to sge_primitives.
21
+ * Body#bounce returns true if it did bounce.
22
+ * Defined Graphics::Simulation::CLEAR_COLOR.
23
+ * Graphics::Simulation now stores a collection of collections of bodies. Those bodies are updated and drawn automatically each tick.
24
+ * Graphics::Simulation.clear defaults to CLEAR_COLOR.
25
+ * Improved MVC pattern in Simulation.
26
+ * Pushed Simulation up to AbstractSimulation.
27
+ * Registered more default colors. Should also make arrays of names.
28
+ * Reimplement Body#bounce to use clip & NORMAL. Much cleaner.
29
+ * Split Simulation#draw into pre_draw and post_draw to give overrides more flexibility.
30
+ * Split up Simulation for Drawings.
31
+
32
+ * 4 bug fixes:
33
+
34
+ * Fixed a bug in boid refactoring.
35
+ * Fixed bounce math to try to place the body to where it would bounce. (sotoseattle)
36
+ * No more srand in examples.
37
+ * Only apply friction on #bounce if greater than 0.
38
+
1
39
  === 1.0.0b5 / 2015-10-31
2
40
 
3
41
  * 10 minor enhancements:
@@ -16,7 +16,9 @@ examples/gol2.rb
16
16
  examples/logo.rb
17
17
  examples/math.rb
18
18
  examples/maze.rb
19
+ examples/pi_polygon.rb
19
20
  examples/radar.rb
21
+ examples/rainbow_fluid.rb
20
22
  examples/tank.rb
21
23
  examples/tank2.rb
22
24
  examples/targeting.rb
@@ -59,11 +61,14 @@ ext/sdl/sge/sge_tt_text.h
59
61
  graphics_setup.sh
60
62
  lib/graphics.rb
61
63
  lib/graphics/body.rb
64
+ lib/graphics/decorators.rb
62
65
  lib/graphics/extensions.rb
66
+ lib/graphics/rainbows.rb
63
67
  lib/graphics/simulation.rb
64
68
  lib/graphics/trail.rb
65
69
  lib/graphics/v.rb
66
70
  resources/images/body.png
67
71
  resources/images/turret.png
68
72
  test/test_graphics.rb
73
+ test/test_rainbows.rb
69
74
  test/test_sdl.rb
data/Rakefile CHANGED
@@ -9,6 +9,8 @@ Hoe.plugin :rdoc
9
9
  Hoe.plugin :git
10
10
  Hoe.plugin :compiler
11
11
 
12
+ Hoe.add_include_dirs File.expand_path "~/Work/p4/zss/src/minitest/dev/lib"
13
+
12
14
  Hoe.spec "graphics" do
13
15
  developer "Ryan Davis", "ryand-ruby@zenspider.com"
14
16
 
@@ -27,13 +27,6 @@ class Boid < Graphics::Body
27
27
  self.a = random_angle
28
28
  end
29
29
 
30
- def draw
31
- # the blit looks HORRIBLE when rotated... dunno why
32
- w.circle x, y, @@max_distance, :gray if w.visual_debug?
33
- w.blit w.body_img, x, y
34
- w.angle x, y, a, 3 * m, :red
35
- end
36
-
37
30
  def label
38
31
  l = "%.1f [%.2f, %.2f]" % [a, *dx_dy]
39
32
  w.text l, x-10, y+10, :white
@@ -48,6 +41,8 @@ class Boid < Graphics::Body
48
41
  limit_velocity
49
42
  self.position += self.velocity
50
43
 
44
+ wrap
45
+
51
46
  @nearby = nil
52
47
  end
53
48
 
@@ -242,6 +237,16 @@ class Boid < Graphics::Body
242
237
 
243
238
  (v - self.velocity) / 4
244
239
  end
240
+
241
+ class View
242
+ def self.draw w, b
243
+ x, y, a, m = b.x, b.y, b.a, b.m
244
+
245
+ w.circle x, y, Boid.max_distance, :gray if w.visual_debug?
246
+ w.blit w.body_img, x, y # the blit looks HORRIBLE when rotated... dunno why
247
+ w.angle x, y, a, 3 * m, :red
248
+ end
249
+ end
245
250
  end
246
251
 
247
252
  class Boids < Graphics::Simulation
@@ -255,6 +260,7 @@ class Boids < Graphics::Simulation
255
260
  self.visual_debug = false
256
261
 
257
262
  self.boids = populate Boid
263
+ register_bodies boids
258
264
 
259
265
  self.body_img = sprite 20, 20 do
260
266
  circle 10, 10, 5, :white, :filled
@@ -269,15 +275,8 @@ class Boids < Graphics::Simulation
269
275
  add_key_handler(:S) { Boid.max_distance -= 5 }
270
276
  end
271
277
 
272
- def update n
273
- boids.each(&:update)
274
- self.boids.each(&:wrap)
275
- end
276
-
277
278
  def draw n
278
- clear
279
-
280
- boids.each(&:draw)
279
+ super
281
280
 
282
281
  debug "r = #{Boid.max_distance}" if visual_debug?
283
282
  fps n
@@ -17,11 +17,6 @@ class Ball < Graphics::Body
17
17
  self.g = G
18
18
  end
19
19
 
20
- def draw
21
- w.angle x, y, a, 10+3*m, :red
22
- w.circle x, y, 5, :white, :filled
23
- end
24
-
25
20
  def update
26
21
  fall
27
22
  move
@@ -31,6 +26,13 @@ class Ball < Graphics::Body
31
26
  def fall
32
27
  self.velocity += g
33
28
  end
29
+
30
+ class View
31
+ def self.draw w, b
32
+ w.angle b.x, b.y, b.a, 10+3*b.m, :red
33
+ w.circle b.x, b.y, 5, :white, :filled
34
+ end
35
+ end
34
36
  end
35
37
 
36
38
  class BounceSimulation < Graphics::Simulation
@@ -40,6 +42,7 @@ class BounceSimulation < Graphics::Simulation
40
42
  super 640, 640, 16, "Bounce"
41
43
 
42
44
  self.bs = populate Ball
45
+ register_bodies bs
43
46
  end
44
47
 
45
48
  def initialize_keys
@@ -48,13 +51,8 @@ class BounceSimulation < Graphics::Simulation
48
51
  add_keydown_handler "r", &:reverse
49
52
  end
50
53
 
51
- def update n
52
- bs.each(&:update)
53
- end
54
-
55
54
  def draw n
56
- clear
57
- bs.each(&:draw)
55
+ super
58
56
  fps n
59
57
  end
60
58
 
@@ -4,6 +4,8 @@
4
4
  require "graphics"
5
5
 
6
6
  class Demo < Graphics::Simulation
7
+ CLEAR_COLOR = :white
8
+
7
9
  attr_accessor :woot, :menlo32
8
10
  attr_accessor :rct
9
11
 
@@ -27,7 +29,7 @@ class Demo < Graphics::Simulation
27
29
  A = R2D*Math.atan2(200, 100)
28
30
 
29
31
  def draw n
30
- clear :white
32
+ clear
31
33
 
32
34
  (0..90).step(30) do |deg|
33
35
  shift = (woot.h*Math.sin(D2R*deg))
@@ -23,13 +23,15 @@ class Sprite < Graphics::Body
23
23
  self.a = (a + 180).degrees
24
24
  end
25
25
 
26
- def draw
27
- w.blit image, x, y, a
28
- end
29
-
30
26
  def collide_with? other
31
27
  w.cmap.check(x, y, w.cmap, other.x, other.y)
32
28
  end
29
+
30
+ class View
31
+ def self.draw w, b
32
+ w.blit b.image, b.x, b.y, b.a
33
+ end
34
+ end
33
35
  end
34
36
 
35
37
  class Collision < Graphics::Simulation
@@ -44,6 +46,8 @@ class Collision < Graphics::Simulation
44
46
  self.sprites = populate Sprite do |s|
45
47
  s.image = tank_img
46
48
  end
49
+
50
+ register_bodies sprites
47
51
  end
48
52
 
49
53
  def inspect
@@ -59,14 +63,12 @@ class Collision < Graphics::Simulation
59
63
  end
60
64
 
61
65
  def update n
62
- sprites.each(&:update)
66
+ super
63
67
  detect_collisions(sprites).each(&:collide)
64
68
  end
65
69
 
66
70
  def draw n
67
- clear
68
-
69
- sprites.each(&:draw)
71
+ super
70
72
  fps n
71
73
  end
72
74
  end
@@ -19,32 +19,34 @@ class Ball < Graphics::Body
19
19
  wrap
20
20
  end
21
21
 
22
- def draw n
23
- a = n % 360
22
+ class View
23
+ def self.draw w, b
24
+ a = w.n % 360
25
+ x, y = b.x, b.y
24
26
 
25
- w.angle x, y, a, 50, :green
26
- w.circle x, y, 5, :white, :filled
27
+ w.angle x, y, a, 50, :green
28
+ w.circle x, y, 5, :white, :filled
29
+ end
27
30
  end
28
31
  end
29
32
 
30
33
  class Demo < Graphics::Simulation
31
- attr_accessor :ball, :img
34
+ attr_accessor :ball, :img, :n
32
35
 
33
36
  def initialize
34
37
  super 800, 800, 16, "Boid"
35
38
  self.ball = Ball.new self
39
+ register_body ball
36
40
 
37
41
  self.img = sprite 10, 10 do
38
42
  circle 5, 5, 5, :white, :filled
39
43
  end
40
44
  end
41
45
 
42
- def update n
43
- ball.update
44
- end
45
-
46
46
  def draw n
47
- clear
47
+ self.n = n
48
+
49
+ super
48
50
 
49
51
  line 100, 50, 125, 75, :white
50
52
 
@@ -79,8 +81,6 @@ class Demo < Graphics::Simulation
79
81
 
80
82
  debug "debug"
81
83
 
82
- ball.draw n
83
-
84
84
  fps n
85
85
  end
86
86
  end
@@ -22,14 +22,17 @@ class Particle < Graphics::Body
22
22
  clear
23
23
  end
24
24
 
25
- def draw
26
- x = self.x * s
27
- y = self.y * s
25
+ class View
26
+ def self.draw w, b
27
+ s, a, m, d = b.s, b.a, b.m, b.density
28
+ x = b.x * s
29
+ y = b.y * s
28
30
 
29
- w.circle(x, y, density, :gray)
30
- w.circle(x, y, 5, :white)
31
+ w.circle(x, y, d, :gray)
32
+ w.circle(x, y, 5, :white)
31
33
 
32
- w.angle x, y, a, m * s, :red
34
+ w.angle x, y, a, m * s, :red
35
+ end
33
36
  end
34
37
 
35
38
  def clear
@@ -180,11 +183,13 @@ class FluidDynamics < Graphics::Simulation
180
183
  particles << Particle.new(self, x + 1 + jitter, y + 5, S)
181
184
  end
182
185
  end
186
+
187
+ register_bodies particles
183
188
  end
184
189
 
185
190
  def draw n
186
- clear
187
- particles.each(&:draw)
191
+ super
192
+
188
193
  fps n
189
194
  end
190
195
 
@@ -1,7 +1,5 @@
1
1
  #!/usr/bin/ruby -w
2
2
 
3
- srand 42
4
-
5
3
  require "graphics"
6
4
 
7
5
  class Array
@@ -3,6 +3,8 @@
3
3
  require "graphics"
4
4
 
5
5
  class MathSimulation < Graphics::Simulation
6
+ CLEAR_COLOR = :white
7
+
6
8
  def initialize
7
9
  super 640, 640, 16, "Math"
8
10
  end
@@ -24,7 +26,7 @@ class MathSimulation < Graphics::Simulation
24
26
  end
25
27
 
26
28
  def graph_paper
27
- clear :white
29
+ clear
28
30
 
29
31
  hline 1, :black
30
32
  vline 0, :black
@@ -0,0 +1,145 @@
1
+ require "graphics"
2
+ require "graphics/trail"
3
+
4
+ # A way to compute pi as the ratio of the area of a polygon and the diameter of
5
+ # the enclosing circle. A set of bouncing bullets create new vertices as they
6
+ # hit the perimeter of the circle. As the number of vertices tends to infinite
7
+ # the polygon will converge to the circle and the ratio to pi.
8
+
9
+ class Polygnome < Array
10
+ attr_reader :origin, :r, :s
11
+
12
+ def initialize center_x, center_y, w
13
+ @s = w
14
+ @r = w.r
15
+ @origin = V[center_x, center_y]
16
+ end
17
+
18
+ def draw
19
+ if size > 2
20
+ points = self << first
21
+ points.each_cons(2) { |a, b| @s.line a.x, a.y, b.x, b.y, :yellow }
22
+ end
23
+ end
24
+
25
+ def add vertex
26
+ self << vertex
27
+
28
+ if size > 2
29
+ sort_radar
30
+ SDL::WM.set_caption compute_pi, ''
31
+ end
32
+ end
33
+
34
+ ##
35
+ # Sort vertex like a radar, by angle to center
36
+ def sort_radar
37
+ sort_by! do |v|
38
+ (360 + Math.atan2((v.y - origin.y), (v.x - origin.x))) % 360
39
+ end
40
+ end
41
+
42
+ ##
43
+ # Algorithm to compute area of polygon, needs vertex sorted in radar mode
44
+ def compute_area
45
+ sol = 0.0
46
+ j = size - 1
47
+ each_with_index do |v, i|
48
+ sol += (self[j].x + v.x) * (self[j].y - v.y)
49
+ j = i
50
+ end
51
+ (sol / 2.0).abs
52
+ end
53
+
54
+ def compute_pi
55
+ "Pi: " + "%1.5f" % [compute_area / @r**2]
56
+ end
57
+ end
58
+
59
+ class Bouncer < Graphics::Body
60
+ attr_reader :x, :y, :a, :m, :r, :s
61
+ attr_accessor :trail
62
+
63
+ def initialize w, magnitude
64
+ super w
65
+ self.trail = Graphics::Trail.new(w, 6, color = :red)
66
+ @s = w
67
+ @r = w.r
68
+ @x = rand(w.screen.w/4) + w.r
69
+ @y = rand(w.screen.h/4) + w.r
70
+ @a = random_angle
71
+ @m = magnitude
72
+ end
73
+
74
+ def target_point
75
+ rad = @a * D2R
76
+ V[@x + Math.cos(rad) * @m, @y + Math.sin(rad) * @m]
77
+ end
78
+
79
+ def outside_circle? v
80
+ (v.x - @r)**2 + (v.y - @r)**2 > @r**2
81
+ end
82
+
83
+ ##
84
+ # Slope and offset of line given 2 points
85
+ def line_to p
86
+ slope = (p.y - @y) / (p.x - @x)
87
+ offset = @y - (slope * @x)
88
+ [slope, offset]
89
+ end
90
+
91
+ ##
92
+ # Intersection of enclosing circle and line y = ax + b. Algebraic solution
93
+ def intersection_circle_and l
94
+ a, b = l
95
+ beta = Math.sqrt((2 * a * @r**2) - (2 * a * b * @r) - b**2 + (2 * b * @r))
96
+ alfa = @r - (a * (b - @r))
97
+ gama = (1 + a**2)
98
+
99
+ x0 = [(alfa + beta)/gama, (alfa - beta)/gama].min_by {|e| (e - @x).abs}
100
+ y0 = a*x0 + b
101
+ V[x0, y0]
102
+ end
103
+
104
+ class View
105
+ def self.draw w, b
106
+ b.trail.draw # TODO: remove w stored everywhere
107
+ end
108
+ end
109
+
110
+ def update
111
+ t = target_point
112
+ if outside_circle? t
113
+ t = intersection_circle_and line_to(t)
114
+ turn (160 + rand(15) - 15)
115
+ @s.poly.add t
116
+ end
117
+ self.position = t
118
+ trail << self
119
+ end
120
+ end
121
+
122
+ class PiPolygon < Graphics::Simulation
123
+ RADIO = 400
124
+ BALLS = 10 # 2 30 100
125
+ MAGND = 10 # 10 10 50
126
+
127
+ attr_reader :r, :ball, :poly
128
+
129
+ def initialize
130
+ @r = RADIO
131
+ super @r * 2, @r * 2
132
+ @poly = Polygnome.new @r, @r, self
133
+ @balls = []
134
+ register_bodies @balls
135
+ BALLS.times { @balls << Bouncer.new(self, MAGND) }
136
+ end
137
+
138
+ def draw n
139
+ super
140
+ circle @r, @r, @r, :green
141
+ @poly.draw
142
+ end
143
+ end
144
+
145
+ PiPolygon.new.run if $0 == __FILE__