From eceae08c4f6985c3cc30191ab33b22302578b81e Mon Sep 17 00:00:00 2001 From: David Thompson Date: Mon, 20 Aug 2018 20:22:44 -0400 Subject: math: vector: Add read syntax. * chickadee/math/vector.scm (read-vec): New procedure. Extend reader. * examples/lines.scm: Use new vector read syntax. * examples/nine-patch.scm: Ditto. * examples/sprite.scm: Ditto. * examples/text.scm: Ditto. * examples/tiled.scm: Ditto. --- chickadee/math/vector.scm | 26 ++++++++++++++++++++++++++ examples/lines.scm | 4 ++-- examples/nine-patch.scm | 2 +- examples/sprite.scm | 2 +- examples/text.scm | 6 +++--- examples/tiled.scm | 2 +- 6 files changed, 34 insertions(+), 8 deletions(-) diff --git a/chickadee/math/vector.scm b/chickadee/math/vector.scm index 78aa564..2ae9187 100644 --- a/chickadee/math/vector.scm +++ b/chickadee/math/vector.scm @@ -311,3 +311,29 @@ polar coordinate (R, THETA)." (set-vec3-x! v (- (vec3-x v) (vec3-x x))) (set-vec3-y! v (- (vec3-y v) (vec3-y x))) (set-vec3-z! v (- (vec3-z v) (vec3-z x)))))) + +;; Reader macro for vectors. +(define (read-vec chr port) + (define (consume-whitespace port) + (when (char-whitespace? (peek-char port)) + (read-char port) + (consume-whitespace port))) + (if (eq? (peek-char port) #\() + (read-char port) + (error "expected opening #\\(")) + (consume-whitespace port) + (let ((x (read port)) + (y (read port))) + (if (eq? (peek-char port) #\)) + (begin + (read-char port) + `(vec2 ,x ,y)) + (let ((z (read port))) + (consume-whitespace port) + (if (eq? (peek-char port) #\)) + (begin + (read-char port) + `(vec3 ,x ,y ,z)) + (error "expected terminating #\\)")))))) + +(read-hash-extend #\v read-vec) diff --git a/examples/lines.scm b/examples/lines.scm index 5e800e0..75aadf1 100644 --- a/examples/lines.scm +++ b/examples/lines.scm @@ -8,8 +8,8 @@ (define lines (list-tabulate 48 (lambda (n) - (list (vec2 (random 640) (random 480)) - (vec2 (random 640) (random 480)) + (list #v((random 640) (random 480)) + #v((random 640) (random 480)) (make-color (random:uniform) (random:uniform) (random:uniform) diff --git a/examples/nine-patch.scm b/examples/nine-patch.scm index ed03cab..2852245 100644 --- a/examples/nine-patch.scm +++ b/examples/nine-patch.scm @@ -14,6 +14,6 @@ (define (draw alpha) (draw-nine-patch image (make-rect 192.0 192.0 256.0 96.0) #:margin 6) - (draw-text font "I am error." (vec2 200.0 266.0))) + (draw-text font "I am error." #v(200.0 266.0))) (run-game/sdl #:load load #:draw draw) diff --git a/examples/sprite.scm b/examples/sprite.scm index 12a9ffb..8e54f21 100644 --- a/examples/sprite.scm +++ b/examples/sprite.scm @@ -9,6 +9,6 @@ (set! sprite (load-image "images/chickadee.png"))) (define (draw alpha) - (draw-sprite sprite (vec2 256.0 176.0))) + (draw-sprite sprite #v(256.0 176.0))) (run-game/sdl #:load load #:draw draw) diff --git a/examples/text.scm b/examples/text.scm index fd299b7..74c2301 100644 --- a/examples/text.scm +++ b/examples/text.scm @@ -9,11 +9,11 @@ (define (draw alpha) (draw-text font "(draw-text font \"Hello, world!\" (vec2 100.0 200.0))" - (vec2 100.0 200.0)) + #v(100.0 200.0)) (draw-text font "Chickadee is a game development toolkit for Guile Scheme." - (vec2 100.0 150.0)) + #v(100.0 150.0)) (draw-text font "The quick brown fox jumps over the lazy dog" - (vec2 100.0 100.0))) + #v(100.0 100.0))) (define (key-press key scan modifiers repeat?) (when (eq? key 'q) diff --git a/examples/tiled.scm b/examples/tiled.scm index 3d33052..b5fb8b6 100644 --- a/examples/tiled.scm +++ b/examples/tiled.scm @@ -5,7 +5,7 @@ (ice-9 match)) (define map #f) -(define camera (vec2 0.0 0.0)) +(define camera #v(0.0 0.0)) (define (load) (set! map (load-tile-map "maps/example.tmx"))) -- cgit v1.2.3