diff options
author | David Thompson <dthompson@member.fsf.org> | 2013-08-27 18:22:06 -0400 |
---|---|---|
committer | David Thompson <dthompson@member.fsf.org> | 2013-08-27 18:22:06 -0400 |
commit | 5841889f53465b4096830f3778b209063e1fe8bc (patch) | |
tree | 724087d4dbfeb08349401d7ff19e0f099a550667 | |
parent | e719d1e988617f623fac29c1bd347785833c5f71 (diff) |
Replace old vector module with new vector2 module.
-rw-r--r-- | 2d/font.scm | 2 | ||||
-rw-r--r-- | 2d/sprite.scm | 14 | ||||
-rw-r--r-- | 2d/vector.scm | 90 | ||||
-rw-r--r-- | examples/action.scm | 8 | ||||
-rw-r--r-- | examples/animation.scm | 26 | ||||
-rw-r--r-- | examples/coroutine.scm | 11 | ||||
-rw-r--r-- | examples/font.scm | 5 | ||||
-rw-r--r-- | examples/particles.scm | 12 | ||||
-rw-r--r-- | examples/simple.scm | 7 | ||||
-rw-r--r-- | examples/tilemap.scm | 9 |
10 files changed, 49 insertions, 135 deletions
diff --git a/2d/font.scm b/2d/font.scm index 8bf66a3..45b59d0 100644 --- a/2d/font.scm +++ b/2d/font.scm @@ -27,7 +27,7 @@ #:use-module (system foreign) #:use-module (2d wrappers ftgl) #:use-module (2d color) - #:use-module (2d vector)) + #:use-module (2d vector2)) ;;; ;;; Font diff --git a/2d/sprite.scm b/2d/sprite.scm index b652f4d..ac57adb 100644 --- a/2d/sprite.scm +++ b/2d/sprite.scm @@ -33,8 +33,8 @@ #:use-module (2d helpers) #:use-module (2d math) #:use-module (2d texture) - #:use-module (2d wrappers gl) - #:use-module (2d vector)) + #:use-module (2d vector2) + #:use-module (2d wrappers gl)) ;;; ;;; Sprite Vertices @@ -156,7 +156,8 @@ (dirty sprite-dirty? set-sprite-dirty!) (animation-state sprite-animation-state set-sprite-animation-state!)) -(define* (make-sprite drawable #:optional #:key (position #(0 0)) (scale #(1 1)) +(define* (make-sprite drawable #:optional #:key + (position (vector2 0 0)) (scale (vector2 1 1)) (rotation 0) (color white) (anchor 'center)) "Makes a new sprite object." (let ((vertices (make-packed-array sprite-vertex 4)) @@ -179,7 +180,8 @@ operation that requires a refresh of the vertex array should use this macro." (dirty-sprite-setter set-sprite-color! %set-sprite-color!) (dirty-sprite-setter set-sprite-anchor! %set-sprite-anchor!) -(define* (load-sprite filename #:optional #:key (position #(0 0)) (scale #(1 1)) +(define* (load-sprite filename #:optional #:key + (position (vector2 0 0)) (scale (vector2 1 1)) (rotation 0) (color white) (anchor 'center)) "Loads a sprite from file." (make-sprite (load-texture filename) #:position position #:scale scale @@ -203,8 +205,8 @@ sprite." (texture (sprite-texture sprite))) (cond ((eq? anchor 'center) - (vector (/ (texture-width texture) 2) - (/ (texture-height texture) 2))) + (vector2 (/ (texture-width texture) 2) + (/ (texture-height texture) 2))) (else anchor)))) (define (update-sprite-vertices! sprite) diff --git a/2d/vector.scm b/2d/vector.scm deleted file mode 100644 index c6817ec..0000000 --- a/2d/vector.scm +++ /dev/null @@ -1,90 +0,0 @@ -;;; guile-2d -;;; Copyright (C) 2013 David Thompson <dthompson2@worcester.edu> -;;; -;;; Guile-2d is free software: you can redistribute it and/or modify it -;;; under the terms of the GNU Lesser General Public License as -;;; published by the Free Software Foundation, either version 3 of the -;;; License, or (at your option) any later version. -;;; -;;; Guile-2d is distributed in the hope that it will be useful, but -;;; WITHOUT ANY WARRANTY; without even the implied warranty of -;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -;;; Lesser General Public License for more details. -;;; -;;; You should have received a copy of the GNU Lesser General Public -;;; License along with this program. If not, see -;;; <http://www.gnu.org/licenses/>. - -;;; Commentary: -;; -;; Vector math operations. -;; -;;; Code: - -(define-module (2d vector) - #:use-module (rnrs base) - #:export (vx - vy - vz - v+ - v* - scale - mag - normalize - vector-reduce - vector-from-polar)) - -(define (vx vector) - "Returns the first element of a vector." - (vector-ref vector 0)) - -(define (vy vector) - "Returns the second element of a vector." - (vector-ref vector 1)) - -(define (vz vector) - "Returns the third element of a vector." - (vector-ref vector 2)) - -(define (v+ . vectors) - "Adds vectors." - (apply vector-map + vectors)) - -(define (v* . vectors) - "Multiplies vectors." - (apply vector-map * vectors)) - -(define (scale vector scalar) - "Multiplies a vector by a scalar." - (vector-map (lambda (e) (* scalar e)) vector)) - -(define (mag vector) - "Returns the magnitude of a vector." - (sqrt (vector-reduce + 0 (vector-map (lambda (e) (* e e)) vector)))) - -(define (normalize vector) - "Normalizes a vector." - (let ((m (mag vector))) - (vector-map (lambda (e) (/ e m)) vector))) - -(define (vector-reduce proc default vector) - "Performs a reduce operation on a vector." - (cond ((= (vector-length vector) 0) - default) - ((= (vector-length vector) 1) - (vector-ref vector 0)) - (else - (let loop ((i 2) - (prev (proc (vector-ref vector 0) - (vector-ref vector 1)))) - (if (= (vector-length vector) i) - prev - (loop (1+ i) (proc prev (vector-ref vector i)))))))) - -(define (vector-from-polar r theta) - "Converts polar coordinates into a cartesian vector." - (vector (* r (cos theta)) - (* r (sin theta)))) - -(re-export (vector-map . vector-map) - (vector-for-each . vector-for-each)) diff --git a/examples/action.scm b/examples/action.scm index e5dce93..dab15c1 100644 --- a/examples/action.scm +++ b/examples/action.scm @@ -5,7 +5,7 @@ (2d agenda) (2d coroutine) (2d actions) - (2d vector)) + (2d vector2)) (define window-width 800) (define window-height 600) @@ -16,8 +16,8 @@ ;; Load a sprite and center it on the screen. (define sprite (load-sprite "images/sprite.png" - #:position (vector (/ window-width 2) - (/ window-height 2)))) + #:position (vector2 (/ window-width 2) + (/ window-height 2)))) (define (key-down key mod unicode) (cond ((any-equal? key 'escape 'q) @@ -36,7 +36,7 @@ (action-parallel ;; Move horizontally across the screen in 60 frames. (lerp (lambda (x) - (set-sprite-position! sprite (vector x (/ window-height 2)))) + (set-sprite-position! sprite (vector2 x (/ window-height 2)))) 0 800 60) ;; Rotate 1080 degrees in 120 frames. (lerp (lambda (angle) diff --git a/examples/animation.scm b/examples/animation.scm index d00a9df..0e58b8e 100644 --- a/examples/animation.scm +++ b/examples/animation.scm @@ -2,8 +2,8 @@ (2d game-loop) (2d helpers) (2d sprite) - (2d texture) - (2d vector) + (2d tileset) + (2d vector2) (2d window)) (define window-width 800) @@ -28,21 +28,21 @@ ;; Load a texture, split it into 64x64 tiles, and build an animated ;; sprite out of it. (define animation - (let* ((tiles (split-texture (load-texture "images/princess.png") 64 64)) - (frames (vector (vector-ref tiles 19) - (vector-ref tiles 20) - (vector-ref tiles 21) - (vector-ref tiles 22) - (vector-ref tiles 23) - (vector-ref tiles 24) - (vector-ref tiles 25) - (vector-ref tiles 26)))) + (let* ((tiles (load-tileset "images/princess.png" 64 64)) + (frames (vector (tileset-ref tiles 19) + (tileset-ref tiles 20) + (tileset-ref tiles 21) + (tileset-ref tiles 22) + (tileset-ref tiles 23) + (tileset-ref tiles 24) + (tileset-ref tiles 25) + (tileset-ref tiles 26)))) (make-animation frames 6 #t))) (define sprite (make-sprite animation - #:position (vector (/ window-width 2) - (/ window-height 2)))) + #:position (vector2 (/ window-width 2) + (/ window-height 2)))) (run-game-loop) diff --git a/examples/coroutine.scm b/examples/coroutine.scm index 250cdbb..0a4d000 100644 --- a/examples/coroutine.scm +++ b/examples/coroutine.scm @@ -3,7 +3,8 @@ (2d window) (2d helpers) (2d agenda) - (2d coroutine)) + (2d coroutine) + (2d vector2)) (define window-width 800) (define window-height 600) @@ -13,8 +14,8 @@ (define sprite (load-sprite "images/sprite.png" - #:position (vector (/ window-width 2) - (/ window-height 2)))) + #:position (vector2 (/ window-width 2) + (/ window-height 2)))) (define (key-down key mod unicode) (cond ((any-equal? key 'escape 'q) @@ -34,8 +35,8 @@ (agenda-schedule (colambda () (while #t - (set-sprite-position! sprite (vector (random window-width) - (random window-height))) + (set-sprite-position! sprite (vector2 (random window-width) + (random window-height))) (wait 60)))) (run-game-loop) diff --git a/examples/font.scm b/examples/font.scm index 8cdfd3a..69da60a 100644 --- a/examples/font.scm +++ b/examples/font.scm @@ -3,13 +3,14 @@ (2d game-loop) (2d window) (2d helpers) - (2d font)) + (2d font) + (2d vector2)) (define window-width 800) (define window-height 600) (define font (load-font "fonts/Boxy-Bold.ttf" 48)) (define text "The quick brown fox jumped over the lazy dog.") -(define textbox (make-textbox font text #(320 300) white 'left 200)) +(define textbox (make-textbox font text (vector2 320 300) white 'left 200)) ;; Open the window. (open-window window-width window-height) diff --git a/examples/particles.scm b/examples/particles.scm index 5251ac0..dfe3322 100644 --- a/examples/particles.scm +++ b/examples/particles.scm @@ -5,7 +5,7 @@ (2d helpers) (2d sprite) (2d texture) - (2d vector) + (2d vector2) (2d window)) (set! *random-state* (random-state-from-platform)) @@ -35,7 +35,7 @@ (open-window window-width window-height) -(define stars (load-sprite "images/stars.png" #:anchor #(0 0))) +(define stars (load-sprite "images/stars.png" #:anchor null-vector2)) (define particle-image (load-texture "images/bullet.png")) (define particle-width (texture-width particle-image)) (define particle-height (texture-height particle-image)) @@ -44,10 +44,10 @@ (list-tabulate particle-count (lambda (n) (make-particle (make-sprite particle-image) - (vector (random window-width) - (random window-height)) - (vector (* (random:normal) 1) - (* (random:normal) 1)))))) + (vector2 (random window-width) + (random window-height)) + (vector2 (* (random:normal) 1) + (* (random:normal) 1)))))) (define batch (make-sprite-batch (* particle-count 4))) (define (draw-particles particles) diff --git a/examples/simple.scm b/examples/simple.scm index e5f5ae0..6a4e928 100644 --- a/examples/simple.scm +++ b/examples/simple.scm @@ -1,7 +1,8 @@ (use-modules (2d sprite) (2d game-loop) (2d window) - (2d helpers)) + (2d helpers) + (2d vector2)) (define window-width 800) (define window-height 600) @@ -11,8 +12,8 @@ (define sprite (load-sprite "images/sprite.png" - #:position (vector (/ window-width 2) - (/ window-height 2)))) + #:position (vector2 (/ window-width 2) + (/ window-height 2)))) (define (quit-demo) (close-window) diff --git a/examples/tilemap.scm b/examples/tilemap.scm index 3d15049..b64c04e 100644 --- a/examples/tilemap.scm +++ b/examples/tilemap.scm @@ -6,7 +6,7 @@ (2d texture) (2d tileset) (2d sprite) - (2d vector) + (2d vector2) (2d window)) (define window-width 800) @@ -55,7 +55,6 @@ (define tile-width 32) (define tile-height 32) -(define map #f) (define (random-map width height tileset) (let ((tiles (make-array 0 height width)) @@ -68,9 +67,9 @@ (define (build-sprite x y) (let ((region (tileset-ref tileset (array-ref tiles y x)))) (make-sprite region - #:position (vector (* x tile-width) - (* y tile-height)) - #:anchor #(0 0)))) + #:position (vector2 (* x tile-width) + (* y tile-height)) + #:anchor null-vector2))) (let ((sprites (list-ec (: y height) (list-ec (: x width) |