summaryrefslogtreecommitdiffhomepage
path: root/samples/09_performance
diff options
context:
space:
mode:
Diffstat (limited to 'samples/09_performance')
-rw-r--r--samples/09_performance/01_sprites_as_hash/app/main.rb6
-rw-r--r--samples/09_performance/02_sprites_as_entities/app/main.rb6
-rw-r--r--samples/09_performance/03_sprites_as_strict_entities/app/main.rb6
-rw-r--r--samples/09_performance/03_sprites_as_struct/app/main.rb82
-rw-r--r--samples/09_performance/03_sprites_as_struct/license-for-sample.txt9
-rw-r--r--samples/09_performance/03_sprites_as_struct/sprites/tiny-star.pngbin0 -> 112 bytes
-rw-r--r--samples/09_performance/04_sprites_as_classes/app/main.rb5
-rw-r--r--samples/09_performance/04_sprites_as_strict_entities/app/main.rb72
-rw-r--r--samples/09_performance/04_sprites_as_strict_entities/license-for-sample.txt9
-rw-r--r--samples/09_performance/04_sprites_as_strict_entities/sprites/tiny-star.pngbin0 -> 112 bytes
-rw-r--r--samples/09_performance/05_sprites_as_classes/app/main.rb55
-rw-r--r--samples/09_performance/05_sprites_as_classes/license-for-sample.txt9
-rw-r--r--samples/09_performance/05_sprites_as_classes/sprites/tiny-star.pngbin0 -> 112 bytes
-rw-r--r--samples/09_performance/05_static_sprites_as_classes/app/main.rb7
-rw-r--r--samples/09_performance/06_static_sprites_as_classes/app/main.rb56
-rw-r--r--samples/09_performance/06_static_sprites_as_classes/license-for-sample.txt9
-rw-r--r--samples/09_performance/06_static_sprites_as_classes/sprites/tiny-star.pngbin0 -> 112 bytes
-rw-r--r--samples/09_performance/06_static_sprites_as_classes_with_custom_drawing/app/main.rb18
-rw-r--r--samples/09_performance/07_collision_limits/app/main.rb2
-rw-r--r--samples/09_performance/07_static_sprites_as_classes_with_custom_drawing/app/main.rb88
-rw-r--r--samples/09_performance/07_static_sprites_as_classes_with_custom_drawing/license-for-sample.txt9
-rw-r--r--samples/09_performance/07_static_sprites_as_classes_with_custom_drawing/sprites/tiny-star.pngbin0 -> 112 bytes
-rw-r--r--samples/09_performance/08_collision_limits/app/main.rb55
-rw-r--r--samples/09_performance/08_collision_limits/license-for-sample.txt9
24 files changed, 507 insertions, 5 deletions
diff --git a/samples/09_performance/01_sprites_as_hash/app/main.rb b/samples/09_performance/01_sprites_as_hash/app/main.rb
index 6bbb295..d032900 100644
--- a/samples/09_performance/01_sprites_as_hash/app/main.rb
+++ b/samples/09_performance/01_sprites_as_hash/app/main.rb
@@ -39,7 +39,11 @@ def tick args
# sets console command when sample app initially opens
if Kernel.global_tick_count == 0
- puts "* INFO - Please specify the number of sprites to render."
+ puts ""
+ puts ""
+ puts "========================================================="
+ puts "* INFO: Sprites, Hashes"
+ puts "* INFO: Please specify the number of sprites to render."
args.gtk.console.set_command "reset_with count: 100"
end
diff --git a/samples/09_performance/02_sprites_as_entities/app/main.rb b/samples/09_performance/02_sprites_as_entities/app/main.rb
index 21babda..dba02ae 100644
--- a/samples/09_performance/02_sprites_as_entities/app/main.rb
+++ b/samples/09_performance/02_sprites_as_entities/app/main.rb
@@ -39,7 +39,11 @@ def tick args
# sets console command when sample app initially opens
if Kernel.global_tick_count == 0
- puts "* INFO - Please specify the number of sprites to render."
+ puts ""
+ puts ""
+ puts "========================================================="
+ puts "* INFO: Sprites, Open Entities"
+ puts "* INFO: Please specify the number of sprites to render."
args.gtk.console.set_command "reset_with count: 100"
end
diff --git a/samples/09_performance/03_sprites_as_strict_entities/app/main.rb b/samples/09_performance/03_sprites_as_strict_entities/app/main.rb
index 376d9a1..a2688c2 100644
--- a/samples/09_performance/03_sprites_as_strict_entities/app/main.rb
+++ b/samples/09_performance/03_sprites_as_strict_entities/app/main.rb
@@ -43,7 +43,11 @@ def tick args
# sets console command when sample app initially opens
if Kernel.global_tick_count == 0
- puts "* INFO - Please specify the number of sprites to render."
+ puts ""
+ puts ""
+ puts "========================================================="
+ puts "* INFO: Sprites, Strict Entities"
+ puts "* INFO: Please specify the number of sprites to render."
args.gtk.console.set_command "reset_with count: 100"
end
diff --git a/samples/09_performance/03_sprites_as_struct/app/main.rb b/samples/09_performance/03_sprites_as_struct/app/main.rb
new file mode 100644
index 0000000..7b738b7
--- /dev/null
+++ b/samples/09_performance/03_sprites_as_struct/app/main.rb
@@ -0,0 +1,82 @@
+# create a Struct variant that allows for named parameters on construction.
+class NamedStruct < Struct
+ def initialize **opts
+ super(*members.map { |k| opts[k] })
+ end
+end
+
+# create a Star NamedStruct
+Star = NamedStruct.new(:x, :y, :w, :h, :path, :s,
+ :angle, :angle_anchor_x, :angle_anchor_y,
+ :r, :g, :b, :a,
+ :tile_x, :tile_y,
+ :tile_w, :tile_h,
+ :source_x, :source_y,
+ :source_w, :source_h,
+ :flip_horizontally, :flip_vertically,
+ :blendmode_enum)
+
+# Sprites represented as Structs. They require a little bit more code than Hashes,
+# but are the a little faster to render too.
+def random_x args
+ (args.grid.w.randomize :ratio) * -1
+end
+
+def random_y args
+ (args.grid.h.randomize :ratio) * -1
+end
+
+def random_speed
+ 1 + (4.randomize :ratio)
+end
+
+def new_star args
+ Star.new x: (random_x args),
+ y: (random_y args),
+ w: 4, h: 4,
+ path: 'sprites/tiny-star.png',
+ s: random_speed
+end
+
+def move_star args, star
+ star.x += star[:s]
+ star.y += star[:s]
+ if star.x > args.grid.w || star.y > args.grid.h
+ star.x = (random_x args)
+ star.y = (random_y args)
+ star[:s] = random_speed
+ end
+end
+
+def tick args
+ args.state.star_count ||= 0
+
+ # sets console command when sample app initially opens
+ if Kernel.global_tick_count == 0
+ puts ""
+ puts ""
+ puts "========================================================="
+ puts "* INFO: Sprites, Structs"
+ puts "* INFO: Please specify the number of sprites to render."
+ args.gtk.console.set_command "reset_with count: 100"
+ end
+
+ # init
+ if args.state.tick_count == 0
+ args.state.stars = args.state.star_count.map { |i| new_star args }
+ end
+
+ # update
+ args.state.stars.each { |s| move_star args, s }
+
+ # render
+ args.outputs.sprites << args.state.stars
+ args.outputs.background_color = [0, 0, 0]
+ args.outputs.primitives << args.gtk.current_framerate_primitives
+end
+
+# resets game, and assigns star count given by user
+def reset_with count: count
+ $gtk.reset
+ $gtk.args.state.star_count = count
+end
diff --git a/samples/09_performance/03_sprites_as_struct/license-for-sample.txt b/samples/09_performance/03_sprites_as_struct/license-for-sample.txt
new file mode 100644
index 0000000..175ac25
--- /dev/null
+++ b/samples/09_performance/03_sprites_as_struct/license-for-sample.txt
@@ -0,0 +1,9 @@
+Copyright 2021 DragonRuby LLC
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/samples/09_performance/03_sprites_as_struct/sprites/tiny-star.png b/samples/09_performance/03_sprites_as_struct/sprites/tiny-star.png
new file mode 100644
index 0000000..e04786a
--- /dev/null
+++ b/samples/09_performance/03_sprites_as_struct/sprites/tiny-star.png
Binary files differ
diff --git a/samples/09_performance/04_sprites_as_classes/app/main.rb b/samples/09_performance/04_sprites_as_classes/app/main.rb
index 2b43a98..67cde79 100644
--- a/samples/09_performance/04_sprites_as_classes/app/main.rb
+++ b/samples/09_performance/04_sprites_as_classes/app/main.rb
@@ -26,6 +26,11 @@ end
def tick args
# sets console command when sample app initially opens
if Kernel.global_tick_count == 0
+ puts ""
+ puts ""
+ puts "========================================================="
+ puts "* INFO: Sprites, Classes"
+ puts "* INFO: Please specify the number of sprites to render."
args.gtk.console.set_command "reset_with count: 100"
end
diff --git a/samples/09_performance/04_sprites_as_strict_entities/app/main.rb b/samples/09_performance/04_sprites_as_strict_entities/app/main.rb
new file mode 100644
index 0000000..a2688c2
--- /dev/null
+++ b/samples/09_performance/04_sprites_as_strict_entities/app/main.rb
@@ -0,0 +1,72 @@
+# Sprites represented as StrictEntities using the queue ~args.outputs.sprites~
+# yields apis access similar to Entities, but all properties that can be set on the
+# entity must be predefined with a default value. Strict entities do not support the
+# addition of new properties after the fact. They are more performant than OpenEntities
+# because of this constraint.
+def random_x args
+ (args.grid.w.randomize :ratio) * -1
+end
+
+def random_y args
+ (args.grid.h.randomize :ratio) * -1
+end
+
+def random_speed
+ 1 + (4.randomize :ratio)
+end
+
+def new_star args
+ args.state.new_entity_strict(:star,
+ x: (random_x args),
+ y: (random_y args),
+ w: 4, h: 4,
+ path: 'sprites/tiny-star.png',
+ s: random_speed) do |entity|
+ # invoke attr_sprite so that it responds to
+ # all properties that are required to render a sprite
+ entity.attr_sprite
+ end
+end
+
+def move_star args, star
+ star.x += star.s
+ star.y += star.s
+ if star.x > args.grid.w || star.y > args.grid.h
+ star.x = (random_x args)
+ star.y = (random_y args)
+ star.s = random_speed
+ end
+end
+
+def tick args
+ args.state.star_count ||= 0
+
+ # sets console command when sample app initially opens
+ if Kernel.global_tick_count == 0
+ puts ""
+ puts ""
+ puts "========================================================="
+ puts "* INFO: Sprites, Strict Entities"
+ puts "* INFO: Please specify the number of sprites to render."
+ args.gtk.console.set_command "reset_with count: 100"
+ end
+
+ # init
+ if args.state.tick_count == 0
+ args.state.stars = args.state.star_count.map { |i| new_star args }
+ end
+
+ # update
+ args.state.stars.each { |s| move_star args, s }
+
+ # render
+ args.outputs.sprites << args.state.stars
+ args.outputs.background_color = [0, 0, 0]
+ args.outputs.primitives << args.gtk.current_framerate_primitives
+end
+
+# resets game, and assigns star count given by user
+def reset_with count: count
+ $gtk.reset
+ $gtk.args.state.star_count = count
+end
diff --git a/samples/09_performance/04_sprites_as_strict_entities/license-for-sample.txt b/samples/09_performance/04_sprites_as_strict_entities/license-for-sample.txt
new file mode 100644
index 0000000..100dcec
--- /dev/null
+++ b/samples/09_performance/04_sprites_as_strict_entities/license-for-sample.txt
@@ -0,0 +1,9 @@
+Copyright 2019 DragonRuby LLC
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/samples/09_performance/04_sprites_as_strict_entities/sprites/tiny-star.png b/samples/09_performance/04_sprites_as_strict_entities/sprites/tiny-star.png
new file mode 100644
index 0000000..e04786a
--- /dev/null
+++ b/samples/09_performance/04_sprites_as_strict_entities/sprites/tiny-star.png
Binary files differ
diff --git a/samples/09_performance/05_sprites_as_classes/app/main.rb b/samples/09_performance/05_sprites_as_classes/app/main.rb
new file mode 100644
index 0000000..67cde79
--- /dev/null
+++ b/samples/09_performance/05_sprites_as_classes/app/main.rb
@@ -0,0 +1,55 @@
+# Sprites represented as Classes using the queue ~args.outputs.sprites~.
+# gives you full control of property declaration and method invocation.
+# They are more performant than OpenEntities and StrictEntities, but more code upfront.
+class Star
+ attr_sprite
+
+ def initialize grid
+ @grid = grid
+ @x = (rand @grid.w) * -1
+ @y = (rand @grid.h) * -1
+ @w = 4
+ @h = 4
+ @s = 1 + (4.randomize :ratio)
+ @path = 'sprites/tiny-star.png'
+ end
+
+ def move
+ @x += @s
+ @y += @s
+ @x = (rand @grid.w) * -1 if @x > @grid.right
+ @y = (rand @grid.h) * -1 if @y > @grid.top
+ end
+end
+
+# calls methods needed for game to run properly
+def tick args
+ # sets console command when sample app initially opens
+ if Kernel.global_tick_count == 0
+ puts ""
+ puts ""
+ puts "========================================================="
+ puts "* INFO: Sprites, Classes"
+ puts "* INFO: Please specify the number of sprites to render."
+ args.gtk.console.set_command "reset_with count: 100"
+ end
+
+ # init
+ if args.state.tick_count == 0
+ args.state.stars = args.state.star_count.map { |i| Star.new args.grid }
+ end
+
+ # update
+ args.state.stars.each(&:move)
+
+ # render
+ args.outputs.sprites << args.state.stars
+ args.outputs.background_color = [0, 0, 0]
+ args.outputs.primitives << args.gtk.current_framerate_primitives
+end
+
+# resets game, and assigns star count given by user
+def reset_with count: count
+ $gtk.reset
+ $gtk.args.state.star_count = count
+end
diff --git a/samples/09_performance/05_sprites_as_classes/license-for-sample.txt b/samples/09_performance/05_sprites_as_classes/license-for-sample.txt
new file mode 100644
index 0000000..100dcec
--- /dev/null
+++ b/samples/09_performance/05_sprites_as_classes/license-for-sample.txt
@@ -0,0 +1,9 @@
+Copyright 2019 DragonRuby LLC
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/samples/09_performance/05_sprites_as_classes/sprites/tiny-star.png b/samples/09_performance/05_sprites_as_classes/sprites/tiny-star.png
new file mode 100644
index 0000000..e04786a
--- /dev/null
+++ b/samples/09_performance/05_sprites_as_classes/sprites/tiny-star.png
Binary files differ
diff --git a/samples/09_performance/05_static_sprites_as_classes/app/main.rb b/samples/09_performance/05_static_sprites_as_classes/app/main.rb
index cbfe00a..db5bf8e 100644
--- a/samples/09_performance/05_static_sprites_as_classes/app/main.rb
+++ b/samples/09_performance/05_static_sprites_as_classes/app/main.rb
@@ -27,19 +27,24 @@ end
def tick args
# sets console command when sample app initially opens
if Kernel.global_tick_count == 0
+ puts ""
+ puts ""
+ puts "========================================================="
+ puts "* INFO: Static Sprites, Classes"
+ puts "* INFO: Please specify the number of sprites to render."
args.gtk.console.set_command "reset_with count: 100"
end
# init
if args.state.tick_count == 0
args.state.stars = args.state.star_count.map { |i| Star.new args.grid }
+ args.outputs.static_sprites << args.state.stars
end
# update
args.state.stars.each(&:move)
# render
- args.outputs.sprites << args.state.stars
args.outputs.background_color = [0, 0, 0]
args.outputs.primitives << args.gtk.current_framerate_primitives
end
diff --git a/samples/09_performance/06_static_sprites_as_classes/app/main.rb b/samples/09_performance/06_static_sprites_as_classes/app/main.rb
new file mode 100644
index 0000000..db5bf8e
--- /dev/null
+++ b/samples/09_performance/06_static_sprites_as_classes/app/main.rb
@@ -0,0 +1,56 @@
+# Sprites represented as Classes using the queue ~args.outputs.static_sprites~.
+# bypasses the queue behavior of ~args.outputs.sprites~. All instances are held
+# by reference. You get better performance, but you are mutating state of held objects
+# which is less functional/data oriented.
+class Star
+ attr_sprite
+
+ def initialize grid
+ @grid = grid
+ @x = (rand @grid.w) * -1
+ @y = (rand @grid.h) * -1
+ @w = 4
+ @h = 4
+ @s = 1 + (4.randomize :ratio)
+ @path = 'sprites/tiny-star.png'
+ end
+
+ def move
+ @x += @s
+ @y += @s
+ @x = (rand @grid.w) * -1 if @x > @grid.right
+ @y = (rand @grid.h) * -1 if @y > @grid.top
+ end
+end
+
+# calls methods needed for game to run properly
+def tick args
+ # sets console command when sample app initially opens
+ if Kernel.global_tick_count == 0
+ puts ""
+ puts ""
+ puts "========================================================="
+ puts "* INFO: Static Sprites, Classes"
+ puts "* INFO: Please specify the number of sprites to render."
+ args.gtk.console.set_command "reset_with count: 100"
+ end
+
+ # init
+ if args.state.tick_count == 0
+ args.state.stars = args.state.star_count.map { |i| Star.new args.grid }
+ args.outputs.static_sprites << args.state.stars
+ end
+
+ # update
+ args.state.stars.each(&:move)
+
+ # render
+ args.outputs.background_color = [0, 0, 0]
+ args.outputs.primitives << args.gtk.current_framerate_primitives
+end
+
+# resets game, and assigns star count given by user
+def reset_with count: count
+ $gtk.reset
+ $gtk.args.state.star_count = count
+end
diff --git a/samples/09_performance/06_static_sprites_as_classes/license-for-sample.txt b/samples/09_performance/06_static_sprites_as_classes/license-for-sample.txt
new file mode 100644
index 0000000..100dcec
--- /dev/null
+++ b/samples/09_performance/06_static_sprites_as_classes/license-for-sample.txt
@@ -0,0 +1,9 @@
+Copyright 2019 DragonRuby LLC
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/samples/09_performance/06_static_sprites_as_classes/sprites/tiny-star.png b/samples/09_performance/06_static_sprites_as_classes/sprites/tiny-star.png
new file mode 100644
index 0000000..e04786a
--- /dev/null
+++ b/samples/09_performance/06_static_sprites_as_classes/sprites/tiny-star.png
Binary files differ
diff --git a/samples/09_performance/06_static_sprites_as_classes_with_custom_drawing/app/main.rb b/samples/09_performance/06_static_sprites_as_classes_with_custom_drawing/app/main.rb
index 4d8719d..3291f5e 100644
--- a/samples/09_performance/06_static_sprites_as_classes_with_custom_drawing/app/main.rb
+++ b/samples/09_performance/06_static_sprites_as_classes_with_custom_drawing/app/main.rb
@@ -39,11 +39,22 @@ class Star
# x, y, w, h,
# path,
# angle,
- # alpha, red_saturation, green_saturation, blue_saturation,
+ # alpha, red_saturation, green_saturation, blue_saturation
# tile_x, tile_y, tile_w, tile_h,
# flip_horizontally, flip_vertically,
# angle_anchor_x, angle_anchor_y,
# source_x, source_y, source_w, source_h
+
+ # The argument order for ffi_draw.draw_sprite_4 is:
+ # x, y, w, h,
+ # path,
+ # angle,
+ # alpha, red_saturation, green_saturation, blue_saturation
+ # tile_x, tile_y, tile_w, tile_h,
+ # flip_horizontally, flip_vertically,
+ # angle_anchor_x, angle_anchor_y,
+ # source_x, source_y, source_w, source_h,
+ # blendmode_enum
end
end
@@ -51,6 +62,11 @@ end
def tick args
# sets console command when sample app initially opens
if Kernel.global_tick_count == 0
+ puts ""
+ puts ""
+ puts "========================================================="
+ puts "* INFO: Static Sprites, Classes, Draw Override"
+ puts "* INFO: Please specify the number of sprites to render."
args.gtk.console.set_command "reset_with count: 100"
end
diff --git a/samples/09_performance/07_collision_limits/app/main.rb b/samples/09_performance/07_collision_limits/app/main.rb
index 4a7945c..01ad308 100644
--- a/samples/09_performance/07_collision_limits/app/main.rb
+++ b/samples/09_performance/07_collision_limits/app/main.rb
@@ -6,9 +6,11 @@
- args.outputs.solids: An array. The values generate a solid.
The parameters are [X, Y, WIDTH, HEIGHT, RED, GREEN, BLUE]
+ For more information about solids, go to mygame/documentation/03-solids-and-borders.md.
- args.outputs.labels: An array. The values generate a label.
The parameters are [X, Y, TEXT, SIZE, ALIGNMENT, RED, GREEN, BLUE, ALPHA, FONT STYLE]
+ For more information about labels, go to mygame/documentation/02-labels.md.
- ARRAY#intersect_rect?: Returns true or false depending on if two rectangles intersect.
diff --git a/samples/09_performance/07_static_sprites_as_classes_with_custom_drawing/app/main.rb b/samples/09_performance/07_static_sprites_as_classes_with_custom_drawing/app/main.rb
new file mode 100644
index 0000000..3291f5e
--- /dev/null
+++ b/samples/09_performance/07_static_sprites_as_classes_with_custom_drawing/app/main.rb
@@ -0,0 +1,88 @@
+# Sprites represented as Classes, with a draw_override method, and using the queue ~args.outputs.static_sprites~.
+# is the fastest approach. This is comparable to what other game engines set as the default behavior.
+# There are tradeoffs for all this speed if the creation of a full blown class, and bypassing
+# functional/data-oriented practices.
+class Star
+ def initialize grid
+ @grid = grid
+ @x = (rand @grid.w) * -1
+ @y = (rand @grid.h) * -1
+ @w = 4
+ @h = 4
+ @s = 1 + (4.randomize :ratio)
+ @path = 'sprites/tiny-star.png'
+ end
+
+ def move
+ @x += @s
+ @y += @s
+ @x = (rand @grid.w) * -1 if @x > @grid.right
+ @y = (rand @grid.h) * -1 if @y > @grid.top
+ end
+
+ # if the object that is in args.outputs.sprites (or static_sprites)
+ # respond_to? :draw_override, then the method is invoked giving you
+ # access to the class used to draw to the canvas.
+ def draw_override ffi_draw
+ # first move then draw
+ move
+
+ # The argument order for ffi.draw_sprite is:
+ # x, y, w, h, path
+ ffi_draw.draw_sprite @x, @y, @w, @h, @path
+
+ # The argument order for ffi_draw.draw_sprite_2 is (pass in nil for default value):
+ # x, y, w, h, path,
+ # angle, alpha
+
+ # The argument order for ffi_draw.draw_sprite_3 is:
+ # x, y, w, h,
+ # path,
+ # angle,
+ # alpha, red_saturation, green_saturation, blue_saturation
+ # tile_x, tile_y, tile_w, tile_h,
+ # flip_horizontally, flip_vertically,
+ # angle_anchor_x, angle_anchor_y,
+ # source_x, source_y, source_w, source_h
+
+ # The argument order for ffi_draw.draw_sprite_4 is:
+ # x, y, w, h,
+ # path,
+ # angle,
+ # alpha, red_saturation, green_saturation, blue_saturation
+ # tile_x, tile_y, tile_w, tile_h,
+ # flip_horizontally, flip_vertically,
+ # angle_anchor_x, angle_anchor_y,
+ # source_x, source_y, source_w, source_h,
+ # blendmode_enum
+ end
+end
+
+# calls methods needed for game to run properly
+def tick args
+ # sets console command when sample app initially opens
+ if Kernel.global_tick_count == 0
+ puts ""
+ puts ""
+ puts "========================================================="
+ puts "* INFO: Static Sprites, Classes, Draw Override"
+ puts "* INFO: Please specify the number of sprites to render."
+ args.gtk.console.set_command "reset_with count: 100"
+ end
+
+ # init
+ if args.state.tick_count == 0
+ args.state.stars = args.state.star_count.map { |i| Star.new args.grid }
+ args.outputs.static_sprites << args.state.stars
+ end
+
+ # render framerate
+ args.outputs.background_color = [0, 0, 0]
+ args.outputs.primitives << args.gtk.current_framerate_primitives
+end
+
+# resets game, and assigns star count given by user
+def reset_with count: count
+ $gtk.reset
+ $gtk.args.state.star_count = count
+end
diff --git a/samples/09_performance/07_static_sprites_as_classes_with_custom_drawing/license-for-sample.txt b/samples/09_performance/07_static_sprites_as_classes_with_custom_drawing/license-for-sample.txt
new file mode 100644
index 0000000..100dcec
--- /dev/null
+++ b/samples/09_performance/07_static_sprites_as_classes_with_custom_drawing/license-for-sample.txt
@@ -0,0 +1,9 @@
+Copyright 2019 DragonRuby LLC
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/samples/09_performance/07_static_sprites_as_classes_with_custom_drawing/sprites/tiny-star.png b/samples/09_performance/07_static_sprites_as_classes_with_custom_drawing/sprites/tiny-star.png
new file mode 100644
index 0000000..e04786a
--- /dev/null
+++ b/samples/09_performance/07_static_sprites_as_classes_with_custom_drawing/sprites/tiny-star.png
Binary files differ
diff --git a/samples/09_performance/08_collision_limits/app/main.rb b/samples/09_performance/08_collision_limits/app/main.rb
new file mode 100644
index 0000000..01ad308
--- /dev/null
+++ b/samples/09_performance/08_collision_limits/app/main.rb
@@ -0,0 +1,55 @@
+=begin
+
+ Reminders:
+ - find_all: Finds all elements of a collection that meet certain requirements.
+ In this sample app, we're finding all bodies that intersect with the center body.
+
+ - args.outputs.solids: An array. The values generate a solid.
+ The parameters are [X, Y, WIDTH, HEIGHT, RED, GREEN, BLUE]
+ For more information about solids, go to mygame/documentation/03-solids-and-borders.md.
+
+ - args.outputs.labels: An array. The values generate a label.
+ The parameters are [X, Y, TEXT, SIZE, ALIGNMENT, RED, GREEN, BLUE, ALPHA, FONT STYLE]
+ For more information about labels, go to mygame/documentation/02-labels.md.
+
+ - ARRAY#intersect_rect?: Returns true or false depending on if two rectangles intersect.
+
+=end
+
+# This code demonstrates moving objects that loop around once they exceed the scope of the screen,
+# which has dimensions of 1280 by 720, and also detects collisions between objects called "bodies".
+
+def body_count num
+ $gtk.args.state.other_bodies = num.map { [1280 * rand, 720 * rand, 10, 10] } # other_bodies set using num collection
+end
+
+def tick args
+
+ # Center body's values are set using an array
+ # Map is used to set values of 2000 other bodies
+ # All bodies that intersect with center body are stored in collisions collection
+ args.state.center_body ||= [640 - 100, 360 - 100, 200, 200] # calculations done to place body in center
+ args.state.other_bodies ||= 2000.map { [1280 * rand, 720 * rand, 10, 10] } # 2000 bodies given random position on screen
+
+ # finds all bodies that intersect with center body, stores them in collisions
+ collisions = args.state.other_bodies.find_all { |b| b.intersect_rect? args.state.center_body }
+
+ args.borders << args.state.center_body # outputs center body as a black border
+
+ # transparency changes based on number of collisions; the more collisions, the redder (more transparent) the box becomes
+ args.solids << [args.state.center_body, 255, 0, 0, collisions.length * 5] # center body is red solid
+ args.solids << args.state.other_bodies # other bodies are output as (black) solids, as well
+
+ args.labels << [10, 30, args.gtk.current_framerate] # outputs frame rate in bottom left corner
+
+ # Bodies are returned to bottom left corner if positions exceed scope of screen
+ args.state.other_bodies.each do |b| # for each body in the other_bodies collection
+ b.x += 5 # x and y are both incremented by 5
+ b.y += 5
+ b.x = 0 if b.x > 1280 # x becomes 0 if star exceeds scope of screen (goes too far right)
+ b.y = 0 if b.y > 720 # y becomes 0 if star exceeds scope of screen (goes too far up)
+ end
+end
+
+# Resets the game.
+$gtk.reset
diff --git a/samples/09_performance/08_collision_limits/license-for-sample.txt b/samples/09_performance/08_collision_limits/license-for-sample.txt
new file mode 100644
index 0000000..100dcec
--- /dev/null
+++ b/samples/09_performance/08_collision_limits/license-for-sample.txt
@@ -0,0 +1,9 @@
+Copyright 2019 DragonRuby LLC
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.