[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: drawing to offscreen bitmap bombs

At  3:40 PM 9/3/93 -0600, Tom McDougal wrote:
>This very short piece of code crashes MCL 2.0p2.  Would you mind taking a
>quick look at it and helping me understand what I am doing wrong?   I have
>some similar code that seems to work fine.
>Thanks very much.
>For what it is worth, Macsbug says the error is:
>Bus Error at 01643FD2 01234567+1B52
>while writing long word (data = BC1DBC16) to BD43813E in User data space
>This is on a MacIIfx with all extensions turned off.
>;;; Create a bitmap and draw something to it.
>(require :quickdraw)
>(defvar *bitmap-1* (make-bitmap 0 0 200 200))
>(rlet ((port :grafport))
>  (unwind-protect
>    (without-interrupts
>     (#_openPort port)
>     (with-port port
>       (#_setPortBits *bitmap-1*)
>       (#_moveTo 20 20)
>       (#_lineTo 70 30)))
>    (#_closePort port)))

I believe that the problem is that #_OpenPort sets the port. The WITH-PORT
exits with the port being PORT, then you close it while it is still
installed. The following works for me:

(rlet ((port :grafport))
     (with-focused-view nil
       (with-port (ccl::%getport)
         (#_openPort port)
         (#_setPortBits *bitmap-1*)
         (#_moveTo 20 20)
         (#_lineTo 70 30))))
    (#_closePort port)))

I noticed that it is also necessary to clear the contents of *bitmap-1*
before executing the above code in order to be able to copy-bits it to a
window and see the expected line segment. I did this with the following
code which knows exactly how MAKE-BITMAP allocates its memory. If you do
this in producation code, I suggest making it more generic:

(let ((size (#_getptrsize *bitmap-1*))
      (b *bitmap-1*))
  (dotimes (i size)
    (unless (< i (record-length :bitmap))
      (setf (%get-byte b i) 0))))