summaryrefslogtreecommitdiff
path: root/examples/coroutine.scm
diff options
context:
space:
mode:
Diffstat (limited to 'examples/coroutine.scm')
-rw-r--r--examples/coroutine.scm69
1 files changed, 31 insertions, 38 deletions
diff --git a/examples/coroutine.scm b/examples/coroutine.scm
index 0a4d000..7fc3c2b 100644
--- a/examples/coroutine.scm
+++ b/examples/coroutine.scm
@@ -1,42 +1,35 @@
-(use-modules (2d sprite)
- (2d game-loop)
- (2d window)
- (2d helpers)
- (2d agenda)
+(use-modules (2d agenda)
(2d coroutine)
+ (2d game)
+ (2d sprite)
(2d vector2))
-(define window-width 800)
-(define window-height 600)
-
-;; Open the window.
-(open-window window-width window-height)
-
-(define sprite
+(define (demo-sprite)
(load-sprite "images/sprite.png"
- #:position (vector2 (/ window-width 2)
- (/ window-height 2))))
-
-(define (key-down key mod unicode)
- (cond ((any-equal? key 'escape 'q)
- (close-window)
- (quit))))
-
-;; Draw our sprite
-(define (render)
- (draw-sprite sprite))
-
-;; Register callbacks.
-(add-hook! on-render-hook (lambda () (render)))
-(add-hook! on-key-down-hook (lambda (key mod unicode) (key-down key mod unicode)))
-
-;; Simple script that moves the sprite to a random location every
-;; second.
-(agenda-schedule
- (colambda ()
- (while #t
- (set-sprite-position! sprite (vector2 (random window-width)
- (random window-height)))
- (wait 60))))
-
-(run-game-loop)
+ #:position (vector2 320 240)))
+
+(define (start sprite)
+ ;; Simple script that moves the sprite to a random location every
+ ;; second.
+ (agenda-schedule
+ (colambda ()
+ (while #t
+ (set-sprite-position!
+ sprite
+ (vector2 (random (vx (game-resolution coroutines)))
+ (random (vy (game-resolution coroutines)))))
+ (wait 60)))))
+
+(define-scene demo
+ #:title "Demo"
+ #:draw (lambda (sprite) (draw-sprite sprite))
+ #:events (append
+ (default-scene-events)
+ `((start . ,(lambda (sprite) (start sprite)))))
+ #:state (demo-sprite))
+
+(define-game coroutines
+ #:title "Coroutines"
+ #:first-scene demo)
+
+(run-game coroutines)