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