c91699d9500746c56b4a885465e94fc4cc51850e
[guile-sdl2.git] / sdl2 / render.scm
1 ;;; guile-sdl2 --- FFI bindings for SDL2
2 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
3 ;;;
4 ;;; This file is part of guile-sdl2.
5 ;;;
6 ;;; Guile-sdl2 is free software; you can redistribute it and/or modify
7 ;;; it under the terms of the GNU Lesser General Public License as
8 ;;; published by the Free Software Foundation; either version 3 of the
9 ;;; License, or (at your option) any later version.
10 ;;;
11 ;;; Guile-sdl2 is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;; General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU Lesser General Public
17 ;;; License along with guile-sdl2. If not, see
18 ;;; <http://www.gnu.org/licenses/>.
19
20 ;;; Commentary:
21 ;;
22 ;; SDL 2D accelerated rendering.
23 ;;
24 ;;; Code:
25
26 (define-module (sdl2 render)
27 #:use-module (ice-9 format)
28 #:use-module (ice-9 match)
29 #:use-module (system foreign)
30 #:use-module (sdl2)
31 #:use-module ((sdl2 bindings) #:prefix ffi:)
32 #:export (make-renderer
33 renderer?
34 delete-renderer!
35 call-with-renderer
36 clear-renderer
37 present-renderer
38 render-copy
39 set-render-draw-color
40
41 surface->texture))
42
43 \f
44 ;;;
45 ;;; Renderer
46 ;;;
47
48 (define-wrapped-pointer-type <renderer>
49 renderer?
50 wrap-renderer unwrap-renderer
51 (lambda (context port)
52 (format port "#<renderer ~x>"
53 (pointer-address (unwrap-renderer context)))))
54
55 (define (renderer-flags->bitmask flags)
56 (apply logior
57 (map (match-lambda
58 ('software ffi:SDL_RENDERER_SOFTWARE)
59 ('accelerated ffi:SDL_RENDERER_ACCELERATED)
60 ('vsync ffi:SDL_RENDERER_PRESENTVSYNC)
61 ('texture ffi:SDL_RENDERER_TARGETTEXTURE))
62 flags)))
63
64 (define* (make-renderer window #:optional (flags '(accelerated vsync)))
65 "Return a new renderer for WINDOW created with the options specified
66 in FLAGS, a list of symbols. The valid symbols that may appear in
67 FLAGS are:
68
69 * software, to use a software renderer fallback
70 * accelerated, to use hardware acceleration
71 * vsync, to synchronize rendering with the monitor's refresh rate
72 * texture, for render to texture support"
73 (let ((ptr (ffi:sdl-create-renderer ((@@ (sdl2 video) unwrap-window)
74 window)
75 -1 ; pick driver automatically
76 (renderer-flags->bitmask flags))))
77 (if (null-pointer? ptr)
78 (sdl-error "make-renderer" "failed to create renderer")
79 (wrap-renderer ptr))))
80
81 (define (delete-renderer! renderer)
82 "Delete the rendering context RENDERER."
83 (ffi:sdl-destroy-renderer (unwrap-renderer renderer)))
84
85 (define (call-with-renderer renderer proc)
86 "Call PROC, passing it RENDERER and closing RENDERER upon exit of
87 PROC."
88 (dynamic-wind
89 (const #t)
90 (lambda ()
91 (proc renderer))
92 (lambda ()
93 (delete-renderer! renderer))))
94
95 (define (clear-renderer renderer)
96 "Clear the rendering target RENDERER with the current drawing
97 color."
98 (unless (zero? (ffi:sdl-render-clear (unwrap-renderer renderer)))
99 (sdl-error "clear-renderer!" "failed to clear renderer")))
100
101 (define (present-renderer renderer)
102 "Display RENDERER."
103 (ffi:sdl-render-present (unwrap-renderer renderer)))
104
105 (define (set-render-draw-color renderer r g b a)
106 "Set draw color of RENDERER."
107 (ffi:sdl-set-render-draw-color (unwrap-renderer renderer) r g b a))
108
109 \f
110 ;;;
111 ;;; Texture
112 ;;;
113
114 (define-wrapped-pointer-type <texture>
115 texture?
116 wrap-texture unwrap-texture
117 (lambda (context port)
118 (format port "#<texture ~x>"
119 (pointer-address (unwrap-texture context)))))
120
121 (define (surface->texture renderer surface)
122 "Convert SURFACE to a texture suitable for RENDERER."
123 (let ((ptr (ffi:sdl-create-texture-from-surface
124 (unwrap-renderer renderer)
125 ((@@ (sdl2 surface) unwrap-surface) surface))))
126 (if (null-pointer? ptr)
127 (sdl-error "surface->texture" "failed to convert surface to texture")
128 (wrap-texture ptr))))
129
130
131 (define* (render-copy renderer texture
132 #:key (angle 0) srcrect dstrect center)
133 "Copy TEXTURE to the rendering target of RENDERER."
134 (let ((result (ffi:sdl-render-copy-ex
135 (unwrap-renderer renderer)
136 (unwrap-texture texture)
137 (if srcrect
138 (make-c-struct ffi:sdl-rect srcrect)
139 %null-pointer)
140 (if dstrect
141 (make-c-struct ffi:sdl-rect dstrect)
142 %null-pointer)
143 angle
144 (if center
145 (make-c-struct ffi:sdl-point center)
146 %null-pointer)
147 0)))
148 (unless (zero? result)
149 (sdl-error "render-copy" "failed to copy texture"))))