trip logs / gnuvola


Trip Log 2022-02-09 h06 -- Hooray! Part 11

This is the next-to-last Trip Log in the Hooray! series, covering two patches that make use of the newly released Guile-SDL 0.6.0 (see last episode for a link to the announcement).  Patch 32 is minor thang, really, but patch 33 finally gets us the last “X” on the checklist. 

patch 32

     1  commit 4f0bc2fd5257e1fb18f5caec4f18d7cfb6343f80
     2  Author: Thien-Thi Nguyen <ttn@gnuvola.org>
     3  Date:   2022-02-06 00:38:20 -0500
     4
     5      Simplify ‘blit-rgba’ call
     6
     7      * hooray.scm (make-fg-rect): Delete proc.
     8      (hooray! make-put): Use ‘#f’ for 2nd, 4th args.
     9      (hooray!): Don't bother creating ‘src’ rectangle.
    10  ---
    11   hooray.scm | 15 +++------------
    12   1 file changed, 3 insertions(+), 12 deletions(-)
    13
    14  diff --git a/hooray.scm b/hooray.scm
    15  index 55f6317..43de58d 100644
    16  --- a/hooray.scm
    17  +++ b/hooray.scm
    18  @@ -162,25 +162,19 @@ (define smokin
    19
    20   (define FG-EDGE-LENGTH 32)
    21
    22  -(define (make-fg-rect)
    23  -  (SDL:make-rect 0 0
    24  -                 FG-EDGE-LENGTH
    25  -                 FG-EDGE-LENGTH))
    26  -
    27   (define hooray!
    28     (let ((throng (cond ((pair? (cddr (command-line)))
    29                          (string->number (caddr (command-line))))
    30                         (else
    31                          9)))
    32  -        (step-count 42)
    33  -        (src (make-fg-rect)))
    34  +        (step-count 42))
    35
    36       (define (svec)
    37         (make-vector step-count))
    38
    39       (define (make-put win)
    40         ;; Avoid ‘copy-surface’; it uses ‘blit-surface’ which disregards alpha.
    41  -      ;;- (copy-surface win src)
    42  +      ;;- (copy-surface win ...)
    43         (let ((surface (SDL:create-rgb-surface
    44                         '(src-alpha)
    45                         FG-EDGE-LENGTH    ; w
    46  @@ -190,10 +184,7 @@ (define hooray!
    47                         #x0000FF00
    48                         #x00FF0000
    49                         #xFF000000)))
    50  -        ;; We can use ‘src’ for dest rect only because its xy stays 0 0.
    51  -        ;; Some day ‘blit-rgba’ will accept ‘#f’ for 2nd, 4th args to mean
    52  -        ;; "full surface", and we will be able to change this accordingly.
    53  -        (blit-rgba win src surface src)
    54  +        (blit-rgba win #f surface #f)
    55           surface))
    56
    57       (define (fg-step put fraction)

To grok this patch, we'll work backward and start towards the end, where the action is (lines 50-54).  There, the 2nd and 4th args of ‘blit-rgba’ are now indeed ‘#f’ — yay, less stuff to keep track of.  In fact, no longer using ‘src’ in line 54 means we can cascade its deletion backwards.  Lines 41-42 show simple comment munging that, again, removes ‘src’ from the source (yuk yuk).  Because now we don't use ‘src’ at all, we can delete it completely (lines 32-34), and furthermore, because we don't use ‘make-fg-rect’ for anything, we can delete that, too (lines 22-26).  Ah, nice to see delta of -9 lines. 

patch 33

     1  commit 88884f5a3f416954f39c1089ba9f23625b5156a3
     2  Author: Thien-Thi Nguyen <ttn@gnuvola.org>
     3  Date:   2022-02-06 00:45:28 -0500
     4
     5      Fade fg to transparent
     6
     7      * hooray.scm: From ‘(sdl gfx)’, no longer import
     8      ‘imfi-sub-c’; instead, import ‘multiply-pixel-alpha!’.
     9      (hooray! fg-step dim!): Delete proc.
    10      (hooray! fg-step fade!): New proc.
    11      (hooray! fg-step): Use ‘fade!’.
    12  ---
    13   hooray.scm | 19 +++++++------------
    14   1 file changed, 7 insertions(+), 12 deletions(-)
    15
    16  diff --git a/hooray.scm b/hooray.scm
    17  index 43de58d..8dc6c8c 100644
    18  --- a/hooray.scm
    19  +++ b/hooray.scm
    20  @@ -34,7 +34,7 @@ (use-modules
    21                                exact-truncate))
    22    ((sdl gfx) #:select (blit-rgba
    23                         roto-zoom-surface
    24  -                      imfi-sub-c))
    25  +                      multiply-pixel-alpha!))
    26    ((sdl sdl) #:prefix SDL:))
    27
    28   (SDL:init 'video)
    29  @@ -189,16 +189,6 @@ (define hooray!
    30
    31       (define (fg-step put fraction)
    32
    33  -      (define (dim! surface)
    34  -        (let ((c (exact-truncate (* 256 fraction))))
    35  -          ;; Ideally we would operate only on the alpha channel
    36  -          ;; by using ‘(ash c 24)’ instead of ‘c’ here.
    37  -          ;; Unfortunately Guile-SDL 0.5.3 ‘imfi-sub-c’ has a bug
    38  -          ;; where values ‘(ash 128 24)’ and up signal range error.
    39  -          ;; When next Guile-SDL is released, we will update this.
    40  -          (imfi-sub-c surface surface c)
    41  -          surface))
    42  -
    43         (define (s-curve x)
    44           (define beta (exp 0.8))
    45           (/ 1.0
    46  @@ -215,7 +205,12 @@ (define hooray!
    47                              (* 4.20 (shift-up (s-curve fraction)))
    48                              #t))
    49
    50  -      (dim! (size/rotate! put)))
    51  +      (define (fade! surface)
    52  +        (let ((c (exact-truncate (* 256 (s-curve (- 1.0 fraction))))))
    53  +          (multiply-pixel-alpha! surface c)
    54  +          surface))
    55  +
    56  +      (fade! (size/rotate! put)))
    57
    58       (define (/2 n)
    59         (ash n -1))

Here, we substitute dimming with fading (lines 9-10).  Guile-SDL 0.6.0 introduces ‘multiply-pixel-alpha!’, which is exactly what we need to do a proper fade to transparent effect (line 53).  Two notes (both on line 52):  (a) The fading is not linear, but uses ‘s-curve’ procedure;  (b) Transparency should decrease, so we feed ‘s-curve’ the ‘(- 1.0 fraction)’ instead of plain ‘fraction’. 

Here's what “guile -s hooray.scm Network.png” looks like now:

The light-colored foreground fades to transparent, finally!  Here's the checklist now: 

* PipeWalker fg behavior (to reproduce if possible)
  - [X] starts near top of center tile
  - [X] mostly upward velocity
  - [X] rotates clockwise slightly
  - [X] starts smaller than 32x32
  - [X] grows quickly
  - [ ] non synchronized (all aspects)
  - [X] fades to transparent

The small “x” from before has become a big “X” now.  Alright, that concludes the patches.  See you next time for the series conclusion. 


Copyright (C) 2022 Thien-Thi Nguyen