diff options
Diffstat (limited to 'sly/render/sprite.scm')
-rw-r--r-- | sly/render/sprite.scm | 54 |
1 files changed, 32 insertions, 22 deletions
diff --git a/sly/render/sprite.scm b/sly/render/sprite.scm index d0ba059..c0ad5bb 100644 --- a/sly/render/sprite.scm +++ b/sly/render/sprite.scm @@ -24,29 +24,35 @@ (define-module (sly render sprite) #:use-module (srfi srfi-1) - #:use-module (srfi srfi-26) + #:use-module (srfi srfi-9) #:use-module (gl) - #:use-module (gl contrib packed-struct) - #:use-module ((sdl sdl) #:prefix SDL:) - #:use-module (sly render color) - #:use-module (sly config) #:use-module (sly agenda) #:use-module (sly utils) - #:use-module (sly math) + #:use-module (sly render) + #:use-module (sly render color) #:use-module (sly render mesh) - #:use-module (sly render model) #:use-module (sly render texture) + #:use-module (sly render utils) #:use-module (sly math vector) - #:export (make-sprite sprite load-sprite)) + #:export (make-sprite + load-sprite + sprite? + sprite-texture + sprite-mesh + render-sprite)) -;;; -;;; Sprites -;;; +(define-record-type <sprite> + (%make-sprite texture mesh) + sprite? + (texture sprite-texture) + (mesh sprite-mesh)) -(define* (make-sprite texture #:optional #:key - (anchor 'center)) - "Return a 2D rectangular mesh that displays the image TEXTURE. The -size of the mesh is the size of TEXTURE, in pixels." +(define* (make-sprite texture #:key (anchor 'center)) + "Create a sprite that displays the image in TEXTURE. The size of +the mesh is the size of TEXTURE in pixels. ANCHOR defines the origin +of the sprite. By default, the anchor is 'center', which puts the +origin in the middle of the sprite. See 'anchor-texture' for more +anchoring options." (let* ((anchor (anchor-texture texture anchor)) (x1 (- (floor (vx anchor)))) (y1 (- (floor (vy anchor)))) @@ -67,12 +73,16 @@ size of the mesh is the size of TEXTURE, in pixels." (vector2 s2 t1) (vector2 s2 t2) (vector2 s1 t2))))) - (make-model #:texture texture - #:mesh mesh - #:depth-test? #f))) + (%make-sprite texture mesh))) -(define sprite make-sprite) +(define* (load-sprite file #:key (anchor 'center)) + "Create a sprite from the texture in FILE whose origin is defined by +ANCHOR. The default anchor is 'center'." + (make-sprite (load-texture file) #:anchor anchor)) -(define* (load-sprite file-name #:key (anchor 'center)) - "Return a sprite mesh for the texture loaded from FILE-NAME." - (make-sprite (load-texture file-name) #:anchor anchor)) +(define* (render-sprite sprite) + "Create a renderer that draws a 2D rectangular mesh that displays +the image TEXTURE. The size of the mesh is the size of TEXTURE in +pixels." + (with-texture (sprite-texture sprite) + (render-mesh (sprite-mesh sprite)))) |