Next: , Previous: , Up: The (sdl *) Modules   [Contents][Index]


11 Miscellaneous Utilities

These are available in module (sdl misc-utils).

Procedure: exact-truncate number

Return the exact truncation (rounding to zero) of number. This is “safer” than simply inexact->exact for some Guile versions.

(define scale 0.180281690140845)
(inexact->exact scale)
  ⇒ 3247666210160131/18014398509481984 ; Guile 1.8.7
  ⇒ 0                                  ; Guile 1.4.x
(exact-truncate scale)
  ⇒ 0
Procedure: exact-floor number

Return the exact floor (rounding to negative infinity) of number.

Procedure: call-with-clip-rect rect thunk

Set default clip rect to rect, call thunk, and restore it. thunk is a procedure that takes no arguments.

Procedure: create-rgba-surface w h

Return a new 32bpp RGBA surface with dimensions w by h pixels. The surface has flag src-alpha set. The masks are as follows:

red     #x000000FF
green   #x0000FF00
blue    #x00FF0000
alpha   #xFF000000
Procedure: create-rgba-square edge-len

Return a new 32bpp RGBA square surface with edge-len sides. (Both width and height are edge-len, an integer.)

Procedure: rotate-square square angle [mksquare]

Return a new surface made by rotating square by angle degrees. The square retains its original size. If the new surface has flag src-alpha set, use (sdl gfx) blit-rgba, otherwise (sdl sdl) blit-surface, for the resizing blit.

Optional arg mksquare is a procedure of one arg that creates a square surface. If unspecified, use create-rgba-square.

See roto-zoom-surface for interpretation of angle.

Procedure: rectangle-closure [rect]

Return a closure that manages a single rectangle object. Calling the closure with no args returns the rectangle object. Otherwise, the messages #:w, #:h, #:x and #:y return the rectangle’s width, height, horizontal offset and vertical offset, respectively; and the messages #:w!, #:h!, #:x! and #:y!, followed by an integer, update the rectangle’s width, height, horizontal offset and vertical offset, respectively.

Optional arg rect specifies a rectangle object to manage instead of allocating a new one.

Procedure: rectangle<-geometry-string spec

Return a rectangle made from parsing the geometry string spec, which typically has the form WxH+X+Y, where +X+Y is optional (defaults to “+0+0”), and W, H, X and Y are integers. Actually, the + can also be a -. If spec cannot be parsed, return #f. Examples:

(rectangle<-geometry-string "42x43+44+45")
⇒ #<SDL-Rect 42x43+44+45>

(rectangle<-geometry-string "42x43-10-20")
⇒ #<SDL-Rect 42x43+-10+-20>

(rectangle<-geometry-string "42x43")
⇒ #<SDL-Rect 42x43+0+0>

(rectangle<-geometry-string "42")
⇒ #f

Note that the print representation of a rectangle always has “+”. The term “geometry string” derives from the X Window System, where many programs take a --geometry (or -g for short) command-line option.

Procedure: poll-with-push-on-timeout-proc timeout slice [get-timeout-events]

Return a procedure P that checks the event queue for timeout ms, polling every slice ms. If an event arrives during that time, return #t. Otherwise return #f. Optional arg get-timeout-events is either a list of events to be pushed on the queue in the case of timeout, or a thunk to be called that produces such a list. If get-timeout-events is specified, return the result of another event queue polling. (This may still be #f if the pushed events are masked in some way.)

P is called with a single arg, a pre-constructed event object. This interface is congruent with that of wait-event and poll-event. See Events.

Procedure: rect<-surface surface [x y]

Return a new rectangle with the same width and height as surface. Optional second and third arg (which must appear together or not at all) specifies the x and y components, respectively, to use instead of the default of 0 (zero).

Procedure: copy-rectangle rect [modify args...]

Return a new rectangle copied from rect.

Optional second arg modify specifies which portions, if any, to modify using the values in the rest args. If modify is #:xy, the two args specify new x and y values. If modify is #:wh, the two args specify new w and h values.

rect
⇒ #<SDL-Rect 3x4+1+2>

(copy-rectangle rect)
⇒ #<SDL-Rect 3x4+1+2>

(copy-rectangle rect #:xy 11 22)
⇒ #<SDL-Rect 3x4+11+22>

(copy-rectangle rect #:wh 33 44)
⇒ #<SDL-Rect 33x44+1+2>
Procedure: copy-surface surface [clip]

Create a new surface and blit surface onto it. The new surface has the same pixel format as surface. Return the new surface.

Optional second arg clip is a rectangle describing the portion of surface to copy (default is the entire surface).

Procedure: ignore-all-event-types-except [types…]

Arrange to ignore all event types except those in types (see event-type enums). As a special case, if types is #f, arrange to not ignore any event types (all are enabled).

In the following procs, those named ending with /3p return three values, each a thunk (unless specified otherwise) handling the three-phase calling convention, namely init, next, and done.

(call-with-values (lambda () (foo/3p ...))
  (lambda (init! foo! done!)
    (init!)
    (let loop ((continue? (foo!)))
      (and continue? (loop (foo!))))
    (done!)))

Note that foo! returns non-#f to indicate that the looping is not yet complete.

Procedure: fader/3p sec realized location image replacement

Return three values, each a thunk, that can be used to loop for sec seconds, blitting onto realized at location (a rectangle or #f to indicate the origin) the alpha-composition of image and its replacement (both surfaces), to effect a fade-in of replacement over image. The alpha value is directly proportional to the time between the “next!” phase call and the “init!” phase call.

realized may be either a surface, in which case at the end of each loop it is shown via update-rect; or a pair whose CAR is a surface and whose CDR is a thunk that should do the showing.

Note that location is used for blitting, so its width and height should match those of image and replacement.

Procedure: toroidal-panner/3p surface dx dy [sub [batch?]]

Return three values, the first a procedure of one arg, the other two thunks, that can be used to toroidally pan surface by dx and dy pixels. This means that data disappearing from one side of the surface (left, right, top, bottom) is rotated to appear at the other side (right, left, bottom, top). The init! procedure takes one arg count, the number of pans to do.

Positive dx moves surface data to the left (panning right), and likewise, positive dy, up (panning down).

Optional third arg sub is a rectangle object specifying a subset of the surface. The default is to pan the entire surface.

Optional fourth arg batch? non-#f means to call update-rect on the (sub)surface after all the panning is done. The default is to update the surface after each pan. Batch mode is useful for implementing variable-speed panning, for example:

(define (pan dir)
  (call-with-values (lambda ()
                      (toroidal-panner/3p screen
                                          (* dir 21)
                                          (* dir 12)
                                          #f #t))
    (lambda (init! next! done!)
      (lambda (count)
        (init! count)
        (let loop ((continue? (next!)))
          (and continue? (loop (next!))))
        (done!)))))

(define pan-away (pan  1))
(define pan-back (pan -1))
(define ramp (map 1+ (append (make-list 21 0)
                             (identity (iota 12))
                             (reverse! (iota 12))
                             (make-list 21 0))))
(for-each pan-away ramp)
(for-each pan-back ramp)

Next: Simple Closures, Previous: SDL_gfx by Andreas Schiffler, Up: The (sdl *) Modules   [Contents][Index]