diff options
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/2048/2048.scm | 43 | ||||
-rw-r--r-- | examples/animation.scm | 17 | ||||
-rw-r--r-- | examples/font.scm | 23 | ||||
-rw-r--r-- | examples/joystick.scm | 10 | ||||
-rw-r--r-- | examples/mines/mines.scm | 84 | ||||
-rw-r--r-- | examples/simple.scm | 35 | ||||
-rw-r--r-- | examples/tilemap.scm | 16 |
7 files changed, 125 insertions, 103 deletions
diff --git a/examples/2048/2048.scm b/examples/2048/2048.scm index 2b520ac..6027866 100755 --- a/examples/2048/2048.scm +++ b/examples/2048/2048.scm @@ -38,6 +38,7 @@ (sly render context) (sly render font) (sly render model) + (sly render scene) (sly render sprite) (sly render texture) (sly input keyboard) @@ -401,20 +402,18 @@ (tween vlerp ease-linear from to duration))) (color-tween (tween color-lerp ease-linear transparent text-color-1 duration)) - (score (signal-drop-repeats score)) (header (label score-header-font text #:anchor 'top-center))) - (signal-map (lambda (score timer) - (let ((score (label score-font (number->string score) - #:anchor 'center))) - (model-move (vector2 x (- window-height 28)) - (model-group - (model-paint text-color-1 header) - (chain score - (model-paint (color-tween timer)) - (model-move (position-tween timer))))))) - score - (signal-drop (lambda (t) (> t duration)) - 0 (signal-since 1 score))))) + (signal-let* ((score (signal-drop-repeats score)) + (timer (signal-drop (lambda (t) (> t duration)) + 0 (signal-since 1 score)))) + (let ((score (label score-font (number->string score) + #:anchor 'center))) + (model-move (vector2 x (- window-height 28)) + (model-group + (model-paint text-color-1 header) + (chain score + (model-paint (color-tween timer)) + (model-move (position-tween timer))))))))) (define-signal score (score-label "SCORE" (signal-map 2048-score 2048-state) (/ board-width 4))) @@ -423,19 +422,20 @@ (score-label "BEST" (signal-map 2048-best-score 2048-state) (- board-width (/ board-width 4)))) -(define-signal 2048-scene +(define-signal 2048-model (signal-map (cut model-move center-pos <>) (signal-map model-group instructions tiles score best-score status-message))) (define camera - (orthographic-camera window-width window-height - #:viewport (make-viewport (make-rect 0 0 640 480) - #:clear-color background))) + (let ((viewport (make-viewport (make-rect 0 0 640 480) + #:clear-color background))) + (orthographic-camera window-width window-height + #:viewport viewport))) -(define (draw-2048 dt alpha) - (signal-let ((scene 2048-scene)) - (draw-model scene camera))) +(define-signal 2048-scene + (signal-let ((model 2048-model)) + (make-scene camera model))) ;;; ;;; Initialization @@ -444,10 +444,9 @@ (start-sly-repl) (add-hook! window-close-hook stop-game-loop) -(add-hook! draw-hook (trampoline draw-2048)) (with-window (make-window #:title "2048") - (start-game-loop)) + (start-game-loop 2048-scene)) ;;; Local Variables: ;;; compile-command: "../../pre-inst-env guile 2048.scm" diff --git a/examples/animation.scm b/examples/animation.scm index c64d581..0a72068 100644 --- a/examples/animation.scm +++ b/examples/animation.scm @@ -24,7 +24,8 @@ (sly render camera) (sly render model) (sly render sprite) - (sly render tileset)) + (sly render tileset) + (sly render scene)) (load "common.scm") @@ -45,18 +46,16 @@ (tween (compose floor lerp) (compose ease-linear ease-loop) 0 frame-count (* frame-count frame-rate)))) -(define-signal scene - (signal-map (lambda (time) - (model-move (position-tween time) - (vector-ref walk-cycle (frame-tween time)))) - (signal-timer))) - (define camera (orthographic-camera 640 480)) -(add-hook! draw-hook (lambda _ (draw-model (signal-ref scene) camera))) +(define-signal scene + (signal-let ((time (signal-timer))) + (let* ((frame (vector-ref walk-cycle (frame-tween time))) + (model (model-move (position-tween time) frame))) + (make-scene camera model)))) (with-window (make-window #:title "Animation") - (start-game-loop)) + (start-game-loop scene)) ;;; Local Variables: ;;; compile-command: "../pre-inst-env guile animation.scm" diff --git a/examples/font.scm b/examples/font.scm index 33c4940..47e4153 100644 --- a/examples/font.scm +++ b/examples/font.scm @@ -24,7 +24,7 @@ (sly render color) (sly render font) (sly render model) - (sly render model) + (sly render scene) (sly input mouse)) (load "common.scm") @@ -39,26 +39,25 @@ #:anchor 'center))) (define-signal fps-label - (signal-map (lambda (fps) - (let ((text (format #f "FPS: ~d" fps))) - (model-move (vector2 0 480) (label font text)))) - fps)) + (signal-let ((fps fps)) + (let ((text (format #f "FPS: ~d" fps))) + (model-move (vector2 0 480) (label font text))))) (define-signal mouse-label - (signal-map (lambda (p) - (let ((text (format #f "Mouse: (~d, ~d)" (vx p) (vy p)))) - (model-move (vector2 0 460) (label font text)))) - (signal-throttle 5 mouse-position))) + (signal-let ((pos (signal-throttle 5 mouse-position))) + (let ((text (format #f "Mouse: (~d, ~d)" (vx pos) (vy pos)))) + (model-move (vector2 0 460) (label font text))))) -(define-signal scene +(define-signal model (signal-map model-group message-label fps-label mouse-label)) (define camera (orthographic-camera 640 480)) -(add-hook! draw-hook (lambda _ (draw-model (signal-ref scene) camera))) +(define-signal scene + (signal-map (lambda (model) (make-scene camera model)) model)) (with-window (make-window #:title "Fonts") - (start-game-loop)) + (start-game-loop scene)) ;;; Local Variables: ;;; compile-command: "../pre-inst-env guile font.scm" diff --git a/examples/joystick.scm b/examples/joystick.scm index ae090da..43484e3 100644 --- a/examples/joystick.scm +++ b/examples/joystick.scm @@ -30,7 +30,8 @@ (sly render model) (sly render sprite) (sly render texture) - (sly render font)) + (sly render font) + (sly render scene)) (load "common.scm") @@ -63,14 +64,15 @@ (button-caption-signal "This is the other caption" 2) (button-caption-signal "This is the other other caption" 3)))) -(define-signal scene +(define-signal model (signal-map (lambda (position caption) (model-move position (model-group player caption))) player-position caption)) (define camera (orthographic-camera (vx resolution) (vy resolution))) -(add-hook! draw-hook (lambda _ (draw-model (signal-ref scene) camera))) +(define-signal scene + (signal-map (lambda (model) (make-scene camera model)) model)) (add-hook! joystick-axis-hook (lambda (which axis value) @@ -89,7 +91,7 @@ (with-window (make-window #:title "Joystick test" #:resolution resolution) - (start-game-loop)) + (start-game-loop scene)) ;;; Local Variables: ;;; compile-command: "../pre-inst-env guile joystick.scm" diff --git a/examples/mines/mines.scm b/examples/mines/mines.scm index ff18f39..f030e51 100644 --- a/examples/mines/mines.scm +++ b/examples/mines/mines.scm @@ -39,6 +39,7 @@ (sly render font) (sly render model) (sly render sprite) + (sly render scene) (sly input keyboard) (sly input mouse)) @@ -227,19 +228,21 @@ (define tile-size 32) (define-signal board-size 8) + (define-signal board-area - (signal-map (lambda (size) (make-rect 0 0 size size)) board-size)) + (signal-let ((size board-size)) + (make-rect 0 0 size size))) (define-signal center-position - (signal-map (lambda (board-size) - (v- (v* 1/2 resolution) - (/ (* board-size tile-size) 2))) - board-size)) + (signal-let ((board-size board-size)) + (v- (v* 1/2 resolution) + (/ (* board-size tile-size) 2)))) (define-signal tile-position - (signal-map (lambda (p size center) - (vmap floor (v* (v- p center) (/ 1 tile-size)))) - mouse-position board-size center-position)) + (signal-let ((p mouse-position) + (size board-size) + (center center-position)) + (vmap floor (v* (v- p center) (/ 1 tile-size))))) (define-signal reveal-clicks (chain mouse-last-up @@ -355,40 +358,38 @@ null-model)))))) (define-signal board-view - (signal-map (lambda (board) - (define (draw-column tile x) - (model-move (vector2 (* x tile-size) 0) - (draw-tile tile))) + (signal-let ((board board)) + (define (draw-column tile x) + (model-move (vector2 (* x tile-size) 0) + (draw-tile tile))) - (define (draw-row row y) - (chain (enumerate-map draw-column row) - (list->model) - (model-move (vector2 0 (* y tile-size))))) + (define (draw-row row y) + (chain (enumerate-map draw-column row) + (list->model) + (model-move (vector2 0 (* y tile-size))))) - (list->model (enumerate-map draw-row board))) - board)) + (list->model (enumerate-map draw-row board)))) (define-signal status-message - (signal-map (lambda (board) - (define (make-message message) - (label font message #:anchor 'center)) - - (model-move - (vector2 (/ (vx resolution) 2) (- (vy resolution) 64)) - (list->model - (cond - ((board-lose? board) - (list (make-message "GAME OVER - Press N to play again"))) - ((board-win? board) - (list (make-message "YOU WIN! - Press N to play again"))) - (else '()))))) - board)) - -(define-signal scene - (signal-map (lambda (board-view status center-position) - (model-group status - (model-move center-position board-view))) - board-view status-message center-position)) + (signal-let ((board board)) + (define (make-message message) + (label font message #:anchor 'center)) + + (model-move + (vector2 (/ (vx resolution) 2) (- (vy resolution) 64)) + (list->model + (cond + ((board-lose? board) + (list (make-message "GAME OVER - Press N to play again"))) + ((board-win? board) + (list (make-message "YOU WIN! - Press N to play again"))) + (else '())))))) + +(define-signal model + (signal-let ((view board-view) + (status status-message) + (center center-position)) + (model-group status (model-move center view)))) (define camera (orthographic-camera @@ -396,8 +397,8 @@ #:viewport (make-viewport (make-rect (vector2 0 0) resolution) #:clear-color tango-dark-plum))) -(define (draw-scene dt alpha) - (draw-model (signal-ref scene) camera)) +(define-signal scene + (signal-map (lambda (model) (make-scene camera model)) model)) ;;; ;;; Initialization @@ -406,10 +407,9 @@ (start-sly-repl) (add-hook! window-close-hook stop-game-loop) -(add-hook! draw-hook (trampoline draw-scene)) (with-window (make-window #:title "Mines" #:resolution resolution) - (start-game-loop)) + (start-game-loop scene)) ;;; Local Variables: ;;; compile-command: "../../pre-inst-env guile mines.scm" diff --git a/examples/simple.scm b/examples/simple.scm index 820a3ce..7692331 100644 --- a/examples/simple.scm +++ b/examples/simple.scm @@ -18,24 +18,47 @@ (use-modules (sly game) (sly window) (sly utils) + (sly signal) + (sly math transform) (sly math vector) (sly render camera) (sly render model) (sly render sprite) - (sly render color)) + (sly render texture) + (sly render color) + (sly render framebuffer) + (sly render scene) + (sly render context)) (load "common.scm") -(define scene - (model-move (vector2 320 240) - (load-sprite "images/p1_front.png"))) +(define s (load-sprite "images/p1_front.png")) + +(define bg (load-sprite "images/lava.png")) + +(define fb (make-framebuffer 320 240)) + +(define base-model + (model-move (vector2 160 120) s)) + +(define base-camera + (orthographic-camera 320 240)) + +(define base-scene + (scene base-camera base-model fb)) + +(define model + (model-group (model-move (vector2 320 240) bg) + (chain (scene->sprite base-scene) + (model-move (vector2 320 240)) + (model-scale 2)))) (define camera (orthographic-camera 640 480)) -(add-hook! draw-hook (lambda _ (draw-model scene camera))) +(define-signal scene (make-scene camera model)) (with-window (make-window #:title "Simple Sprite Demo") - (start-game-loop)) + (start-game-loop scene)) ;;; Local Variables: ;;; compile-command: "../pre-inst-env guile simple.scm" diff --git a/examples/tilemap.scm b/examples/tilemap.scm index bb4d9ef..089946e 100644 --- a/examples/tilemap.scm +++ b/examples/tilemap.scm @@ -33,6 +33,7 @@ (sly render texture) (sly render tileset) (sly render tile-map) + (sly render scene) (sly math vector) (sly math tween) (sly input keyboard)) @@ -89,20 +90,19 @@ (193 225 225 226 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65) (225 225 176 242 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65)))) -(define scene - (chain (compile-tile-layer map-tiles 32 32) - (list->model) - (model-move (v- (vector2 320 240) - (v* (vector2 tile-width tile-height) - (vector2 10 15/2)))))) +(define model + (model-move (v- (vector2 320 240) + (v* (vector2 tile-width tile-height) + (vector2 10 15/2))) + (list->model (compile-tile-layer map-tiles 32 32)))) (define camera (orthographic-camera 640 480)) -(add-hook! draw-hook (lambda _ (draw-model scene camera))) +(define-signal scene (make-scene camera model)) (with-window (make-window #:title "Tilemap") - (start-game-loop)) + (start-game-loop scene)) ;;; Local Variables: ;;; compile-command: "../pre-inst-env guile tilemap.scm" |