summaryrefslogtreecommitdiff
path: root/chickadee/graphics/viewport.scm
diff options
context:
space:
mode:
Diffstat (limited to 'chickadee/graphics/viewport.scm')
-rw-r--r--chickadee/graphics/viewport.scm96
1 files changed, 34 insertions, 62 deletions
diff --git a/chickadee/graphics/viewport.scm b/chickadee/graphics/viewport.scm
index e305c19..4258e05 100644
--- a/chickadee/graphics/viewport.scm
+++ b/chickadee/graphics/viewport.scm
@@ -26,31 +26,26 @@
#:use-module (chickadee utils)
#:use-module (chickadee graphics color)
#:use-module (chickadee graphics engine)
+ #:use-module (chickadee graphics gpu)
#:use-module (chickadee graphics gl)
#:export (make-viewport
viewport?
- viewport-x
- viewport-y
- viewport-width
- viewport-height
+ viewport-rect
viewport-clear-color
viewport-clear-flags
null-viewport
clear-viewport
- g:viewport
- current-viewport
+ with-viewport
%default-clear-flags
%default-clear-color))
(define-record-type <viewport>
- (%make-viewport x y width height clear-color clear-flags)
+ (%make-viewport rect clear-color clear-flags clear-mask)
viewport?
- (x viewport-x)
- (y viewport-y)
- (width viewport-width)
- (height viewport-height)
+ (rect viewport-rect)
(clear-color viewport-clear-color)
- (clear-flags viewport-clear-flags))
+ (clear-flags viewport-clear-flags)
+ (clear-mask viewport-clear-mask))
(define %default-clear-flags '(color-buffer depth-buffer stencil-buffer))
;; Just a fun color from the Dawnbringer 32-color palette instead of
@@ -62,6 +57,19 @@
n
(error "expecting non-negative integer:" n)))
+;; TODO: This is gross. Get rid of it.
+(define clear-buffer-mask
+ (memoize
+ (lambda (flags)
+ (apply logior
+ ;; Map symbols to OpenGL constants.
+ (map (match-lambda
+ ('depth-buffer 256)
+ ('accum-buffer 512)
+ ('stencil-buffer 1024)
+ ('color-buffer 16384))
+ flags)))))
+
(define* (make-viewport x y width height #:key
(clear-color %default-clear-color)
(clear-flags %default-clear-flags))
@@ -71,59 +79,23 @@ viewport with CLEAR-COLOR when clearing the screen. Clear the buffers
denoted by the list of symbols in CLEAR-FLAGS. Possible values for
CLEAR-FLAGS are 'color-buffer', 'depth-buffer', 'accum-buffer', and
'stencil-buffer'."
- (%make-viewport (assert-non-negative-integer x)
- (assert-non-negative-integer y)
- (assert-non-negative-integer width)
- (assert-non-negative-integer height)
+ (%make-viewport (make-window-rect (assert-non-negative-integer x)
+ (assert-non-negative-integer y)
+ (assert-non-negative-integer width)
+ (assert-non-negative-integer height))
clear-color
- clear-flags))
+ clear-flags
+ (clear-buffer-mask clear-flags)))
(define null-viewport (make-viewport 0 0 0 0))
-(define clear-buffer-mask
- (memoize
- (lambda (flags)
- (apply logior
- ;; Map symbols to OpenGL constants.
- (map (match-lambda
- ('depth-buffer 256)
- ('accum-buffer 512)
- ('stencil-buffer 1024)
- ('color-buffer 16384))
- flags)))))
+(define-syntax-rule (with-viewport v body ...)
+ (let ((viewport v))
+ (with-graphics-state ((viewport (viewport-rect viewport))
+ (clear-color (viewport-clear-color viewport)))
+ body ...)))
+;; TODO: Add clear buffer mask to managed gpu state.
(define (clear-viewport)
- (gl-clear (clear-buffer-mask (viewport-clear-flags (current-viewport)))))
-
-(define (apply-viewport viewport)
- "Set the OpenGL state for VIEWPORT. Clip rendering to the viewport
-area, set the clear color, and clear necessary buffers."
- (unless (eq? viewport null-viewport)
- (let ((x (viewport-x viewport))
- (y (viewport-y viewport))
- (w (viewport-width viewport))
- (h (viewport-height viewport))
- (c (viewport-clear-color viewport)))
- (gl-enable (enable-cap scissor-test))
- (gl-viewport x y w h)
- (gl-scissor x y w h)
- (gl-clear-color (color-r c) (color-g c) (color-b c) (color-a c)))))
-
-(define (bind-viewport viewport)
- "Set the OpenGL state for VIEWPORT. Clip rendering to the viewport
-area, and set the clear color.."
- (unless (eq? viewport null-viewport)
- (let ((x (viewport-x viewport))
- (y (viewport-y viewport))
- (w (viewport-width viewport))
- (h (viewport-height viewport))
- (c (viewport-clear-color viewport)))
- (gl-enable (enable-cap scissor-test))
- (gl-viewport x y w h)
- (gl-scissor x y w h)
- (gl-clear-color (color-r c) (color-g c) (color-b c) (color-a c)))))
-
-(define-graphics-state g:viewport
- current-viewport
- #:default null-viewport
- #:bind bind-viewport)
+ (graphics-engine-commit! (current-graphics-engine))
+ (gl-clear (clear-buffer-mask %default-clear-flags)))