summaryrefslogtreecommitdiff
path: root/examples/sprite-autobatch.scm
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2023-12-30 19:32:19 -0500
committerDavid Thompson <dthompson2@worcester.edu>2024-07-24 21:54:32 -0400
commit76dc3f0af36775d2fdaab61134dd0f875ee48292 (patch)
tree33fe349038005f7e5f733856895903257a448b54 /examples/sprite-autobatch.scm
parent41e7e7152a66f40f3a23439fb0ea4b2b03acbf1c (diff)
WIP graphics engine rewrite.
Diffstat (limited to 'examples/sprite-autobatch.scm')
-rw-r--r--examples/sprite-autobatch.scm78
1 files changed, 78 insertions, 0 deletions
diff --git a/examples/sprite-autobatch.scm b/examples/sprite-autobatch.scm
new file mode 100644
index 0000000..bbae02a
--- /dev/null
+++ b/examples/sprite-autobatch.scm
@@ -0,0 +1,78 @@
+(use-modules (chickadee)
+ (chickadee math matrix)
+ (chickadee math rect)
+ (chickadee math vector)
+ (chickadee graphics sprite)
+ (chickadee graphics texture)
+
+ (chickadee graphics)
+ (chickadee graphics color)
+ (chickadee scripting)
+ (ice-9 match)
+ (statprof))
+
+(define start-time 0.0)
+(define avg-frame-time 16.0)
+(define texture #f)
+(define view #f)
+(define rect #f)
+(define matrix (make-identity-matrix4))
+
+(define (load)
+ (set! texture (load-image "images/shot.png"))
+ (set! view (make-texture-view texture))
+ (set! rect (make-rect 0.0 0.0
+ (texture-width texture)
+ (texture-height texture)))
+ (script
+ (forever
+ (sleep 60)
+ (pk 'fps (/ 1.0 avg-frame-time)))))
+
+(define (frand x)
+ (* (random:uniform) x))
+(define sprites
+ (map (lambda (i)
+ (vec2 (- (frand 640.0) 8.0)
+ (- (frand 480.0) 8.0)))
+ (iota 30000)))
+
+(define (draw alpha)
+ (let loop ((sprites sprites))
+ (match sprites
+ (() (values))
+ ((p . rest)
+ (set-rect-x! rect (vec2-x p))
+ (set-rect-y! rect (vec2-y p))
+ (draw-sprite* view rect matrix)
+ (loop rest))))
+ (let ((current-time (elapsed-time)))
+ (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))
+
+(define (key-press key modifiers repeat)
+ (when (eq? key 'q) (abort-game)))
+
+(define (print-gc-stats)
+ (let ((stats (gc-stats)))
+ (pk 'gc
+ (assq-ref stats 'gc-times)
+ (exact->inexact
+ (/ (get-internal-real-time)
+ internal-time-units-per-second))
+ (exact->inexact
+ (/ (assq-ref stats 'gc-time-taken)
+ 1000000000)))))
+
+(add-hook! after-gc-hook print-gc-stats)
+
+(define (start)
+ (run-game #:load load #:draw draw #:update update #:key-press key-press))
+
+;; (statprof start)
+(start)