graphics 1.0.0b6 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -38,7 +38,7 @@ class Collision < Graphics::Simulation
38
38
  attr_accessor :sprites, :cmap, :tank_img
39
39
 
40
40
  def initialize
41
- super 850, 850, 16, "Collision"
41
+ super 850, 850
42
42
 
43
43
  self.tank_img = image "resources/images/body.png"
44
44
  self.cmap = tank_img.make_collision_map
@@ -34,7 +34,7 @@ class Demo < Graphics::Simulation
34
34
  attr_accessor :ball, :img, :n
35
35
 
36
36
  def initialize
37
- super 800, 800, 16, "Boid"
37
+ super 800, 800
38
38
  self.ball = Ball.new self
39
39
  register_body ball
40
40
 
@@ -9,7 +9,7 @@ class Editor < Graphics::Simulation
9
9
  alias :overlay? :overlay
10
10
 
11
11
  def initialize
12
- super 850, 850, 16, self.class.name
12
+ super 850, 850
13
13
 
14
14
  self.overlay = true
15
15
  self.s = ""
@@ -203,7 +203,9 @@ class SPH
203
203
  end
204
204
  end
205
205
 
206
- class SimulationWindow < Graphics::Simulation
206
+ class FluidDynamics < Graphics::Simulation
207
+ include ShowFPS
208
+
207
209
  WINSIZE = 500
208
210
 
209
211
  attr_reader :simulation, :s
@@ -211,7 +213,7 @@ class SimulationWindow < Graphics::Simulation
211
213
  DELTA_TIME = 0.1
212
214
 
213
215
  def initialize
214
- super WINSIZE, WINSIZE, 16, "Smoothed Particle Hydrodynamics"
216
+ super WINSIZE, WINSIZE, "Smoothed Particle Hydrodynamics"
215
217
  @simulation = SPH.new
216
218
  @scale = 15
217
219
  @s = WINSIZE.div @scale
@@ -223,7 +225,7 @@ class SimulationWindow < Graphics::Simulation
223
225
  end
224
226
 
225
227
  def draw time
226
- clear
228
+ super
227
229
 
228
230
  simulation.particles.each do |particle|
229
231
  pos = particle.position * s
@@ -236,10 +238,8 @@ class SimulationWindow < Graphics::Simulation
236
238
  # Velocity vectors
237
239
  p2 = pos + vel
238
240
  line(pos.x, pos.y, p2.x, p2.y, :red)
239
-
240
- fps time
241
241
  end
242
242
  end
243
243
  end
244
244
 
245
- SimulationWindow.new.run
245
+ FluidDynamics.new.run
@@ -125,6 +125,12 @@ class Particle < Graphics::Body
125
125
  ######################################################################
126
126
  # Helpers
127
127
 
128
+ ##
129
+ # A weighting function (kernel) for the contribution of each neighbor
130
+ # to a particle's density. Forms a nice smooth gradient from the center
131
+ # of a particle to H, where it's 0
132
+ #
133
+
128
134
  def weight r, h
129
135
  len_r = r.magnitude
130
136
 
@@ -135,6 +141,12 @@ class Particle < Graphics::Body
135
141
  end
136
142
  end
137
143
 
144
+ ##
145
+ # Gradient ( that is, V(dx, dy) ) of a weighting function for
146
+ # a particle's pressure. This weight function is spiky (not flat or
147
+ # smooth at x=0) so particles close together repel strongly.
148
+ #
149
+
138
150
  def gradient_weight_spiky r, h
139
151
  len_r = r.magnitude
140
152
 
@@ -145,6 +157,11 @@ class Particle < Graphics::Body
145
157
  end
146
158
  end
147
159
 
160
+ ##
161
+ # The laplacian of a weighting function that tends towards infinity when
162
+ # approching 0 (slows down particles moving faster than their neighbors)
163
+ #
164
+
148
165
  def laplacian_weight_viscosity r, h
149
166
  len_r = r.magnitude
150
167
 
@@ -162,7 +179,9 @@ class Float
162
179
  end
163
180
  end
164
181
 
165
- class FluidDynamics < Graphics::Simulation
182
+ class FluidDynamics2 < Graphics::Simulation
183
+ include ShowFPS
184
+
166
185
  WINSIZE = 500
167
186
  SCALE = 15
168
187
  S = WINSIZE / SCALE
@@ -170,7 +189,7 @@ class FluidDynamics < Graphics::Simulation
170
189
  attr_accessor :particles, :scale
171
190
 
172
191
  def initialize
173
- super WINSIZE, WINSIZE, 16, "Smoothed Particle Hydrodynamics"
192
+ super WINSIZE, WINSIZE, "Smoothed Particle Hydrodynamics"
174
193
 
175
194
  self.particles = []
176
195
  self.scale = SCALE
@@ -187,12 +206,6 @@ class FluidDynamics < Graphics::Simulation
187
206
  register_bodies particles
188
207
  end
189
208
 
190
- def draw n
191
- super
192
-
193
- fps n
194
- end
195
-
196
209
  def update n
197
210
  particles.each(&:clear)
198
211
  particles.each(&:calculate_density)
@@ -201,4 +214,4 @@ class FluidDynamics < Graphics::Simulation
201
214
  end
202
215
  end
203
216
 
204
- FluidDynamics.new.run
217
+ FluidDynamics2.new.run
@@ -85,7 +85,7 @@ class LitoGolSimulation < Graphics::Simulation
85
85
  SIZE, WIDTH = 10, 64
86
86
 
87
87
  def initialize
88
- super 640, 640, 16, "Conway's Game of Life"
88
+ super 640, 640, "Conway's Game of Life"
89
89
 
90
90
  self.gol = LitoGol.new WIDTH
91
91
  end
@@ -1,22 +1,13 @@
1
1
  #!/usr/bin/ruby -w
2
2
 
3
3
  require "graphics"
4
-
5
- class Array
6
- def sorted_include? o
7
- a, b = o
8
- !!bsearch { |(x, y)|
9
- c = a - x
10
- c.zero? ? b - y : c
11
- }
12
- end
13
- end
4
+ require "set"
14
5
 
15
6
  class ZenspiderGol
16
7
  delta = [-1, 0, 1]
17
8
  same = [0, 0]
18
9
 
19
- DELTAS = (delta.product(delta) - [same]).sort
10
+ DELTAS = (delta.product(delta) - [same])
20
11
  MIN = { true => 2, false => 3 }
21
12
 
22
13
  @@neighbors = Hash.new { |h, k| h[k] = {} }
@@ -25,30 +16,30 @@ class ZenspiderGol
25
16
  attr_accessor :cache
26
17
 
27
18
  def initialize
28
- self.cells = []
19
+ self.cells = Set.new
29
20
  end
30
21
 
31
22
  def randomize n, pct
32
23
  m = ((n*n) * pct).to_i
33
24
  dimensions = n.times.to_a
34
- cells.replace dimensions.product(dimensions).sample(m).sort
25
+ self.cells = dimensions.product(dimensions).sample(m).to_set
35
26
  end
36
27
 
37
28
  def update
38
- cells.replace considered.select { |(x, y)| alive? x, y }.sort
29
+ cells.replace considered.keep_if { |c| alive? c }
39
30
  end
40
31
 
41
32
  def considered
42
- cells.map { |(x, y)| neighbors_for(x, y) }.flatten(1).uniq
33
+ cells.to_a.map { |c| neighbors_for c }.flatten(1).uniq
43
34
  end
44
35
 
45
- def alive? x, y
46
- count = (neighbors_for(x, y) & cells).size
47
- min = MIN[cells.sorted_include? [x, y]]
48
- count.between? min, 3
36
+ def alive? c
37
+ neighbors_for(c).count { |o| cells.include? o }
38
+ .between? MIN[cells.include? c], 3
49
39
  end
50
40
 
51
- def neighbors_for x, y
41
+ def neighbors_for c
42
+ x, y = c
52
43
  @@neighbors[x][y] ||=
53
44
  DELTAS.map { |(dx, dy)| [x+dx, y+dy] }.reject { |(m, n)| m < 0 || n < 0 }
54
45
  end
@@ -60,7 +51,7 @@ class ZenspiderGolSimulation < Graphics::Simulation
60
51
  SIZE, WIDTH = 10, 64
61
52
 
62
53
  def initialize
63
- super 640, 640, 16, "Conway's Game of Life"
54
+ super 640, 640, "Conway's Game of Life"
64
55
 
65
56
  self.gol = ZenspiderGol.new
66
57
  gol.randomize WIDTH, 0.15
@@ -69,7 +60,8 @@ class ZenspiderGolSimulation < Graphics::Simulation
69
60
  def draw n
70
61
  clear
71
62
 
72
- gol.cells.each do |(x, y)|
63
+ gol.cells.each do |c|
64
+ x, y = c
73
65
  ellipse x*SIZE, y*SIZE, (SIZE-1)/2, (SIZE-1)/2, :white, :filled
74
66
  end
75
67
 
@@ -6,7 +6,7 @@ class MathSimulation < Graphics::Simulation
6
6
  CLEAR_COLOR = :white
7
7
 
8
8
  def initialize
9
- super 640, 640, 16, "Math"
9
+ super 640, 640
10
10
  end
11
11
 
12
12
  def draw n
@@ -27,7 +27,6 @@ class Polygnome < Array
27
27
 
28
28
  if size > 2
29
29
  sort_radar
30
- SDL::WM.set_caption compute_pi, ''
31
30
  end
32
31
  end
33
32
 
@@ -65,8 +64,8 @@ class Bouncer < Graphics::Body
65
64
  self.trail = Graphics::Trail.new(w, 6, color = :red)
66
65
  @s = w
67
66
  @r = w.r
68
- @x = rand(w.screen.w/4) + w.r
69
- @y = rand(w.screen.h/4) + w.r
67
+ @x = rand(w.renderer.w/4) + w.r
68
+ @y = rand(w.renderer.h/4) + w.r
70
69
  @a = random_angle
71
70
  @m = magnitude
72
71
  end
@@ -135,6 +134,12 @@ class PiPolygon < Graphics::Simulation
135
134
  BALLS.times { @balls << Bouncer.new(self, MAGND) }
136
135
  end
137
136
 
137
+ def update n
138
+ super
139
+
140
+ self.renderer.title = poly.compute_pi
141
+ end
142
+
138
143
  def draw n
139
144
  super
140
145
  circle @r, @r, @r, :green
@@ -2,11 +2,11 @@
2
2
 
3
3
  require "graphics"
4
4
 
5
- class TargetSimulation < Graphics::Simulation
5
+ class Radar < Graphics::Simulation
6
6
  CLEAR_COLOR = :darker_green
7
7
 
8
8
  def initialize
9
- super 640, 640, 16, "Target Practice"
9
+ super 640, 640
10
10
 
11
11
  register_color :darker_green, 0, 16, 0
12
12
  register_color :dark_green, 64, 96, 64
@@ -30,4 +30,4 @@ class TargetSimulation < Graphics::Simulation
30
30
  end
31
31
  end
32
32
 
33
- TargetSimulation.new.run
33
+ Radar.new.run
@@ -109,6 +109,7 @@ class SPH
109
109
  end
110
110
 
111
111
  def calculate_density
112
+ # TODO: Switch to partitioning for better speed
112
113
  # Calculate fluid density around each particle
113
114
  particles.each do |particle|
114
115
  particles.each do |neighbor|
@@ -212,7 +213,7 @@ class SimulationWindow < Graphics::Simulation
212
213
  DELTA_TIME = 0.1
213
214
 
214
215
  def initialize
215
- super WINSIZE, WINSIZE, 16, "Smoothed Particle Hydrodynamics"
216
+ super WINSIZE, WINSIZE, "Smoothed Particle Hydrodynamics"
216
217
  @simulation = SPH.new
217
218
  @scale = 15
218
219
  @s = WINSIZE.div @scale
@@ -234,9 +235,9 @@ class SimulationWindow < Graphics::Simulation
234
235
 
235
236
  # Particles
236
237
  circle(pos.x, pos.y, 5, "cubehelix_#{color}".to_sym, true)
237
-
238
- fps time
239
238
  end
239
+
240
+ fps time
240
241
  end
241
242
  end
242
243
 
@@ -106,6 +106,7 @@ class Bullet < Graphics::Body
106
106
  self.a = a
107
107
  self.m = m + 5
108
108
  w.bullets << self
109
+ w.bullet_snd.play
109
110
  end
110
111
 
111
112
  def update
@@ -121,22 +122,25 @@ class Bullet < Graphics::Body
121
122
  end
122
123
  end
123
124
 
124
- class TargetSimulation < Graphics::Simulation
125
+ class Tanks < Graphics::Simulation
125
126
  attr_accessor :tank, :bullets
126
127
  attr_accessor :body_img
127
128
  attr_accessor :turret_img
129
+ attr_accessor :bullet_snd
128
130
 
129
131
  def initialize
130
- super 640, 640, 16, "Target Practice"
132
+ super 640, 640
131
133
 
132
134
  self.tank = Tank.new self
133
135
  self.bullets = []
134
136
 
137
+ open_mixer 8
135
138
  register_body tank
136
139
  register_bodies bullets
137
140
 
138
141
  self.body_img = image "resources/images/body.png"
139
142
  self.turret_img = image "resources/images/turret.png"
143
+ self.bullet_snd = audio "resources/sounds/bullet.wav"
140
144
  end
141
145
 
142
146
  def initialize_keys
@@ -158,4 +162,4 @@ class TargetSimulation < Graphics::Simulation
158
162
  end
159
163
  end
160
164
 
161
- TargetSimulation.new.run
165
+ Tanks.new.run
@@ -106,13 +106,13 @@ class Bullet
106
106
  end
107
107
  end
108
108
 
109
- class TargetSimulation < Graphics::Simulation
109
+ class TankSprites < Graphics::Simulation
110
110
  attr_accessor :tank, :bullets
111
111
  attr_accessor :body_img
112
112
  attr_accessor :turret_img
113
113
 
114
114
  def initialize
115
- super 640, 640, 16, "Target Practice"
115
+ super 640, 640
116
116
 
117
117
  self.tank = Tank.new w/2, h/2
118
118
  self.bullets = []
@@ -169,4 +169,4 @@ class TargetSimulation < Graphics::Simulation
169
169
  end
170
170
  end
171
171
 
172
- TargetSimulation.new.run
172
+ TankSprites.new.run
@@ -2,13 +2,13 @@
2
2
 
3
3
  require "graphics"
4
4
 
5
- class TargetSimulation < Graphics::Simulation
5
+ class Targeting < Graphics::Simulation
6
6
  CLEAR_COLOR = :darker_green
7
7
 
8
8
  attr_accessor :bombs
9
9
 
10
10
  def initialize
11
- super 640, 640, 16, "Target Practice"
11
+ super 640, 640
12
12
 
13
13
  self.bombs = []
14
14
  register_color :darker_green, 0, 16, 0
@@ -45,4 +45,4 @@ class TargetSimulation < Graphics::Simulation
45
45
  end
46
46
  end
47
47
 
48
- TargetSimulation.new.run
48
+ Targeting.new.run
@@ -15,7 +15,7 @@ class Vant < Graphics::Body
15
15
  def initialize w
16
16
  super
17
17
  self.a = random_angle
18
- self.s = w.screen
18
+ self.s = w.renderer
19
19
 
20
20
  self.white = w.color[:white]
21
21
  self.black = w.color[:black]
@@ -23,10 +23,9 @@ class Vant < Graphics::Body
23
23
 
24
24
  def update
25
25
  move_by a, M
26
- mutate
27
26
  end
28
27
 
29
- def mutate
28
+ def draw
30
29
  if s[x, y] == white then
31
30
  s[x, y] = black
32
31
  turn 270
@@ -43,11 +42,21 @@ class Vants < Graphics::Drawing
43
42
  CLEAR_COLOR = :white
44
43
 
45
44
  def initialize
46
- super 850, 850, 16, self.class.name
45
+ super 850, 850
47
46
 
48
47
  self.vs = populate Vant
49
48
  register_bodies vs
50
49
  end
50
+
51
+ def draw n
52
+ draw_on texture do
53
+ _bodies.each do |a|
54
+ a.each do |v|
55
+ v.draw
56
+ end
57
+ end
58
+ end
59
+ end
51
60
  end
52
61
 
53
62
  Vants.new.run if $0 == __FILE__