Added more utilities: WITH-INIT, WIDTH-OF, HEIGHT-OF, and WITH-BOUND-IMAGE.
[cl-devil.git] / utilities.lisp
blob093eff588cd0fd53e2c85945e190121d52933e1f
2 (in-package :cl-devil)
4 (defun w-i-args-helper (args)
5 (when args
6 `((bind-image it)
7 ,(cons
8 (cond ((= 1 (length args)) 'il:load-image)
9 ((= 3 (length args)) 'il:load-l)
10 ((stringp (second args)) 'il:load)
11 (t 'il:load-f))
12 args))))
14 ;;; XXX I don't like the potential confusion between WITH-BOUND-IMAGE
15 ;;; and WITH-IMAGES, but WITH-NEW-IMAGES and WITH-LOADED-IMAGES all
16 ;;; give the wrong impression, alas.
17 (defmacro with-images ((&rest images) &body body)
18 "Generates an IL image for each of IMAGES, binding and loading if a parameter is supplied. BODY is executed, and the images are freed thereafter."
19 (let ((ids (gensym))
20 (count (length images)))
21 `(cffi:with-foreign-object (,ids :uint ,count)
22 (il:gen-images ,count ,ids)
23 (unwind-protect
24 (let (,@(loop for x in images
25 for (var . args) = (if (listp x) x (list x))
26 for i from 0
27 collect `(,var (anaphora:aprog1 (cffi:mem-ref ,ids :uint ,i)
28 ,@(w-i-args-helper args)))))
29 ,@body)
30 (il:delete-images ,count ,ids)))))
32 (defmacro with-init (&body body)
33 `(progn (init)
34 (unwind-protect (progn ,@body)
35 (shutdown))))
37 (defun width-of (id)
38 (bind-image id)
39 (get-integer :image-width))
41 (defun height-of (id)
42 (bind-image id)
43 (get-integer :image-height))
46 (defmacro with-bound-image ((id) &body body)
47 "Binds ID for the duration of BODY, returning to the previously bound image thereafter."
48 (let ((old-image (gensym)))
49 `(let ((,old-image (il:get-integer :cur-image)))
50 (il:bind-image ,id)
51 (unwind-protect (progn ,@body)
52 (il:bind-image ,old-image)))))