-
Notifications
You must be signed in to change notification settings - Fork 24
/
viewport.lisp
98 lines (82 loc) · 2.09 KB
/
viewport.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
;;;; viewport.lisp
;;;; Please see the licence.txt for the CLinch
(in-package #:clinch)
(defclass viewport ()
((x
:initform 0
:initarg :x
:accessor x)
(y
:initform 0
:initarg :y
:accessor y)
(width
:initform 0
:initarg :width
:accessor width)
(height
:initform 0
:initarg :height
:accessor height)
(clear-color
:accessor clear-color
:initform nil
:initarg :clear-color))
(:documentation "Creates a viewport."))
(defmethod initialize-instance :after ((this viewport) &key)
"Creates a new viewport."
(with-accessors ((x x)
(y y)
(width width)
(height height)) this
(when (and x y width height)
(render this))))
(defmethod resize ((this viewport) x y w h)
"Resize the viewport."
(setf (x this) x
(y this) y
(width this) w
(height this) h)
(render this))
(defmethod width ((this viewport))
"Get viewport width."
(with-slots ((w width)) this
w))
(defmethod (setf width) (new-val (this viewport))
"Set viewport width."
(setf (slot-value this 'width) new-val))
(defmethod height ((this viewport))
"Get the viewport height."
(with-slots ((h height)) this
h))
(defmethod (setf height) (new-val (this viewport))
"Set the viewport height."
(setf (slot-value this 'height) new-val))
(defmethod render ((this viewport) &key projection)
"Makes this viewport active."
(with-accessors ((x x)
(y y)
(w width)
(h height)) this
(!!
(gl:scissor x y w h)
(gl:viewport x y w h)
(when (clear-color this)
(destructuring-bind (&optional (r 0) (g 0) (b 0) (a 1)) (clear-color this)
(gl:clear-color r g b a))))))
(defmethod quick-set ((this viewport) x y w h)
"A quick method to set all the values in the viewport."
(setf (x this) x
(y this) y
(width this) w
(height this) h)
(render this))
(defmacro with-viewport ((vp) &body body)
"A wrapper which sets and unsets a viewport."
(let ((old (gensym)))
`(let ((,old *viewport*)
(*viewport* ,vp))
(render *viewport*)
(unwind-protect
(progn ,@body)
(render ,old)))))