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