diff options
Diffstat (limited to 'chickadee/graphics/tiled.scm')
-rw-r--r-- | chickadee/graphics/tiled.scm | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/chickadee/graphics/tiled.scm b/chickadee/graphics/tiled.scm index 6500a17..2692418 100644 --- a/chickadee/graphics/tiled.scm +++ b/chickadee/graphics/tiled.scm @@ -240,12 +240,21 @@ (duration (attr node 'duration string->number))) ;; TODO: lookup actual tile in tileset (%make-animation-frame tile-id duration))) + (define (atlas-ref atlas id rows columns) + ;; Tiled enumerates tiles from the top-left of the tileset image, + ;; but here in OpenGL land the origin is in the bottom-left, so we + ;; have to do some math invert the rows. + (texture-atlas-ref atlas + (+ (* (- rows (floor (/ id columns)) 1) + columns) + (modulo id columns)))) (define (parse-tile node rows columns atlas batch) (let ((id (attr node 'id string->number)) (animation (map parse-frame ((sxpath '(animation frame)) node))) (properties (map parse-property ((sxpath '(properties property)) node)))) - (%make-tile id (texture-atlas-ref atlas id) batch animation properties))) + (%make-tile id (atlas-ref atlas id rows columns) + batch animation properties))) (define (parse-tiles nodes size columns atlas batch) (let ((table (make-hash-table)) (tiles (make-vector size)) @@ -257,7 +266,8 @@ (for-range ((i size)) (let ((tile (or (hash-ref table i) - (%make-tile i (texture-atlas-ref atlas i) batch #f '())))) + (%make-tile i (atlas-ref atlas i rows columns) + batch #f '())))) (vector-set! tiles i tile))) tiles)) (define (first-gid node) |