summaryrefslogtreecommitdiff
path: root/starling/node-2d.scm
diff options
context:
space:
mode:
Diffstat (limited to 'starling/node-2d.scm')
-rw-r--r--starling/node-2d.scm128
1 files changed, 37 insertions, 91 deletions
diff --git a/starling/node-2d.scm b/starling/node-2d.scm
index 947e377..142da31 100644
--- a/starling/node-2d.scm
+++ b/starling/node-2d.scm
@@ -47,15 +47,11 @@
target
offset
position
- width
- height
-
- <view-2d>
- camera
- area
+ resolution
+ viewport
<canvas>
- views
+ cameras
<scene-2d>
@@ -126,29 +122,36 @@
;; Cameras define a view into the scene. They are attached to a 2D
;; node and follow it around.
+(define (default-resolution)
+ (match (current-window-size)
+ ((width height)
+ (vec2 width height))))
+
+(define (default-viewport)
+ (match (current-window-size)
+ ((width height)
+ (make-viewport 0 0 width height))))
+
(define-class <camera-2d> ()
(target #:accessor target #:init-form #f #:init-keyword #:target)
- (offset #:getter offset #:init-form #v(0.0 0.0) #:init-keyword #:offset)
- (position #:getter position #:init-form #v(0.0 0.0))
- (last-position #:getter last-position #:init-form #v(0.0 0.0))
- (width #:getter width #:init-keyword #:width)
- (height #:getter height #:init-keyword #:height)
+ (offset #:getter offset #:init-form (vec2 0.0 0.0) #:init-keyword #:offset)
+ (position #:getter position #:init-form (vec2 0.0 0.0))
+ (last-position #:getter last-position #:init-form (vec2 0.0 0.0))
+ (resolution #:getter resolution #:init-keyword #:resolution
+ #:init-thunk default-resolution)
+ (viewport #:getter viewport #:init-keyword #:viewport
+ #:init-thunk default-viewport)
(projection-matrix #:accessor projection-matrix)
;; Combined projection/view matrix
- (view-matrix #:getter view-matrix #:init-form (make-identity-matrix4))
- (framebuffer #:accessor framebuffer))
+ (view-matrix #:getter view-matrix #:init-form (make-identity-matrix4)))
(define-method (initialize (camera <camera-2d>) initargs)
(next-method)
- ;; Initialize framebuffer and orthographic projection matrix based
- ;; on the resolution of the camera.
- (set! (framebuffer camera)
- (make-framebuffer (width camera)
- (height camera)
- #:min-filter 'nearest
- #:mag-filter 'nearest))
- (set! (projection-matrix camera)
- (orthographic-projection 0 (width camera) (height camera) 0 0 1)))
+ ;; Initialize orthographic projection matrix based on the resolution
+ ;; of the camera.
+ (let ((r (resolution camera)))
+ (set! (projection-matrix camera)
+ (orthographic-projection 0 (vec2-x r) (vec2-y r) 0 0 1))))
;; This method can be overridden by subclasses to create custom camera
;; movement.
@@ -172,95 +175,38 @@
(matrix4-mult! m m (projection-matrix camera))))))
(define-syntax-rule (with-camera camera body ...)
- (with-framebuffer (framebuffer camera)
- (clear-screen)
+ (with-viewport (viewport camera)
(with-projection (if (target camera)
(view-matrix camera)
(projection-matrix camera))
body ...)))
-
-
-;;;
-;;; 2D View
-;;;
-
-;; Views render the output of a camera to a portion of the game
-;; window.
-
-(define-class <view-2d> ()
- (camera #:accessor camera #:init-keyword #:camera)
- (area #:getter area #:init-keyword #:area)
- (clear-color #:getter clear-color #:init-keyword #:clear-color
- #:init-value tango-light-sky-blue)
- (viewport #:accessor viewport)
- (projection-matrix #:accessor projection-matrix)
- (sprite-rect #:accessor sprite-rect))
-
-(define-method (initialize (view <view-2d>) initargs)
- (next-method)
- (let* ((area (area view))
- (x (rect-x area))
- (y (rect-y area))
- (w (rect-width area))
- (h (rect-height area)))
- (set! (viewport view)
- (make-viewport (inexact->exact x)
- (inexact->exact y)
- (inexact->exact w)
- (inexact->exact h)
- #:clear-color (clear-color view)))
- (set! (sprite-rect view) (make-rect 0.0 0.0 w h))
- (set! (projection-matrix view) (orthographic-projection 0 w h 0 0 1))))
-
-(define %identity-matrix (make-identity-matrix4))
-
-(define-method (render (view <view-2d>))
- (with-viewport (viewport view)
- (with-projection (projection-matrix view)
- (draw-sprite* (framebuffer-texture (framebuffer (camera view)))
- (sprite-rect view)
- %identity-matrix))))
-
;;;
;;; 2D Canvas
;;;
;; The canvas is the root of a 2D scene. It handles rendering one or
-;; more views.
-
-(define (make-default-views)
- (match (current-window-size)
- ((width height)
- (list
- (make <view-2d>
- #:camera (make <camera-2d>
- #:width width
- #:height height)
- #:area (make-rect 0 0 width height))))))
+;; more cameras.
(define-class <canvas> (<node>)
- (views #:accessor views #:init-thunk make-default-views
- #:init-keyword #:views))
+ (cameras #:accessor cameras #:init-form (list (make <camera-2d>))
+ #:init-keyword #:cameras))
(define-method (update (canvas <canvas>) dt)
- (for-each (lambda (view)
- (update (camera view) dt))
- (views canvas)))
+ (for-each (lambda (camera)
+ (update camera dt))
+ (cameras canvas)))
(define-method (render-tree (canvas <canvas>) alpha)
+ (render canvas alpha)
;; Draw scene from the viewpoint of each camera.
- (for-each (lambda (view)
- (with-camera (camera view)
+ (for-each (lambda (camera)
+ (with-camera camera
(for-each-child (lambda (child)
(render-tree child alpha))
canvas)))
- (views canvas))
- (render canvas alpha))
-
-(define-method (render (canvas <canvas>) alpha)
- (for-each render (views canvas)))
+ (cameras canvas)))
;;;