[plt-scheme] rotate bmp onclick

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Nov 16 12:28:32 EST 2008

At Fri, 14 Nov 2008 10:04:37 +0000, Steve De Ridder wrote:
> 
> dear newslist-members,  i'm pretty new in the scheme / mred language. for the 
> moment, i'm writing a "magic the gathering" application in scheme. therefore, 
> i have to be able to tap / rotate my own defined card (a simple bitmap) when i 
> click it. i read about this on http://osdir.com/ml/lisp.scheme.plt/2003-
> 05/msg00117.html but didn't find the solution (or is this due to my poor 
> english -english is only my third language)can anyone please point this out 
> for me?

The `games/cards' library in the current release still doesn't support
rotation. I've added rotation support in the current development
version; it's now available via SVN, and it will be available in the
next nightly build. (It probably won't be included in a regular release
until early next year.)

If you can't or don't want to use the latest version, you could create
cards that use rotated images, and then "rotate" a card by swapping the
original card with one using a rotated image.

Here's a function that rotates a bitmap (either clockwise or
counter-clockwise):

  (define (rotate-bm bm cw?)
    (let ([w (send bm get-width)]
          [h (send bm get-height)])
      (let ([bm2 (make-object mred:bitmap% h w)]
            [s (make-bytes (* w h 4))]
            [s2 (make-bytes (* h w 4))])
        (send bm get-argb-pixels 0 0 w h s)
        (for ([i (in-range w)])
          (for ([j (in-range h)])
            (let ([src-pos (* (+ i (* j w)) 4)])
              (bytes-copy! s2 
                           (if cw?
                               (* (+ (- (- h j) 1) (* i h)) 4) 
                               (* (+ j (* (- (- w i) 1) h)) 4))
                           s src-pos (+ src-pos 4)))))
        (let ([dc (make-object mred:bitmap-dc% bm2)])
          (send dc set-argb-pixels 0 0 h w s2)
          (send dc set-bitmap #f))
        bm2)))

The documentation for the cards library says that all cards need to
have size 71x96, so creating rotated cards wouldn't work according to
the docs. Actually, though, only some table methods assume a 71x96
card, such as the method that arranges cards with a region. I've
generalized the card-arranging methods to merely assume that all cards
have the same shape.


Matthew



Posted on the users mailing list.