chingu 0.8.0.5 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'benchmark'
4
+
5
+
6
+ @array = []
7
+ @hash = {}
8
+ Benchmark.bm(22) do |x|
9
+ x.report('add/remove from array') { 100000.times { |nr| @array.push(nr); @array.delete(nr-1) if nr > 100 } }
10
+ x.report('add/remove from hash') { 100000.times { |nr| @hash[nr] = true; @hash.delete(nr-1) if nr > 100 } }
11
+
12
+ @array.clear
13
+ @hash.clear
14
+ x.report('add to array') { 100000.times { |nr| @array.push(nr) } }
15
+ x.report('add to hash') { 100000.times { |nr| @hash[nr] = true } }
16
+
17
+ x.report('array.each') { 100.times { |nr| @array.each { |x| x } } }
18
+ x.report('hash.each') { 100.times { |nr| @hash.each { |k,v| k } } }
19
+ x.report('hash.each_key') { 100.times { |nr| @hash.each_key { |x| x } } }
20
+ end
@@ -0,0 +1,92 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'benchmark'
4
+ require File.join(File.dirname($0), "..", "lib", "chingu")
5
+ #gem 'chingu', '0.6'
6
+ #require 'chingu'
7
+ include Gosu
8
+ include Chingu
9
+
10
+ class MyGameObject < GameObject
11
+ end
12
+
13
+ class MyBasicGameObject < GameObject
14
+ end
15
+
16
+ class ObjectWithTraits < GameObject
17
+ traits :velocity, :timer
18
+ end
19
+
20
+
21
+ class Game < Chingu::Window
22
+ def initialize
23
+ super(600,200)
24
+ self.input = { :esc => :exit }
25
+
26
+ Benchmark.bm(22) do |x|
27
+ x.report('create 50000 game objects') { 50000.times { MyGameObject.create } }
28
+ x.report('force_update') { 60.times { $window.game_objects.force_update } }
29
+ x.report('force_draw') { 60.times { $window.game_objects.force_draw } }
30
+ x.report('update') { 60.times { $window.game_objects.update } }
31
+ x.report('draw') { 60.times { $window.game_objects.draw } }
32
+ x.report('update + create') { 60.times { $window.game_objects.update; MyGameObject.create; } }
33
+ x.report('draw + create') { 60.times { $window.game_objects.draw; MyGameObject.create; } }
34
+ x.report('update + create + destroy') { 60.times { $window.game_objects.update; MyGameObject.create; MyGameObject.all.first.destroy } }
35
+ x.report('draw + create + destroy') { 60.times { $window.game_objects.draw; MyGameObject.create; MyGameObject.all.first.destroy } }
36
+ end
37
+ end
38
+ end
39
+
40
+ Game.new.show
41
+
42
+ #
43
+ # BEFORE GameObjectList visible_game_object/unpaused_game_object - refactor
44
+ #
45
+ #create 50000 game objects 1.919000 0.000000 1.919000 ( 1.906109)
46
+ #force_update 0.702000 0.000000 0.702000 ( 0.716041)
47
+ #force_draw 0.920000 0.000000 0.920000 ( 0.924053)
48
+ #update 1.420000 0.000000 1.420000 ( 1.421081)
49
+ #draw 1.497000 0.000000 1.497000 ( 1.520087)
50
+ #update + create 1.420000 0.000000 1.420000 ( 1.420081)
51
+ #draw + create 1.497000 0.000000 1.497000 ( 1.528088)
52
+ #update + create + destroy 2.106000 0.000000 2.106000 ( 2.099120)
53
+ #draw + create + destroy 2.262000 0.000000 2.262000 ( 2.265129)
54
+
55
+ #
56
+ # AFTER
57
+ #
58
+ #create 50000 game objects 1.934000 0.000000 1.934000 ( 1.927111)
59
+ #force_update 0.718000 0.000000 0.718000 ( 0.712040)
60
+ #force_draw 0.920000 0.000000 0.920000 ( 0.929053)
61
+ #update 0.702000 0.000000 0.702000 ( 0.708041)
62
+ #draw 0.904000 0.000000 0.904000 ( 0.932053)
63
+ #update + create 0.671000 0.000000 0.671000 ( 0.688040)
64
+ #draw + create 0.921000 0.000000 0.921000 ( 0.915052)
65
+ #update + create + destroy 1.950000 0.000000 1.950000 ( 1.957112)
66
+ #draw + create + destroy 2.184000 0.000000 2.184000 ( 2.230128)
67
+
68
+ #
69
+ # WITH HASH instead of ARRAY
70
+ #
71
+ #create 50000 game objects 0.733000 0.016000 0.749000 ( 0.737042)
72
+ #force_update 1.045000 0.000000 1.045000 ( 1.048060)
73
+ #force_draw 1.154000 0.000000 1.154000 ( 1.150065)
74
+ #update 1.014000 0.000000 1.014000 ( 1.028058)
75
+ #draw 1.139000 0.000000 1.139000 ( 1.152066)
76
+ #update + create 1.045000 0.000000 1.045000 ( 1.041060)
77
+ #draw + create 1.155000 0.000000 1.155000 ( 1.155066)
78
+ #update + create + destroy 1.529000 0.000000 1.529000 ( 1.525087)
79
+ #draw + create + destroy 1.653000 0.000000 1.653000 ( 1.661095)
80
+
81
+
82
+ #
83
+ # CONCLUSION
84
+ #
85
+ # Even when we're adding or destroying objects each game tick
86
+ # we benefit greatly from keeping separate draw and update lists
87
+ # instead of checking visible/paused flags in the draw/update loop itself
88
+ #
89
+ # Hash would be better then Array if we create/destroy tons of object.
90
+ # Arrays are faster to iterate through, which we do alot of gametick.
91
+ #
92
+ #
@@ -10,13 +10,28 @@ include Chingu
10
10
  class ObjectX < GameObject; end;
11
11
  class ObjectY < GameObject; end;
12
12
  class ObjectZ < GameObject; end;
13
+ class ObjectWithTraits < GameObject
14
+ traits :velocity, :timer
15
+ end
16
+
17
+ class Test
18
+ @instances = []
19
+ class << self; attr_accessor :instances end
20
+
21
+ def initialize
22
+ self.class.instances << self
23
+ end
24
+ end
13
25
 
14
26
  class Game < Chingu::Window
15
27
  def initialize
16
28
  super(600,200)
17
- self.input = { :esc => :exit }
18
-
29
+ self.input = { :esc => :exit }
30
+ end
31
+
32
+ def update
19
33
  Benchmark.bm(22) do |x|
34
+ game_object = Test.new
20
35
  game_object = GameObject.new
21
36
  x.report('respond_to?') { 100000.times { game_object.respond_to?(:update_trait) } }
22
37
  x.report('call empty method') { 100000.times { game_object.update_trait } }
@@ -25,20 +40,41 @@ class Game < Chingu::Window
25
40
  game_objects.clear
26
41
  x.report('basic_game_objects') { 10000.times { BasicGameObject.create } }
27
42
  game_objects.clear
28
- x.report('ObjectX') { 10000.times { ObjectX.create } }
29
- x.report('ObjectY') { 10000.times { ObjectY.create } }
30
- x.report('ObjectZ') { 10000.times { ObjectZ.create } }
31
-
32
- x.report('ObjectX.each') { ObjectX.all.each { |x| } }
33
- x.report('ObjectY.each') { ObjectY.all.each { |y| } }
34
- x.report('ObjectZ.each') { ObjectZ.all.each { |z| } }
35
-
43
+ x.report('ObjectX') { 100000.times { ObjectX.create } }
44
+ x.report('ObjectY') { 100000.times { ObjectY.create } }
45
+ x.report('ObjectZ') { 100000.times { ObjectZ.create } }
46
+ x.report('ObjectWithTraits') { 100000.times { ObjectWithTraits.create } }
47
+
48
+ x.report('ObjectX full update') { ObjectX.each { |x| x.update_trait; x.update; } }
49
+ x.report('ObjectWithTraits full update') { ObjectWithTraits.each { |x| x.update_trait; x.update; } }
50
+
51
+ x.report('ObjectX.each') { ObjectX.all.each { |x| x } }
52
+ x.report('ObjectY.each') { ObjectY.all.each { |y| y } }
53
+ x.report('ObjectZ.each') { ObjectZ.all.each { |z| z } }
54
+
55
+
36
56
  p game_objects.size
37
- x.report('ObjectX destroy') { ObjectX.each_with_index { |x, i| x.destroy if i%2==0 }; game_objects.sync; }
57
+ #x.report('ObjectX destroy') { ObjectX.each_with_index { |x, i| x.destroy if i%2==0 }; game_objects.sync; }
38
58
  p game_objects.size
59
+ exit
39
60
  end
40
-
41
61
  end
42
62
  end
43
63
 
44
64
  Game.new.show
65
+
66
+
67
+ # user system total real
68
+ #respond_to? 0.015000 0.000000 0.015000 ( 0.016001)
69
+ #call empty method 0.016000 0.000000 0.016000 ( 0.013001)
70
+ #game_objects 0.327000 0.000000 0.327000 ( 0.329019)
71
+ #basic_game_objects 0.047000 0.000000 0.047000 ( 0.038002)
72
+ #ObjectX 0.312000 0.000000 0.312000 ( 0.314018)
73
+ #ObjectY 0.375000 0.000000 0.375000 ( 0.372021)
74
+ #ObjectZ 0.327000 0.000000 0.327000 ( 0.335020)
75
+ #ObjectWithTraits 0.484000 0.000000 0.484000 ( 0.484027)
76
+ #ObjectX full update 0.015000 0.000000 0.015000 ( 0.012001)
77
+ #ObjectWithTraits full update 0.032000 0.000000 0.032000 ( 0.034002)
78
+ #ObjectX.each 0.015000 0.000000 0.015000 ( 0.009000)
79
+ #ObjectY.each 0.000000 0.000000 0.000000 ( 0.010001)
80
+ #ObjectZ.each 0.016000 0.000000 0.016000 ( 0.009001)
@@ -0,0 +1,9 @@
1
+ require 'benchmark'
2
+
3
+ Benchmark.bm(22) do |x|
4
+ @map = Array.new
5
+ @map[0] = Array.new
6
+ @map[0][0] = "item"
7
+ x.report('lookup fail exception') { 1000000.times { @map[0][10] rescue nil } }
8
+ x.report('lookup fail') { 1000000.times { @map[0] && @map[0][10] } }
9
+ end
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'benchmark'
4
+ require File.join(File.dirname($0), "..", "lib", "chingu")
5
+ include Gosu
6
+ include Chingu
7
+
8
+ # This includes trait :sprite
9
+ class SimpleGameObject < BasicGameObject
10
+ trait :simple_sprite
11
+ end
12
+
13
+ class ObjectX < GameObject
14
+ traits :velocity, :timer
15
+ end
16
+
17
+ class Test
18
+ @instances = []
19
+ class << self; attr_accessor :instances end
20
+
21
+ def initialize
22
+ self.class.instances << self
23
+ end
24
+ end
25
+
26
+ class Game < Chingu::Window
27
+ def initialize
28
+ super(600,200)
29
+ self.input = { :esc => :exit }
30
+
31
+ Benchmark.bm(22) do |x|
32
+ #x.report('GameObject.destroy_all') { GameObject.destroy_all } # SLOW!
33
+ x.report('SimpleameObject.create') { 50000.times { SimpleGameObject.create } }
34
+ x.report('ClassicGameObject.create') { 50000.times { ClassicGameObject.create } }
35
+ x.report('GameObject.create') { 50000.times { GameObject.create } }
36
+ x.report('GameObject.create') { 50000.times { GameObject.create } }
37
+ x.report('GameObject.create') { 50000.times { GameObject.create } }
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+
44
+ Game.new.show
45
+
46
+
47
+ # 50000 x GameObject
48
+
49
+ #
50
+ # With set_options()
51
+ # user system total real
52
+ #GameObject.create 1.825000 0.000000 1.825000 ( 1.824104)
53
+ #GameObject.create 2.044000 0.016000 2.060000 ( 2.083119)
54
+ #GameObject.create 1.934000 0.000000 1.934000 ( 1.996114)
55
+
56
+ #
57
+ # With oldschool option-parsing
58
+ #
59
+ # user system total real
60
+ #GameObject.create 0.577000 0.000000 0.577000 ( 0.574033)
61
+ #GameObject.create 0.577000 0.015000 0.592000 ( 0.599034)
62
+ #GameObject.create 0.624000 0.000000 0.624000 ( 0.623036)
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'benchmark'
4
+ require File.join(File.dirname($0), "..", "lib", "chingu")
5
+ #gem 'chingu', '0.6'
6
+ #require 'chingu'
7
+ include Gosu
8
+ include Chingu
9
+
10
+ class MyGameObject < GameObject
11
+ end
12
+
13
+ class Player < GameObject
14
+ end
15
+
16
+
17
+
18
+ class Game < Chingu::Window
19
+ def initialize
20
+ super(600,200)
21
+
22
+ player = Player.create
23
+
24
+
25
+ Benchmark.bm(22) do |x|
26
+ x.report('create 10000 game objects') { 10000.times { MyGameObject.create } }
27
+ x.report('update') { 60.times { $window.update } }
28
+
29
+ self.input = { :esc => :exit, :d => :exit, :a => :exit }
30
+ player.input = { :holding_up => :exit, :holding_down => :exit, :holding_left => :exit, :holding_right => :exit, :space => :exit, :left_ctrl => :exit }
31
+
32
+ x.report('update window/player input') { 60.times { $window.update } }
33
+ end
34
+ end
35
+ end
36
+
37
+ Game.new.show
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{chingu}
8
- s.version = "0.8.0.5"
8
+ s.version = "0.8.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["ippa"]
12
- s.date = %q{2011-01-03}
12
+ s.date = %q{2011-01-08}
13
13
  s.description = %q{OpenGL accelerated 2D game framework for Ruby. Builds on Gosu (Ruby/C++) which provides all the core functionality. Chingu adds simple yet powerful game states, prettier input handling, deployment safe asset-handling, a basic re-usable game object and stackable game logic.}
14
14
  s.email = %q{ippa@rubylicio.us}
15
15
  s.extra_rdoc_files = [
@@ -23,15 +23,20 @@ Gem::Specification.new do |s|
23
23
  "README.rdoc",
24
24
  "Rakefile",
25
25
  "benchmarks/README.txt",
26
+ "benchmarks/array_vs_hash.rb",
26
27
  "benchmarks/arrays_bench.rb",
27
28
  "benchmarks/benchmark.rb",
28
29
  "benchmarks/benchmark3.rb",
29
30
  "benchmarks/benchmark4.rb",
30
31
  "benchmarks/benchmark5.rb",
31
32
  "benchmarks/benchmark6.rb",
33
+ "benchmarks/game_object_list_benchmark.rb",
32
34
  "benchmarks/game_objects_benchmark.rb",
35
+ "benchmarks/lookup_benchmark.rb",
33
36
  "benchmarks/meta_benchmark.rb",
34
37
  "benchmarks/meta_benchmark2.rb",
38
+ "benchmarks/trait_benchmark.rb",
39
+ "benchmarks/window_benchmark.rb",
35
40
  "chingu.gemspec",
36
41
  "examples/example10_traits_retrofy.rb",
37
42
  "examples/example11_animation.rb",
@@ -155,6 +160,7 @@ Gem::Specification.new do |s|
155
160
  "lib/chingu/traits/collision_detection.rb",
156
161
  "lib/chingu/traits/effect.rb",
157
162
  "lib/chingu/traits/retrofy.rb",
163
+ "lib/chingu/traits/simple_sprite.rb",
158
164
  "lib/chingu/traits/sprite.rb",
159
165
  "lib/chingu/traits/timer.rb",
160
166
  "lib/chingu/traits/velocity.rb",
@@ -167,6 +173,7 @@ Gem::Specification.new do |s|
167
173
  "spec/chingu/console_spec.rb",
168
174
  "spec/chingu/fpscounter_spec.rb",
169
175
  "spec/chingu/game_object_list_spec.rb",
176
+ "spec/chingu/game_object_map_spec.rb",
170
177
  "spec/chingu/game_object_spec.rb",
171
178
  "spec/chingu/game_state_manager_spec.rb",
172
179
  "spec/chingu/helpers/input_client_spec.rb",
@@ -224,6 +231,7 @@ Gem::Specification.new do |s|
224
231
  "spec/chingu/console_spec.rb",
225
232
  "spec/chingu/fpscounter_spec.rb",
226
233
  "spec/chingu/game_object_list_spec.rb",
234
+ "spec/chingu/game_object_map_spec.rb",
227
235
  "spec/chingu/game_object_spec.rb",
228
236
  "spec/chingu/game_state_manager_spec.rb",
229
237
  "spec/chingu/helpers/input_client_spec.rb",
@@ -78,9 +78,10 @@ class Star < GameObject
78
78
  trait :collision_detection
79
79
 
80
80
  def initialize(options={})
81
- super(:zorder=>1)
82
- @animation = Chingu::Animation.new(:file => media_path("Star.png"), :size => 25)
81
+ super
82
+ @animation = Chingu::Animation.new(:file => "Star.png", :size => 25)
83
83
  @image = @animation.next
84
+ self.zorder = 1
84
85
  self.color = Gosu::Color.new(0xff000000)
85
86
  self.color.red = rand(255 - 40) + 40
86
87
  self.color.green = rand(255 - 40) + 40
@@ -10,8 +10,8 @@ include Chingu
10
10
  #
11
11
  class Game < Chingu::Window
12
12
  def initialize
13
- #super(1000,700)
14
- super(1024,768, true)
13
+ super(1000,700)
14
+ #super(1024,768, true)
15
15
  end
16
16
 
17
17
  def setup
@@ -89,7 +89,7 @@ class Droid < Chingu::GameObject
89
89
  trait :bounding_box, :scale => 0.80
90
90
  traits :timer, :collision_detection , :timer, :velocity
91
91
 
92
- attr_reader :jumpign
92
+ attr_reader :jumping
93
93
 
94
94
  def setup
95
95
  self.input = { [:holding_left, :holding_a] => :holding_left,
@@ -110,8 +110,9 @@ class Droid < Chingu::GameObject
110
110
  self.acceleration_y = 0.5
111
111
  self.max_velocity = 10
112
112
  self.rotation_center = :bottom_center
113
-
113
+
114
114
  update
115
+ cache_bounding_box
115
116
  end
116
117
 
117
118
  def die
@@ -287,6 +288,7 @@ class Block < GameObject
287
288
  def setup
288
289
  @image = Image["black_block.png"]
289
290
  @color = Color.new(0xff808080)
291
+ cache_bounding_box
290
292
  end
291
293
  end
292
294
 
@@ -12,6 +12,7 @@ class Game < Chingu::Window
12
12
  self.input = { :escape => :exit } # exits example on Escape
13
13
 
14
14
  Player.create(:x => 0, :y => 0, :rotation_center => :top_left)
15
+ Text.create("NOTHING TO SEE HERE YET ;-)", :align => :center)
15
16
  end
16
17
  end
17
18
 
@@ -37,7 +37,7 @@ require_all "#{CHINGU_ROOT}/chingu/traits"
37
37
  require_all "#{CHINGU_ROOT}/chingu"
38
38
 
39
39
  module Chingu
40
- VERSION = "0.8.0.5"
40
+ VERSION = "0.8.1"
41
41
 
42
42
  DEBUG_COLOR = Gosu::Color.new(0xFFFF0000)
43
43
  DEBUG_ZORDER = 9999
@@ -25,7 +25,8 @@ module Chingu
25
25
  # - step: [steps] move animation forward [steps] frames each time we call #next
26
26
  #
27
27
  def initialize(options)
28
- options = {:step => 1, :loop => true, :bounce => false, :width => 32, :height => 32, :index => 0, :delay => 100}.merge(options)
28
+ #options = {:step => 1, :loop => true, :bounce => false, :width => 32, :height => 32, :index => 0, :delay => 100}.merge(options)
29
+ options = {:step => 1, :loop => true, :bounce => false, :index => 0, :delay => 100}.merge(options)
29
30
 
30
31
  @loop = options[:loop]
31
32
  @bounce = options[:bounce]