-
Notifications
You must be signed in to change notification settings - Fork 3
/
tex.lisp
72 lines (64 loc) · 2.63 KB
/
tex.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(in-package :lispstone)
(defclass tex ()
((renderer
:initarg :renderer
:initform (error "Must supply a renderer")
:accessor tex-renderer)
(width
:accessor tex-width
:initform 0 )
(height
:accessor tex-height
:initform 0)
(texture
:accessor tex-texture
:initform nil)))
(defun load-texture-from-texture (renderer sdl-texture)
(let ((tex (make-instance 'tex :renderer renderer)))
(with-slots (renderer texture width height) tex
(setf width (sdl2:texture-width sdl-texture))
(setf height (sdl2:texture-height sdl-texture))
(setf texture sdl-texture))
tex))
(defun load-texture-from-file (renderer filename)
(let ((tex (make-instance 'tex :renderer renderer)))
(with-slots (renderer texture width height) tex
(let ((surface (sdl2-image:load-image filename)))
(setf width (sdl2:surface-width surface))
(setf height (sdl2:surface-height surface))
(sdl2:set-color-key surface :true (sdl2:map-rgb (sdl2:surface-format surface)
0 #xFF #xFF))
(setf texture (sdl2:create-texture-from-surface renderer surface))))
tex))
(defun load-texture-from-text (renderer text &key (font *font*) (r 227) (g 215) (b 186) (a 255))
(let ((tex (make-instance 'tex :renderer renderer)))
(with-slots (renderer texture width height) tex
(let ((surface (sdl2-ttf:render-utf8-blended font text r g b a)))
(setf width (sdl2:surface-width surface))
(setf height (sdl2:surface-height surface))
(setf texture (sdl2:create-texture-from-surface renderer surface))
(sdl2:free-surface surface)))
tex))
(defun set-color (tex r g b)
(sdl2:set-texture-color-mod (tex-texture tex) r g b))
(defun render-tex (tex x y &key clip angle center flip)
(with-slots (renderer texture width height) tex
(let ((rect (sdl2:make-rect x
y
(if clip (sdl2:rect-width clip) width)
(if clip (sdl2:rect-height clip) height))))
(sdl2:render-copy-ex renderer
texture
:source-rect clip
:dest-rect rect
:angle angle
:center center
:flip (list (or flip :none)))
(sdl2:free-rect rect))))
(defun destroy-tex (tex)
(with-slots (texture) tex
(sdl2:destroy-texture texture)))
(defmacro with-text ((renderer tex text &rest rest) &body body)
`(let ((,tex (load-texture-from-text ,renderer ,text ,@rest)))
,@body
(destroy-tex ,tex)))