trip logs / gnuvola


Trip Log 2022-01-30 h11 -- Hooray! Part 8

Wow, part 7 was such a hot ride, why not cool down and continue the Hooray! series with part 8, wherein we fix some infelicities and call it a day.  Before we get into the patches, a brief word on what is meant by “infelicities”.  These are bits of working code that nonetheless do not sit quite right, whether due to inefficiency or aesthetic or a regretted decision in the past.  So, the vibe going in is “that's okaaaay, but... I'd rather not — let's find a better way”. 

patch 22

     1  commit 386af96f9417b815b38aba104df234cfd3f1b761
     2  Author: Thien-Thi Nguyen <ttn@gnuvola.org>
     3  Date:   2022-01-30 01:07:02 -0500
     4
     5      Fix infelicity: Use ‘array-for-each’ instead of ‘array-map!’
     6
     7      * hooray.scm (refresh-scenery! place-tile!):
     8      No longer return ‘rect’.
     9      (refresh-scenery! layer!): Use ‘array-for-each’.
    10  ---
    11   hooray.scm | 5 ++---
    12   1 file changed, 2 insertions(+), 3 deletions(-)
    13
    14  diff --git a/hooray.scm b/hooray.scm
    15  index 9b57804..34289d1 100644
    16  --- a/hooray.scm
    17  +++ b/hooray.scm
    18  @@ -109,11 +109,10 @@ (define refresh-scenery!
    19     (let ((rect (array-like-FAR)))
    20
    21       (define (place-tile! name rect)
    22  -      (and name (blit-to-screen! (surf name) rect))
    23  -      rect)
    24  +      (and name (blit-to-screen! (surf name) rect)))
    25
    26       (define (layer! ground)
    27  -      (array-map! rect place-tile! ground rect))
    28  +      (array-for-each place-tile! ground rect))
    29
    30       ;; initialize ‘rect’
    31       (array-index-map! rect make-tile-rect)

Our unhappiness here lies w/ the unnecessary assignment that ‘array-map!’ entails.  Furthermore, ‘refresh-scenery!’ is called at the beginning of every frame, so this wastefulness is compounded.  The variable ‘rect’ is, after all, supposed to be “set and forget” (lines 30-31).  The change (lines 27-28) also has a nice side effect: We can simplify ‘place-tile!’ a bit (lines 22-24), by no longer demanding functional nature from it. 

patch 23

     1  commit 2e551d87e6994177336537b36005284e3be8585b
     2  Author: Thien-Thi Nguyen <ttn@gnuvola.org>
     3  Date:   2022-01-30 01:11:18 -0500
     4
     5      Fix infelicity: Operate silently
     6
     7      * hooray.scm (smokin random-tile): No longer display name.
     8      (smokin): No longer display newline at end.
     9  ---
    10   hooray.scm | 8 ++------
    11   1 file changed, 2 insertions(+), 6 deletions(-)
    12
    13  diff --git a/hooray.scm b/hooray.scm
    14  index 34289d1..ab0f750 100644
    15  --- a/hooray.scm
    16  +++ b/hooray.scm
    17  @@ -147,10 +147,7 @@ (define smokin
    18         (* TILE-EDGE-LENGTH (random 5)))
    19
    20       (define (random-tile)
    21  -      (let ((name (vrandom keys)))
    22  -        (display " ")
    23  -        (display name)
    24  -        (surf name)))
    25  +      (surf (vrandom keys)))
    26
    27       ;; smokin
    28       (lambda (millisecs)
    29  @@ -161,8 +158,7 @@ (define smokin
    30           (update-rect-and-blit! (random-tile) rect
    31                                  (random-coord)
    32                                  (random-coord))
    33  -        (show! millisecs))
    34  -      (newline))))
    35  +        (show! millisecs)))))
    36
    37   (define FG-EDGE-LENGTH 32)

This fix lies on the aesthetic plane.  Basically, when we started, the display of each tile name helped soothe our insecurities about Things Actually Working.  Now that we have progressed a bit, that output is just noise and we can obtain perfect silence by removing it. 

patch 24

     1  commit 0fad7e91f5e4c0cdae85747f39eda93c60cbe60f
     2  Author: Thien-Thi Nguyen <ttn@gnuvola.org>
     3  Date:   2022-01-30 01:13:25 -0500
     4
     5      Fix infelicity: Never read ‘unused’
     6
     7      * hooray.scm (load-theme): Drop ‘unused’ from row 0 names.
     8  ---
     9   hooray.scm | 4 +---
    10   1 file changed, 1 insertion(+), 3 deletions(-)
    11
    12  diff --git a/hooray.scm b/hooray.scm
    13  index ab0f750..2c9a88f 100644
    14  --- a/hooray.scm
    15  +++ b/hooray.scm
    16  @@ -72,12 +72,10 @@ (define (load-theme filename)           ; => hash table
    17           (SDL:rect:set-x! rect x)
    18           (hashq-set! ht (car names) (copy-surface theme rect))))
    19
    20  -    (read-row! 0 '(window cell sender rec-0 rec-1 lock win unused))
    21  +    (read-row! 0 '(window cell sender rec-0 rec-1 lock win))
    22       (read-row! 1 '(up-0 up-1 opp-0 opp-1 corn-0 corn-1 tee-0 tee-1))
    23       (read-row! 2 BUTTONS)
    24
    25  -    (hashq-remove! ht 'unused)
    26  -
    27       ht))
    28
    29   (define HT (load-theme (cadr (command-line))))

Remember the challenge put to readers in part 3 about removing the ‘hashq-remove!’ (lines 25-26, here)?  The solution is the change on lines 20-21: Simply remove ‘unused’ from the list of names for row 0.  This works because ‘unused’ is the last one in the list and ‘read-row!’ looping terminates when there are no more names to read.  Ah, code reduction, I love it! 

patch 25

     1  commit 69ac176129f4bbfabcd1a818ce0bb1a9d94094ef
     2  Author: Thien-Thi Nguyen <ttn@gnuvola.org>
     3  Date:   2022-01-30 05:52:33 -0500
     4
     5      Fix infelicity: Use exactly one drect for ‘hooray!’
     6
     7      * hooray.scm (hooray! place-fg!): Take additional arg ‘dst’;
     8      return a procedure that takes only two args ‘x’ and ‘y’.
     9      (hooray!): Change ‘dst’ to be a simple SDL-Rectangle
    10      initialized exactly once via ‘make-fg-rect’;
    11      update call to ‘place-fg!’ to also pass ‘dst’ as 4th arg;
    12      remove ‘dst’ from ‘array-for-each’ arglist.
    13  ---
    14   hooray.scm | 12 +++++-------
    15   1 file changed, 5 insertions(+), 7 deletions(-)
    16
    17  diff --git a/hooray.scm b/hooray.scm
    18  index 2c9a88f..75c0520 100644
    19  --- a/hooray.scm
    20  +++ b/hooray.scm
    21  @@ -238,9 +238,9 @@ (define hooray!
    22       (define (bump! orig delta)
    23         (array-map! orig + orig delta))
    24
    25  -    (define (place-fg! fg w/2 h/2)
    26  +    (define (place-fg! fg w/2 h/2 dst)
    27         ;; rv
    28  -      (lambda (dst x y)
    29  +      (lambda (x y)
    30           (update-rect-and-blit!
    31            fg dst
    32            (- x w/2)
    33  @@ -254,20 +254,18 @@ (define hooray!
    34             (vx (tvec))                   ; velocity
    35             (vy (tvec))
    36             (gravity (tvec))
    37  -          (dst (tvec)))
    38  +          (dst (make-fg-rect)))
    39
    40         (array-index-map! fg (lambda (i)
    41                                (fg-step (make-put (surf 'win))
    42                                         (/ i step-count))))
    43         (array-map! w/2 (half SDL:surface:w) fg)
    44         (array-map! h/2 (half SDL:surface:h) fg)
    45  -      (array-map! dst make-fg-rect)
    46
    47         ;; hooray!
    48         (lambda ()
    49           (array-map! x (random-c +))
    50           (array-map! y (random-c -))
    51  -        (array-for-each rect-xy! dst x y)
    52           (array-map! vx (random-v 0))
    53           (array-map! vy (random-v (random -4.20)))
    54           (array-map! gravity random-gravity)
    55  @@ -277,8 +275,8 @@ (define hooray!
    56              (bump! x vx)
    57              (bump! y vy)
    58              (bump! vy gravity)
    59  -           (array-for-each (place-fg! fg w/2 h/2)
    60  -                           dst x y)
    61  +           (array-for-each (place-fg! fg w/2 h/2 dst)
    62  +                           x y)
    63              (show! 42))
    64            fg w/2 h/2)))))

This is more code reduction, this time driven by a bad design decision in the past.  There is no need to maintain a ‘THRONG’ of destination rectangles.  One is sufficient.  I think what happened was I was enamored w/ ‘array-for-each’ and was looking to pass it lots of args.  (Like they say, “When all you have is a hammer...”) 

Alright, that's enough for now.  See you next weekend! 


Copyright (C) 2022 Thien-Thi Nguyen