From: Mei Li Triantafyllidi
Subject: Allegro Common Graphics: Transparency problem in static pictures
Date: 
Message-ID: <e61dn0$sg2$1@volcano1.grnet.gr>
Hi,

We are trying to make a billards game in Allegro Lisp using Common Graphics. 
We use static pictures for the balls. The problem is that static pictures 
are square and balls are round so we want the rest of the picture to be 
transparent.

We tried to make parts of the picture transparent with an image editing 
program and with the generate-mask function.

The problem is that these parts of the picture are not really transparent 
but they just take the color of the form background. We want them to be 
transparent even when they are over other static pictures.

Any ideas?

Thanks,
Mei Li and Foivos

From: Ken Tilton
Subject: Re: Allegro Common Graphics: Transparency problem in static pictures
Date: 
Message-ID: <tiXgg.27$TL1.24@fe09.lga>
Mei Li Triantafyllidi wrote:
> Hi,
> 
> We are trying to make a billards game in Allegro Lisp using Common Graphics. 
> We use static pictures for the balls. The problem is that static pictures 
> are square and balls are round so we want the rest of the picture to be 
> transparent.
> 
> We tried to make parts of the picture transparent with an image editing 
> program and with the generate-mask function.
> 
> The problem is that these parts of the picture are not really transparent 
> but they just take the color of the form background.

Hunh? Can't you make these truly transparent using the image editing 
program? PhotoShop, for example, has a special eraser or eraser mode for 
that. If that does not work...

> We want them to be 
> transparent even when they are over other static pictures.
> 
> Any ideas?

Sounds like you are on win32. Well, doesn't matter. Common Graphics does 
not limit you to CG functionality. You will either find available 
bindings to native OS graphics calls (such as BitBlt on win32) or be 
able to use CFFI (or ACL's native FFI) to call OS graphics routines. 
So... this is not an ACL question, it is an OS graphics question. Look 
at that doc for ways (such as BitBlt coupled with a mask or clipping 
region) to copy just the bits within the ball circumference.

If you are really ambitious, grab my Celtk project and render the game 
itself in an OpenGL pane (can be the whole window). Then your game will 
be portable to the Mac and *nix (celtk sits atop Tcl/Tk and the OpenGL 
Togl widget).

kenny

-- 
Cells: http://common-lisp.net/project/cells/

"Have you ever been in a relationship?"
    Attorney for Mary Winkler, confessed killer of her
    minister husband, when asked if the couple had
    marital problems.
From: Wade Humeniuk
Subject: Re: Allegro Common Graphics: Transparency problem in static pictures
Date: 
Message-ID: <iI2hg.32159$JX1.15775@edtnps82>
Mei Li Triantafyllidi wrote:
> Hi,
> 
> We are trying to make a billards game in Allegro Lisp using Common Graphics. 
> We use static pictures for the balls. The problem is that static pictures 
> are square and balls are round so we want the rest of the picture to be 
> transparent.
> 
> We tried to make parts of the picture transparent with an image editing 
> program and with the generate-mask function.
> 
> The problem is that these parts of the picture are not really transparent 
> but they just take the color of the form background. We want them to be 
> transparent even when they are over other static pictures.
> 
> Any ideas?
> 

I had a similar issue with LWW.  I ended up calling the GDI TransparentBlt
directly to achieve the functionality.  Here are various excerpts of how
I did it in LW.  I assume that Common Graphics in ACL will allow you to
get the Handles of GDI device so you can write the image directly.  I wish I
could say it could be easy, but unless Franz codes it up for you it will
take some work.

(winapi-function |TransparentBlt|
                  ((hdcDest handle)
                   (nXOriginDest :int)
                   (nYOriginDest :int)
                   (nWidthDest :int)
                   (hHeightDest :int)
                   (hdcSrc handle)
                   (nXOriginSrc :int)
                   (nYOriginSrc :int)
                   (nWidthSrc :int)
                   (nHeightSrc :int)
                   (crTransparent colorref))
                  :boolean
                  :module :msimg32)
(defun load-bitmap (path)
   (with-open-file (file path :direction :input :element-type '(unsigned-byte 8))
     (let* ((length (file-length file))
            (bitmap-array (make-array length :element-type '(unsigned-byte 8))))
       (read-sequence bitmap-array file)
       bitmap-array)))

(defun read-long (byte-array offset)
   (let ((long 0))
     (setf (ldb (byte 8 0) long) (aref byte-array offset)
           (ldb (byte 8 8) long) (aref byte-array (+ offset 1))
           (ldb (byte 8 16) long) (aref byte-array (+ offset 2))
           (ldb (byte 8 24) long) (aref byte-array (+ offset 3)))
     long))

(defun byte-swapped-word (word)
   (let ((swapped 0))
     (setf (ldb (byte 8 0) swapped) (ldb (byte 8 8) word)
           (ldb (byte 8 8) swapped) (ldb (byte 8 0) word))
     swapped))

(defun write-long-to-byte-array (array integer &optional (offset 0))
   (setf  (aref array offset) (ldb (byte 8 0) integer)
	 (aref array (+ offset 1))(ldb (byte 8 8) integer)
	 (aref array (+ offset 2))(ldb (byte 8 16) integer)
	 (aref array (+ offset 3))(ldb (byte 8 24) integer)))

(defconstant +offset-file-size+ 2)
(defconstant +offset-offset-bits+ 10)
(defconstant +offset-info-header+ 14)
(defconstant +offset-to-bitmap-width+ 18)
(defconstant +offset-to-bitmap-height+ 22)

(defun bitmap-file-size (raw-bitmap)
   (read-long raw-bitmap +offset-file-size+))
(defun bitmap-width (raw-bitmap)
   (read-long raw-bitmap +offset-to-bitmap-width+))
(defun bitmap-height (raw-bitmap)
   (read-long raw-bitmap +offset-to-bitmap-height+))
(defun bitmap-offset-bits (raw-bitmap)
   (read-long raw-bitmap +offset-offset-bits+))

(defun create-memory-dc-with-bitmap (hdc raw-bitmap)
   (let* ((width (bitmap-width raw-bitmap))
          (height (bitmap-height raw-bitmap))
          (size (bitmap-file-size raw-bitmap))
          (offset-bits (bitmap-offset-bits raw-bitmap))
          (hcdc (winapi:|CreateCompatibleDC| hdc))
          (hbitmap (winapi:|CreateCompatibleBitmap| hdc width height)))
     (winapi:|SelectObject| hcdc hbitmap)
     (fli:with-dynamic-foreign-objects ()
       (let* ((c-raw-bitmap (fli:allocate-dynamic-foreign-object :type :byte :nelems size))
              (bitmap-bits (fli:copy-pointer c-raw-bitmap))
              (info (fli:copy-pointer c-raw-bitmap)))
         (loop for byte from 0 below size
               do
               (setf (fli:dereference c-raw-bitmap :index byte) (aref raw-bitmap byte)))
         (fli:incf-pointer bitmap-bits offset-bits)
         (fli:incf-pointer info +offset-info-header+)
         (winapi:|SetDIBits| hcdc hbitmap 0 height bitmap-bits info winapi::DIB_RGB_COLORS))
       hcdc)))



(eval-when (:compile-toplevel :load-toplevel :execute)
   (defstruct image
     identifier
     external-image
     (memory-dc nil)
     x
     y
     width
     height
     (transparent-color (gdi::rgb 0 0 0))))


(defmethod pixblt-image (badge (image image))
   (if (image-memory-dc image)
       (winapi::|TransparentBlt| (gdi::hdc badge)
                                 (image-x image)
                                 (image-y image)
                                 (image-width image) (image-height image)
                                 (image-memory-dc image) 0 0 (image-width image) 
(image-height image)
                                 (image-transparent-color image))
     (progn
       (setf (image-memory-dc image)
             (gdi::create-memory-dc-with-bitmap (gdi::hdc badge) (image-external-image 
image)))
       (pixblt-image badge image))))
From: Luke Crook
Subject: Re: Allegro Common Graphics: Transparency problem in static pictures
Date: 
Message-ID: <1149562921.408473.59470@u72g2000cwu.googlegroups.com>
Wade Humeniuk wrote:
> I had a similar issue with LWW.  I ended up calling the GDI TransparentBlt
> directly to achieve the functionality.  Here are various excerpts of how
> I did it in LW.  I assume that Common Graphics in ACL will allow you to
> get the Handles of GDI device so you can write the image directly.  I wish I
> could say it could be easy, but unless Franz codes it up for you it will
> take some work.

Here is how I do it in lispbuilder-sdl ( lispbuilder.sourceforge.net )

Load in a bmp, set the key color (sets the transparancy for a specific
color), and render the image to the screen.

(defun bmp-sample ()
  (sdl:with-init ()
    (let ((display (sdl:set-window 640 480)))
      (setf *bmp* (sdl::convert-surface-to-display-format (sdl:load-bmp
"test.bmp")
									     :key-color #(253 59 251)
									     :free-surface t))
      (sdl:blit-surface *bmp* display :dst-rect #(10 10))
      (sdl:with-events
	(:quit t)
	(:idle
	 (sdl:update-screen display))))))