Next: , Up: Introduction


1.1 Getting started

This section assumes that SLAYER is already installed and properly configured on your system, and that you are familiar with programming in general, and Guile Scheme in particular.

To get the grasp on how SLAYER works, we'll create a simple application for browsing images1.

In order to do so, we need to create a text file; let's call it “image-browser.scm”2. Let's fill the file with the following content:

     (use-modules (slayer) (slayer image)
     	     (srfi srfi-1) (srfi srfi-2))
     (keydn 'esc quit)
     
     (define (list-directory path)
       (let ((dir (opendir path)))
         (unfold eof-object? (lambda(f)(string-append path f))
     	    (lambda x (readdir dir)) (readdir dir))))
     
     (define (file? f) (eq? 'regular (stat:type (stat f))))
     
     (define *directory* (if (defined? '$1) $1 "/usr/share/pixmaps/"))
     (define *image-names* (filter file? (list-directory *directory*)))
     (define *current-image* #f)
     (define *image-index* 0)
     
     (if (<= (length *image-names*) 0)
         (begin (display "no images found\n") (quit)))
     
     (set-display-procedure! (lambda () (draw-image! *current-image* 0 0)))
     
     (define (show-image! i)
       (set! *image-index* (modulo i (length *image-names*)))
       (and-let* ((image-name (list-ref *image-names* *image-index*))
     	     (image (load-image image-name)))
         (set! *current-image* image)
         (apply set-screen-size! (image-size *current-image*))
         (set-window-title! image-name)))
     
     (show-image! 0)
     
     (keydn 'left (lambda () (show-image! (+ *image-index* 1))))
     (keydn 'right (lambda () (show-image! (- *image-index* 1))))

In order to execute the program, one shall type:

$ slayer image-browser.scm

The argument to set-display-procedure! is executed after a series of events has been processed – so evey time an event occurs (like keyboard or mouse input, or window resize, or timer event), the whole screen is wiped and its content is displayed on the screen from scratch.

While this might seem a waste of resources, this is a desired behaviour for the class of applications that SLAYER aims to deliver, i.e. 3d games and real-time multimedia processing apps. Since the input can be gathered during the redisplay stage, the applications generally tend to be responsive.


Przypisy

[1] The idea for this program was inspired by Thien-Thi Nguyen, who placed a similar program in the manual for the Guile-SDL package

[2] SLAYER uses Scheme programming language, so it is convinient to name files with .scm extension, because various editors can detect that and load appropreate editing mode