diff options
-rw-r--r-- | examples/action.scm | 26 | ||||
-rw-r--r-- | examples/animation.scm | 19 | ||||
-rw-r--r-- | examples/coroutine.scm | 23 | ||||
-rw-r--r-- | examples/font.scm | 13 | ||||
-rw-r--r-- | examples/particles.scm | 27 | ||||
-rw-r--r-- | examples/scenes.scm | 72 | ||||
-rw-r--r-- | examples/simple.scm | 26 | ||||
-rw-r--r-- | examples/tilemap.scm | 20 |
8 files changed, 122 insertions, 104 deletions
diff --git a/examples/action.scm b/examples/action.scm index 9f4dd65..fa68dc0 100644 --- a/examples/action.scm +++ b/examples/action.scm @@ -8,33 +8,33 @@ (2d stage) (2d vector2)) -(define-stage-variable sprite - (load-sprite "images/ghost.png" - #:position (vector2 320 240))) - -(define (init) +(define (enter sprite) (let ((size (game-resolution actions-demo))) (schedule-action (action-parallel ;; Move horizontally across the screen in 60 frames. (lerp (lambda (x) (set-sprite-position! - (sprite) + sprite (vector2 x (/ (vy size) 2)))) - 0 (vx size) 120) + 0 (vx size) 60) ;; Rotate 1080 degrees in 120 frames. (lerp (lambda (angle) - (set-sprite-rotation! (sprite) angle)) + (set-sprite-rotation! sprite angle)) 0 360 120))))) -(define demo-scene +(define actions-scene (make-scene - #:init init - #:draw (lambda () (draw-sprite (sprite))))) + "Actions" + #:init (lambda () + (load-sprite "images/ghost.png" + #:position (vector2 320 240))) + #:enter enter + #:draw draw-sprite)) (define actions-demo (make-game - #:title "Actions" - #:first-scene demo-scene)) + #:title "Actions" + #:first-scene actions-scene)) (run-game actions-demo) diff --git a/examples/animation.scm b/examples/animation.scm index 92da549..6e0c5c7 100644 --- a/examples/animation.scm +++ b/examples/animation.scm @@ -8,7 +8,7 @@ (2d vector2) (2d window)) -(define (demo-animation) +(define (make-demo-animation) "Load a texture, split it into 64x64 tiles, and build an animated sprite out of it." (let* ((tiles (load-tileset "images/princess.png" 64 64)) @@ -22,18 +22,17 @@ sprite out of it." (tileset-ref tiles 26)))) (make-animation frames 6 #t))) -(define-stage-variable sprite - (make-sprite (demo-animation) - #:position (vector2 320 240))) - -(define demo-scene +(define animation-scene (make-scene - #:draw (lambda () - (draw-sprite (sprite))))) + "Animation" + #:init (lambda () + (make-sprite (make-demo-animation) + #:position (vector2 320 240))) + #:draw draw-sprite)) (define animation-demo (make-game - #:title "Animation" - #:first-scene demo-scene)) + #:title "Animation" + #:first-scene animation-scene)) (run-game animation-demo) diff --git a/examples/coroutine.scm b/examples/coroutine.scm index 2f9b0ae..bdfc973 100644 --- a/examples/coroutine.scm +++ b/examples/coroutine.scm @@ -7,31 +7,30 @@ (2d stage) (2d vector2)) -(define (demo-sprite) - (load-sprite "images/ghost.png" - #:position (vector2 320 240))) - -(define (init) +(define (enter sprite) ;; Simple script that moves the sprite to a random location every ;; second. - (stage-define sprite (demo-sprite)) (agenda-schedule (colambda () (while #t (set-sprite-position! - (stage-ref sprite) + sprite (vector2 (random (vx (game-resolution coroutine-demo))) (random (vy (game-resolution coroutine-demo))))) (wait 60))))) -(define demo-scene +(define coroutine-scene (make-scene - #:init init - #:draw (lambda () (draw-sprite (stage-ref sprite))))) + "Coroutine" + #:init (lambda () + (load-sprite "images/ghost.png" + #:position (vector2 320 240))) + #:enter enter + #:draw draw-sprite)) (define coroutine-demo (make-game - #:title "Coroutines" - #:first-scene demo-scene)) + #:title "Coroutines" + #:first-scene coroutine-scene)) (run-game coroutine-demo) diff --git a/examples/font.scm b/examples/font.scm index 070da2d..3c8e085 100644 --- a/examples/font.scm +++ b/examples/font.scm @@ -8,7 +8,7 @@ (2d stage) (2d vector2)) -(define (demo-textbox) +(define (make-demo-textbox) (make-textbox (load-font "fonts/Boxy-Bold.ttf" 48) "The quick brown fox jumped over the lazy dog." (vector2 240 160) @@ -16,14 +16,15 @@ 'left 200)) -(define demo-scene +(define fonts-scene (make-scene - #:init (lambda () (stage-define textbox (demo-textbox))) - #:draw (lambda () (draw-textbox (stage-ref textbox))))) + "Fonts" + #:init make-demo-textbox + #:draw draw-textbox)) (define fonts-demo (make-game - #:title "Fonts" - #:first-scene demo-scene)) + #:title "Fonts" + #:first-scene fonts-scene)) (run-game fonts-demo) diff --git a/examples/particles.scm b/examples/particles.scm index 1c5a3db..cc7ca82 100644 --- a/examples/particles.scm +++ b/examples/particles.scm @@ -2,6 +2,8 @@ (use-modules (srfi srfi-1) (srfi srfi-9) (2d game) + (2d game-loop) + (2d scene) (2d sprite) (2d texture) (2d vector2)) @@ -63,16 +65,21 @@ (define (update state) (for-each update-particle! (demo-particles state))) -(define-scene demo - #:title "Demo" - #:draw (lambda (state) (draw state)) - #:update (lambda (state) (update state)) - #:state (make-demo-state (load-sprite "images/stars.png" - #:anchor null-vector2) - (generate-particles particle-count))) +(define (init) + (make-demo-state (load-sprite "images/stars.png" + #:anchor null-vector2) + (generate-particles particle-count))) -(define-game particles - #:title "Particles" - #:first-scene demo) +(define particles-scene + (make-scene + "Particles" + #:draw draw + #:update update + #:init init)) + +(define particles + (make-game + #:title "Particles" + #:first-scene particles-scene)) (run-game particles) diff --git a/examples/scenes.scm b/examples/scenes.scm index 534eb56..ec0de6d 100644 --- a/examples/scenes.scm +++ b/examples/scenes.scm @@ -1,5 +1,8 @@ -(use-modules (2d sprite) - (2d game) +(use-modules (2d game) + (2d game-loop) + (2d scene) + (2d stage) + (2d sprite) (2d vector2)) ;; Press the RETURN key to toggle between the 2 scenes. @@ -12,20 +15,20 @@ (load-sprite "images/ghost.png" #:position (vector2 320 240))) -(define (scene-1-key-press sprite key mod unicode) +(define (scene-1-key-down sprite key mod unicode) (when (eq? key 'return) - (replace-scene (scene-2)))) - -(define-scene scene-1 - #:title "Scene 1" - #:draw (lambda (sprite) (draw-sprite sprite)) - #:events (append - (default-scene-events) - `((start . ,(lambda (state) (display "Start Scene 1\n"))) - (stop . ,(lambda (state) (display "Stop Scene 1\n"))) - (key-down . ,(lambda (state key mod unicode) - (scene-1-key-press state key mod unicode))))) - #:state (make-sprite-1)) + (replace-scene scene-2))) + +(define scene-1 + (make-scene + "Scene 1" + #:init make-sprite-1 + #:enter (lambda (sprite) (display "Enter Scene 1\n")) + #:exit (lambda (sprite) (display "Exit Scene 1\n")) + #:draw draw-sprite + #:events (append + (default-events) + `((key-down . ,scene-1-key-down))))) ;;; ;;; Scene 2 @@ -35,23 +38,24 @@ (load-sprite "images/stars.png" #:position (vector2 320 240))) -(define (scene-2-key-press sprite key mod unicode) +(define (scene-2-key-down sprite key mod unicode) (when (eq? key 'return) - (replace-scene (scene-1)))) - -(define-scene scene-2 - #:title "Scene 2" - #:draw (lambda (sprite) (draw-sprite sprite)) - #:events (append - (default-scene-events) - `((start . ,(lambda (state) (display "Start Scene 2\n"))) - (stop . ,(lambda (state) (display "Stop Scene 2\n"))) - (key-down . ,(lambda (state key mod unicode) - (scene-2-key-press state key mod unicode))))) - #:state (make-sprite-2)) - -(define-game scenes - #:title "Scenes" - #:first-scene scene-1) - -(run-game scenes) + (replace-scene scene-1))) + +(define scene-2 + (make-scene + "Scene 2" + #:init make-sprite-2 + #:enter (lambda (sprite) (display "Enter Scene 2\n")) + #:exit (lambda (sprite) (display "Exit Scene 2\n")) + #:draw draw-sprite + #:events (append + (default-events) + `((key-down . ,scene-2-key-down))))) + +(define scenes-demo + (make-game + #:title "Scenes" + #:first-scene scene-1)) + +(run-game scenes-demo) diff --git a/examples/simple.scm b/examples/simple.scm index 8a1fd8f..09a403f 100644 --- a/examples/simple.scm +++ b/examples/simple.scm @@ -1,18 +1,22 @@ -(use-modules (2d sprite) - (2d game) +(use-modules (2d game) + (2d game-loop) + (2d scene) + (2d sprite) (2d vector2)) -(define (demo-sprite) +(define (make-demo-sprite) (load-sprite "images/ghost.png" #:position (vector2 320 240))) -(define-scene demo - #:title "Demo" - #:draw (lambda (sprite) (draw-sprite sprite)) - #:state (demo-sprite)) +(define simple-scene + (make-scene + "Simple" + #:init make-demo-sprite + #:draw draw-sprite)) -(define-game simple - #:title "Simple Demo" - #:first-scene demo) +(define simple-demo + (make-game + #:title "Simple Demo" + #:first-scene simple-scene)) -(run-game simple) +(run-game simple-demo) diff --git a/examples/tilemap.scm b/examples/tilemap.scm index a60ee18..141184e 100644 --- a/examples/tilemap.scm +++ b/examples/tilemap.scm @@ -2,9 +2,11 @@ (srfi srfi-9) (srfi srfi-42) (2d game) + (2d game-loop) + (2d scene) + (2d sprite) (2d texture) (2d tileset) - (2d sprite) (2d vector2)) ;;; @@ -81,13 +83,15 @@ tileset map-tiles)))) -(define-scene demo - #:title "Demo" - #:draw (lambda (map) (draw-map-layer map)) - #:state (build-map)) +(define tilemap-scene + (make-scene + "Tilemap" + #:init build-map + #:draw draw-map-layer)) -(define-game tilemap - #:title "Tilemap" - #:first-scene demo) +(define tilemap + (make-game + #:title "Tilemap" + #:first-scene tilemap-scene)) (run-game tilemap) |