summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2017-11-10 23:17:36 -0500
committerDavid Thompson <dthompson2@worcester.edu>2017-11-10 23:21:22 -0500
commitb61558f5210ad2007b8874df6779e9cd098dad41 (patch)
tree8fed10b2773bcb2451ee401c357085dbd0d38845
parent45c5d68092d713dee1969c590a5e9f52745021a6 (diff)
render: font: Add support for tile fonts.
* chickadee/render/font.scm (load-tile-font): New procedure.
-rw-r--r--chickadee/render/font.scm30
1 files changed, 30 insertions, 0 deletions
diff --git a/chickadee/render/font.scm b/chickadee/render/font.scm
index c2c1bea..97a680b 100644
--- a/chickadee/render/font.scm
+++ b/chickadee/render/font.scm
@@ -36,6 +36,7 @@
#:use-module (chickadee render sprite)
#:use-module (chickadee render texture)
#:export (load-font
+ load-tile-font
font?
font-face
font-line-height
@@ -71,6 +72,35 @@
(set-record-type-printer! <font> display-font)
+(define* (load-tile-font file tile-width tile-height characters #:key
+ (face "untitled") (margin 0) (spacing 0))
+ "Load the font named FACE from FILE, a bitmap image containing the
+characters in the string CHARACTERS that are TILE-WIDTH by TILE-HEIGHT
+pixels in size. The characters in the image *must* appear in the
+order that they are specified in the character set or text will not
+render properly. Optionally, each tile may have SPACING pixels of
+horizontal and vertical space between surrounding tiles and the entire
+image may have MARGIN pixels of empty space around its border."
+ (let* ((atlas (split-texture (load-image file) tile-width tile-height
+ #:margin margin
+ #:spacing spacing))
+ (chars
+ (let ((table (make-hash-table)))
+ (string-for-each-index
+ (lambda (i)
+ (hash-set! table (string-ref characters i)
+ (make-font-char (string-ref characters i)
+ (texture-atlas-ref atlas i)
+ (vec2 0.0 0.0)
+ (vec2 tile-width tile-height)
+ (vec2 tile-width 0.0))))
+ characters)
+ table))
+ ;; These fonts are by definition monospace fonts, so no
+ ;; kerning.
+ (kernings (make-hash-table)))
+ (make-font face #f #f tile-height chars kernings)))
+
(define (load-font file)
"Load the Angel Code XML formatted font within FILE."
(define directory (dirname file))