summaryrefslogtreecommitdiff
path: root/sdl2/render.scm
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@hornquist.se>2019-06-06 04:43:38 +0200
committerDavid Thompson <dthompson2@worcester.edu>2019-06-06 11:00:12 -0400
commit779de2fcdca7943a4abf4426cb282a9ce9bc4621 (patch)
tree191034daa18fe16840e0829c7997862b5fc09e92 /sdl2/render.scm
parent895180d78f951d92b7888f134da426ad606d043d (diff)
render: Add SDL_SetRenderTarget bindings.
* sdl2/bindings.scm (SDL_TEXTUREACCESS_STATIC, SDL_TEXTUREACCESS_STREAMING, SDL_TEXTUREACCESS_TARGET): New variables. (sdl-create-texture, sdl-set-render-target, sdl-get-render-target): New procedures. * sdl2/render.scm (symbol->sdl-access-format, make-texture, set-render-target!, get-render-target): New procedures. * doc/api.texi: Document new procedures.
Diffstat (limited to 'sdl2/render.scm')
-rw-r--r--sdl2/render.scm41
1 files changed, 41 insertions, 0 deletions
diff --git a/sdl2/render.scm b/sdl2/render.scm
index 6504ce9..57a0e17 100644
--- a/sdl2/render.scm
+++ b/sdl2/render.scm
@@ -38,6 +38,8 @@
clear-renderer
present-renderer
render-copy
+ set-render-target!
+ get-render-target
set-render-draw-color
render-draw-line
render-draw-lines
@@ -48,6 +50,7 @@
render-fill-rect
render-fill-rects
+ make-texture
delete-texture!
surface->texture))
@@ -203,6 +206,25 @@ color."
(format port "#<texture ~x>"
(pointer-address (unwrap-texture context)))))
+(define (make-texture renderer format access width height)
+ "Returns a new texture for RENDERER with pixel FORMAT.
+ACCESS is one of the symbols:
+
+* static: changes rarely, not lockable
+* streaming: changes frequently, lockable
+* target: can be used as a render target (requires that renderer was
+created with 'texture')"
+ (let ((ptr (ffi:sdl-create-texture (unwrap-renderer renderer)
+ ((@@ (sdl2 surface) symbol->sdl-pixel-format) format)
+ (match access
+ ('static ffi:SDL_TEXTUREACCESS_STATIC)
+ ('streaming ffi:SDL_TEXTUREACCESS_STREAMING)
+ ('target ffi:SDL_TEXTUREACCESS_TARGET))
+ width height)))
+ (if (null-pointer? ptr)
+ (sdl-error "make-texture" "Failed to create texture")
+ (wrap-texture ptr))))
+
(define (surface->texture renderer surface)
"Convert SURFACE to a texture suitable for RENDERER."
(let ((ptr (ffi:sdl-create-texture-from-surface
@@ -235,3 +257,22 @@ color."
0)))
(unless (zero? result)
(sdl-error "render-copy" "failed to copy texture"))))
+
+(define (set-render-target! renderer texture)
+ "Sets the render target for RENDERER to TEXTURE, making all comming draw
+requests redirect to TEXTURE.
+
+Pass #f to reset it to the default target."
+ (let ((result (ffi:sdl-set-render-target
+ (unwrap-renderer renderer)
+ (if texture
+ (unwrap-texture texture)
+ %null-pointer))))
+ (unless (zero? result)
+ (sdl-error "set-render-target!" "failed to set render target"))))
+
+(define (get-render-target renderer)
+ "Returns the current render target of RENDERER. #f if it's a texture."
+ (let ((ptr (ffi:sdl-get-render-target (unwrap-renderer renderer))))
+ (if (null-pointer? ptr)
+ #f (wrap-texture ptr))))