From 5523027f6369e9ec3f990144e8ef2cd4f243476e Mon Sep 17 00:00:00 2001 From: David Thompson Date: Fri, 10 Jan 2014 07:45:41 -0500 Subject: Update some of the example programs to use the new API. * 2d/examples/common.scm: New file. * 2d/examples/coroutine.scm: Updated. * 2d/examples/font.scm: Updated. * 2d/examples/simple.scm: Updated. * 2d/examples/tilemap.scm: Updated. --- examples/common.scm | 8 ++++++++ examples/coroutine.scm | 50 +++++++++++++++++++++++--------------------------- examples/font.scm | 23 ++++++++--------------- examples/simple.scm | 23 ++++++++--------------- examples/tilemap.scm | 20 ++++++++------------ 5 files changed, 55 insertions(+), 69 deletions(-) create mode 100644 examples/common.scm (limited to 'examples') diff --git a/examples/common.scm b/examples/common.scm new file mode 100644 index 0000000..15bc53b --- /dev/null +++ b/examples/common.scm @@ -0,0 +1,8 @@ +(use-modules (2d keyboard) + (2d repl server)) + +(spawn-server) + +(add-hook! key-press-hook (lambda (key unicode) + (when (eq? key 'escape) + (quit-game)))) diff --git a/examples/coroutine.scm b/examples/coroutine.scm index 58b3a1b..cd01381 100644 --- a/examples/coroutine.scm +++ b/examples/coroutine.scm @@ -1,35 +1,31 @@ (use-modules (2d agenda) (2d coroutine) (2d game) - (2d scene) (2d sprite) - (2d stage) - (2d vector2)) + (2d vector2) + (2d window)) -(define (enter 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 coroutine-demo))) - (random (vy (game-resolution coroutine-demo))))) - (wait 60))))) +(load "common.scm") -(define coroutine-scene - (make-scene - "Coroutine" - #:init (lambda () - (load-sprite "images/ghost.png" - #:position (vector2 320 240))) - #:enter enter - #:draw draw-sprite)) +(define window-width 640) +(define window-height 480) -(define coroutine-demo - (make-game - #:title "Coroutines" - #:first-scene coroutine-scene)) +(define sprite (load-sprite "images/p1_front.png" + #:position (vector2 320 240))) -(run-game coroutine-demo) +;; Simple script that moves the sprite to a random location every +;; second. +(schedule-interval + (lambda () + (set-sprite-position! + sprite + (vector2 (random window-width) + (random window-height)))) + 60) + +(add-hook! draw-hook (lambda (dt alpha) (draw-sprite sprite))) + +(with-window (make-window #:title "Coroutines" + #:resolution (vector2 window-width + window-height)) + (run-game-loop)) diff --git a/examples/font.scm b/examples/font.scm index 05eb4bf..465600c 100644 --- a/examples/font.scm +++ b/examples/font.scm @@ -3,11 +3,12 @@ (2d color) (2d font) (2d game) - (2d scene) - (2d stage) - (2d vector2)) + (2d vector2) + (2d window)) -(define (make-demo-textbox) +(load "common.scm") + +(define textbox (make-textbox (load-font "fonts/Boxy-Bold.ttf" 48) "The quick brown fox jumped over the lazy dog." (vector2 240 160) @@ -15,15 +16,7 @@ 'left 200)) -(define fonts-scene - (make-scene - "Fonts" - #:init make-demo-textbox - #:draw draw-textbox)) - -(define fonts-demo - (make-game - #:title "Fonts" - #:first-scene fonts-scene)) +(add-hook! draw-hook (lambda (dt alpha) (draw-textbox textbox))) -(run-game fonts-demo) +(with-window (make-window #:title "Fonts") + (run-game-loop)) diff --git a/examples/simple.scm b/examples/simple.scm index 04711a5..d00a7d0 100644 --- a/examples/simple.scm +++ b/examples/simple.scm @@ -1,21 +1,14 @@ (use-modules (2d game) - (2d scene) (2d sprite) - (2d vector2)) + (2d vector2) + (2d window)) -(define (make-demo-sprite) - (load-sprite "images/ghost.png" - #:position (vector2 320 240))) +(load "common.scm") -(define simple-scene - (make-scene - "Simple" - #:init make-demo-sprite - #:draw draw-sprite)) +(define sprite (load-sprite "images/p1_front.png" + #:position (vector2 320 240))) -(define simple-demo - (make-game - #:title "Simple Demo" - #:first-scene simple-scene)) +(add-hook! draw-hook (lambda (dt alpha) (draw-sprite sprite))) -(run-game simple-demo) +(with-window (make-window #:title "Simple Sprite Demo") + (run-game-loop)) diff --git a/examples/tilemap.scm b/examples/tilemap.scm index 011136d..6b024c7 100644 --- a/examples/tilemap.scm +++ b/examples/tilemap.scm @@ -2,11 +2,13 @@ (srfi srfi-9) (srfi srfi-42) (2d game) - (2d scene) (2d sprite) (2d texture) (2d tileset) - (2d vector2)) + (2d vector2) + (2d window)) + +(load "common.scm") ;;; ;;; Orthogonal tile map example @@ -82,15 +84,9 @@ tileset map-tiles)))) -(define tilemap-scene - (make-scene - "Tilemap" - #:init build-map - #:draw draw-map-layer)) +(define layer (build-map)) -(define tilemap - (make-game - #:title "Tilemap" - #:first-scene tilemap-scene)) +(add-hook! draw-hook (lambda (dt alpha) (draw-map-layer layer))) -(run-game tilemap) +(with-window (make-window #:title "Tilemap") + (run-game-loop)) -- cgit v1.2.3