Ported to OpenGL
[uxul-world.git] / files.lisp
1 ;;; Copyright 2009-2011 Christoph Senjak
2
3 ;; This file declares the constants for loading different files and
4 ;; file-formats.
5
6 (in-package :uxul-world)
7
8 (defun si (var val)
9   (setf (symbol-value (intern var)) val))
10
11 (defun stretch-image (x y img)
12   "Resize that file to x times y."
13   (uxul-world::resize-bmp-blob img x y))
14
15 (defun ash-sized-image (img a)
16   "Calculate an image of half/eighth/quarter of the size."
17   (let ((w (bmp-width img))
18         (h (bmp-height img)))
19   (uxul-world::resize-bmp-blob img (max 1 (floor (/ w a))) (max 1 (floor (/ h a))))))
20
21 (defun all-sizes (img)
22   (list img
23         (ash-sized-image img 2)
24         (ash-sized-image img 4)
25         (ash-sized-image img 8)))
26
27 (defun init-bmp-file (file)
28   "Load an image file into a Variable. Set |filename| (without .png
29 and path) to a list with all sizes of that image."
30   (si (pathname-name file) 
31       (all-sizes
32        (with-open-file (in file :element-type '(unsigned-byte 8)) 
33          (let* ((length (file-length in))
34                 (content (make-array (list length)
35                                      :element-type '(unsigned-byte 8)
36                                      :adjustable nil)))
37            (read-sequence content in)
38            content)))))
39
40 (defun bmp-p (file)
41   "Is the file relevant for initialization? So far only .png-files are
42 relevant."
43   (string= (pathname-type file) "bmp"))
44
45 (defun init-bmp-files ()
46   (cl-fad:walk-directory
47    (asdf:component-pathname (asdf:find-system :uxul-world))
48    #'init-bmp-file :test #'bmp-p))
49
50 (defun init-files ()
51   "Load the relevant files into variables"
52   (init-bmp-files))
53
54 (init-files)