Skip to content

Instantly share code, notes, and snippets.

@yi-jiayu
Last active May 9, 2019 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yi-jiayu/7b3a2506f7bf66d721992456295a1604 to your computer and use it in GitHub Desktop.
Save yi-jiayu/7b3a2506f7bf66d721992456295a1604 to your computer and use it in GitHub Desktop.
Modifying the Chicken Scheme SDL2 basics demo (https://gitlab.com/chicken-sdl2/chicken-sdl2/blob/25d66ebd4383a9847bd26c9f69dbfc0e54e2a8b4/demos/basics.scm) to use 2D Accelerated Rendering
diff --git a/demos/basics.scm b/demos/basics.scm
index 65280b6..a9b16f9 100644
--- a/demos/basics.scm
+++ b/demos/basics.scm
@@ -150,20 +150,31 @@
(define smiley1 (make-obj (make-smile-surf (make-random-color)) 300 300))
(define smiley2 (make-obj (make-smile-surf (make-random-color)) 500 300))
-
+(define renderer (sdl2:create-renderer! window -1 (list 'accelerated)))
;;; Draw (or redraw) the entire scene. It would be more efficient to
;;; only redraw the parts of the scene that have changed, but since
;;; this is just a demo program we don't want to get too complex.
(define (draw-scene!)
- (let ((window-surf (sdl2:window-surface window)))
- ;; Clear the whole screen using a blue background color
- (sdl2:fill-rect! window-surf #f (sdl2:make-color 0 80 160))
+ ;; Create a new surface the same size as the window
+ (let ((surf (sdl2:make-surface* 800 600 32)))
+ ;; Clear the new surface screen using a blue background color
+ (sdl2:fill-rect! surf #f (sdl2:make-color 0 80 160))
;; Draw the smileys
- (draw-obj! smiley2 window-surf)
- (draw-obj! smiley1 window-surf)
- ;; Refresh the screen
- (sdl2:update-window-surface! window)))
+ (draw-obj! smiley2 surf)
+ (draw-obj! smiley1 surf)
+ ;; Convert the surface to a texture
+ (let ((texture (sdl2:create-texture-from-surface* renderer surf)))
+ ;; TODO: check if the texture creation was successful
+ ;; Free the surface since we're done converting it to a texture
+ (sdl2:free-surface! surf)
+ ;; Clear the screen
+ (sdl2:render-clear! renderer)
+ ;; Copy our texture into the renderer
+ (sdl2:render-copy! renderer texture)
+ (sdl2:render-present! renderer)
+ ;; Destroy the texture after we're done
+ (sdl2:destroy-texture! texture))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment