diff options
-rw-r--r-- | 2d/sprite.scm | 49 | ||||
-rw-r--r-- | 2d/texture.scm | 47 |
2 files changed, 49 insertions, 47 deletions
diff --git a/2d/sprite.scm b/2d/sprite.scm index 330c1e8..f2c9c83 100644 --- a/2d/sprite.scm +++ b/2d/sprite.scm @@ -60,49 +60,6 @@ draw-sprite)) ;;; -;;; Sprite Vertices -;;; - -;; Used to build OpenGL vertex array for a sprite. -(define-packed-struct sprite-vertex - ;; Position - (x float) - (y float) - ;; Texture Coordinates - (s float) - (t float)) - -(define sprite-vertex-size (packed-struct-size sprite-vertex)) -(define x-offset (packed-struct-offset sprite-vertex x)) -(define s-offset (packed-struct-offset sprite-vertex s)) - -(define (pack-sprite-vertices vertices offset width height s1 t1 s2 t2) - ;; Vertices go counter clockwise, starting from the top-left - ;; corner. - (pack vertices offset sprite-vertex 0 0 s1 t1) - (pack vertices (+ offset 1) sprite-vertex 0 height s1 t2) - (pack vertices (+ offset 2) sprite-vertex width height s2 t2) - (pack vertices (+ offset 3) sprite-vertex width 0 s2 t1)) - -(define (draw-sprite-vertices texture vertices size) - (let ((pointer-type (tex-coord-pointer-type float))) - (gl-enable-client-state (enable-cap vertex-array)) - (gl-enable-client-state (enable-cap texture-coord-array)) - (with-gl-bind-texture (texture-target texture-2d) (texture-id texture) - (set-gl-vertex-array pointer-type - vertices - 2 - #:stride sprite-vertex-size - #:offset x-offset) - (set-gl-texture-coordinates-array pointer-type - vertices - #:stride sprite-vertex-size - #:offset s-offset) - (gl-draw-arrays (begin-mode quads) 0 (* size 4))) - (gl-disable-client-state (enable-cap texture-coord-array)) - (gl-disable-client-state (enable-cap vertex-array)))) - -;;; ;;; Sprites ;;; @@ -142,7 +99,7 @@ sprite." (define (update-sprite-vertices! sprite) (let ((texture (sprite-texture sprite))) - (pack-sprite-vertices (sprite-vertices sprite) + (pack-texture-vertices (sprite-vertices sprite) 0 (texture-width texture) (texture-height texture) @@ -163,7 +120,7 @@ with a default of 0. COLOR is a color object with a default of white. ANCHOR is either a vector2 that represents the center point of the sprite, or 'center which will place the anchor at the center of DRAWABLE. Sprites are centered by default." - (let* ((vertices (make-packed-array sprite-vertex 4)) + (let* ((vertices (make-packed-array texture-vertex 4)) (animator (if (animation? drawable) (make-animator drawable) #f)) @@ -219,7 +176,7 @@ currently bound." (rotation (sprite-rotation sprite)) (color (sprite-color sprite)) (projection (signal-ref window-projection))) - (draw-sprite-vertices (sprite-texture sprite) + (draw-texture-vertices (sprite-texture sprite) (sprite-vertices sprite) 1)))) diff --git a/2d/texture.scm b/2d/texture.scm index cfcd2bf..684ad42 100644 --- a/2d/texture.scm +++ b/2d/texture.scm @@ -25,6 +25,7 @@ (define-module (2d texture) #:use-module (srfi srfi-9) #:use-module (figl gl) + #:use-module (figl contrib packed-struct) #:use-module (2d color) #:use-module (2d helpers) #:use-module (2d wrappers gl) @@ -42,7 +43,9 @@ texture-s2 texture-t2 surface->texture - draw-texture)) + texture-vertex + pack-texture-vertices + draw-texture-vertices)) ;;; ;;; Textures @@ -150,3 +153,45 @@ that will be rendered, in pixels." (texture (bitmap->texture bitmap))) (freeimage-unload bitmap) texture)) + +;;; +;;; Texture Vertices +;;; + +(define-packed-struct texture-vertex + ;; Position + (x float) + (y float) + ;; Texture Coordinates + (s float) + (t float)) + +(define texture-vertex-size (packed-struct-size texture-vertex)) +(define x-offset (packed-struct-offset texture-vertex x)) +(define s-offset (packed-struct-offset texture-vertex s)) + +(define (pack-texture-vertices vertices offset width height s1 t1 s2 t2) + ;; Vertices go counter clockwise, starting from the top-left + ;; corner. + (pack vertices offset texture-vertex 0 0 s1 t1) + (pack vertices (+ offset 1) texture-vertex 0 height s1 t2) + (pack vertices (+ offset 2) texture-vertex width height s2 t2) + (pack vertices (+ offset 3) texture-vertex width 0 s2 t1)) + +(define (draw-texture-vertices texture vertices size) + (let ((pointer-type (tex-coord-pointer-type float))) + (gl-enable-client-state (enable-cap vertex-array)) + (gl-enable-client-state (enable-cap texture-coord-array)) + (with-gl-bind-texture (texture-target texture-2d) (texture-id texture) + (set-gl-vertex-array pointer-type + vertices + 2 + #:stride texture-vertex-size + #:offset x-offset) + (set-gl-texture-coordinates-array pointer-type + vertices + #:stride texture-vertex-size + #:offset s-offset) + (gl-draw-arrays (begin-mode quads) 0 (* size 4))) + (gl-disable-client-state (enable-cap texture-coord-array)) + (gl-disable-client-state (enable-cap vertex-array)))) |