[comp.sys.amiga.tech] ClipRect help wanted

rosenbergr@abo.fi (Robin Rosenberg, Computer Science, ]bo Akademi) (02/22/89)

A simple question: What is the best way of doing clipping. I.e
I would like to render some text and images into a part of a window. If
the object goes outside the required borders (a rectangle) I want that part
chopped of. I know the ROMS have a InstallClipRect() function but how do
I use it?
Could anyone give me an example of how to do this the right way?

---
Robin Rosenberg,  ]bo Akademi, FINLAND
Address: Finn|, 22340 Geta, ]LAND - FINLAND
     or  Studentbyn 40A3, 20510 ]BO, FINLAND
Translation: ']' is an 'A' with a ring on top, '|' is an 'o' with two dots.

ksp@anuck.UUCP (p.s.kasten) (02/24/89)

In article <5379@abo.fi>, rosenbergr@abo.fi (Robin Rosenberg, Computer Science, ]bo Akademi) writes:
> A simple question: What is the best way of doing clipping. I.e
> Robin Rosenberg,  ]bo Akademi, FINLAND
> Address: Finn|, 22340 Geta, ]LAND - FINLAND
>      or  Studentbyn 40A3, 20510 ]BO, FINLAND
> Translation: ']' is an 'A' with a ring on top, '|' is an 'o' with two dots.

I am working on a CAD package in which I was hoping to have one window with
3 different clipping regions.  When I used InstallClipRect() (or whatever
it is called!), the rendering was noticably slower than when I have 3
separate windows.  I never looked into it further, but was always puzzled.
Any clues to this mystery, as well as the answer to Robin's question?

Phil Kasten
AT&T Bell Labs
!att!mvuxi!ksp

scott@applix.UUCP (Scott Evernden) (02/26/89)

In article <5379@abo.fi> rosenbergr@abo.fi (Robin Rosenberg, Computer Science, ]bo Akademi) writes:
>A simple question: What is the best way of doing clipping. I.e
>I would like to render some text and images into a part of a window. If
>the object goes outside the required borders (a rectangle) I want that part
>chopped of. I know the ROMS have a InstallClipRect() function but how do
>I use it?
>Could anyone give me an example of how to do this the right way?

Sure.  It's really not too hard...

As part of your init code do:

	struct Library *LayersBase;
	struct Region *region;

	LayersBase = OpenLibrary("layers.library", 0L);
	region = NewRegion();

Then, when you want clipping within (xmin,ymin)-(xmax,ymax):

	struct Rectangle rect;

	rect.MinX = minx;
	rect.MinY = miny;
	rect.MaxX = maxx;
	rect.MaxY = maxy;

	ClearRegion(region);
	OrRectRegion(region, &rect);
	InstallClipRegion(window->RPort->Layer, region);

There are lots of region operations, and you can create clip
areas that are not necessarily rectangular by using combinations
of And/Or/XorRectRegion() and -RegionRegion().

Finally, at cleanup time:

	DisposeRegion(region);
	InstallClipRegion(window->RPort->Layer, (struct Region *) NULL);
	CloseLibrary(LayersBase);

-scott

gay@elde.epfl.ch (David Gay) (02/27/89)

In article <911@applix.UUCP> scott@applix.uucp (Scott Evernden) writes:
>
>In article <5379@abo.fi> rosenbergr@abo.fi (Robin Rosenberg, Computer Science,
> ]bo Akademi) writes:
>>A simple question: What is the best way of doing clipping. I.e
>>I would like to render some text and images into a part of a window. If
>>the object goes outside the required borders (a rectangle) I want that part
>>chopped of. I know the ROMS have a InstallClipRect() function but how do
>>I use it?
>>Could anyone give me an example of how to do this the right way?
>
>Sure.  It's really not too hard...

Well, if this worked it wouldn't be too hard, but ... it doesn't (at least
not with simple refresh windows). I found this out the hard way, as this was
my initial approach, and then I had to fool around to find a way that worked.

[...]

>Then, when you want clipping within (xmin,ymin)-(xmax,ymax):
[...]
>    InstallClipRegion(window->RPort->Layer, region);
[...]
>Finally, at cleanup time:
>
>    DisposeRegion(region);
>    InstallClipRegion(window->RPort->Layer, (struct Region *) NULL);
>
>-scott
>

This creates problems as soon as your window gets a damage list (eg if you
display a requester in the window, or if your window is behind another).
I had to do the following to get everything working:

Setup clipping region:

    /* Save old damage list */
    oldRegion = win->WLayer->DamageList;
    /* New clip region is old region AND my clipping region */
    if (oldRegion == NULL || oldRegion->RegionRectangle == NULL)
        success = TRUE;
    else
        /* Argh ! */
        success = AndRegionRegion(win->WLayer->DamageList, region);
    if (success) InstallClipRegion(win->WLayer, region);


And the following to clean up:

    InstallClipRegion(win->WLayer, oldRegion);
    DisposeRegion(region);

This works, but I have grave doubts about it working in future releases ...
(if the xxRegion routines are changed for instance).

Anybody have a better method ?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
David Gay                                  6 x 9 = 42

GAY@ELDE.EPFL.CH, or GAY%ELDE.EPFL.CH@CLSEPF51.bitnet

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

dale@boing.UUCP (Dale Luck) (02/27/89)

In article <911@applix.UUCP> scott@applix.UUCP (Scott Evernden) writes:

Unless The code is broke somewhere that we don't know InstallClipRegion
returns a ptr to the original ClipRegion. It should be restored.

	struct Region * OldRegion;

>	ClearRegion(region);
>	OrRectRegion(region, &rect);
>	InstallClipRegion(window->RPort->Layer, region);

	OldRegion = InstallClipRegion(window->RPort->Layer, region);
>
>Finally, at cleanup time:
>
>	InstallClipRegion(window->RPort->Layer, (struct Region *) NULL);

	InstallClipRegion(window->RPort->Layer, OldRegion );

>-scott


-- 
Dale Luck     GfxBase/Boing, Inc.
{uunet!cbmvax|pyramid}!amiga!boing!dale

c60c-1ea@web-1d.berkeley.edu (Yen Yuanchi Hsieh) (02/28/89)

>Finally, at cleanup time:
>
>	DisposeRegion(region);
>	InstallClipRegion(window->RPort->Layer, (struct Region *) NULL);

**	Please, don't DisposeRegion BEFORE it's been removed from it's
	Layer, that's asking for trouble.

>	CloseLibrary(LayersBase);
>
>-scott

	Remember that clipping=slow graphics rendering speeds.  I have found
it useful to clip on graphic demand, that way Intuition runs at normal
non-snail speeds when I'm not drawing Icons.  Remember to trap and handle
window-resizing if you're going to keep a static clipping rectangle hanging
around.

David Navas, hiding at c60c-1ea@WEB.Berkeley.Edu