summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2016-02-13 13:14:30 -0500
committerDavid Thompson <dthompson2@worcester.edu>2016-02-13 13:14:30 -0500
commit000315b8f5e2cfe45db0ebfe42f9208c9c0d95e1 (patch)
tree3936b47365daeb3180fb00919098cdbd054afdb7
parent2e91dc573c62cd8f32e58a5bd62444e3568c34ab (diff)
examples: Add sprite batch example.
* examples/sprite-batch.scm: New file.
-rw-r--r--examples/sprite-batch.scm62
1 files changed, 62 insertions, 0 deletions
diff --git a/examples/sprite-batch.scm b/examples/sprite-batch.scm
new file mode 100644
index 0000000..3ced4ed
--- /dev/null
+++ b/examples/sprite-batch.scm
@@ -0,0 +1,62 @@
+;;; Sly
+;;; Copyright © 2016 David Thompson <davet@gnu.org>
+;;;
+;;; This program is free software: you can redistribute it and/or
+;;; modify it under the terms of the GNU General Public License as
+;;; published by the Free Software Foundation, either version 3 of the
+;;; License, or (at your option) any later version.
+;;;
+;;; This program is distributed in the hope that it will be useful,
+;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+;;; General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with this program. If not, see
+;;; <http://www.gnu.org/licenses/>.
+
+(use-modules (sly)
+ (sly render sprite-batch)
+ (srfi srfi-1))
+
+(load "common.scm")
+
+(define (make-sprite-rects size texture)
+ (let ((width (texture-width texture))
+ (height (texture-height texture)))
+ (list-tabulate size
+ (lambda _
+ (make-rect (- (random 640) 32) (- (random 480) 32)
+ width height)))))
+
+(define (render-sprites batch texture rects)
+ (lambda (gfx)
+ (with-sprite-batch batch gfx
+ (for-each (lambda (rect)
+ (sprite-batch-add! batch gfx texture rect))
+ rects))))
+
+(define-signal size 1000)
+
+(define-signal sprites
+ (let ((texture (on-start (load-texture "images/p1_front.png"))))
+ (signal-map-maybe render-sprites
+ (signal-map (lambda (size started?)
+ (and started? (make-sprite-batch size)))
+ size game-started?)
+ texture
+ (signal-map-maybe make-sprite-rects size texture))))
+
+(define-signal scene
+ (signal-let ((sprites sprites))
+ (if sprites
+ (with-camera (2d-camera #:area (make-rect 0 0 640 480))
+ sprites)
+ render-nothing)))
+
+(with-window (make-window #:title "Sprite Batch")
+ (run-game-loop scene))
+
+;;; Local Variables:
+;;; compile-command: "../pre-inst-env guile sprite-batch.scm"
+;;; End: