From 48b81e45c2e043e5481b80695821ad2bbfcb6e62 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Tue, 8 Jan 2019 21:46:37 -0500 Subject: examples: Add sprite batch example. * examples/sprite-batch.scm: New file. * examples/images/shot.png: New file. * Makefile.am (EXTRA_DIST): Add them. --- Makefile.am | 2 ++ examples/images/shot.png | Bin 0 -> 626 bytes examples/sprite-batch.scm | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 examples/images/shot.png create mode 100644 examples/sprite-batch.scm diff --git a/Makefile.am b/Makefile.am index 74f81f7..6d25629 100644 --- a/Makefile.am +++ b/Makefile.am @@ -88,11 +88,13 @@ EXTRA_DIST += \ examples/tiled.scm \ examples/grid.scm \ examples/game-controller.scm \ + examples/sprite-batch.scm \ examples/images/AUTHORS \ examples/images/chickadee.png \ examples/images/controller-buttons.png \ examples/images/dialog-box.png \ examples/images/explosion.png \ + examples/images/shot.png \ examples/images/tiles.png \ examples/maps/example.tmx diff --git a/examples/images/shot.png b/examples/images/shot.png new file mode 100644 index 0000000..7e308d7 Binary files /dev/null and b/examples/images/shot.png differ diff --git a/examples/sprite-batch.scm b/examples/sprite-batch.scm new file mode 100644 index 0000000..c31ffbf --- /dev/null +++ b/examples/sprite-batch.scm @@ -0,0 +1,66 @@ +(use-modules (chickadee) + (chickadee math matrix) + (chickadee math rect) + (chickadee math vector) + (chickadee render) + (chickadee render font) + (chickadee render sprite) + (chickadee render texture) + (chickadee scripting) + (ice-9 format) + (ice-9 match) + ((sdl2) #:select (sdl-ticks)) + (srfi srfi-1) + (statprof)) + +(define texture #f) +(define start-time (sdl-ticks)) +(define avg-frame-time 16) +(define num-sprites 5000) +(define sprites + (list-tabulate num-sprites + (lambda (n) + (list (rect (* (random:uniform) 640.0) + (* (random:uniform) 480.0) + 16.0 16.0) + (vec2 (* (- (random:uniform) 0.5) 0.5) + (* (- (random:uniform) 0.5) 0.5)))))) +(define matrix (make-identity-matrix4)) + +(define (stats-message) + (format #f "sprites: ~d fps: ~1,2f" + num-sprites + (/ 1000.0 avg-frame-time))) + +(define stats-text (stats-message)) + +(define (load) + (set! *random-state* (random-state-from-platform)) + (set! texture (load-image "images/shot.png")) + (script + (forever + (sleep 60) + (set! stats-text (stats-message))))) + +(define stats-text-pos (vec2 4.0 464.0)) +(define (draw alpha) + (with-batched-sprites + (for-each (match-lambda + ((r v) + (set-rect-x! r (+ (rect-x r) (vec2-x v))) + (set-rect-y! r (+ (rect-y r) (vec2-y v))) + (draw-sprite* texture r matrix))) + sprites)) + (draw-text stats-text stats-text-pos) + (let ((current-time (sdl-ticks))) + (set! avg-frame-time + (+ (* (- current-time start-time) 0.1) + (* avg-frame-time 0.9))) + (set! start-time current-time))) + +(define (update dt) + (update-agenda 1)) + +(gcprof + (lambda () + (run-game #:load load #:draw draw #:update update))) -- cgit v1.2.3