by Daniel Pimentel — Tue 06 June 2017
The (sdl *) modules are an interface to the SDL (Simple Direct Media Layer) library. The goal is to provide both a clean and direct interface to the lowest level SDL, while extending with higher level concepts where useful, such as default arguments and functional-style application of graphics routines. Several SDL add-on libraries have been wrapped and included with Guile-SDL, including SDL_image (for loading multiple image formats), SDL_ttf (for rendering true type fonts), SDL_mixer (for playing/mixing different audio formats), and SDL_rotozoom (for rotating and scaling images). In addition, some low-level 2D graphics primitives have been provided.
;; load the SDL module and some useful srfi's (use-modules ((sdl sdl) #:prefix SDL:) (srfi srfi-1) (srfi srfi-2)) ;; initialize the video subsystem (SDL:init 'video) ;; directory to search for images in (define image-dir '/usr/share/pixmaps/') ;; utility to test if a path is a directory (define (file? f) (let* ((stats (stat f)) (type (stat:type stats))) (eq? type 'regular))) ;; build a ring of image file names (define image-ring (let ((dir (opendir image-dir))) (letrec ((D (lambda (ls) (let ((file (readdir dir))) (if (eof-object? file) (begin (closedir dir) ls) (D (cons (string-append image-dir file) ls))))))) (apply circular-list (reverse (filter file? (D '()))))))) ;; functions to cycle through the ring (define (next-image) (let ((next (car image-ring))) (set! image-ring (cdr image-ring)) next)) (define (prev-image) (let ((orig image-ring)) (while (not (eq? (cddr image-ring) orig)) (set! image-ring (cdr image-ring))) (let ((image (car image-ring))) (set! image-ring (cdr image-ring)) image))) ;; display an image given a filename (define (show file) (and-let* ((image (SDL:load-image file))) (SDL:set-video-mode (SDL:surface:w image) (SDL:surface:h image) 24) (SDL:blit-surface image) (SDL:flip))) ;; show the first image (show (next-image)) ;; event handler (let handle ((e (SDL:make-event))) (if (SDL:wait-event e) (case (SDL:event:type e) ((key-down) (case (SDL:event:key:keysym:sym e) ((left backspace) (show (prev-image))) ((right space) (show (next-image))) ((escape q) (SDL:quit) (quit)))))) (handle e))