diff options
-rw-r--r-- | 2d/sprite.scm | 30 | ||||
-rw-r--r-- | examples/coroutine.scm | 22 | ||||
-rw-r--r-- | examples/particles.scm | 3 |
3 files changed, 29 insertions, 26 deletions
diff --git a/2d/sprite.scm b/2d/sprite.scm index c050a39..c3dc4f1 100644 --- a/2d/sprite.scm +++ b/2d/sprite.scm @@ -25,6 +25,7 @@ (define-module (2d sprite) #:use-module (srfi srfi-1) #:use-module (srfi srfi-9) + #:use-module (srfi srfi-9 gnu) #:use-module (gl) #:use-module (gl contrib packed-struct) #:use-module ((sdl sdl) #:prefix SDL:) @@ -46,16 +47,16 @@ animated-sprite? sprite-drawable sprite-position - set-sprite-drawable! - set-sprite-position! - set-sprite-scale! - set-sprite-rotation! - set-sprite-color! - set-sprite-anchor! sprite-scale sprite-rotation sprite-color sprite-anchor + set-sprite-drawable + set-sprite-position + set-sprite-scale + set-sprite-rotation + set-sprite-color + set-sprite-anchor load-sprite draw-sprite)) @@ -78,15 +79,15 @@ ;; The <sprite> type represents a drawable object (texture, ;; texture-region, animation, etc.) with a given position, scale, ;; rotation, and color. -(define-record-type <sprite> +(define-immutable-record-type <sprite> (%make-sprite drawable position scale rotation color anchor vertices animator) sprite? - (drawable sprite-drawable set-sprite-drawable!) - (position sprite-position set-sprite-position!) - (scale sprite-scale set-sprite-scale!) - (rotation sprite-rotation set-sprite-rotation!) - (color sprite-color set-sprite-color!) - (anchor sprite-anchor %set-sprite-anchor!) + (drawable sprite-drawable set-sprite-drawable) + (position sprite-position set-sprite-position) + (scale sprite-scale set-sprite-scale) + (rotation sprite-rotation set-sprite-rotation) + (color sprite-color set-sprite-color) + (anchor sprite-anchor set-sprite-anchor) (vertices sprite-vertices) (animator sprite-animator)) @@ -151,9 +152,6 @@ optional keyword arguments." (drawable-texture (sprite-drawable sprite) (sprite-animator sprite)))) -(define (set-sprite-anchor! sprite anchor) - (%set-sprite-anchor! sprite (anchor-texture (sprite-texture sprite) anchor))) - (define (update-sprite-animator! sprite) (animator-update! (sprite-animator sprite)) (update-sprite-vertices! sprite)) diff --git a/examples/coroutine.scm b/examples/coroutine.scm index 7b4a1e6..741b357 100644 --- a/examples/coroutine.scm +++ b/examples/coroutine.scm @@ -15,10 +15,12 @@ ;;; along with this program. If not, see ;;; <http://www.gnu.org/licenses/>. -(use-modules (2d agenda) +(use-modules (srfi srfi-26) + (2d agenda) (2d coroutine) (2d game) (2d sprite) + (2d texture) (2d vector2) (2d window)) @@ -27,19 +29,23 @@ (define window-width 640) (define window-height 480) -(define sprite (load-sprite "images/p1_front.png" - #:position (vector2 320 240))) +(define texture (load-texture "images/p1_front.png")) + +(define sprite + (make-sprite texture + #:position (vector 320 240))) + +(define (random-vector2) + (vector2 (random window-width) + (random window-height))) ;; Simple script that moves the sprite to a random location every ;; second. (coroutine (while #t - (set-sprite-position! - sprite - (vector2 (random window-width) - (random window-height))) + (set! sprite (set-sprite-position sprite (random-vector2))) (wait 15) - (set-sprite-rotation! sprite (random 360)) + (set! sprite (set-sprite-rotation sprite (random 360))) (wait 15))) (add-hook! draw-hook (lambda (dt alpha) (draw-sprite sprite))) diff --git a/examples/particles.scm b/examples/particles.scm index 4269b99..b60a537 100644 --- a/examples/particles.scm +++ b/examples/particles.scm @@ -65,8 +65,7 @@ (for-each (lambda (p) (let* ((sprite (particle-sprite p))) - (set-sprite-position! sprite (particle-position p)) - (draw-sprite sprite))) + (draw-sprite (set-sprite-position sprite (particle-position p))))) particles)) (define (draw dt alpha) |