summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2019-06-04 17:12:36 -0400
committerDavid Thompson <dthompson2@worcester.edu>2019-06-04 17:12:36 -0400
commit9d050e6ba7c40c8ed92aa1eeab7a98d89ca85970 (patch)
tree54065f8904533fb9d3c8e894cbdebc3513f0f3dc
parent3da34ff5fea10d783711a63d8f53851ce633600d (diff)
render: tiled: Add point->tile.
* chickadee/render/tiled.scm (point->tile): New procedure. * doc/api.texi (Tiled): Document it. * examples/tiled.scm: Render tile coordinates in bottom-left corner.
-rw-r--r--chickadee/render/tiled.scm6
-rw-r--r--doc/api.texi5
-rw-r--r--examples/tiled.scm17
3 files changed, 25 insertions, 3 deletions
diff --git a/chickadee/render/tiled.scm b/chickadee/render/tiled.scm
index 1fbdbe6..e51e3c0 100644
--- a/chickadee/render/tiled.scm
+++ b/chickadee/render/tiled.scm
@@ -47,6 +47,7 @@
tile-map-properties
tile-map-rect
tile-map-layer-ref
+ point->tile
animation-frame?
animation-frame-tile
@@ -194,6 +195,11 @@
(else
(loop (+ i 1)))))))
+(define (point->tile tile-map x y)
+ "Translate the pixel coordinates (X, Y) into tile coordinates."
+ (values (floor (/ x (tile-map-tile-width tile-map)))
+ (floor (/ y (tile-map-tile-height tile-map)))))
+
(define (load-tile-map file-name)
"Load the Tiled TMX formatted map in FILE-NAME."
(define map-directory
diff --git a/doc/api.texi b/doc/api.texi
index 3ea911b..5d7623b 100644
--- a/doc/api.texi
+++ b/doc/api.texi
@@ -1612,6 +1612,11 @@ Return the layers of @var{tile-map}.
Return the custom properties of @var{tile-map}.
@end deffn
+@deffn {Procedure} point->tile tile-map x y
+Translate the pixel coordinates (@var{x}, @var{y}) into tile
+coordinates.
+@end deffn
+
@deffn {Procedure} draw-tile-map tile-map [#:layers] [#:region] @
[#:origin] [#:position] [#:scale] [#:rotation]
diff --git a/examples/tiled.scm b/examples/tiled.scm
index 5ff2b7f..e6099df 100644
--- a/examples/tiled.scm
+++ b/examples/tiled.scm
@@ -1,17 +1,23 @@
(use-modules (chickadee)
(chickadee math vector)
(chickadee math rect)
+ (chickadee render font)
(chickadee render tiled)
- (ice-9 match))
+ (ice-9 format)
+ (ice-9 match)
+ (srfi srfi-11))
(define map #f)
(define camera #v(0.0 0.0))
+(define text-position #v(4.0 4.0))
+(define text "0, 0")
(define (load)
(set! map (load-tile-map "maps/example.tmx")))
(define (draw alpha)
- (draw-tile-map map #:position camera))
+ (draw-tile-map map #:position camera)
+ (draw-text text text-position))
(define inc 8.0)
(define (key-press key scancode modifiers repeat?)
@@ -23,9 +29,14 @@
('q (abort-game))
(_ #t)))
+(define (mouse-move x y x-rel y-rel buttons)
+ (let-values (((tx ty) (point->tile map x y)))
+ (set! text (format #f "~d, ~d" tx ty))))
+
(run-game #:window-width 320
#:window-height 240
#:window-title "tile map demo"
#:load load
#:draw draw
- #:key-press key-press)
+ #:key-press key-press
+ #:mouse-move mouse-move)