bloatrunner/game/util/assets.scm

62 lines
2.2 KiB
Scheme
Raw Normal View History

2024-05-22 23:29:52 +02:00
(define-module (game util assets)
#:use-module (ice-9 ftw)
#:use-module (ice-9 textual-ports)
#:use-module (chickadee graphics texture)
#:use-module (chickadee graphics color)
2024-05-25 15:19:30 +02:00
#:export (assets-load assets-load-image tile-texture tile-atlas hero-texture hero-atlas assets-map-levels read-level-map)
2024-05-22 23:29:52 +02:00
)
(define tile-texture #f)
(define tile-atlas #f)
(define hero-texture #f)
(define hero-atlas #f)
2024-05-22 23:29:52 +02:00
2024-05-25 13:19:31 +02:00
(define (assets-location)
2024-05-25 15:19:30 +02:00
"find the location of the assets directory. The location is specified
by the ASSET_DIR environment variable, or defaults to 'assets' in the
current directory."
2024-05-25 14:10:23 +02:00
(pk "Assets location: "
2024-05-25 15:19:30 +02:00
(or (getenv "ASSET_DIR") "assets" )))
2024-05-25 13:19:31 +02:00
(define (assets-file-name . names)
"Return the full path of a file in the assets directory. The file is
specified by a list of names, which are joined together. The
ASSET_DIR environment variable can be used to specify a prefix to the
asset directory."
(string-append (assets-location) "/" (string-join names "/")))
(define (read-level-map filename)
2024-05-25 15:19:30 +02:00
"Read a level map from a file. The map is a list of strings, where each
string is a row of the map. The map is read from the file in the
'levels' directory."
(call-with-input-file (assets-file-name "levels" filename) get-string-all))
(define (level-map? filename )
"Return true if the filename is a level map file. Level map files have
the extension '.map'."
(string-suffix? ".map" filename))
(define (assets-map-levels f)
2024-05-25 15:19:30 +02:00
"Apply a function to each level map file in the 'levels' directory. The
function is passed the filename of the level map file."
(map
(compose f read-level-map)
(scandir (assets-file-name "levels") level-map?)))
2024-05-25 14:10:23 +02:00
(define (assets-load-image filename)
2024-05-25 15:19:30 +02:00
"Load an image from a file in the assets directory. The filename is
specified as a list of names, which are joined together. The image is
returned as a texture."
2024-05-25 14:10:23 +02:00
(load-image (assets-file-name "images" filename)))
(define (assets-load)
2024-05-25 15:19:30 +02:00
"Load all the assets for the game."
2024-05-25 14:10:23 +02:00
(set! tile-texture (assets-load-image "simples_pimples.png"))
(set! tile-atlas (split-texture tile-texture 16 16))
2024-05-25 14:10:23 +02:00
(set! hero-texture (assets-load-image "lr_penguin2.png"))
(set! hero-atlas (split-texture hero-texture 32 32)))