[comp.sys.mac.programmer] Dragging Bitmaps?

siegel@endor.harvard.edu (Rich Siegel) (07/05/90)

Does anyone have any suggestions on how to drag small bitmaps around, in the
same fashion as MacPaint? Currently I drag outlines around, but it would be
much nicer to move the object as a whole.

R.
~~~~~~~~~~~~~~~
 Rich Siegel
 Staff Software Developer
 Symantec Corporation, Language Products Group
 Internet: siegel@endor.harvard.edu
 UUCP: ..harvard!endor!siegel

"In this world there is nothing more thrilling than a lone man facing
singlehandedly half a ton of angry pot roast." - Tom Lehrer

~~~~~~~~~~~~~~~

minow@mountn.dec.com (Martin Minow) (07/05/90)

In article <3398@husc6.harvard.edu> siegel@endor.harvard.edu (Rich Siegel)
writes:
>
>Does anyone have any suggestions on how to drag small bitmaps around, in the
>same fashion as MacPaint?

Look through MacTutor back issues for an article on "animated bitmaps" --
I don't remember if it's in "Best of, vol 3" or in a more recent issue.
If you can't find it, send mail and I'll dig it out of my pile.

Martin.
minow@bolt.enet.dec.com

jackiw@cs.swarthmore.edu (Nick Jackiw) (07/06/90)

siegel@endor.harvard.edu (Rich Siegel) writes:
> 
> Does anyone have any suggestions on how to drag small bitmaps around, in the
> same fashion as MacPaint? Currently I drag outlines around, but it would be
> much nicer to move the object as a whole.
> 
> R.

Sure: you'll need to use some offscreen bitmaps though.  The major design
decisions that affect the process are: (1) do you want it to be really
smooth? (2) must your bitmaps be of eccentric shape or can they be limited
to rects?

(1) For rectangles

The Cheap Way:

Get two offscreen bitmaps the size of the one to drag, and fill them both
with the bitimages.  Call these SOURCE and BUFFER. Now watch
the mouse. Every time it moves, calculate the new location (presumably
you measure the offset into the bitmap at which the mouse went down, and
then calculate the new location based on this offset and the vector of
mouse movement, so the mouse "sticks" to the same part of the rectangle).
Copy BUFFER to the old location,  copy the rect of "underlaid" bits at
the new location to BUFFER, and then copy SOURCE to the new location.
Repeat until mouseup.

The problem with this is that you'll have flicker--you'll be overwriting
background bits with source bits and source bits with background bits.

The more expensive way:

Get an offscreen copy of the area in which you wish to permit dragging,
without the dragable image in it.  Call this BACKBITS.  Now get an
offscreen copy of the draggable image, call this SOURCE. Now get a third
bitmap, the size of BACKBITS, left blank; call this SCRATCH.  For each
new mouse move,  copy the UnionRect of the oldLocation and the newLocation
from BackBits to Scratch, and then throw in (to Scratch) a copy of the
SOURCE at new location.  Copy this unionRect to the screen.

(2) For irregularly-shaped bitmaps

You know about BitMap2Rgn, I presume.  It's on the DTS cds in object code,
or you can count on it being available if you'll only run on 32Bit QD
machines.  (There may be licensing hassles concerning the object code;
read the docs or write your own.)  With BitMap2Rgn, get a region that
includes/excludes the appropriate parts of your SOURCE.  On a blank bitmap
the size of SOURCE, do a FillRgn(bitsRgn,black). This gives you a mask,
which you should use in either of the above schemes: wherever you
formerly did CopyBits(source,somePlace...) now do a CopyMask(source,
somePlace,maskBits,...).

If you want source code,  I can whip something up.  There are slightly
more efficient ways to do it than the above, but that's the basic
scheme as I understand it.


Call this BACKBITS.  


--
+-------------------+-jackiw@cs.swarthmore.edu / !rutgers!bpa!swatsun!jackiw-+
|  nicholas jackiw  | jackiw%campus.swarthmore.edu@swarthmr.bitnet           |
+-------------------+-VGP/MathDept/Swarthmore College, Swarthmore, PA 19081--+
"Ah...I've got this CHRONIC pain."                             _True Believer_

mxmora@unix.SRI.COM (Matt Mora) (07/06/90)

In article <1741@mountn.dec.com> minow@bolt.enet.dec.com (Martin Minow) writes:
>In article <3398@husc6.harvard.edu> siegel@endor.harvard.edu (Rich Siegel)
>writes:
>>
>>Does anyone have any suggestions on how to drag small bitmaps around, in the
>>same fashion as MacPaint?
>
>Look through MacTutor back issues for an article on "animated bitmaps" --

>Martin.
>minow@bolt.enet.dec.com

Also look at Apples sample code on offscreen bitmaps. The code lets you
drag small bitmaps around and looks real smooth. I had a tough time trying
to get it to work in THINK pascal but should be no problem for you. It also
comes in C.

The program also lets you toggle the offscreen bitmaps on and off to see
the differnce with using multiple bitmaps.

I think the code was based on their new Gworld stuff. I can't remember its
been a while since I played with it.






-- 
___________________________________________________________
Matthew Mora                |   my Mac  Matt_Mora@sri.com
SRI International           |  my unix  mxmora@unix.sri.com
___________________________________________________________

lsr@Apple.COM (Larry Rosenstein) (07/06/90)

In article <3398@husc6.harvard.edu> siegel@endor.harvard.edu (Rich Siegel) writes:
>
>Does anyone have any suggestions on how to drag small bitmaps around, in the
>same fashion as MacPaint? Currently I drag outlines around, but it would be
>much nicer to move the object as a whole.

The basic approach is to cache the image behind the moving bitmap, and use
that cache to erase the bitmap before it moves.  So the basic code would be
to use 1 CopyBits to erase the old bitmap, and 1 CopyBits to draw the bitmap
in its new position.  (This assumes that you have cached the entire view
beforehand.  This is always better than copying the bits from the screen.)

You will get better results if you do both steps with 1 CopyBits.  To do
this you need another offscreen bitmap the size of the union of the old and
new rectangles.  You initialize this bitmap from the larger cache, and draw
the moving bitmap in its desired position.  Then you use 1 CopyBits to copy
everything onto the screen at once.

-- 
		 Larry Rosenstein,  Object Specialist
 Apple Computer, Inc.  20525 Mariani Ave, MS 46-B  Cupertino, CA 95014
	    AppleLink:Rosenstein1    domain:lsr@Apple.COM
		UUCP:{sun,voder,nsc,decwrl}!apple!lsr

keith@Apple.COM (Keith Rollin) (07/06/90)

In article <13874@unix.SRI.COM> mxmora@unix.UUCP (Matt Mora) writes:
>In article <1741@mountn.dec.com> minow@bolt.enet.dec.com (Martin Minow) writes:
>>In article <3398@husc6.harvard.edu> siegel@endor.harvard.edu (Rich Siegel)
>>writes:
>>>
>>>Does anyone have any suggestions on how to drag small bitmaps around, in the
>>>same fashion as MacPaint?
>>
>>Look through MacTutor back issues for an article on "animated bitmaps" --
>
>Also look at Apples sample code on offscreen bitmaps. The code lets you
>drag small bitmaps around and looks real smooth. I had a tough time trying
>to get it to work in THINK pascal but should be no problem for you. It also
>comes in C.
>
>The program also lets you toggle the offscreen bitmaps on and off to see
>the differnce with using multiple bitmaps.
>
>I think the code was based on their new Gworld stuff. I can't remember its
>been a while since I played with it.

Thanks for the plug, Matt. However, those routines do NOT rely (or even
use) the new GWorld routines in 32-bit Color QuickDraw, so they'll work
on any Macintosh. Also, they were written way before 32bcqd was
available. A couple of people here have taken a look at what it would
take to backfit the GWorld routines onto our OffScreen unit, but
initial estimates indicate that it would take a bit of work, and none
of our graphics support people have had the time to undertake the task
yet. We'll get to it someday, though...

-- 
------------------------------------------------------------------------------
Keith Rollin  ---  Apple Computer, Inc.  ---  Developer Technical Support
INTERNET: keith@apple.com
    UUCP: {decwrl, hoptoad, nsc, sun, amdahl}!apple!keith
"Argue for your Apple, and sure enough, it's yours" - Keith Rollin, Contusions

minow@mountn.dec.com (Martin Minow) (07/08/90)

In article <1741@mountn.dec.com> minow@bolt.enet.dec.com (Martin Minow) said:
>Look through MacTutor back issues for an article on "animated bitmaps" --

It's in The Essential MacTutor, vol. 3, and MacTutor, vol 3, no 7.
Too much to type in, though.

Martin.
minow@bolt.enet.dec.com