render: Add SDL_RenderDrawPoints binding.
[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 render-draw-points
44
45 delete-texture!
46 surface->texture))
47
48 \f
49 ;;;
50 ;;; Renderer
51 ;;;
52
53 (define-wrapped-pointer-type <renderer>
54 renderer?
55 wrap-renderer unwrap-renderer
56 (lambda (context port)
57 (format port "#<renderer ~x>"
58 (pointer-address (unwrap-renderer context)))))
59
60 (define (renderer-flags->bitmask flags)
61 (apply logior
62 (map (match-lambda
63 ('software ffi:SDL_RENDERER_SOFTWARE)
64 ('accelerated ffi:SDL_RENDERER_ACCELERATED)
65 ('vsync ffi:SDL_RENDERER_PRESENTVSYNC)
66 ('texture ffi:SDL_RENDERER_TARGETTEXTURE))
67 flags)))
68
69 (define* (make-renderer window #:optional (flags '(accelerated vsync)))
70 "Return a new renderer for WINDOW created with the options specified
71 in FLAGS, a list of symbols. The valid symbols that may appear in
72 FLAGS are:
73
74 * software, to use a software renderer fallback
75 * accelerated, to use hardware acceleration
76 * vsync, to synchronize rendering with the monitor's refresh rate
77 * texture, for render to texture support"
78 (let ((ptr (ffi:sdl-create-renderer ((@@ (sdl2 video) unwrap-window)
79 window)
80 -1 ; pick driver automatically
81 (renderer-flags->bitmask flags))))
82 (if (null-pointer? ptr)
83 (sdl-error "make-renderer" "failed to create renderer")
84 (wrap-renderer ptr))))
85
86 (define (delete-renderer! renderer)
87 "Delete the rendering context RENDERER."
88 (ffi:sdl-destroy-renderer (unwrap-renderer renderer)))
89
90 (define (call-with-renderer renderer proc)
91 "Call PROC, passing it RENDERER and closing RENDERER upon exit of
92 PROC."
93 (dynamic-wind
94 (const #t)
95 (lambda ()
96 (proc renderer))
97 (lambda ()
98 (delete-renderer! renderer))))
99
100 (define (clear-renderer renderer)
101 "Clear the rendering target RENDERER with the current drawing
102 color."
103 (unless (zero? (ffi:sdl-render-clear (unwrap-renderer renderer)))
104 (sdl-error "clear-renderer!" "failed to clear renderer")))
105
106 (define (present-renderer renderer)
107 "Display RENDERER."
108 (ffi:sdl-render-present (unwrap-renderer renderer)))
109
110 (define (set-render-draw-color renderer r g b a)
111 "Set draw color of RENDERER."
112 (ffi:sdl-set-render-draw-color (unwrap-renderer renderer) r g b a))
113
114 (define (render-draw-line renderer x1 y1 x2 y2)
115 "Draw line on RENDERER."
116 (ffi:sdl-render-draw-line (unwrap-renderer renderer) x1 y1 x2 y2))
117
118 (define (render-draw-point renderer x y)
119 "Draw point on RENDERER."
120 (ffi:sdl-render-draw-point (unwrap-renderer renderer) x y))
121
122 (define (render-draw-points renderer points)
123 "Draw POINTS on RENDERER."
124 (define (fill-bv bv l n)
125 (match l
126 (() bv)
127 (((x y) . r)
128 (s32vector-set! bv n x)
129 (s32vector-set! bv (+ n 1) y)
130 (fill-bv bv r (+ 2 n)))))
131 (let* ((count (length points))
132 (bv (fill-bv (make-s32vector (* count 2)) points 0)))
133 (ffi:sdl-render-draw-points (unwrap-renderer renderer)
134 (bytevector->pointer bv)
135 count)))
136
137 \f
138 ;;;
139 ;;; Texture
140 ;;;
141
142 (define-wrapped-pointer-type <texture>
143 texture?
144 wrap-texture unwrap-texture
145 (lambda (context port)
146 (format port "#<texture ~x>"
147 (pointer-address (unwrap-texture context)))))
148
149 (define (surface->texture renderer surface)
150 "Convert SURFACE to a texture suitable for RENDERER."
151 (let ((ptr (ffi:sdl-create-texture-from-surface
152 (unwrap-renderer renderer)
153 ((@@ (sdl2 surface) unwrap-surface) surface))))
154 (if (null-pointer? ptr)
155 (sdl-error "surface->texture" "failed to convert surface to texture")
156 (wrap-texture ptr))))
157
158 (define (delete-texture! texture)
159 "Free the memory used by TEXTURE."
160 (ffi:sdl-destroy-texture (unwrap-texture texture)))
161
162 (define* (render-copy renderer texture
163 #:key (angle 0) srcrect dstrect center)
164 "Copy TEXTURE to the rendering target of RENDERER."
165 (let ((result (ffi:sdl-render-copy-ex
166 (unwrap-renderer renderer)
167 (unwrap-texture texture)
168 (if srcrect
169 (make-c-struct ffi:sdl-rect srcrect)
170 %null-pointer)
171 (if dstrect
172 (make-c-struct ffi:sdl-rect dstrect)
173 %null-pointer)
174 angle
175 (if center
176 (make-c-struct ffi:sdl-point center)
177 %null-pointer)
178 0)))
179 (unless (zero? result)
180 (sdl-error "render-copy" "failed to copy texture"))))