summaryrefslogtreecommitdiffhomepage
path: root/samples/09_performance/07_static_sprites_as_classes_with_custom_drawing
diff options
context:
space:
mode:
author_Tradam <[email protected]>2021-12-16 19:22:26 -0500
committerGitHub <[email protected]>2021-12-16 19:22:26 -0500
commit5954b9beb4d4a3b4f248d72d1851195f030558a8 (patch)
treefecd8aa840a25afdb502915b0fdb4d03b7ed339a /samples/09_performance/07_static_sprites_as_classes_with_custom_drawing
parent2f845281f133849256b57bb08fd3e9ae57600784 (diff)
parenteaa29e72939f5edf61735ccbb73c36ee89369f65 (diff)
downloaddragonruby-game-toolkit-contrib-master.tar.gz
dragonruby-game-toolkit-contrib-master.zip
Merge branch 'DragonRuby:master' into masterHEADmaster
Diffstat (limited to 'samples/09_performance/07_static_sprites_as_classes_with_custom_drawing')
-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
3 files changed, 97 insertions, 0 deletions
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